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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/unittest"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAPITopicSearch(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. searchURL, _ := url.Parse("/api/v1/topics/search")
  18. var topics struct {
  19. TopicNames []*api.TopicResponse `json:"topics"`
  20. }
  21. query := url.Values{"page": []string{"1"}, "limit": []string{"4"}}
  22. searchURL.RawQuery = query.Encode()
  23. res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  24. DecodeJSON(t, res, &topics)
  25. assert.Len(t, topics.TopicNames, 4)
  26. assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
  27. query.Add("q", "topic")
  28. searchURL.RawQuery = query.Encode()
  29. res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  30. DecodeJSON(t, res, &topics)
  31. assert.Len(t, topics.TopicNames, 2)
  32. query.Set("q", "database")
  33. searchURL.RawQuery = query.Encode()
  34. res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
  35. DecodeJSON(t, res, &topics)
  36. if assert.Len(t, topics.TopicNames, 1) {
  37. assert.EqualValues(t, 2, topics.TopicNames[0].ID)
  38. assert.EqualValues(t, "database", topics.TopicNames[0].Name)
  39. assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount)
  40. }
  41. }
  42. func TestAPIRepoTopic(t *testing.T) {
  43. defer prepareTestEnv(t)()
  44. user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of repo2
  45. user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of repo3
  46. user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // write access to repo 3
  47. repo2 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  48. repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
  49. // Get user2's token
  50. session := loginUser(t, user2.Name)
  51. token2 := getTokenForLoggedInUser(t, session)
  52. // Test read topics using login
  53. url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
  54. req := NewRequest(t, "GET", url)
  55. res := session.MakeRequest(t, req, http.StatusOK)
  56. var topics *api.TopicName
  57. DecodeJSON(t, res, &topics)
  58. assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames)
  59. // Log out user2
  60. session = emptyTestSession(t)
  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. session.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. session.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. session.MakeRequest(t, req, http.StatusNoContent)
  71. // Test read topics using token
  72. req = NewRequest(t, "GET", url)
  73. res = session.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. session.MakeRequest(t, req, http.StatusNoContent)
  82. req = NewRequest(t, "GET", url)
  83. res = session.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. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  92. req = NewRequest(t, "GET", url)
  93. res = session.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. session.MakeRequest(t, req, http.StatusNoContent)
  102. req = NewRequest(t, "GET", url)
  103. res = session.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. session.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. session.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. session.MakeRequest(t, req, http.StatusNotFound)
  118. // Get user4's token
  119. session = loginUser(t, user4.Name)
  120. token4 := getTokenForLoggedInUser(t, session)
  121. session = emptyTestSession(t)
  122. // Test read topics with write access
  123. url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user3.Name, repo3.Name, token4)
  124. req = NewRequest(t, "GET", url)
  125. res = session.MakeRequest(t, req, http.StatusOK)
  126. DecodeJSON(t, res, &topics)
  127. assert.Empty(t, topics.TopicNames)
  128. // Test add a topic to repo with write access (requires repo admin access)
  129. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user3.Name, repo3.Name, "topicName", token4)
  130. session.MakeRequest(t, req, http.StatusForbidden)
  131. }