2021-06-06 05:29:27 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-06 05:29:27 +05:30
|
|
|
|
2022-09-03 00:48:23 +05:30
|
|
|
package integration
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-18 03:16:03 +05:30
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-12-03 08:18:26 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 14:23:21 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-06-06 05:29:27 +05:30
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-09-03 00:48:23 +05:30
|
|
|
"code.gitea.io/gitea/tests"
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPILFSNotStarted(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
setting.LFS.StartServer = false
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := NewRequestf(t, "POST", "/%s/%s.git/info/lfs/objects/batch", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequestf(t, "PUT", "/%s/%s.git/info/lfs/objects/oid/10", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequestf(t, "GET", "/%s/%s.git/info/lfs/objects/oid/name", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequestf(t, "GET", "/%s/%s.git/info/lfs/objects/oid", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequestf(t, "POST", "/%s/%s.git/info/lfs/verify", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPILFSMediaType(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
setting.LFS.StartServer = true
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := NewRequestf(t, "POST", "/%s/%s.git/info/lfs/objects/batch", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusUnsupportedMediaType)
|
|
|
|
req = NewRequestf(t, "POST", "/%s/%s.git/info/lfs/verify", user.Name, repo.Name)
|
|
|
|
MakeRequest(t, req, http.StatusUnsupportedMediaType)
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
func createLFSTestRepository(t *testing.T, name string) *repo_model.Repository {
|
Redesign Scoped Access Tokens (#24767)
## Changes
- Adds the following high level access scopes, each with `read` and
`write` levels:
- `activitypub`
- `admin` (hidden if user is not a site admin)
- `misc`
- `notification`
- `organization`
- `package`
- `issue`
- `repository`
- `user`
- Adds new middleware function `tokenRequiresScopes()` in addition to
`reqToken()`
- `tokenRequiresScopes()` is used for each high-level api section
- _if_ a scoped token is present, checks that the required scope is
included based on the section and HTTP method
- `reqToken()` is used for individual routes
- checks that required authentication is present (but does not check
scope levels as this will already have been handled by
`tokenRequiresScopes()`
- Adds migration to convert old scoped access tokens to the new set of
scopes
- Updates the user interface for scope selection
### User interface example
<img width="903" alt="Screen Shot 2023-05-31 at 1 56 55 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/654766ec-2143-4f59-9037-3b51600e32f3">
<img width="917" alt="Screen Shot 2023-05-31 at 1 56 43 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/1ad64081-012c-4a73-b393-66b30352654c">
## tokenRequiresScopes Design Decision
- `tokenRequiresScopes()` was added to more reliably cover api routes.
For an incoming request, this function uses the given scope category
(say `AccessTokenScopeCategoryOrganization`) and the HTTP method (say
`DELETE`) and verifies that any scoped tokens in use include
`delete:organization`.
- `reqToken()` is used to enforce auth for individual routes that
require it. If a scoped token is not present for a request,
`tokenRequiresScopes()` will not return an error
## TODO
- [x] Alphabetize scope categories
- [x] Change 'public repos only' to a radio button (private vs public).
Also expand this to organizations
- [X] Disable token creation if no scopes selected. Alternatively, show
warning
- [x] `reqToken()` is missing from many `POST/DELETE` routes in the api.
`tokenRequiresScopes()` only checks that a given token has the correct
scope, `reqToken()` must be used to check that a token (or some other
auth) is present.
- _This should be addressed in this PR_
- [x] The migration should be reviewed very carefully in order to
minimize access changes to existing user tokens.
- _This should be addressed in this PR_
- [x] Link to api to swagger documentation, clarify what
read/write/delete levels correspond to
- [x] Review cases where more than one scope is needed as this directly
deviates from the api definition.
- _This should be addressed in this PR_
- For example:
```go
m.Group("/users/{username}/orgs", func() {
m.Get("", reqToken(), org.ListUserOrgs)
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser,
auth_model.AccessTokenScopeCategoryOrganization),
context_service.UserAssignmentAPI())
```
## Future improvements
- [ ] Add required scopes to swagger documentation
- [ ] Redesign `reqToken()` to be opt-out rather than opt-in
- [ ] Subdivide scopes like `repository`
- [ ] Once a token is created, if it has no scopes, we should display
text instead of an empty bullet point
- [ ] If the 'public repos only' option is selected, should read
categories be selected by default
Closes #24501
Closes #24799
Co-authored-by: Jonathan Tran <jon@allspice.io>
Co-authored-by: Kyle D <kdumontnu@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2023-06-05 00:27:16 +05:30
|
|
|
ctx := NewAPITestContext(t, "user2", "lfs-"+name+"-repo", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
2021-06-06 05:29:27 +05:30
|
|
|
t.Run("CreateRepo", doAPICreateRepository(ctx, false))
|
|
|
|
|
2022-12-03 08:18:26 +05:30
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "lfs-"+name+"-repo")
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPILFSBatch(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
setting.LFS.StartServer = true
|
|
|
|
|
|
|
|
repo := createLFSTestRepository(t, "batch")
|
|
|
|
|
|
|
|
content := []byte("dummy1")
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, &content)
|
2023-01-09 09:20:54 +05:30
|
|
|
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
2023-12-22 05:29:59 +05:30
|
|
|
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.MediaType).
|
|
|
|
SetHeader("Content-Type", lfs.MediaType)
|
2021-06-06 05:29:27 +05:30
|
|
|
}
|
|
|
|
decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
|
|
|
|
var br lfs.BatchResponse
|
|
|
|
|
|
|
|
assert.NoError(t, json.Unmarshal(b.Bytes(), &br))
|
|
|
|
return &br
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InvalidJsonRequest", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, nil)
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("InvalidOperation", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "dummy",
|
|
|
|
})
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusBadRequest)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("InvalidPointer", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "download",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: "dummy"},
|
|
|
|
{Oid: oid, Size: -1},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 2)
|
|
|
|
assert.Equal(t, "dummy", br.Objects[0].Oid)
|
|
|
|
assert.Equal(t, oid, br.Objects[1].Oid)
|
|
|
|
assert.Equal(t, int64(0), br.Objects[0].Size)
|
|
|
|
assert.Equal(t, int64(-1), br.Objects[1].Size)
|
|
|
|
assert.NotNil(t, br.Objects[0].Error)
|
|
|
|
assert.NotNil(t, br.Objects[1].Error)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, br.Objects[0].Error.Code)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, br.Objects[1].Error.Code)
|
|
|
|
assert.Equal(t, "Oid or size are invalid", br.Objects[0].Error.Message)
|
|
|
|
assert.Equal(t, "Oid or size are invalid", br.Objects[1].Error.Message)
|
|
|
|
})
|
|
|
|
|
2021-07-08 18:25:09 +05:30
|
|
|
t.Run("PointerSizeMismatch", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "download",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: oid, Size: 1},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.NotNil(t, br.Objects[0].Error)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, br.Objects[0].Error.Code)
|
|
|
|
assert.Equal(t, "Object "+oid+" is not 1 bytes", br.Objects[0].Error.Message)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Download", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
t.Run("PointerNotInStore", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "download",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.NotNil(t, br.Objects[0].Error)
|
|
|
|
assert.Equal(t, http.StatusNotFound, br.Objects[0].Error.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MetaNotFound", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
p := lfs.Pointer{Oid: "05eeb4eb5be71f2dd291ca39157d6d9effd7d1ea19cbdc8a99411fe2a8f26a00", Size: 6}
|
|
|
|
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
|
|
|
err = contentStore.Put(p, bytes.NewReader([]byte("dummy0")))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "download",
|
|
|
|
Objects: []lfs.Pointer{p},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.NotNil(t, br.Objects[0].Error)
|
|
|
|
assert.Equal(t, http.StatusNotFound, br.Objects[0].Error.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "download",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: oid, Size: 6},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.Nil(t, br.Objects[0].Error)
|
|
|
|
assert.Contains(t, br.Objects[0].Actions, "download")
|
|
|
|
l := br.Objects[0].Actions["download"]
|
|
|
|
assert.NotNil(t, l)
|
|
|
|
assert.NotEmpty(t, l.Href)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Upload", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
t.Run("FileTooBig", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
oldMaxFileSize := setting.LFS.MaxFileSize
|
|
|
|
setting.LFS.MaxFileSize = 2
|
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "upload",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.NotNil(t, br.Objects[0].Error)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, br.Objects[0].Error.Code)
|
|
|
|
assert.Equal(t, "Size must be less than or equal to 2", br.Objects[0].Error.Message)
|
|
|
|
|
|
|
|
setting.LFS.MaxFileSize = oldMaxFileSize
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AddMeta", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
p := lfs.Pointer{Oid: "05eeb4eb5be71f2dd291ca39157d6d9effd7d1ea19cbdc8a99411fe2a8f26a00", Size: 6}
|
|
|
|
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
|
|
|
|
2021-08-31 19:05:08 +05:30
|
|
|
repo2 := createLFSTestRepository(t, "batch2")
|
|
|
|
content := []byte("dummy0")
|
|
|
|
storeObjectInRepo(t, repo2.ID, &content)
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.Nil(t, meta)
|
2022-06-12 21:21:54 +05:30
|
|
|
assert.Equal(t, git_model.ErrLFSObjectNotExist, err)
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "upload",
|
|
|
|
Objects: []lfs.Pointer{p},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.Nil(t, br.Objects[0].Error)
|
|
|
|
assert.Empty(t, br.Objects[0].Actions)
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
meta, err = git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, meta)
|
2021-12-21 08:42:27 +05:30
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
err = contentStore.Delete(p.RelativePath())
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 05:29:27 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AlreadyExists", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "upload",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: oid, Size: 6},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.Nil(t, br.Objects[0].Error)
|
|
|
|
assert.Empty(t, br.Objects[0].Actions)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NewFile", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.BatchRequest{
|
|
|
|
Operation: "upload",
|
|
|
|
Objects: []lfs.Pointer{
|
|
|
|
{Oid: "d6f175817f886ec6fbbc1515326465fa96c3bfd54a4ea06cfd6dbbd8340e0153", Size: 1},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
br := decodeResponse(t, resp.Body)
|
|
|
|
assert.Len(t, br.Objects, 1)
|
|
|
|
assert.Nil(t, br.Objects[0].Error)
|
|
|
|
assert.Contains(t, br.Objects[0].Actions, "upload")
|
|
|
|
ul := br.Objects[0].Actions["upload"]
|
|
|
|
assert.NotNil(t, ul)
|
|
|
|
assert.NotEmpty(t, ul.Href)
|
|
|
|
assert.Contains(t, br.Objects[0].Actions, "verify")
|
|
|
|
vl := br.Objects[0].Actions["verify"]
|
|
|
|
assert.NotNil(t, vl)
|
|
|
|
assert.NotEmpty(t, vl.Href)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPILFSUpload(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
setting.LFS.StartServer = true
|
|
|
|
|
|
|
|
repo := createLFSTestRepository(t, "upload")
|
|
|
|
|
|
|
|
content := []byte("dummy3")
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, &content)
|
2023-01-09 09:20:54 +05:30
|
|
|
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
2023-12-22 05:29:59 +05:30
|
|
|
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))
|
2021-06-06 05:29:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InvalidPointer", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, lfs.Pointer{Oid: "dummy"}, "")
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AlreadyExistsInStore", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
p := lfs.Pointer{Oid: "83de2e488b89a0aa1c97496b888120a28b0c1e15463a4adb8405578c540f36d4", Size: 6}
|
|
|
|
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
|
|
|
err = contentStore.Put(p, bytes.NewReader([]byte("dummy5")))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.Nil(t, meta)
|
2022-06-12 21:21:54 +05:30
|
|
|
assert.Equal(t, git_model.ErrLFSObjectNotExist, err)
|
2021-06-06 05:29:27 +05:30
|
|
|
|
2021-08-31 19:05:08 +05:30
|
|
|
t.Run("InvalidAccess", func(t *testing.T) {
|
|
|
|
req := newRequest(t, p, "invalid")
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
2021-06-06 05:29:27 +05:30
|
|
|
|
2021-08-31 19:05:08 +05:30
|
|
|
t.Run("ValidAccess", func(t *testing.T) {
|
|
|
|
req := newRequest(t, p, "dummy5")
|
2021-06-06 05:29:27 +05:30
|
|
|
|
2021-08-31 19:05:08 +05:30
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
2023-01-09 09:20:54 +05:30
|
|
|
meta, err = git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
|
2021-08-31 19:05:08 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, meta)
|
|
|
|
})
|
2021-12-21 08:42:27 +05:30
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
err = contentStore.Delete(p.RelativePath())
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 05:29:27 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MetaAlreadyExists", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, lfs.Pointer{Oid: oid, Size: 6}, "")
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
})
|
|
|
|
|
2021-07-08 18:25:09 +05:30
|
|
|
t.Run("HashMismatch", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, lfs.Pointer{Oid: "2581dd7bbc1fe44726de4b7dd806a087a978b9c5aec0a60481259e34be09b06a", Size: 1}, "a")
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
|
|
|
|
2021-07-08 18:25:09 +05:30
|
|
|
t.Run("SizeMismatch", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, lfs.Pointer{Oid: "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", Size: 2}, "a")
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
p := lfs.Pointer{Oid: "6ccce4863b70f258d691f59609d31b4502e1ba5199942d3bc5d35d17a4ce771d", Size: 5}
|
|
|
|
|
|
|
|
req := newRequest(t, p, "gitea")
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, p.Oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, meta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPILFSVerify(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
setting.LFS.StartServer = true
|
|
|
|
|
|
|
|
repo := createLFSTestRepository(t, "verify")
|
|
|
|
|
|
|
|
content := []byte("dummy3")
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, &content)
|
2023-01-09 09:20:54 +05:30
|
|
|
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
2023-12-22 05:29:59 +05:30
|
|
|
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.MediaType).
|
|
|
|
SetHeader("Content-Type", lfs.MediaType)
|
2021-06-06 05:29:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InvalidJsonRequest", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, nil)
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("InvalidPointer", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.Pointer{})
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("PointerNotExisting", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.Pointer{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab042", Size: 6})
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
2021-06-06 05:29:27 +05:30
|
|
|
|
|
|
|
req := newRequest(t, &lfs.Pointer{Oid: oid, Size: 6})
|
|
|
|
|
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
})
|
|
|
|
}
|