-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add some api integration tests #18872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
505ddb1
Added some api integration tests.
KN4CK3R 3f41493
Fixed wrong status code.
KN4CK3R a901dae
Merge branch 'main' into integration-add-tests
lunny e84b3a4
Merge branch 'main' into integration-add-tests
lunny 5a3724e
Merge branch 'main' into integration-add-tests
wxiaoguang 0b07680
Merge branch 'main' into integration-add-tests
zeripath 58a5aee
Merge branch 'main' into integration-add-tests
techknowlogick 2e23b2f
Merge branch 'main' into integration-add-tests
KN4CK3R 4d7225e
Update fixtures to prevent critical null values.
KN4CK3R a3b1e50
fmt
KN4CK3R f7325f0
Merge branch 'main' into integration-add-tests
wxiaoguang 75b9b64
Merge branch 'main' into integration-add-tests
wxiaoguang b96c546
Merge branch 'main' into integration-add-tests
wxiaoguang 25df72b
Fix tests.
KN4CK3R cd19941
Merge branch 'main' into integration-add-tests
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFeed(t *testing.T) { | ||
t.Run("User", func(t *testing.T) { | ||
t.Run("Atom", func(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
req := NewRequest(t, "GET", "/user2.atom") | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
data := resp.Body.String() | ||
assert.Contains(t, data, `<feed xmlns="http://www.w3.org/2005/Atom"`) | ||
}) | ||
|
||
t.Run("RSS", func(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
req := NewRequest(t, "GET", "/user2.rss") | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
data := resp.Body.String() | ||
assert.Contains(t, data, `<rss version="2.0"`) | ||
}) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIFollow(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
user1 := "user4" | ||
user2 := "user1" | ||
|
||
session1 := loginUser(t, user1) | ||
token1 := getTokenForLoggedInUser(t, session1) | ||
|
||
session2 := loginUser(t, user2) | ||
token2 := getTokenForLoggedInUser(t, session2) | ||
|
||
t.Run("Follow", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/user/following/%s?token=%s", user1, token2)) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
}) | ||
|
||
t.Run("ListFollowing", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/following?token=%s", user2, token2)) | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var users []api.User | ||
DecodeJSON(t, resp, &users) | ||
assert.Len(t, users, 1) | ||
assert.Equal(t, user1, users[0].UserName) | ||
}) | ||
|
||
t.Run("ListMyFollowing", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/following?token=%s", token2)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var users []api.User | ||
DecodeJSON(t, resp, &users) | ||
assert.Len(t, users, 1) | ||
assert.Equal(t, user1, users[0].UserName) | ||
}) | ||
|
||
t.Run("ListFollowers", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/followers?token=%s", user1, token1)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var users []api.User | ||
DecodeJSON(t, resp, &users) | ||
assert.Len(t, users, 1) | ||
assert.Equal(t, user2, users[0].UserName) | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
t.Run("ListMyFollowers", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/followers?token=%s", token1)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var users []api.User | ||
DecodeJSON(t, resp, &users) | ||
assert.Len(t, users, 1) | ||
assert.Equal(t, user2, users[0].UserName) | ||
}) | ||
|
||
t.Run("CheckFollowing", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/following/%s?token=%s", user2, user1, token2)) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/following/%s?token=%s", user1, user2, token2)) | ||
MakeRequest(t, req, http.StatusNotFound) | ||
}) | ||
|
||
t.Run("CheckMyFollowing", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/following/%s?token=%s", user1, token2)) | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/following/%s?token=%s", user2, token1)) | ||
MakeRequest(t, req, http.StatusNotFound) | ||
}) | ||
|
||
t.Run("Unfollow", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/user/following/%s?token=%s", user1, token2)) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIUserInfo(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
user := "user1" | ||
user2 := "user31" | ||
|
||
session := loginUser(t, user) | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
t.Run("GetInfo", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s?token=%s", user2, token)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var u api.User | ||
DecodeJSON(t, resp, &u) | ||
assert.Equal(t, user2, u.UserName) | ||
|
||
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user2)) | ||
MakeRequest(t, req, http.StatusNotFound) | ||
}) | ||
|
||
t.Run("GetAuthenticatedUser", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user?token=%s", token)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var u api.User | ||
DecodeJSON(t, resp, &u) | ||
assert.Equal(t, user, u.UserName) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIStar(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
user := "user1" | ||
repo := "user2/repo1" | ||
|
||
session := loginUser(t, user) | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
t.Run("Star", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, token)) | ||
MakeRequest(t, req, http.StatusOK) | ||
}) | ||
|
||
t.Run("GetStarredRepos", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/starred?token=%s", user, token)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
assert.Equal(t, "1", resp.Header().Get("X-Total-Count")) | ||
|
||
var repos []api.Repository | ||
DecodeJSON(t, resp, &repos) | ||
assert.Len(t, repos, 1) | ||
assert.Equal(t, repo, repos[0].FullName) | ||
}) | ||
|
||
t.Run("GetMyStarredRepos", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred?token=%s", token)) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
assert.Equal(t, "1", resp.Header().Get("X-Total-Count")) | ||
|
||
var repos []api.Repository | ||
DecodeJSON(t, resp, &repos) | ||
assert.Len(t, repos, 1) | ||
assert.Equal(t, repo, repos[0].FullName) | ||
}) | ||
|
||
t.Run("IsStarring", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, token)) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo+"notexisting", token)) | ||
MakeRequest(t, req, http.StatusNotFound) | ||
}) | ||
|
||
t.Run("Unstar", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
|
||
req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, token)) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.