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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIRepoTopic(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of repo2
  16. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of repo3
  17. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // write access to repo 3
  18. repo2 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  19. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
  20. // Get user2's token
  21. session := loginUser(t, user2.Name)
  22. token2 := getTokenForLoggedInUser(t, session)
  23. // Test read topics using login
  24. url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
  25. req := NewRequest(t, "GET", url)
  26. res := session.MakeRequest(t, req, http.StatusOK)
  27. var topics *api.TopicName
  28. DecodeJSON(t, res, &topics)
  29. assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames)
  30. // Log out user2
  31. session = emptyTestSession(t)
  32. url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2)
  33. // Test delete a topic
  34. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
  35. res = session.MakeRequest(t, req, http.StatusNoContent)
  36. // Test add an existing topic
  37. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2)
  38. res = session.MakeRequest(t, req, http.StatusNoContent)
  39. // Test add a topic
  40. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2)
  41. res = session.MakeRequest(t, req, http.StatusNoContent)
  42. // Test read topics using token
  43. req = NewRequest(t, "GET", url)
  44. res = session.MakeRequest(t, req, http.StatusOK)
  45. DecodeJSON(t, res, &topics)
  46. assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames)
  47. // Test replace topics
  48. newTopics := []string{" windows ", " ", "MAC "}
  49. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  50. Topics: newTopics,
  51. })
  52. res = session.MakeRequest(t, req, http.StatusNoContent)
  53. req = NewRequest(t, "GET", url)
  54. res = session.MakeRequest(t, req, http.StatusOK)
  55. DecodeJSON(t, res, &topics)
  56. assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
  57. // Test replace topics with something invalid
  58. newTopics = []string{"topicname1", "topicname2", "topicname!"}
  59. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  60. Topics: newTopics,
  61. })
  62. res = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  63. req = NewRequest(t, "GET", url)
  64. res = session.MakeRequest(t, req, http.StatusOK)
  65. DecodeJSON(t, res, &topics)
  66. assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
  67. // Test with some topics multiple times, less than 25 unique
  68. 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"}
  69. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  70. Topics: newTopics,
  71. })
  72. res = session.MakeRequest(t, req, http.StatusNoContent)
  73. req = NewRequest(t, "GET", url)
  74. res = session.MakeRequest(t, req, http.StatusOK)
  75. DecodeJSON(t, res, &topics)
  76. assert.Equal(t, 25, len(topics.TopicNames))
  77. // Test writing more topics than allowed
  78. newTopics = append(newTopics, "t26")
  79. req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
  80. Topics: newTopics,
  81. })
  82. res = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  83. // Test add a topic when there is already maximum
  84. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2)
  85. res = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  86. // Test delete a topic that repo doesn't have
  87. req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
  88. res = session.MakeRequest(t, req, http.StatusNotFound)
  89. // Get user4's token
  90. session = loginUser(t, user4.Name)
  91. token4 := getTokenForLoggedInUser(t, session)
  92. session = emptyTestSession(t)
  93. // Test read topics with write access
  94. url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user3.Name, repo3.Name, token4)
  95. req = NewRequest(t, "GET", url)
  96. res = session.MakeRequest(t, req, http.StatusOK)
  97. DecodeJSON(t, res, &topics)
  98. assert.Equal(t, 0, len(topics.TopicNames))
  99. // Test add a topic to repo with write access (requires repo admin access)
  100. req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user3.Name, repo3.Name, "topicName", token4)
  101. res = session.MakeRequest(t, req, http.StatusForbidden)
  102. }