1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-07-26 04:59:39 +02:00
forgejo/tests/integration/api_repo_file_get_test.go

57 lines
1.6 KiB
Go
Raw Normal View History

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"net/url"
"testing"
auth_model "forgejo.org/models/auth"
"forgejo.org/modules/git"
api "forgejo.org/modules/structs"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
func TestAPIGetRawFileOrLFS(t *testing.T) {
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
onGiteaRun(t, func(t *testing.T, u *url.URL) {
t.Run("Normal raw file", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/README.md")
resp := MakeRequest(t, req, http.StatusOK)
assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String())
})
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
t.Run("LFS raw file", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository)
doAPICreateRepository(httpContext, nil, git.Sha1ObjectFormat, func(t *testing.T, repository api.Repository) { // FIXME: use forEachObjectFormat
u.Path = httpContext.GitPath()
dstPath := t.TempDir()
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
u.Path = httpContext.GitPath()
u.User = url.UserPassword("user2", userPassword)
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
t.Run("Clone", doGitClone(dstPath, u))
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
dstPath2 := t.TempDir()
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
t.Run("Partial Clone", doPartialGitClone(dstPath2, u))
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
lfs, _ := lfsCommitAndPushTest(t, dstPath)
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
reqLFS := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/"+lfs)
respLFS := MakeRequestNilResponseRecorder(t, reqLFS, http.StatusOK)
assert.Equal(t, littleSize, respLFS.Length)
feat: detect incorrect integration test functions (#8352) - I have seen multiple times where a test function tries to prepare the testing environment more than once, this can lead to bugs and false positives of testing code. I would attribute this to lack of documentation on how to write integration tests. - To detect such cases, keep track when we are in a prepared test environment and fail when some testing code is tries to once again prepare the test environment. - The message is logged to the function call that is requesting to prepare the test environment, for example: `change_default_branch_test.go:19: Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.` A example of what this will be able to catch, 6226f464cec870991302c62a514d11ddb2066b69: ```go func TestFoo(t *testing.T) { defer PrepareTestEnv(t)() t.Run("Bar", func(t *testing.T) { defer PrepareTestEnv(t)() // Should very likely be PrintCurrentTest }) } ``` ```go func TestBar(t *testing.T) { onGiteaRun(t, func(t *testing.T, _ *url.URL) { defer PrepareTestEnv(t)() // Already called by onGiteaRun. }) } ``` ```go func TestFooBar(t *testing.T) { defer PrepareTestEnv(t)() // This will be called by onGiteaRun later on and very unlikely to do this before the call to onGiteaRun. onGiteaRun(t, func(t *testing.T, _ *url.URL) { // [...] }) } ``` Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8352 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-06-29 23:09:29 +02:00
doAPIDeleteRepository(httpContext)
})
})
})
}