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

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