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.

124 lines
5.0 KiB

Add API endpoint for accessing repo topics (#7963) * Create API endpoints for repo topics. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Generate swagger Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add documentation to functions Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Grammar fix Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix function comment Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Can't use FindTopics when looking for a single repo topic, as it doesnt use exact match Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add PUT ​/repos​/{owner}​/{repo}​/topics and remove GET ​/repos​/{owner}​/{repo}​/topics * Ignore if topic is sent twice in same request, refactoring. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix topic dropdown with api changes. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Style fix Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Update API documentation Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Better way to handle duplicate topics in slice Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Make response element TopicName an array of strings, instead of using an array of TopicName Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add test cases for API Repo Topics. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix format of tests Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix comments Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix unit tests after adding some more topics to the test fixture. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Update models/topic.go Limit multiple if else if ... Co-Authored-By: Antoine GIRARD <sapk@users.noreply.github.com> * Engine as first parameter in function Co-Authored-By: Antoine GIRARD <sapk@users.noreply.github.com> * Replace magic numbers with http status code constants. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix variable scope Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Test one read with login and one with token Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add some more tests Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Apply suggestions from code review Use empty struct for efficiency Co-Authored-By: Lauris BH <lauris@nix.lv> * Add test case to check access for user with write access Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix access, repo admin required to change topics Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Correct first test to be without token Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Any repo reader should be able to access topics. * No need for string pointer Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
5 years ago
Add API endpoint for accessing repo topics (#7963) * Create API endpoints for repo topics. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Generate swagger Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add documentation to functions Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Grammar fix Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix function comment Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Can't use FindTopics when looking for a single repo topic, as it doesnt use exact match Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add PUT ​/repos​/{owner}​/{repo}​/topics and remove GET ​/repos​/{owner}​/{repo}​/topics * Ignore if topic is sent twice in same request, refactoring. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix topic dropdown with api changes. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Style fix Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Update API documentation Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Better way to handle duplicate topics in slice Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Make response element TopicName an array of strings, instead of using an array of TopicName Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add test cases for API Repo Topics. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix format of tests Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix comments Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix unit tests after adding some more topics to the test fixture. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Update models/topic.go Limit multiple if else if ... Co-Authored-By: Antoine GIRARD <sapk@users.noreply.github.com> * Engine as first parameter in function Co-Authored-By: Antoine GIRARD <sapk@users.noreply.github.com> * Replace magic numbers with http status code constants. Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix variable scope Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Test one read with login and one with token Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Add some more tests Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Apply suggestions from code review Use empty struct for efficiency Co-Authored-By: Lauris BH <lauris@nix.lv> * Add test case to check access for user with write access Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Fix access, repo admin required to change topics Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Correct first test to be without token Signed-off-by: David Svantesson <davidsvantesson@gmail.com> * Any repo reader should be able to access topics. * No need for string pointer Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
5 years ago
  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. }