diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-06 23:53:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-06 23:53:55 +0800 |
commit | 2b3655fa118abaa33a0e064233fdc8c670487c62 (patch) | |
tree | c160e5aaa886e920f4cef8bd2bb29a002e6399fe /internal/route/lfs/batch_test.go | |
parent | ca2f7a7e1610fd8e99649dc1ed2058c2f362c57d (diff) |
lfs: add tests to batch endpoint (#6073)
Diffstat (limited to 'internal/route/lfs/batch_test.go')
-rw-r--r-- | internal/route/lfs/batch_test.go | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/internal/route/lfs/batch_test.go b/internal/route/lfs/batch_test.go new file mode 100644 index 00000000..7dce2004 --- /dev/null +++ b/internal/route/lfs/batch_test.go @@ -0,0 +1,105 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package lfs + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/macaron.v1" + + "gogs.io/gogs/internal/conf" + "gogs.io/gogs/internal/db" + "gogs.io/gogs/internal/lfsutil" +) + +func Test_serveBatch(t *testing.T) { + conf.SetMockServer(t, conf.ServerOpts{ + ExternalURL: "https://gogs.example.com/", + }) + m := macaron.New() + m.Use(func(c *macaron.Context) { + c.Map(&db.User{Name: "owner"}) + c.Map(&db.Repository{Name: "repo"}) + }) + m.Post("/", serveBatch) + + tests := []struct { + name string + body string + mockLFSStore *db.MockLFSStore + expStatusCode int + expBody string + }{ + { + name: "unrecognized operation", + body: `{"operation": "update"}`, + expStatusCode: http.StatusBadRequest, + expBody: `{"message":"Operation not recognized"}` + "\n", + }, + { + name: "upload: contains invalid oid", + body: `{ +"operation": "upload", +"objects": [ + {"oid": "bad_oid", "size": 123}, + {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123} +]}`, + expStatusCode: http.StatusOK, + 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", + }, + { + name: "download: contains non-existent oid and mismatched size", + body: `{ +"operation": "download", +"objects": [ + {"oid": "bad_oid", "size": 123}, + {"oid": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", "size": 123}, + {"oid": "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", "size": 456} +]}`, + mockLFSStore: &db.MockLFSStore{ + MockGetObjectsByOIDs: func(repoID int64, oids ...lfsutil.OID) ([]*db.LFSObject, error) { + return []*db.LFSObject{ + { + OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", + Size: 1234, + }, { + OID: "5cac0a318669fadfee734fb340a5f5b70b428ac57a9f4b109cb6e150b2ba7e57", + Size: 456, + }, + }, nil + }, + }, + expStatusCode: http.StatusOK, + 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", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + db.SetMockLFSStore(t, test.mockLFSStore) + + r, err := http.NewRequest("POST", "/", bytes.NewBufferString(test.body)) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + m.ServeHTTP(rr, r) + + resp := rr.Result() + assert.Equal(t, test.expStatusCode, resp.StatusCode) + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, test.expBody, string(body)) + }) + } +} |