batch_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptest"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. "gopkg.in/macaron.v1"
  14. "gogs.io/gogs/internal/conf"
  15. "gogs.io/gogs/internal/db"
  16. "gogs.io/gogs/internal/lfsutil"
  17. )
  18. func Test_serveBatch(t *testing.T) {
  19. conf.SetMockServer(t, conf.ServerOpts{
  20. ExternalURL: "https://gogs.example.com/",
  21. })
  22. m := macaron.New()
  23. m.Use(func(c *macaron.Context) {
  24. c.Map(&db.User{Name: "owner"})
  25. c.Map(&db.Repository{Name: "repo"})
  26. })
  27. m.Post("/", serveBatch)
  28. tests := []struct {
  29. name string
  30. body string
  31. mockLFSStore *db.MockLFSStore
  32. expStatusCode int
  33. expBody string
  34. }{
  35. {
  36. name: "unrecognized operation",
  37. body: `{"operation": "update"}`,
  38. expStatusCode: http.StatusBadRequest,
  39. expBody: `{"message": "Operation not recognized"}` + "\n",
  40. },
  41. {
  42. name: "upload: contains invalid oid",
  43. body: `{
  44. "operation": "upload",
  45. "objects": [
  46. {"oid": "bad_oid", "size": 123},
  47. {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123}
  48. ]}`,
  49. expStatusCode: http.StatusOK,
  50. expBody: `{
  51. "transfer": "basic",
  52. "objects": [
  53. {"oid": "bad_oid", "size":123, "actions": {"error": {"code": 422, "message": "Object has invalid oid"}}},
  54. {
  55. "oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  56. "size": 123,
  57. "actions": {
  58. "upload": {
  59. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  60. "header": {"Content-Type": "application/octet-stream"}
  61. },
  62. "verify": {
  63. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/verify"
  64. }
  65. }
  66. }
  67. ]
  68. }` + "\n",
  69. },
  70. {
  71. name: "download: contains non-existent oid and mismatched size",
  72. body: `{
  73. "operation": "download",
  74. "objects": [
  75. {"oid": "bad_oid", "size": 123},
  76. {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123},
  77. {"oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", "size": 456}
  78. ]}`,
  79. mockLFSStore: &db.MockLFSStore{
  80. MockGetObjectsByOIDs: func(repoID int64, oids ...lfsutil.OID) ([]*db.LFSObject, error) {
  81. return []*db.LFSObject{
  82. {
  83. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  84. Size: 1234,
  85. }, {
  86. OID: "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
  87. Size: 456,
  88. },
  89. }, nil
  90. },
  91. },
  92. expStatusCode: http.StatusOK,
  93. expBody: `{
  94. "transfer": "basic",
  95. "objects": [
  96. {"oid": "bad_oid", "size": 123, "actions": {"error": {"code": 404, "message": "Object does not exist"}}},
  97. {
  98. "oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  99. "size": 123,
  100. "actions": {"error": {"code": 422, "message": "Object size mismatch"}}
  101. },
  102. {
  103. "oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
  104. "size": 456,
  105. "actions": {
  106. "download": {
  107. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57"
  108. }
  109. }
  110. }
  111. ]
  112. }` + "\n",
  113. },
  114. }
  115. for _, test := range tests {
  116. t.Run(test.name, func(t *testing.T) {
  117. db.SetMockLFSStore(t, test.mockLFSStore)
  118. r, err := http.NewRequest("POST", "/", bytes.NewBufferString(test.body))
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. rr := httptest.NewRecorder()
  123. m.ServeHTTP(rr, r)
  124. resp := rr.Result()
  125. assert.Equal(t, test.expStatusCode, resp.StatusCode)
  126. body, err := ioutil.ReadAll(resp.Body)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. var expBody bytes.Buffer
  131. err = json.Indent(&expBody, []byte(test.expBody), "", " ")
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. var gotBody bytes.Buffer
  136. err = json.Indent(&gotBody, body, "", " ")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. assert.Equal(t, expBody.String(), gotBody.String())
  141. })
  142. }
  143. }