forked from forgejo/forgejo
Remove dependent on session auth for api/v1 routers (#19321)
* Remove dependent on session auth for api/v1 routers * Remove unnecessary session on API context * remove missed header * fix test * fix missed api/v1
This commit is contained in:
parent
75f8534c3a
commit
3c3d49899f
22 changed files with 219 additions and 161 deletions
|
@ -59,36 +59,34 @@ func TestAPIRepoTopic(t *testing.T) {
|
|||
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
||||
|
||||
// Get user2's token
|
||||
session := loginUser(t, user2.Name)
|
||||
token2 := getTokenForLoggedInUser(t, session)
|
||||
token2 := getUserToken(t, user2.Name)
|
||||
|
||||
// Test read topics using login
|
||||
url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
|
||||
req := NewRequest(t, "GET", url)
|
||||
res := session.MakeRequest(t, req, http.StatusOK)
|
||||
req := NewRequest(t, "GET", url+"?token="+token2)
|
||||
res := MakeRequest(t, req, http.StatusOK)
|
||||
var topics *api.TopicName
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames)
|
||||
|
||||
// Log out user2
|
||||
session = emptyTestSession(t)
|
||||
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2)
|
||||
|
||||
// Test delete a topic
|
||||
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
|
||||
// Test add an existing topic
|
||||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2)
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
|
||||
// Test add a topic
|
||||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2)
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
|
||||
// Test read topics using token
|
||||
req = NewRequest(t, "GET", url)
|
||||
res = session.MakeRequest(t, req, http.StatusOK)
|
||||
res = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames)
|
||||
|
||||
|
@ -97,9 +95,9 @@ func TestAPIRepoTopic(t *testing.T) {
|
|||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
|
||||
Topics: newTopics,
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
req = NewRequest(t, "GET", url)
|
||||
res = session.MakeRequest(t, req, http.StatusOK)
|
||||
res = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
|
||||
|
||||
|
@ -108,9 +106,9 @@ func TestAPIRepoTopic(t *testing.T) {
|
|||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
|
||||
Topics: newTopics,
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
req = NewRequest(t, "GET", url)
|
||||
res = session.MakeRequest(t, req, http.StatusOK)
|
||||
res = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
|
||||
|
||||
|
@ -119,9 +117,9 @@ func TestAPIRepoTopic(t *testing.T) {
|
|||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
|
||||
Topics: newTopics,
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
MakeRequest(t, req, http.StatusNoContent)
|
||||
req = NewRequest(t, "GET", url)
|
||||
res = session.MakeRequest(t, req, http.StatusOK)
|
||||
res = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.Len(t, topics.TopicNames, 25)
|
||||
|
||||
|
@ -130,29 +128,27 @@ func TestAPIRepoTopic(t *testing.T) {
|
|||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
|
||||
Topics: newTopics,
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
// Test add a topic when there is already maximum
|
||||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2)
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
// Test delete a topic that repo doesn't have
|
||||
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
|
||||
session.MakeRequest(t, req, http.StatusNotFound)
|
||||
MakeRequest(t, req, http.StatusNotFound)
|
||||
|
||||
// Get user4's token
|
||||
session = loginUser(t, user4.Name)
|
||||
token4 := getTokenForLoggedInUser(t, session)
|
||||
session = emptyTestSession(t)
|
||||
token4 := getUserToken(t, user4.Name)
|
||||
|
||||
// Test read topics with write access
|
||||
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user3.Name, repo3.Name, token4)
|
||||
req = NewRequest(t, "GET", url)
|
||||
res = session.MakeRequest(t, req, http.StatusOK)
|
||||
res = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, res, &topics)
|
||||
assert.Empty(t, topics.TopicNames)
|
||||
|
||||
// Test add a topic to repo with write access (requires repo admin access)
|
||||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user3.Name, repo3.Name, "topicName", token4)
|
||||
session.MakeRequest(t, req, http.StatusForbidden)
|
||||
MakeRequest(t, req, http.StatusForbidden)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue