batch_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. )
  17. func Test_serveBatch(t *testing.T) {
  18. conf.SetMockServer(t, conf.ServerOpts{
  19. ExternalURL: "https://gogs.example.com/",
  20. })
  21. m := macaron.New()
  22. m.Use(func(c *macaron.Context) {
  23. c.Map(&db.User{Name: "owner"})
  24. c.Map(&db.Repository{Name: "repo"})
  25. })
  26. m.Post("/", serveBatch)
  27. tests := []struct {
  28. name string
  29. body string
  30. mockLFSStore func() db.LFSStore
  31. expStatusCode int
  32. expBody string
  33. }{
  34. {
  35. name: "unrecognized operation",
  36. body: `{"operation": "update"}`,
  37. expStatusCode: http.StatusBadRequest,
  38. expBody: `{"message": "Operation not recognized"}` + "\n",
  39. },
  40. {
  41. name: "upload: contains invalid oid",
  42. body: `{
  43. "operation": "upload",
  44. "objects": [
  45. {"oid": "bad_oid", "size": 123},
  46. {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123}
  47. ]}`,
  48. expStatusCode: http.StatusOK,
  49. expBody: `{
  50. "transfer": "basic",
  51. "objects": [
  52. {"oid": "bad_oid", "size":123, "actions": {"error": {"code": 422, "message": "Object has invalid oid"}}},
  53. {
  54. "oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  55. "size": 123,
  56. "actions": {
  57. "upload": {
  58. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  59. "header": {"Content-Type": "application/octet-stream"}
  60. },
  61. "verify": {
  62. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/verify"
  63. }
  64. }
  65. }
  66. ]
  67. }` + "\n",
  68. },
  69. {
  70. name: "download: contains non-existent oid and mismatched size",
  71. body: `{
  72. "operation": "download",
  73. "objects": [
  74. {"oid": "bad_oid", "size": 123},
  75. {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123},
  76. {"oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", "size": 456}
  77. ]}`,
  78. mockLFSStore: func() db.LFSStore {
  79. mock := NewMockLFSStore()
  80. mock.GetObjectsByOIDsFunc.SetDefaultReturn(
  81. []*db.LFSObject{
  82. {
  83. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  84. Size: 1234,
  85. }, {
  86. OID: "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
  87. Size: 456,
  88. },
  89. },
  90. nil,
  91. )
  92. return mock
  93. },
  94. expStatusCode: http.StatusOK,
  95. expBody: `{
  96. "transfer": "basic",
  97. "objects": [
  98. {"oid": "bad_oid", "size": 123, "actions": {"error": {"code": 404, "message": "Object does not exist"}}},
  99. {
  100. "oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  101. "size": 123,
  102. "actions": {"error": {"code": 422, "message": "Object size mismatch"}}
  103. },
  104. {
  105. "oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
  106. "size": 456,
  107. "actions": {
  108. "download": {
  109. "href": "https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57"
  110. }
  111. }
  112. }
  113. ]
  114. }` + "\n",
  115. },
  116. }
  117. for _, test := range tests {
  118. t.Run(test.name, func(t *testing.T) {
  119. if test.mockLFSStore != nil {
  120. db.SetMockLFSStore(t, test.mockLFSStore())
  121. }
  122. r, err := http.NewRequest("POST", "/", bytes.NewBufferString(test.body))
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. rr := httptest.NewRecorder()
  127. m.ServeHTTP(rr, r)
  128. resp := rr.Result()
  129. assert.Equal(t, test.expStatusCode, resp.StatusCode)
  130. body, err := ioutil.ReadAll(resp.Body)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. var expBody bytes.Buffer
  135. err = json.Indent(&expBody, []byte(test.expBody), "", " ")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. var gotBody bytes.Buffer
  140. err = json.Indent(&gotBody, body, "", " ")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. assert.Equal(t, expBody.String(), gotBody.String())
  145. })
  146. }
  147. }