You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_repo_topic_test.go 6.8KB

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