1
0
Fork 0
forked from forgejo/forgejo

Convert to url auth to header auth in tests (#28484)

Related #28390
This commit is contained in:
KN4CK3R 2023-12-22 00:59:59 +01:00 committed by GitHub
parent 04b235d094
commit 838db2f891
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
102 changed files with 1715 additions and 1523 deletions

View file

@ -93,8 +93,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", user2.Name, repo1.Name, token2)
req := NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name), &changeFilesOptions).
AddTokenAuth(token2)
resp := MakeRequest(t, req, http.StatusCreated)
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName)
@ -138,8 +138,9 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[2].Path = deleteTreePath
createFile(user2, repo1, updateTreePath)
createFile(user2, repo1, deleteTreePath)
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", user2.Name, repo1.Name, token2)
req := NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name)
req := NewRequestWithJSON(t, "POST", url, &changeFilesOptions).
AddTokenAuth(token2)
resp := MakeRequest(t, req, http.StatusCreated)
var filesResponse api.FilesResponse
DecodeJSON(t, resp, &filesResponse)
@ -168,7 +169,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files = []*api.ChangeFileOperation{changeFilesOptions.Files[1]}
changeFilesOptions.Files[0].FromPath = updateTreePath
changeFilesOptions.Files[0].Path = "rename/" + updateTreePath
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions).
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &filesResponse)
expectedUpdateSHA = "08bd14b2e2852529157324de9c226b3364e76136"
@ -191,7 +193,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[2].Path = deleteTreePath
createFile(user2, repo1, updateTreePath)
createFile(user2, repo1, deleteTreePath)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions).
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &filesResponse)
expectedMessage := fmt.Sprintf("Add %v\nUpdate %v\nDelete %v\n", createTreePath, updateTreePath, deleteTreePath)
@ -206,7 +209,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = updateTreePath
correctSHA := changeFilesOptions.Files[0].SHA
changeFilesOptions.Files[0].SHA = "badsha"
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions).
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusUnprocessableEntity)
expectedAPIError := context.APIError{
Message: "sha does not match [given: " + changeFilesOptions.Files[0].SHA + ", expected: " + correctSHA + "]",
@ -227,8 +231,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", user2.Name, repo16.Name, token4)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo16.Name), &changeFilesOptions).
AddTokenAuth(token4)
MakeRequest(t, req, http.StatusNotFound)
// Tests a repo with no token given so will fail
@ -242,8 +246,7 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo16.Name)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo16.Name), &changeFilesOptions)
MakeRequest(t, req, http.StatusNotFound)
// Test using access token for a private repo that the user of the token owns
@ -257,8 +260,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", user2.Name, repo16.Name, token2)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo16.Name), &changeFilesOptions).
AddTokenAuth(token2)
MakeRequest(t, req, http.StatusCreated)
// Test using org repo "org3/repo3" where user2 is a collaborator
@ -272,8 +275,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", org3.Name, repo3.Name, token2)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", org3.Name, repo3.Name), &changeFilesOptions).
AddTokenAuth(token2)
MakeRequest(t, req, http.StatusCreated)
// Test using org repo "org3/repo3" with no user token
@ -287,8 +290,7 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents", org3.Name, repo3.Name)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", org3.Name, repo3.Name), &changeFilesOptions)
MakeRequest(t, req, http.StatusNotFound)
// Test using repo "user2/repo1" where user4 is a NOT collaborator
@ -302,8 +304,8 @@ func TestAPIChangeFiles(t *testing.T) {
changeFilesOptions.Files[0].Path = createTreePath
changeFilesOptions.Files[1].Path = updateTreePath
changeFilesOptions.Files[2].Path = deleteTreePath
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents?token=%s", user2.Name, repo1.Name, token4)
req = NewRequestWithJSON(t, "POST", url, &changeFilesOptions)
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name), &changeFilesOptions).
AddTokenAuth(token4)
MakeRequest(t, req, http.StatusForbidden)
})
}