batch_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "gopkg.in/macaron.v1"
  13. "gogs.io/gogs/internal/conf"
  14. "gogs.io/gogs/internal/db"
  15. "gogs.io/gogs/internal/lfsutil"
  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 *db.MockLFSStore
  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: `{"transfer":"basic","objects":[{"oid":"bad_oid","size":123,"actions":{"error":{"code":422,"message":"Object has invalid oid"}}},{"oid":"ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f","size":123,"actions":{"upload":{"href":"https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"},"verify":{"href":"https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/verify"}}}]}` + "\n",
  50. },
  51. {
  52. name: "download: contains non-existent oid and mismatched size",
  53. body: `{
  54. "operation": "download",
  55. "objects": [
  56. {"oid": "bad_oid", "size": 123},
  57. {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123},
  58. {"oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", "size": 456}
  59. ]}`,
  60. mockLFSStore: &db.MockLFSStore{
  61. MockGetObjectsByOIDs: func(repoID int64, oids ...lfsutil.OID) ([]*db.LFSObject, error) {
  62. return []*db.LFSObject{
  63. {
  64. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  65. Size: 1234,
  66. }, {
  67. OID: "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57",
  68. Size: 456,
  69. },
  70. }, nil
  71. },
  72. },
  73. expStatusCode: http.StatusOK,
  74. expBody: `{"transfer":"basic","objects":[{"oid":"bad_oid","size":123,"actions":{"error":{"code":404,"message":"Object does not exist"}}},{"oid":"ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f","size":123,"actions":{"error":{"code":422,"message":"Object size mismatch"}}},{"oid":"5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57","size":456,"actions":{"download":{"href":"https://gogs.example.com/owner/repo.git/info/lfs/objects/basic/5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57"}}}]}` + "\n",
  75. },
  76. }
  77. for _, test := range tests {
  78. t.Run(test.name, func(t *testing.T) {
  79. db.SetMockLFSStore(t, test.mockLFSStore)
  80. r, err := http.NewRequest("POST", "/", bytes.NewBufferString(test.body))
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. rr := httptest.NewRecorder()
  85. m.ServeHTTP(rr, r)
  86. resp := rr.Result()
  87. assert.Equal(t, test.expStatusCode, resp.StatusCode)
  88. body, err := ioutil.ReadAll(resp.Body)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. assert.Equal(t, test.expBody, string(body))
  93. })
  94. }
  95. }