Debian Codegraph @

locking/api_test.go

raw symbols SCIP
1 package locking
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "github.com/git-lfs/git-lfs/v3/git"
14 "github.com/git-lfs/git-lfs/v3/lfsapi"
15 "github.com/git-lfs/git-lfs/v3/lfshttp"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18 "github.com/xeipuuv/gojsonschema"
19 )
20
21 func TestAPILock(t *testing.T) {
22 require.NotNil(t, createReqSchema)
23 require.NotNil(t, createResSchema)
24
25 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26 if r.URL.Path != "/api/locks" {
27 w.WriteHeader(404)
28 return
29 }
30
31 assert.Equal(t, "POST", r.Method)
32 assert.Equal(t, lfshttp.MediaType, r.Header.Get("Accept"))
33 assert.Equal(t, lfshttp.RequestContentType, r.Header.Get("Content-Type"))
34 assert.Equal(t, "53", r.Header.Get("Content-Length"))
35
36 reqLoader, body := gojsonschema.NewReaderLoader(r.Body)
37 lockReq := &lockRequest{}
38 err := json.NewDecoder(body).Decode(lockReq)
39 r.Body.Close()
40 assert.Nil(t, err)
41 assert.Equal(t, "refs/heads/master", lockReq.Ref.Name)
42 assert.Equal(t, "request", lockReq.Path)
43 assertSchema(t, createReqSchema, reqLoader)
44
45 w.Header().Set("Content-Type", "application/json")
46 resLoader, resWriter := gojsonschema.NewWriterLoader(w)
47 err = json.NewEncoder(resWriter).Encode(&lockResponse{
48 Lock: &Lock{
49 Id: "1",
50 Path: "response",
51 },
52 })
53 assert.Nil(t, err)
54 assertSchema(t, createResSchema, resLoader)
55 }))
56 defer srv.Close()
57
58 c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
59 "lfs.url": srv.URL + "/api",
60 }))
61
62 lc := &httpLockClient{Client: c}
63 lockRes, status, err := lc.Lock("", &lockRequest{Path: "request", Ref: &lockRef{Name: "refs/heads/master"}})
64 require.Nil(t, err)
65 assert.Equal(t, 200, status)
66 assert.Equal(t, "1", lockRes.Lock.Id)
67 assert.Equal(t, "response", lockRes.Lock.Path)
68 }
69
70 func TestAPIUnlock(t *testing.T) {
71 require.NotNil(t, delReqSchema)
72 require.NotNil(t, createResSchema)
73
74 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75 if r.URL.Path != "/api/locks/123/unlock" {
76 w.WriteHeader(404)
77 return
78 }
79
80 assert.Equal(t, "POST", r.Method)
81 assert.Equal(t, lfshttp.MediaType, r.Header.Get("Accept"))
82 assert.Equal(t, lfshttp.RequestContentType, r.Header.Get("Content-Type"))
83
84 reqLoader, body := gojsonschema.NewReaderLoader(r.Body)
85 unlockReq := &unlockRequest{}
86 err := json.NewDecoder(body).Decode(unlockReq)
87 r.Body.Close()
88 assert.Nil(t, err)
89 assert.True(t, unlockReq.Force)
90 assertSchema(t, delReqSchema, reqLoader)
91
92 w.Header().Set("Content-Type", "application/json")
93 resLoader, resWriter := gojsonschema.NewWriterLoader(w)
94 err = json.NewEncoder(resWriter).Encode(&unlockResponse{
95 Lock: &Lock{
96 Id: "123",
97 Path: "response",
98 },
99 })
100 assert.Nil(t, err)
101 assertSchema(t, createResSchema, resLoader)
102 }))
103 defer srv.Close()
104
105 c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
106 "lfs.url": srv.URL + "/api",
107 }))
108
109 lc := &httpLockClient{Client: c}
110 unlockRes, status, err := lc.Unlock(&git.Ref{
111 Name: "master",
112 Sha: "6161616161616161616161616161616161616161",
113 Type: git.RefTypeLocalBranch,
114 }, "", "123", true)
115 require.Nil(t, err)
116 assert.Equal(t, 200, status)
117 assert.Equal(t, "123", unlockRes.Lock.Id)
118 assert.Equal(t, "response", unlockRes.Lock.Path)
119 }
120
121 func TestAPISearch(t *testing.T) {
122 require.NotNil(t, listResSchema)
123
124 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
125 if r.URL.Path != "/api/locks" {
126 w.WriteHeader(404)
127 return
128 }
129
130 assert.Equal(t, "GET", r.Method)
131 assert.Equal(t, lfshttp.MediaType, r.Header.Get("Accept"))
132 assert.Equal(t, "", r.Header.Get("Content-Type"))
133
134 q := r.URL.Query()
135 assert.Equal(t, "A", q.Get("a"))
136 assert.Equal(t, "cursor", q.Get("cursor"))
137 assert.Equal(t, "5", q.Get("limit"))
138
139 w.Header().Set("Content-Type", "application/json")
140 resLoader, resWriter := gojsonschema.NewWriterLoader(w)
141 err := json.NewEncoder(resWriter).Encode(&lockList{
142 Locks: []Lock{
143 {Id: "1"},
144 {Id: "2"},
145 },
146 })
147 assert.Nil(t, err)
148 assertSchema(t, listResSchema, resLoader)
149 }))
150 defer srv.Close()
151
152 c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
153 "lfs.url": srv.URL + "/api",
154 }))
155
156 lc := &httpLockClient{Client: c}
157 locks, status, err := lc.Search("", &lockSearchRequest{
158 Filters: []lockFilter{
159 {Property: "a", Value: "A"},
160 },
161 Cursor: "cursor",
162 Limit: 5,
163 })
164 require.Nil(t, err)
165 assert.Equal(t, 200, status)
166 assert.Equal(t, 2, len(locks.Locks))
167 assert.Equal(t, "1", locks.Locks[0].Id)
168 assert.Equal(t, "2", locks.Locks[1].Id)
169 }
170
171 func TestAPISearchVerifiable(t *testing.T) {
172 require.NotNil(t, verifyResSchema)
173
174 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
175 if r.URL.Path != "/api/locks/verify" {
176 w.WriteHeader(404)
177 return
178 }
179
180 assert.Equal(t, "POST", r.Method)
181 assert.Equal(t, lfshttp.MediaType, r.Header.Get("Accept"))
182 assert.Equal(t, lfshttp.RequestContentType, r.Header.Get("Content-Type"))
183
184 body := lockVerifiableRequest{}
185 if assert.Nil(t, json.NewDecoder(r.Body).Decode(&body)) {
186 assert.Equal(t, "cursor", body.Cursor)
187 assert.Equal(t, 5, body.Limit)
188 }
189
190 w.Header().Set("Content-Type", "application/json")
191 resLoader, resWriter := gojsonschema.NewWriterLoader(w)
192 err := json.NewEncoder(resWriter).Encode(&lockVerifiableList{
193 Ours: []Lock{
194 {Id: "1"},
195 {Id: "2"},
196 },
197 Theirs: []Lock{
198 {Id: "3"},
199 },
200 })
201 assert.Nil(t, err)
202 assertSchema(t, verifyResSchema, resLoader)
203 }))
204 defer srv.Close()
205
206 c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
207 "lfs.url": srv.URL + "/api",
208 }))
209
210 lc := &httpLockClient{Client: c}
211 locks, status, err := lc.SearchVerifiable("", &lockVerifiableRequest{
212 Cursor: "cursor",
213 Limit: 5,
214 })
215 require.Nil(t, err)
216 assert.Equal(t, 200, status)
217 assert.Equal(t, 2, len(locks.Ours))
218 assert.Equal(t, "1", locks.Ours[0].Id)
219 assert.Equal(t, "2", locks.Ours[1].Id)
220 assert.Equal(t, 1, len(locks.Theirs))
221 assert.Equal(t, "3", locks.Theirs[0].Id)
222 }
223
224 var (
225 createReqSchema *sourcedSchema
226 createResSchema *sourcedSchema
227 delReqSchema *sourcedSchema
228 listResSchema *sourcedSchema
229 verifyResSchema *sourcedSchema
230 )
231
232 func init() {
233 wd, err := os.Getwd()
234 if err != nil {
235 fmt.Println("getwd error:", err)
236 return
237 }
238
239 createReqSchema = getSchema(wd, "schemas/http-lock-create-request-schema.json")
240 createResSchema = getSchema(wd, "schemas/http-lock-create-response-schema.json")
241 delReqSchema = getSchema(wd, "schemas/http-lock-delete-request-schema.json")
242 listResSchema = getSchema(wd, "schemas/http-lock-list-response-schema.json")
243 verifyResSchema = getSchema(wd, "schemas/http-lock-verify-response-schema.json")
244 }
245
246 type sourcedSchema struct {
247 Source string
248 *gojsonschema.Schema
249 }
250
251 func getSchema(wd, relpath string) *sourcedSchema {
252 abspath := filepath.ToSlash(filepath.Join(wd, relpath))
253 s, err := gojsonschema.NewSchema(gojsonschema.NewReferenceLoader(fmt.Sprintf("file:///%s", abspath)))
254 if err != nil {
255 fmt.Printf("schema load error for %q: %+v\n", relpath, err)
256 }
257 return &sourcedSchema{Source: relpath, Schema: s}
258 }
259
260 func assertSchema(t *testing.T, schema *sourcedSchema, dataLoader gojsonschema.JSONLoader) {
261 res, err := schema.Validate(dataLoader)
262 if assert.Nil(t, err) {
263 if res.Valid() {
264 return
265 }
266
267 resErrors := res.Errors()
268 valErrors := make([]string, 0, len(resErrors))
269 for _, resErr := range resErrors {
270 valErrors = append(valErrors, resErr.String())
271 }
272 t.Errorf("Schema: %s\n%s", schema.Source, strings.Join(valErrors, "\n"))
273 }
274 }