1
0
Fork 0
forked from forgejo/forgejo

Remove session in api tests (#21984)

It's no meaning to request an API route with session.
This commit is contained in:
Lunny Xiao 2022-12-02 11:39:42 +08:00 committed by GitHub
parent 665d02efaf
commit df676a47d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 387 additions and 433 deletions

View file

@ -52,7 +52,6 @@ func TestAPIDeleteFile(t *testing.T) {
// Get user4's token
session = loginUser(t, user4.Name)
token4 := getTokenForLoggedInUser(t, session)
session = emptyTestSession(t)
// Test deleting a file in repo1 which user2 owns, try both with branch and empty branch
for _, branch := range [...]string{
@ -66,7 +65,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions.BranchName = branch
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
assert.NotNil(t, fileResponse)
@ -82,7 +81,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions.NewBranchName = "new_branch"
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
resp := session.MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
assert.NotNil(t, fileResponse)
@ -97,7 +96,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions.Message = ""
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
resp = session.MakeRequest(t, req, http.StatusOK)
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &fileResponse)
expectedMessage := "Delete '" + treePath + "'\n"
assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
@ -110,7 +109,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions.SHA = "badsha"
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusBadRequest)
MakeRequest(t, req, http.StatusBadRequest)
// Test creating a file in repo16 by user4 who does not have write access
fileID++
@ -119,7 +118,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusNotFound)
MakeRequest(t, req, http.StatusNotFound)
// Tests a repo with no token given so will fail
fileID++
@ -128,7 +127,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusNotFound)
MakeRequest(t, req, http.StatusNotFound)
// Test using access token for a private repo that the user of the token owns
fileID++
@ -137,7 +136,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
// Test using org repo "user3/repo3" where user2 is a collaborator
fileID++
@ -146,7 +145,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusOK)
MakeRequest(t, req, http.StatusOK)
// Test using org repo "user3/repo3" with no user token
fileID++
@ -155,7 +154,7 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusNotFound)
MakeRequest(t, req, http.StatusNotFound)
// Test using repo "user2/repo1" where user4 is a NOT collaborator
fileID++
@ -164,6 +163,6 @@ func TestAPIDeleteFile(t *testing.T) {
deleteFileOptions = getDeleteFileOptions()
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
session.MakeRequest(t, req, http.StatusForbidden)
MakeRequest(t, req, http.StatusForbidden)
})
}