2019-01-23 14:26:51 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-23 14:26:51 +05:30
|
|
|
|
2022-09-03 00:48:23 +05:30
|
|
|
package integration
|
2019-01-23 14:26:51 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2021-09-22 11:08:34 +05:30
|
|
|
"io"
|
2019-01-23 14:26:51 +05:30
|
|
|
"net/http"
|
2020-05-11 14:07:59 +05:30
|
|
|
"net/http/httptest"
|
2019-01-23 14:26:51 +05:30
|
|
|
"testing"
|
|
|
|
|
2023-04-28 02:13:27 +05:30
|
|
|
"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-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-01-23 14:26:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-06-09 05:03:54 +05:30
|
|
|
"code.gitea.io/gitea/routers/web"
|
2022-09-03 00:48:23 +05:30
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-23 14:26:51 +05:30
|
|
|
|
|
|
|
gzipp "github.com/klauspost/compress/gzip"
|
2019-08-23 22:10:30 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-23 14:26:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
|
2021-04-09 03:55:57 +05:30
|
|
|
pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
|
2019-01-23 14:26:51 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
_, err = git_model.NewLFSMetaObject(db.DefaultContext, &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
|
2019-01-23 14:26:51 +05:30
|
|
|
assert.NoError(t, err)
|
2021-04-09 03:55:57 +05:30
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
exist, err := contentStore.Exists(pointer)
|
2020-09-08 21:15:10 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
if !exist {
|
2021-04-09 03:55:57 +05:30
|
|
|
err := contentStore.Put(pointer, bytes.NewReader(*content))
|
2019-01-23 14:26:51 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-04-09 03:55:57 +05:30
|
|
|
return pointer.Oid
|
2019-01-23 14:26:51 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
func storeAndGetLfsToken(t *testing.T, content *[]byte, extraHeader *http.Header, expectedStatus int, ts ...auth.AccessTokenScope) *httptest.ResponseRecorder {
|
2023-04-28 02:13:27 +05:30
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, content)
|
|
|
|
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
|
|
|
|
|
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
|
|
|
token := getUserToken(t, "user2", ts...)
|
2023-04-28 02:13:27 +05:30
|
|
|
|
|
|
|
// Request OID
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
req.SetBasicAuth("user2", token)
|
|
|
|
if extraHeader != nil {
|
|
|
|
for key, values := range *extraHeader {
|
|
|
|
for _, value := range values {
|
|
|
|
req.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := MakeRequest(t, req, expectedStatus)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-05-11 14:07:59 +05:30
|
|
|
func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, expectedStatus int) *httptest.ResponseRecorder {
|
2022-12-03 08:18:26 +05:30
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
|
2019-01-23 14:26:51 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, content)
|
2023-01-09 09:20:54 +05:30
|
|
|
defer git_model.RemoveLFSMetaObjectByOid(db.DefaultContext, repo.ID, oid)
|
2019-01-23 14:26:51 +05:30
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
// Request OID
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
2020-05-11 14:07:59 +05:30
|
|
|
if extraHeader != nil {
|
|
|
|
for key, values := range *extraHeader {
|
|
|
|
for _, value := range values {
|
|
|
|
req.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 02:21:48 +05:30
|
|
|
|
2020-05-11 14:07:59 +05:30
|
|
|
resp := session.MakeRequest(t, req, expectedStatus)
|
2019-01-23 14:26:51 +05:30
|
|
|
|
2020-05-11 14:07:59 +05:30
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httptest.ResponseRecorder, expectGzip bool) {
|
2019-01-23 14:26:51 +05:30
|
|
|
contentEncoding := resp.Header().Get("Content-Encoding")
|
|
|
|
if !expectGzip || !setting.EnableGzip {
|
|
|
|
assert.NotContains(t, contentEncoding, "gzip")
|
|
|
|
|
|
|
|
result := resp.Body.Bytes()
|
|
|
|
assert.Equal(t, *content, result)
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, contentEncoding, "gzip")
|
|
|
|
gzippReader, err := gzipp.NewReader(resp.Body)
|
|
|
|
assert.NoError(t, err)
|
2021-09-22 11:08:34 +05:30
|
|
|
result, err := io.ReadAll(gzippReader)
|
2019-01-23 14:26:51 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, *content, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSSmall(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-01-23 14:26:51 +05:30
|
|
|
content := []byte("A very small file\n")
|
2020-05-11 14:07:59 +05:30
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
2019-01-23 14:26:51 +05:30
|
|
|
}
|
|
|
|
|
2023-04-28 02:13:27 +05:30
|
|
|
func TestGetLFSSmallToken(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
content := []byte("A very small file\n")
|
|
|
|
|
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
|
|
|
resp := storeAndGetLfsToken(t, &content, nil, http.StatusOK, auth.AccessTokenScopePublicOnly, auth.AccessTokenScopeReadRepository)
|
2023-04-28 02:13:27 +05:30
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSSmallTokenFail(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
content := []byte("A very small file\n")
|
|
|
|
|
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
|
|
|
storeAndGetLfsToken(t, &content, nil, http.StatusForbidden, auth.AccessTokenScopeReadNotification)
|
2023-04-28 02:13:27 +05:30
|
|
|
}
|
|
|
|
|
2019-01-23 14:26:51 +05:30
|
|
|
func TestGetLFSLarge(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-09 05:03:54 +05:30
|
|
|
content := make([]byte, web.GzipMinSize*10)
|
2019-01-23 14:26:51 +05:30
|
|
|
for i := range content {
|
|
|
|
content[i] = byte(i % 256)
|
|
|
|
}
|
2020-05-11 14:07:59 +05:30
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, true)
|
2019-01-23 14:26:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSGzip(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-09 05:03:54 +05:30
|
|
|
b := make([]byte, web.GzipMinSize*10)
|
2019-01-23 14:26:51 +05:30
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i % 256)
|
|
|
|
}
|
|
|
|
outputBuffer := bytes.NewBuffer([]byte{})
|
|
|
|
gzippWriter := gzipp.NewWriter(outputBuffer)
|
|
|
|
gzippWriter.Write(b)
|
|
|
|
gzippWriter.Close()
|
|
|
|
content := outputBuffer.Bytes()
|
2020-05-11 14:07:59 +05:30
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
2019-01-23 14:26:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSZip(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-06-09 05:03:54 +05:30
|
|
|
b := make([]byte, web.GzipMinSize*10)
|
2019-01-23 14:26:51 +05:30
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i % 256)
|
|
|
|
}
|
|
|
|
outputBuffer := bytes.NewBuffer([]byte{})
|
|
|
|
zipWriter := zip.NewWriter(outputBuffer)
|
|
|
|
fileWriter, err := zipWriter.Create("default")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fileWriter.Write(b)
|
|
|
|
zipWriter.Close()
|
|
|
|
content := outputBuffer.Bytes()
|
2020-05-11 14:07:59 +05:30
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSRangeNo(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2020-05-11 14:07:59 +05:30
|
|
|
content := []byte("123456789\n")
|
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
assert.Equal(t, content, resp.Body.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSRange(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2020-05-11 14:07:59 +05:30
|
|
|
content := []byte("123456789\n")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
status int
|
|
|
|
}{
|
|
|
|
{"bytes=0-0", "1", http.StatusPartialContent},
|
|
|
|
{"bytes=0-1", "12", http.StatusPartialContent},
|
|
|
|
{"bytes=1-1", "2", http.StatusPartialContent},
|
|
|
|
{"bytes=1-3", "234", http.StatusPartialContent},
|
|
|
|
{"bytes=1-", "23456789\n", http.StatusPartialContent},
|
|
|
|
// end-range smaller than start-range is ignored
|
|
|
|
{"bytes=1-0", "23456789\n", http.StatusPartialContent},
|
|
|
|
{"bytes=0-10", "123456789\n", http.StatusPartialContent},
|
|
|
|
// end-range bigger than length-1 is ignored
|
|
|
|
{"bytes=0-11", "123456789\n", http.StatusPartialContent},
|
2020-11-01 02:21:48 +05:30
|
|
|
{"bytes=11-", "Requested Range Not Satisfiable", http.StatusRequestedRangeNotSatisfiable},
|
2020-05-11 14:07:59 +05:30
|
|
|
// incorrect header value cause whole header to be ignored
|
|
|
|
{"bytes=-", "123456789\n", http.StatusOK},
|
|
|
|
{"foobar", "123456789\n", http.StatusOK},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.in, func(t *testing.T) {
|
|
|
|
h := http.Header{
|
|
|
|
"Range": []string{tt.in},
|
|
|
|
}
|
|
|
|
resp := storeAndGetLfs(t, &content, &h, tt.status)
|
2021-06-06 05:29:27 +05:30
|
|
|
if tt.status == http.StatusPartialContent || tt.status == http.StatusOK {
|
|
|
|
assert.Equal(t, tt.out, resp.Body.String())
|
|
|
|
} else {
|
|
|
|
var er lfs.ErrorResponse
|
2021-07-24 21:33:58 +05:30
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &er)
|
2021-06-06 05:29:27 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.out, er.Message)
|
|
|
|
}
|
2020-05-11 14:07:59 +05:30
|
|
|
})
|
|
|
|
}
|
2019-01-23 14:26:51 +05:30
|
|
|
}
|