code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_topic_test.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/url" 10 "testing" 11 12 auth_model "code.gitea.io/gitea/models/auth" 13 repo_model "code.gitea.io/gitea/models/repo" 14 "code.gitea.io/gitea/models/unittest" 15 user_model "code.gitea.io/gitea/models/user" 16 api "code.gitea.io/gitea/modules/structs" 17 "code.gitea.io/gitea/tests" 18 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestAPITopicSearch(t *testing.T) { 23 defer tests.PrepareTestEnv(t)() 24 searchURL, _ := url.Parse("/api/v1/topics/search") 25 var topics struct { 26 TopicNames []*api.TopicResponse `json:"topics"` 27 } 28 29 query := url.Values{"page": []string{"1"}, "limit": []string{"4"}} 30 31 searchURL.RawQuery = query.Encode() 32 res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 33 DecodeJSON(t, res, &topics) 34 assert.Len(t, topics.TopicNames, 4) 35 assert.EqualValues(t, "6", res.Header().Get("x-total-count")) 36 37 query.Add("q", "topic") 38 searchURL.RawQuery = query.Encode() 39 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 40 DecodeJSON(t, res, &topics) 41 assert.Len(t, topics.TopicNames, 2) 42 43 query.Set("q", "database") 44 searchURL.RawQuery = query.Encode() 45 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 46 DecodeJSON(t, res, &topics) 47 if assert.Len(t, topics.TopicNames, 1) { 48 assert.EqualValues(t, 2, topics.TopicNames[0].ID) 49 assert.EqualValues(t, "database", topics.TopicNames[0].Name) 50 assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount) 51 } 52 } 53 54 func TestAPIRepoTopic(t *testing.T) { 55 defer tests.PrepareTestEnv(t)() 56 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of repo2 57 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of repo3 58 user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // write access to repo 3 59 repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) 60 repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) 61 62 // Get user2's token 63 token2 := getUserToken(t, user2.Name, auth_model.AccessTokenScopeWriteRepository) 64 65 // Test read topics using login 66 url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name) 67 req := NewRequest(t, "GET", url+"?token="+token2) 68 res := MakeRequest(t, req, http.StatusOK) 69 var topics *api.TopicName 70 DecodeJSON(t, res, &topics) 71 assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames) 72 73 // Log out user2 74 url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2) 75 76 // Test delete a topic 77 req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2) 78 MakeRequest(t, req, http.StatusNoContent) 79 80 // Test add an existing topic 81 req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2) 82 MakeRequest(t, req, http.StatusNoContent) 83 84 // Test add a topic 85 req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2) 86 MakeRequest(t, req, http.StatusNoContent) 87 88 // Test read topics using token 89 req = NewRequest(t, "GET", url) 90 res = MakeRequest(t, req, http.StatusOK) 91 DecodeJSON(t, res, &topics) 92 assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames) 93 94 // Test replace topics 95 newTopics := []string{" windows ", " ", "MAC "} 96 req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ 97 Topics: newTopics, 98 }) 99 MakeRequest(t, req, http.StatusNoContent) 100 req = NewRequest(t, "GET", url) 101 res = MakeRequest(t, req, http.StatusOK) 102 DecodeJSON(t, res, &topics) 103 assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames) 104 105 // Test replace topics with something invalid 106 newTopics = []string{"topicname1", "topicname2", "topicname!"} 107 req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ 108 Topics: newTopics, 109 }) 110 MakeRequest(t, req, http.StatusUnprocessableEntity) 111 req = NewRequest(t, "GET", url) 112 res = MakeRequest(t, req, http.StatusOK) 113 DecodeJSON(t, res, &topics) 114 assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames) 115 116 // Test with some topics multiple times, less than 25 unique 117 newTopics = []string{"t1", "t2", "t1", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10", "t11", "t12", "t13", "t14", "t15", "t16", "17", "t18", "t19", "t20", "t21", "t22", "t23", "t24", "t25"} 118 req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ 119 Topics: newTopics, 120 }) 121 MakeRequest(t, req, http.StatusNoContent) 122 req = NewRequest(t, "GET", url) 123 res = MakeRequest(t, req, http.StatusOK) 124 DecodeJSON(t, res, &topics) 125 assert.Len(t, topics.TopicNames, 25) 126 127 // Test writing more topics than allowed 128 newTopics = append(newTopics, "t26") 129 req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ 130 Topics: newTopics, 131 }) 132 MakeRequest(t, req, http.StatusUnprocessableEntity) 133 134 // Test add a topic when there is already maximum 135 req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2) 136 MakeRequest(t, req, http.StatusUnprocessableEntity) 137 138 // Test delete a topic that repo doesn't have 139 req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2) 140 MakeRequest(t, req, http.StatusNotFound) 141 142 // Get user4's token 143 token4 := getUserToken(t, user4.Name, auth_model.AccessTokenScopeWriteRepository) 144 145 // Test read topics with write access 146 url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", org3.Name, repo3.Name, token4) 147 req = NewRequest(t, "GET", url) 148 res = MakeRequest(t, req, http.StatusOK) 149 DecodeJSON(t, res, &topics) 150 assert.Empty(t, topics.TopicNames) 151 152 // Test add a topic to repo with write access (requires repo admin access) 153 req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", org3.Name, repo3.Name, "topicName", token4) 154 MakeRequest(t, req, http.StatusForbidden) 155 }