mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
Merge branch 'main' into lunny/automerge_support_delete_branch
This commit is contained in:
+4
-12
@@ -5,7 +5,6 @@ package fuzz
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
@@ -14,27 +13,20 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
var renderContext = markup.RenderContext{
|
||||
Ctx: context.Background(),
|
||||
Links: markup.Links{
|
||||
Base: "https://example.com/go-gitea/gitea",
|
||||
},
|
||||
Metas: map[string]string{
|
||||
"user": "go-gitea",
|
||||
"repo": "gitea",
|
||||
},
|
||||
func newFuzzRenderContext() *markup.RenderContext {
|
||||
return markup.NewTestRenderContext("https://example.com/go-gitea/gitea", map[string]string{"user": "go-gitea", "repo": "gitea"})
|
||||
}
|
||||
|
||||
func FuzzMarkdownRenderRaw(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
setting.AppURL = "http://localhost:3000/"
|
||||
markdown.RenderRaw(&renderContext, bytes.NewReader(data), io.Discard)
|
||||
markdown.RenderRaw(newFuzzRenderContext(), bytes.NewReader(data), io.Discard)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzMarkupPostProcess(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
setting.AppURL = "http://localhost:3000/"
|
||||
markup.PostProcess(&renderContext, bytes.NewReader(data), io.Discard)
|
||||
markup.PostProcessDefault(newFuzzRenderContext(), bytes.NewReader(data), io.Discard)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -144,12 +144,12 @@ func TestActionsArtifactDownload(t *testing.T) {
|
||||
var downloadResp downloadArtifactResponse
|
||||
DecodeJSON(t, resp, &downloadResp)
|
||||
assert.Len(t, downloadResp.Value, 1)
|
||||
assert.Equal(t, "artifact-download/abc.txt", downloadResp.Value[artifactIdx].Path)
|
||||
assert.Equal(t, "file", downloadResp.Value[artifactIdx].ItemType)
|
||||
assert.Contains(t, downloadResp.Value[artifactIdx].ContentLocation, "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts")
|
||||
assert.Equal(t, "artifact-download/abc.txt", downloadResp.Value[0].Path)
|
||||
assert.Equal(t, "file", downloadResp.Value[0].ItemType)
|
||||
assert.Contains(t, downloadResp.Value[0].ContentLocation, "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts")
|
||||
|
||||
idx = strings.Index(downloadResp.Value[artifactIdx].ContentLocation, "/api/actions_pipeline/_apis/pipelines/")
|
||||
url = downloadResp.Value[artifactIdx].ContentLocation[idx:]
|
||||
idx = strings.Index(downloadResp.Value[0].ContentLocation, "/api/actions_pipeline/_apis/pipelines/")
|
||||
url = downloadResp.Value[0].ContentLocation[idx:]
|
||||
req = NewRequest(t, "GET", url).
|
||||
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
@@ -49,7 +49,7 @@ func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPSta
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
|
||||
func testAPICreateBranchProtection(t *testing.T, branchName string, expectedPriority, expectedHTTPStatus int) {
|
||||
token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
|
||||
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections", &api.BranchProtection{
|
||||
RuleName: branchName,
|
||||
@@ -60,6 +60,7 @@ func testAPICreateBranchProtection(t *testing.T, branchName string, expectedHTTP
|
||||
var branchProtection api.BranchProtection
|
||||
DecodeJSON(t, resp, &branchProtection)
|
||||
assert.EqualValues(t, branchName, branchProtection.RuleName)
|
||||
assert.EqualValues(t, expectedPriority, branchProtection.Priority)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,13 +190,13 @@ func TestAPIBranchProtection(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
// Branch protection on branch that not exist
|
||||
testAPICreateBranchProtection(t, "master/doesnotexist", http.StatusCreated)
|
||||
testAPICreateBranchProtection(t, "master/doesnotexist", 1, http.StatusCreated)
|
||||
// Get branch protection on branch that exist but not branch protection
|
||||
testAPIGetBranchProtection(t, "master", http.StatusNotFound)
|
||||
|
||||
testAPICreateBranchProtection(t, "master", http.StatusCreated)
|
||||
testAPICreateBranchProtection(t, "master", 2, http.StatusCreated)
|
||||
// Can only create once
|
||||
testAPICreateBranchProtection(t, "master", http.StatusForbidden)
|
||||
testAPICreateBranchProtection(t, "master", 0, http.StatusForbidden)
|
||||
|
||||
// Can't delete a protected branch
|
||||
testAPIDeleteBranch(t, "master", http.StatusForbidden)
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
org_model "code.gitea.io/gitea/models/organization"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
org_service "code.gitea.io/gitea/services/org"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -37,7 +37,7 @@ func TestAPIForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
|
||||
ownerTeam1, err := org_model.OrgFromUser(limitedOrg).GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam1, user1))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam1, user1))
|
||||
user1Token := getTokenForLoggedInUser(t, user1Sess, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteOrganization)
|
||||
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/forks", &api.CreateForkOption{
|
||||
Organization: &limitedOrg.Name,
|
||||
@@ -52,7 +52,7 @@ func TestAPIForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
|
||||
ownerTeam2, err := org_model.OrgFromUser(privateOrg).GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam2, user4))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam2, user4))
|
||||
user4Token := getTokenForLoggedInUser(t, user4Sess, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteOrganization)
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/forks", &api.CreateForkOption{
|
||||
Organization: &privateOrg.Name,
|
||||
@@ -84,7 +84,7 @@ func TestAPIForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
assert.Len(t, forks, 1)
|
||||
assert.EqualValues(t, "1", resp.Header().Get("X-Total-Count"))
|
||||
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam2, user1))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam2, user1))
|
||||
|
||||
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/forks").AddTokenAuth(user1Token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
@@ -144,6 +144,18 @@ func TestAPICreateIssue(t *testing.T) {
|
||||
|
||||
func TestAPICreateIssueParallel(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
// FIXME: There seems to be a bug in github.com/mattn/go-sqlite3 with sqlite_unlock_notify, when doing concurrent writes to the same database,
|
||||
// some requests may get stuck in "go-sqlite3.(*SQLiteRows).Next", "go-sqlite3.(*SQLiteStmt).exec" and "go-sqlite3.unlock_notify_wait",
|
||||
// because the "unlock_notify_wait" never returns and the internal lock never gets releases.
|
||||
//
|
||||
// The trigger is: a previous test created issues and made the real issue indexer queue start processing, then this test does concurrent writing.
|
||||
// Adding this "Sleep" makes go-sqlite3 "finish" some internal operations before concurrent writes and then won't get stuck.
|
||||
// To reproduce: make a new test run these 2 tests enough times:
|
||||
// > func TestBug() { for i := 0; i < 100; i++ { testAPICreateIssue(t); testAPICreateIssueParallel(t) } }
|
||||
// Usually the test gets stuck in fewer than 10 iterations without this "sleep".
|
||||
time.Sleep(time.Second)
|
||||
|
||||
const body, title = "apiTestBody", "apiTestTitle"
|
||||
|
||||
repoBefore := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/packages"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
arch_module "code.gitea.io/gitea/modules/packages/arch"
|
||||
arch_service "code.gitea.io/gitea/services/packages/arch"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/ulikunitz/xz"
|
||||
)
|
||||
|
||||
func TestPackageArch(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
|
||||
packageName := "gitea-test"
|
||||
packageVersion := "1.4.1-r3"
|
||||
|
||||
createPackage := func(compression, name, version, architecture string) []byte {
|
||||
var buf bytes.Buffer
|
||||
var cw io.WriteCloser
|
||||
switch compression {
|
||||
case "zst":
|
||||
cw, _ = zstd.NewWriter(&buf)
|
||||
case "xz":
|
||||
cw, _ = xz.NewWriter(&buf)
|
||||
case "gz":
|
||||
cw = gzip.NewWriter(&buf)
|
||||
}
|
||||
tw := tar.NewWriter(cw)
|
||||
|
||||
info := []byte(`pkgname = ` + name + `
|
||||
pkgbase = ` + name + `
|
||||
pkgver = ` + version + `
|
||||
pkgdesc = Description
|
||||
# comment
|
||||
builddate = 1678834800
|
||||
size = 8
|
||||
arch = ` + architecture + `
|
||||
license = MIT`)
|
||||
|
||||
hdr := &tar.Header{
|
||||
Name: ".PKGINFO",
|
||||
Mode: 0o600,
|
||||
Size: int64(len(info)),
|
||||
}
|
||||
tw.WriteHeader(hdr)
|
||||
tw.Write(info)
|
||||
|
||||
for _, file := range []string{"etc/dummy", "opt/file/bin"} {
|
||||
hdr := &tar.Header{
|
||||
Name: file,
|
||||
Mode: 0o600,
|
||||
Size: 4,
|
||||
}
|
||||
tw.WriteHeader(hdr)
|
||||
tw.Write([]byte("test"))
|
||||
}
|
||||
|
||||
tw.Close()
|
||||
cw.Close()
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
compressions := []string{"gz", "xz", "zst"}
|
||||
repositories := []string{"main", "testing", "with/slash", ""}
|
||||
|
||||
rootURL := fmt.Sprintf("/api/packages/%s/arch", user.Name)
|
||||
|
||||
t.Run("RepositoryKey", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "GET", rootURL+"/repository.key")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
assert.Equal(t, "application/pgp-keys", resp.Header().Get("Content-Type"))
|
||||
assert.Contains(t, resp.Body.String(), "-----BEGIN PGP PUBLIC KEY BLOCK-----")
|
||||
})
|
||||
|
||||
contentAarch64Gz := createPackage("gz", packageName, packageVersion, "aarch64")
|
||||
for _, compression := range compressions {
|
||||
contentAarch64 := createPackage(compression, packageName, packageVersion, "aarch64")
|
||||
contentAny := createPackage(compression, packageName+"_"+arch_module.AnyArch, packageVersion, arch_module.AnyArch)
|
||||
|
||||
for _, repository := range repositories {
|
||||
t.Run(fmt.Sprintf("[%s,%s]", repository, compression), func(t *testing.T) {
|
||||
t.Run("Upload", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
uploadURL := fmt.Sprintf("%s/%s", rootURL, repository)
|
||||
|
||||
req := NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{}))
|
||||
MakeRequest(t, req, http.StatusUnauthorized)
|
||||
|
||||
req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{})).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusBadRequest)
|
||||
|
||||
req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(contentAarch64)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusCreated)
|
||||
|
||||
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeArch)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, pvs, 1)
|
||||
|
||||
pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, pd.SemVer)
|
||||
assert.IsType(t, &arch_module.VersionMetadata{}, pd.Metadata)
|
||||
assert.Equal(t, packageName, pd.Package.Name)
|
||||
assert.Equal(t, packageVersion, pd.Version.Version)
|
||||
|
||||
pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, pfs)
|
||||
assert.Condition(t, func() bool {
|
||||
seen := false
|
||||
expectedFilename := fmt.Sprintf("%s-%s-aarch64.pkg.tar.%s", packageName, packageVersion, compression)
|
||||
expectedCompositeKey := fmt.Sprintf("%s|aarch64", repository)
|
||||
for _, pf := range pfs {
|
||||
if pf.Name == expectedFilename && pf.CompositeKey == expectedCompositeKey {
|
||||
if seen {
|
||||
return false
|
||||
}
|
||||
seen = true
|
||||
|
||||
assert.True(t, pf.IsLead)
|
||||
|
||||
pfps, err := packages.GetProperties(db.DefaultContext, packages.PropertyTypeFile, pf.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, pfp := range pfps {
|
||||
switch pfp.Name {
|
||||
case arch_module.PropertyRepository:
|
||||
assert.Equal(t, repository, pfp.Value)
|
||||
case arch_module.PropertyArchitecture:
|
||||
assert.Equal(t, "aarch64", pfp.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return seen
|
||||
})
|
||||
|
||||
req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(contentAarch64)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusConflict)
|
||||
|
||||
// Add same package with different compression leads to conflict
|
||||
req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(contentAarch64Gz)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusConflict)
|
||||
})
|
||||
|
||||
readIndexContent := func(r io.Reader) (map[string]string, error) {
|
||||
gzr, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
content := make(map[string]string)
|
||||
|
||||
tr := tar.NewReader(gzr)
|
||||
for {
|
||||
hd, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err := io.ReadAll(tr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
content[hd.Name] = string(buf)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
||||
t.Run("Index", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s", rootURL, repository, arch_service.IndexArchiveFilename))
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
content, err := readIndexContent(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
|
||||
desc, has := content[fmt.Sprintf("%s-%s/desc", packageName, packageVersion)]
|
||||
assert.True(t, has)
|
||||
assert.Contains(t, desc, "%FILENAME%\n"+fmt.Sprintf("%s-%s-aarch64.pkg.tar.%s", packageName, packageVersion, compression)+"\n\n")
|
||||
assert.Contains(t, desc, "%NAME%\n"+packageName+"\n\n")
|
||||
assert.Contains(t, desc, "%VERSION%\n"+packageVersion+"\n\n")
|
||||
assert.Contains(t, desc, "%ARCH%\naarch64\n")
|
||||
assert.NotContains(t, desc, "%ARCH%\n"+arch_module.AnyArch+"\n")
|
||||
assert.Contains(t, desc, "%LICENSE%\nMIT\n")
|
||||
|
||||
files, has := content[fmt.Sprintf("%s-%s/files", packageName, packageVersion)]
|
||||
assert.True(t, has)
|
||||
assert.Contains(t, files, "%FILES%\netc/dummy\nopt/file/bin\n\n")
|
||||
|
||||
for _, indexFile := range []string{
|
||||
arch_service.IndexArchiveFilename,
|
||||
arch_service.IndexArchiveFilename + ".tar.gz",
|
||||
"index.db",
|
||||
"index.db.tar.gz",
|
||||
"index.files",
|
||||
"index.files.tar.gz",
|
||||
} {
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s", rootURL, repository, indexFile))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s.sig", rootURL, repository, indexFile))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Download", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s-%s-aarch64.pkg.tar.%s", rootURL, repository, packageName, packageVersion, compression))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s-%s-aarch64.pkg.tar.%s.sig", rootURL, repository, packageName, packageVersion, compression))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("Any", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithBody(t, "PUT", fmt.Sprintf("%s/%s", rootURL, repository), bytes.NewReader(contentAny)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusCreated)
|
||||
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s", rootURL, repository, arch_service.IndexArchiveFilename))
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
content, err := readIndexContent(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
|
||||
desc, has := content[fmt.Sprintf("%s-%s/desc", packageName, packageVersion)]
|
||||
assert.True(t, has)
|
||||
assert.Contains(t, desc, "%NAME%\n"+packageName+"\n\n")
|
||||
assert.Contains(t, desc, "%ARCH%\naarch64\n")
|
||||
|
||||
desc, has = content[fmt.Sprintf("%s-%s/desc", packageName+"_"+arch_module.AnyArch, packageVersion)]
|
||||
assert.True(t, has)
|
||||
assert.Contains(t, desc, "%NAME%\n"+packageName+"_any\n\n")
|
||||
assert.Contains(t, desc, "%ARCH%\n"+arch_module.AnyArch+"\n")
|
||||
|
||||
// "any" architecture package should be available with every architecture requested
|
||||
for _, arch := range []string{arch_module.AnyArch, "aarch64", "myarch"} {
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/%s-%s-any.pkg.tar.%s", rootURL, repository, arch, packageName+"_"+arch_module.AnyArch, packageVersion, compression))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
}
|
||||
|
||||
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/%s/%s/any", rootURL, repository, packageName+"_"+arch_module.AnyArch, packageVersion)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
})
|
||||
|
||||
t.Run("Delete", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/%s/%s/aarch64", rootURL, repository, packageName, packageVersion))
|
||||
MakeRequest(t, req, http.StatusUnauthorized)
|
||||
|
||||
req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/%s/%s/aarch64", rootURL, repository, packageName, packageVersion)).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
|
||||
// Deleting the last file of an architecture should remove that index
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/aarch64/%s", rootURL, repository, arch_service.IndexArchiveFilename))
|
||||
MakeRequest(t, req, http.StatusNotFound)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,24 @@ func TestPackageSwift(t *testing.T) {
|
||||
|
||||
url := fmt.Sprintf("/api/packages/%s/swift", user.Name)
|
||||
|
||||
t.Run("CheckLogin", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithBody(t, "POST", url, strings.NewReader(""))
|
||||
MakeRequest(t, req, http.StatusUnauthorized)
|
||||
|
||||
req = NewRequestWithBody(t, "POST", url, strings.NewReader("")).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
req = NewRequestWithBody(t, "POST", url+"/login", strings.NewReader(""))
|
||||
MakeRequest(t, req, http.StatusUnauthorized)
|
||||
|
||||
req = NewRequestWithBody(t, "POST", url+"/login", strings.NewReader("")).
|
||||
AddBasicAuth(user.Name)
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("CheckAcceptMediaType", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
|
||||
@@ -59,3 +59,43 @@ func TestAPIDownloadArchive(t *testing.T) {
|
||||
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name))
|
||||
MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestAPIDownloadArchive2(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
session := loginUser(t, user2.LowerName)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/zipball/master", user2.Name, repo.Name))
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
|
||||
bs, err := io.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bs, 320)
|
||||
|
||||
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/tarball/master", user2.Name, repo.Name))
|
||||
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
|
||||
bs, err = io.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bs, 266)
|
||||
|
||||
// Must return a link to a commit ID as the "immutable" archive link
|
||||
linkHeaderRe := regexp.MustCompile(`^<(https?://.*/api/v1/repos/user2/repo1/archive/[a-f0-9]+\.tar\.gz.*)>; rel="immutable"$`)
|
||||
m := linkHeaderRe.FindStringSubmatch(resp.Header().Get("Link"))
|
||||
assert.NotEmpty(t, m[1])
|
||||
resp = MakeRequest(t, NewRequest(t, "GET", m[1]).AddTokenAuth(token), http.StatusOK)
|
||||
bs2, err := io.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
// The locked URL should give the same bytes as the non-locked one
|
||||
assert.EqualValues(t, bs, bs2)
|
||||
|
||||
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/bundle/master", user2.Name, repo.Name))
|
||||
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
|
||||
bs, err = io.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bs, 382)
|
||||
|
||||
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name))
|
||||
MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -718,8 +718,8 @@ func TestAPIRepoGetReviewers(t *testing.T) {
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var reviewers []*api.User
|
||||
DecodeJSON(t, resp, &reviewers)
|
||||
if assert.Len(t, reviewers, 3) {
|
||||
assert.ElementsMatch(t, []int64{1, 4, 11}, []int64{reviewers[0].ID, reviewers[1].ID, reviewers[2].ID})
|
||||
if assert.Len(t, reviewers, 1) {
|
||||
assert.ElementsMatch(t, []int64{2}, []int64{reviewers[0].ID})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/organization"
|
||||
@@ -19,6 +18,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
"code.gitea.io/gitea/services/auth"
|
||||
"code.gitea.io/gitea/services/auth/source/ldap"
|
||||
org_service "code.gitea.io/gitea/services/org"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -429,9 +429,9 @@ func TestLDAPGroupTeamSyncAddMember(t *testing.T) {
|
||||
isMember, err := organization.IsTeamMember(db.DefaultContext, usersOrgs[0].ID, team.ID, user.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, isMember, "Membership should be added to the right team")
|
||||
err = models.RemoveTeamMember(db.DefaultContext, team, user)
|
||||
err = org_service.RemoveTeamMember(db.DefaultContext, team, user)
|
||||
assert.NoError(t, err)
|
||||
err = models.RemoveOrgUser(db.DefaultContext, usersOrgs[0], user)
|
||||
err = org_service.RemoveOrgUser(db.DefaultContext, usersOrgs[0], user)
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
// assert members of LDAP group "cn=admin_staff" keep initial team membership since mapped team does not exist
|
||||
@@ -461,7 +461,7 @@ func TestLDAPGroupTeamSyncRemoveMember(t *testing.T) {
|
||||
})
|
||||
err = organization.AddOrgUser(db.DefaultContext, org.ID, user.ID)
|
||||
assert.NoError(t, err)
|
||||
err = models.AddTeamMember(db.DefaultContext, team, user)
|
||||
err = org_service.AddTeamMember(db.DefaultContext, team, user)
|
||||
assert.NoError(t, err)
|
||||
isMember, err := organization.IsOrganizationMember(db.DefaultContext, org.ID, user.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -24,15 +24,9 @@ func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
||||
return &HTMLDoc{doc: doc}
|
||||
}
|
||||
|
||||
// GetInputValueByID for get input value by id
|
||||
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
||||
text, _ := doc.doc.Find("#" + id).Attr("value")
|
||||
return text
|
||||
}
|
||||
|
||||
// GetInputValueByName for get input value by name
|
||||
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
||||
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
||||
text, _ := doc.doc.Find(`input[name="` + name + `"]`).Attr("value")
|
||||
return text
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,11 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/services/mailer/incoming"
|
||||
incoming_payload "code.gitea.io/gitea/services/mailer/incoming/payload"
|
||||
sender_service "code.gitea.io/gitea/services/mailer/sender"
|
||||
token_service "code.gitea.io/gitea/services/mailer/token"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
func TestIncomingEmail(t *testing.T) {
|
||||
@@ -189,11 +189,15 @@ func TestIncomingEmail(t *testing.T) {
|
||||
token, err := token_service.CreateToken(token_service.ReplyHandlerType, user, payload)
|
||||
assert.NoError(t, err)
|
||||
|
||||
msg := gomail.NewMessage()
|
||||
msg.SetHeader("To", strings.Replace(setting.IncomingEmail.ReplyToAddress, setting.IncomingEmail.TokenPlaceholder, token, 1))
|
||||
msg.SetHeader("From", user.Email)
|
||||
msg.SetBody("text/plain", token)
|
||||
err = gomail.Send(&smtpTestSender{}, msg)
|
||||
msg := sender_service.NewMessageFrom(
|
||||
strings.Replace(setting.IncomingEmail.ReplyToAddress, setting.IncomingEmail.TokenPlaceholder, token, 1),
|
||||
"",
|
||||
user.Email,
|
||||
"",
|
||||
token,
|
||||
)
|
||||
|
||||
err = sender_service.Send(&smtpTestSender{}, msg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Eventually(t, func() bool {
|
||||
|
||||
@@ -565,7 +565,7 @@ func TestOAuth_GrantScopesReadUserFailRepos(t *testing.T) {
|
||||
|
||||
errorParsed := new(errorResponse)
|
||||
require.NoError(t, json.Unmarshal(errorResp.Body.Bytes(), errorParsed))
|
||||
assert.Contains(t, errorParsed.Message, "token does not have at least one of required scope(s): [read:repository]")
|
||||
assert.Contains(t, errorParsed.Message, "token does not have at least one of required scope(s), required=[read:repository]")
|
||||
}
|
||||
|
||||
func TestOAuth_GrantScopesReadRepositoryFailOrganization(t *testing.T) {
|
||||
@@ -708,7 +708,7 @@ func TestOAuth_GrantScopesReadRepositoryFailOrganization(t *testing.T) {
|
||||
|
||||
errorParsed := new(errorResponse)
|
||||
require.NoError(t, json.Unmarshal(errorResp.Body.Bytes(), errorParsed))
|
||||
assert.Contains(t, errorParsed.Message, "token does not have at least one of required scope(s): [read:user read:organization]")
|
||||
assert.Contains(t, errorParsed.Message, "token does not have at least one of required scope(s), required=[read:user read:organization]")
|
||||
}
|
||||
|
||||
func TestOAuth_GrantScopesClaimPublicOnlyGroups(t *testing.T) {
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) {
|
||||
user2Session := loginUser(t, "user2")
|
||||
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", fmt.Sprintf("%s/files", prURL)), http.StatusOK)
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
nodes := htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .dropdown .menu a")
|
||||
nodes := htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
|
||||
if assert.Equal(t, 1, nodes.Length()) {
|
||||
// there is only "View File" button, no "Edit File" button
|
||||
assert.Equal(t, "View File", nodes.First().Text())
|
||||
@@ -121,7 +121,7 @@ func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) {
|
||||
// user2 (admin of repo3) goes to the PR files page again
|
||||
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", fmt.Sprintf("%s/files", prURL)), http.StatusOK)
|
||||
htmlDoc = NewHTMLParser(t, resp.Body)
|
||||
nodes = htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .dropdown .menu a")
|
||||
nodes = htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
|
||||
if assert.Equal(t, 2, nodes.Length()) {
|
||||
// there are "View File" button and "Edit File" button
|
||||
assert.Equal(t, "View File", nodes.First().Text())
|
||||
|
||||
@@ -554,6 +554,10 @@ func TestPullRetargetChildOnBranchDelete(t *testing.T) {
|
||||
|
||||
testPullMerge(t, session, elemBasePR[1], elemBasePR[2], elemBasePR[4], repo_model.MergeStyleMerge, true)
|
||||
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
|
||||
branchBasePR := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "base-pr"})
|
||||
assert.True(t, branchBasePR.IsDeleted)
|
||||
|
||||
// Check child PR
|
||||
req := NewRequest(t, "GET", test.RedirectURL(respChildPR))
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
@@ -584,6 +588,10 @@ func TestPullDontRetargetChildOnWrongRepo(t *testing.T) {
|
||||
|
||||
testPullMerge(t, session, elemBasePR[1], elemBasePR[2], elemBasePR[4], repo_model.MergeStyleMerge, true)
|
||||
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
|
||||
branchBasePR := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "base-pr"})
|
||||
assert.True(t, branchBasePR.IsDeleted)
|
||||
|
||||
// Check child PR
|
||||
req := NewRequest(t, "GET", test.RedirectURL(respChildPR))
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
@@ -598,6 +606,27 @@ func TestPullDontRetargetChildOnWrongRepo(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestPullRequestMergedWithNoPermissionDeleteBranch(t *testing.T) {
|
||||
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||||
session := loginUser(t, "user4")
|
||||
testRepoFork(t, session, "user2", "repo1", "user4", "repo1", "")
|
||||
testEditFileToNewBranch(t, session, "user4", "repo1", "master", "base-pr", "README.md", "Hello, World\n(Edited - TestPullDontRetargetChildOnWrongRepo - base PR)\n")
|
||||
|
||||
respBasePR := testPullCreate(t, session, "user4", "repo1", false, "master", "base-pr", "Base Pull Request")
|
||||
elemBasePR := strings.Split(test.RedirectURL(respBasePR), "/")
|
||||
assert.EqualValues(t, "pulls", elemBasePR[3])
|
||||
|
||||
// user2 has no permission to delete branch of repo user1/repo1
|
||||
session2 := loginUser(t, "user2")
|
||||
testPullMerge(t, session2, elemBasePR[1], elemBasePR[2], elemBasePR[4], repo_model.MergeStyleMerge, true)
|
||||
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user4", Name: "repo1"})
|
||||
branchBasePR := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "base-pr"})
|
||||
// branch has not been deleted
|
||||
assert.False(t, branchBasePR.IsDeleted)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPullMergeIndexerNotifier(t *testing.T) {
|
||||
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||||
// create a pull request
|
||||
|
||||
@@ -9,12 +9,12 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
org_model "code.gitea.io/gitea/models/organization"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
org_service "code.gitea.io/gitea/services/org"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -91,7 +91,7 @@ func TestForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
assert.EqualValues(t, structs.VisibleTypeLimited, limitedOrg.Visibility)
|
||||
ownerTeam1, err := org_model.OrgFromUser(limitedOrg).GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam1, user1))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam1, user1))
|
||||
testRepoFork(t, user1Sess, "user2", "repo1", limitedOrg.Name, "repo1", "")
|
||||
|
||||
// fork to a private org
|
||||
@@ -101,7 +101,7 @@ func TestForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
assert.EqualValues(t, structs.VisibleTypePrivate, privateOrg.Visibility)
|
||||
ownerTeam2, err := org_model.OrgFromUser(privateOrg).GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam2, user4))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam2, user4))
|
||||
testRepoFork(t, user4Sess, "user2", "repo1", privateOrg.Name, "repo1", "")
|
||||
|
||||
t.Run("Anonymous", func(t *testing.T) {
|
||||
@@ -120,7 +120,7 @@ func TestForkListLimitedAndPrivateRepos(t *testing.T) {
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
assert.EqualValues(t, 1, htmlDoc.Find(forkItemSelector).Length())
|
||||
|
||||
assert.NoError(t, models.AddTeamMember(db.DefaultContext, ownerTeam2, user1))
|
||||
assert.NoError(t, org_service.AddTeamMember(db.DefaultContext, ownerTeam2, user1))
|
||||
resp = user1Sess.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc = NewHTMLParser(t, resp.Body)
|
||||
assert.EqualValues(t, 2, htmlDoc.Find(forkItemSelector).Length())
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
@@ -91,3 +92,31 @@ func TestSigninWithRememberMe(t *testing.T) {
|
||||
req = NewRequest(t, "GET", "/user/settings")
|
||||
session.MakeRequest(t, req, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestEnablePasswordSignInForm(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("EnablePasswordSignInForm=false", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer test.MockVariableValue(&setting.Service.EnablePasswordSignInForm, false)()
|
||||
|
||||
req := NewRequest(t, "GET", "/user/login")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/login']", false)
|
||||
|
||||
req = NewRequest(t, "POST", "/user/login")
|
||||
MakeRequest(t, req, http.StatusForbidden)
|
||||
})
|
||||
|
||||
t.Run("EnablePasswordSignInForm=true", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer test.MockVariableValue(&setting.Service.EnablePasswordSignInForm, true)()
|
||||
|
||||
req := NewRequest(t, "GET", "/user/login")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/login']", true)
|
||||
|
||||
req = NewRequest(t, "POST", "/user/login")
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -17,22 +16,24 @@ import (
|
||||
|
||||
func TestViewTimetrackingControls(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
session := loginUser(t, "user2")
|
||||
testViewTimetrackingControls(t, session, "user2", "repo1", "1", true)
|
||||
// user2/repo1
|
||||
}
|
||||
|
||||
func TestNotViewTimetrackingControls(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
session := loginUser(t, "user5")
|
||||
testViewTimetrackingControls(t, session, "user2", "repo1", "1", false)
|
||||
// user2/repo1
|
||||
}
|
||||
t.Run("Exist", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
session := loginUser(t, "user2")
|
||||
testViewTimetrackingControls(t, session, "user2", "repo1", "1", true)
|
||||
})
|
||||
|
||||
func TestViewTimetrackingControlsDisabled(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
session := loginUser(t, "user2")
|
||||
testViewTimetrackingControls(t, session, "org3", "repo3", "1", false)
|
||||
t.Run("Non-exist", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
session := loginUser(t, "user5")
|
||||
testViewTimetrackingControls(t, session, "user2", "repo1", "1", false)
|
||||
})
|
||||
|
||||
t.Run("Disabled", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
session := loginUser(t, "user2")
|
||||
testViewTimetrackingControls(t, session, "org3", "repo3", "1", false)
|
||||
})
|
||||
}
|
||||
|
||||
func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo, issue string, canTrackTime bool) {
|
||||
@@ -41,40 +42,40 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
|
||||
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
htmlDoc.AssertElement(t, ".timetrack .issue-start-time", canTrackTime)
|
||||
htmlDoc.AssertElement(t, ".timetrack .issue-add-time", canTrackTime)
|
||||
htmlDoc.AssertElement(t, ".issue-start-time", canTrackTime)
|
||||
htmlDoc.AssertElement(t, ".issue-add-time", canTrackTime)
|
||||
|
||||
req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
|
||||
issueLink := path.Join(user, repo, "issues", issue)
|
||||
req = NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "toggle"), map[string]string{
|
||||
"_csrf": htmlDoc.GetCSRF(),
|
||||
})
|
||||
if canTrackTime {
|
||||
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
session.MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
||||
req = NewRequest(t, "GET", issueLink)
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc = NewHTMLParser(t, resp.Body)
|
||||
|
||||
events := htmlDoc.doc.Find(".event > span.text")
|
||||
assert.Contains(t, events.Last().Text(), "started working")
|
||||
|
||||
htmlDoc.AssertElement(t, ".timetrack .issue-stop-time", true)
|
||||
htmlDoc.AssertElement(t, ".timetrack .issue-cancel-time", true)
|
||||
htmlDoc.AssertElement(t, ".issue-stop-time", true)
|
||||
htmlDoc.AssertElement(t, ".issue-cancel-time", true)
|
||||
|
||||
// Sleep for 1 second to not get wrong order for stopping timer
|
||||
time.Sleep(time.Second)
|
||||
|
||||
req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
|
||||
req = NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "toggle"), map[string]string{
|
||||
"_csrf": htmlDoc.GetCSRF(),
|
||||
})
|
||||
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
session.MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
||||
req = NewRequest(t, "GET", issueLink)
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc = NewHTMLParser(t, resp.Body)
|
||||
|
||||
events = htmlDoc.doc.Find(".event > span.text")
|
||||
assert.Contains(t, events.Last().Text(), "stopped working")
|
||||
htmlDoc.AssertElement(t, ".event .detail .octicon-clock", true)
|
||||
assert.Contains(t, events.Last().Text(), "worked for ")
|
||||
} else {
|
||||
session.MakeRequest(t, req, http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Validate that each navbar setting is correct. This checks that the
|
||||
@@ -51,8 +53,10 @@ func WithDisabledFeatures(t *testing.T, features ...string) {
|
||||
}
|
||||
|
||||
func TestUserSettingsAccount(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("all features enabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
req := NewRequest(t, "GET", "/user/settings/account")
|
||||
@@ -68,7 +72,7 @@ func TestUserSettingsAccount(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
@@ -85,7 +89,7 @@ func TestUserSettingsAccount(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("deletion disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureDeletion)
|
||||
|
||||
@@ -102,7 +106,7 @@ func TestUserSettingsAccount(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("deletion, credentials and email notifications are disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
mail := setting.Service.EnableNotifyMail
|
||||
setting.Service.EnableNotifyMail = false
|
||||
@@ -119,8 +123,10 @@ func TestUserSettingsAccount(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserSettingsUpdatePassword(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("enabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
|
||||
@@ -138,7 +144,7 @@ func TestUserSettingsUpdatePassword(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
@@ -156,8 +162,10 @@ func TestUserSettingsUpdatePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserSettingsUpdateEmail(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
@@ -175,8 +183,10 @@ func TestUserSettingsUpdateEmail(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserSettingsDeleteEmail(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
@@ -194,8 +204,10 @@ func TestUserSettingsDeleteEmail(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserSettingsDelete(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("deletion disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureDeletion)
|
||||
|
||||
@@ -224,9 +236,10 @@ func TestUserSettingsAppearance(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserSettingsSecurity(t *testing.T) {
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("credentials disabled", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
@@ -240,8 +253,7 @@ func TestUserSettingsSecurity(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("mfa disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageMFA)
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
@@ -255,8 +267,7 @@ func TestUserSettingsSecurity(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("credentials and mfa disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials, setting.UserFeatureManageMFA)
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
@@ -268,17 +279,75 @@ func TestUserSettingsSecurity(t *testing.T) {
|
||||
func TestUserSettingsApplications(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
req := NewRequest(t, "GET", "/user/settings/applications")
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
t.Run("Applications", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
assertNavbar(t, doc)
|
||||
session := loginUser(t, "user2")
|
||||
req := NewRequest(t, "GET", "/user/settings/applications")
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
assertNavbar(t, doc)
|
||||
})
|
||||
|
||||
t.Run("OAuth2", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
|
||||
t.Run("OAuth2ApplicationShow", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "GET", "/user/settings/applications/oauth2/2")
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
assertNavbar(t, doc)
|
||||
})
|
||||
|
||||
t.Run("OAuthApplicationsEdit", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequest(t, "GET", "/user/settings/applications/oauth2/2")
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
t.Run("Invalid URL", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithValues(t, "POST", "/user/settings/applications/oauth2/2", map[string]string{
|
||||
"_csrf": doc.GetCSRF(),
|
||||
"application_name": "Test native app",
|
||||
"redirect_uris": "ftp://127.0.0.1",
|
||||
"confidential_client": "false",
|
||||
})
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
msg := doc.Find(".flash-error p").Text()
|
||||
assert.Equal(t, `form.RedirectURIs"ftp://127.0.0.1" is not a valid URL.`, msg)
|
||||
})
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
req := NewRequestWithValues(t, "POST", "/user/settings/applications/oauth2/2", map[string]string{
|
||||
"_csrf": doc.GetCSRF(),
|
||||
"application_name": "Test native app",
|
||||
"redirect_uris": "http://127.0.0.1",
|
||||
"confidential_client": "false",
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestUserSettingsKeys(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
t.Run("all enabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
req := NewRequest(t, "GET", "/user/settings/keys")
|
||||
@@ -292,7 +361,7 @@ func TestUserSettingsKeys(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("ssh keys disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageSSHKeys)
|
||||
|
||||
@@ -308,7 +377,7 @@ func TestUserSettingsKeys(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("gpg keys disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageGPGKeys)
|
||||
|
||||
@@ -324,7 +393,7 @@ func TestUserSettingsKeys(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("ssh & gpg keys disabled", func(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageSSHKeys, setting.UserFeatureManageGPGKeys)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user