fix: fix tests from removal of session & reverse proxy auth from LFS endpoints

This commit is contained in:
Mathieu Fenniak 2026-05-25 13:44:55 -06:00
parent 3328d7a69b
commit 50e1ac27bc
No known key found for this signature in database
3 changed files with 59 additions and 39 deletions

View file

@ -103,11 +103,11 @@ func TestAPILFSLocksLogged(t *testing.T) {
// create locks
for _, test := range tests {
session := loginUser(t, test.user.Name)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path})
req.AddBasicAuth(test.user.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType)
resp := session.MakeRequest(t, req, test.httpResult)
resp := MakeRequest(t, req, test.httpResult)
if len(test.addTime) > 0 {
var lfsLock api.LFSLockResponse
DecodeJSON(t, resp, &lfsLock)
@ -121,10 +121,10 @@ func TestAPILFSLocksLogged(t *testing.T) {
// check creation
for _, test := range resultsTests {
session := loginUser(t, test.user.Name)
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
req.AddBasicAuth(test.user.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
var lfsLocks api.LFSLockList
DecodeJSON(t, resp, &lfsLocks)
assert.Len(t, lfsLocks.Locks, test.totalCount)
@ -135,9 +135,10 @@ func TestAPILFSLocksLogged(t *testing.T) {
}
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/verify", test.repo.FullName()), map[string]string{})
req.AddBasicAuth(test.user.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType)
resp = session.MakeRequest(t, req, http.StatusOK)
resp = MakeRequest(t, req, http.StatusOK)
var lfsLocksVerify api.LFSLockListVerify
DecodeJSON(t, resp, &lfsLocksVerify)
assert.Len(t, lfsLocksVerify.Ours, test.oursCount)
@ -157,11 +158,11 @@ func TestAPILFSLocksLogged(t *testing.T) {
// remove all locks
for _, test := range deleteTests {
session := loginUser(t, test.user.Name)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{})
req.AddBasicAuth(test.user.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType)
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
var lfsLockRep api.LFSLockResponse
DecodeJSON(t, resp, &lfsLockRep)
assert.Equal(t, test.lockID, lfsLockRep.Lock.ID)
@ -170,10 +171,10 @@ func TestAPILFSLocksLogged(t *testing.T) {
// check that we don't have any lock
for _, test := range resultsTests {
session := loginUser(t, test.user.Name)
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
req.AddBasicAuth(test.user.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
var lfsLocks api.LFSLockList
DecodeJSON(t, resp, &lfsLocks)
assert.Empty(t, lfsLocks.Locks)

View file

@ -83,8 +83,6 @@ func TestAPILFSBatch(t *testing.T) {
oid := storeObjectInRepo(t, repo.ID, &content)
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
session := loginUser(t, "user2")
newRequest := func(t testing.TB, br *lfs.BatchRequest) *RequestWrapper {
return NewRequestWithJSON(t, "POST", "/user2/lfs-batch-repo.git/info/lfs/objects/batch", br).
SetHeader("Accept", lfs.AcceptHeader).
@ -101,8 +99,9 @@ func TestAPILFSBatch(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, nil)
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusBadRequest)
MakeRequest(t, req, http.StatusBadRequest)
})
t.Run("InvalidOperation", func(t *testing.T) {
@ -111,8 +110,9 @@ func TestAPILFSBatch(t *testing.T) {
req := newRequest(t, &lfs.BatchRequest{
Operation: "dummy",
})
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusBadRequest)
MakeRequest(t, req, http.StatusBadRequest)
})
t.Run("InvalidPointer", func(t *testing.T) {
@ -125,8 +125,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: oid, Size: -1},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 2)
assert.Equal(t, "dummy", br.Objects[0].Oid)
@ -150,8 +151,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: oid, Size: 1},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.NotNil(t, br.Objects[0].Error)
@ -171,8 +173,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.NotNil(t, br.Objects[0].Error)
@ -195,8 +198,9 @@ func TestAPILFSBatch(t *testing.T) {
Operation: "download",
Objects: []lfs.Pointer{p},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.NotNil(t, br.Objects[0].Error)
@ -212,8 +216,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: oid, Size: 6},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.Nil(t, br.Objects[0].Error)
@ -237,8 +242,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.NotNil(t, br.Objects[0].Error)
@ -268,8 +274,9 @@ func TestAPILFSBatch(t *testing.T) {
Operation: "upload",
Objects: []lfs.Pointer{p},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.Nil(t, br.Objects[0].Error)
@ -293,8 +300,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: oid, Size: 6},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.Nil(t, br.Objects[0].Error)
@ -310,8 +318,9 @@ func TestAPILFSBatch(t *testing.T) {
{Oid: "d6f175817f886ec6fbbc1515326465fa96c3bfd54a4ea06cfd6dbbd8340e0153", Size: 1},
},
})
req.AddBasicAuth("user2")
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
br := decodeResponse(t, resp.Body)
assert.Len(t, br.Objects, 1)
assert.Nil(t, br.Objects[0].Error)
@ -338,8 +347,6 @@ func TestAPILFSUpload(t *testing.T) {
oid := storeObjectInRepo(t, repo.ID, &content)
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
session := loginUser(t, "user2")
newRequest := func(t testing.TB, p lfs.Pointer, content string) *RequestWrapper {
return NewRequestWithBody(t, "PUT", path.Join("/user2/lfs-upload-repo.git/info/lfs/objects/", p.Oid, strconv.FormatInt(p.Size, 10)), strings.NewReader(content))
}
@ -348,8 +355,9 @@ func TestAPILFSUpload(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, lfs.Pointer{Oid: "dummy"}, "")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("AlreadyExistsInStore", func(t *testing.T) {
@ -370,13 +378,15 @@ func TestAPILFSUpload(t *testing.T) {
t.Run("InvalidAccess", func(t *testing.T) {
req := newRequest(t, p, "invalid")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
req.AddBasicAuth("user2")
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("ValidAccess", func(t *testing.T) {
req := newRequest(t, p, "dummy5")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
meta, err = git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
require.NoError(t, err)
assert.NotNil(t, meta)
@ -391,24 +401,27 @@ func TestAPILFSUpload(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, lfs.Pointer{Oid: oid, Size: 6}, "")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
})
t.Run("HashMismatch", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, lfs.Pointer{Oid: "2581dd7bbc1fe44726de4b7dd806a087a978b9c5aec0a60481259e34be09b06a", Size: 1}, "a")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("SizeMismatch", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, lfs.Pointer{Oid: "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", Size: 2}, "a")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("Success", func(t *testing.T) {
@ -417,8 +430,9 @@ func TestAPILFSUpload(t *testing.T) {
p := lfs.Pointer{Oid: "6ccce4863b70f258d691f59609d31b4502e1ba5199942d3bc5d35d17a4ce771d", Size: 5}
req := newRequest(t, p, "gitea")
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
contentStore := lfs.NewContentStore()
exist, err := contentStore.Exists(p)
@ -442,8 +456,6 @@ func TestAPILFSVerify(t *testing.T) {
oid := storeObjectInRepo(t, repo.ID, &content)
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
session := loginUser(t, "user2")
newRequest := func(t testing.TB, p *lfs.Pointer) *RequestWrapper {
return NewRequestWithJSON(t, "POST", "/user2/lfs-verify-repo.git/info/lfs/verify", p).
SetHeader("Accept", lfs.AcceptHeader).
@ -454,31 +466,35 @@ func TestAPILFSVerify(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, nil)
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("InvalidPointer", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, &lfs.Pointer{})
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
MakeRequest(t, req, http.StatusUnprocessableEntity)
})
t.Run("PointerNotExisting", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, &lfs.Pointer{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6})
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusNotFound)
MakeRequest(t, req, http.StatusNotFound)
})
t.Run("Success", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := newRequest(t, &lfs.Pointer{Oid: oid, Size: 6})
req.AddBasicAuth("user2")
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
})
}

View file

@ -138,9 +138,10 @@ func TestLFSLockView(t *testing.T) {
lockID := ""
{
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", repo3.FullName()), map[string]string{"path": lockPath})
req.AddBasicAuth(user2.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType)
resp := session.MakeRequest(t, req, http.StatusCreated)
resp := MakeRequest(t, req, http.StatusCreated)
lockResp := &api.LFSLockResponse{}
DecodeJSON(t, resp, lockResp)
lockID = lockResp.Lock.ID
@ -148,9 +149,10 @@ func TestLFSLockView(t *testing.T) {
defer func() {
// release the lock
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", repo3.FullName(), lockID), map[string]string{})
req.AddBasicAuth(user2.Name)
req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType)
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
}()
t.Run("owner name", func(t *testing.T) {
@ -161,6 +163,7 @@ func TestLFSLockView(t *testing.T) {
require.NotEqual(t, user2.DisplayName(), repo3.Owner.DisplayName())
req := NewRequest(t, "GET", fmt.Sprintf("/%s/settings/lfs/locks", repo3.FullName()))
req.AddBasicAuth(user2.Name)
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body).doc