Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

api_repo_topic_test.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. query := url.Values{"page": []string{"1"}, "limit": []string{"4"}}
  24. searchURL.RawQuery = query.Encode()
  25. res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  26. DecodeJSON(t, res, &topics)
  27. assert.Len(t, topics.TopicNames, 4)
  28. assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
  29. query.Add("q", "topic")
  30. searchURL.RawQuery = query.Encode()
  31. res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  32. DecodeJSON(t, res, &topics)
  33. assert.Len(t, topics.TopicNames, 2)
  34. query.Set("q", "database")
  35. searchURL.RawQuery = query.Encode()
  36. res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  37. DecodeJSON(t, res, &topics)
  38. if assert.Len(t, topics.TopicNames, 1) {
  39. assert.EqualValues(t, 2, topics.TopicNames[0].ID)
  40. assert.EqualValues(t, "database", topics.TopicNames[0].Name)
  41. assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount)
  42. }
  43. }
  44. func TestAPIRepoTopic(t *testing.T) {
  45. defer tests.PrepareTestEnv(t)()
  46. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of repo2
  47. user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of repo3
  48. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // write access to repo 3
  49. repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
  50. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
  51. // Get user2's token
  52. token2 := getUserToken(t, user2.Name, auth_model.AccessTokenScopeRepo)
  53. // Test read topics using login
  54. url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
  55. req := NewRequest(t, "GET", url+"?token="+token2)
  56. res := MakeRequest(t, req, http.StatusOK)
  57. var topics *api.TopicName
  58. DecodeJSON(t, res, &topics)
  59. assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames)
  60. // Log out user2
  61. url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2)
  62. // Test delete a topic
  63. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
  64. MakeRequest(t, req, http.StatusNoContent)
  65. // Test add an existing topic
  66. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2)
  67. MakeRequest(t, req, http.StatusNoContent)
  68. // Test add a topic
  69. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2)
  70. MakeRequest(t, req, http.StatusNoContent)
  71. // Test read topics using token
  72. req = NewRequest(t, "GET", url)
  73. res = MakeRequest(t, req, http.StatusOK)
  74. DecodeJSON(t, res, &topics)
  75. assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames)
  76. // Test replace topics
  77. newTopics := []string{" windows ", " ", "MAC "}
  78. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  79. Topics: newTopics,
  80. })
  81. MakeRequest(t, req, http.StatusNoContent)
  82. req = NewRequest(t, "GET", url)
  83. res = MakeRequest(t, req, http.StatusOK)
  84. DecodeJSON(t, res, &topics)
  85. assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
  86. // Test replace topics with something invalid
  87. newTopics = []string{"topicname1", "topicname2", "topicname!"}
  88. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  89. Topics: newTopics,
  90. })
  91. MakeRequest(t, req, http.StatusUnprocessableEntity)
  92. req = NewRequest(t, "GET", url)
  93. res = MakeRequest(t, req, http.StatusOK)
  94. DecodeJSON(t, res, &topics)
  95. assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
  96. // Test with some topics multiple times, less than 25 unique
  97. 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"}
  98. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  99. Topics: newTopics,
  100. })
  101. MakeRequest(t, req, http.StatusNoContent)
  102. req = NewRequest(t, "GET", url)
  103. res = MakeRequest(t, req, http.StatusOK)
  104. DecodeJSON(t, res, &topics)
  105. assert.Len(t, topics.TopicNames, 25)
  106. // Test writing more topics than allowed
  107. newTopics = append(newTopics, "t26")
  108. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  109. Topics: newTopics,
  110. })
  111. MakeRequest(t, req, http.StatusUnprocessableEntity)
  112. // Test add a topic when there is already maximum
  113. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2)
  114. MakeRequest(t, req, http.StatusUnprocessableEntity)
  115. // Test delete a topic that repo doesn't have
  116. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
  117. MakeRequest(t, req, http.StatusNotFound)
  118. // Get user4's token
  119. token4 := getUserToken(t, user4.Name, auth_model.AccessTokenScopeRepo)
  120. // Test read topics with write access
  121. url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user3.Name, repo3.Name, token4)
  122. req = NewRequest(t, "GET", url)
  123. res = MakeRequest(t, req, http.StatusOK)
  124. DecodeJSON(t, res, &topics)
  125. assert.Empty(t, topics.TopicNames)
  126. // Test add a topic to repo with write access (requires repo admin access)
  127. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user3.Name, repo3.Name, "topicName", token4)
  128. MakeRequest(t, req, http.StatusForbidden)
  129. }