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

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