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.

146 lines
5.9 KiB

  1. // Copyright 2017 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. "sort"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/convert"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAPITeam(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. teamUser := models.AssertExistsAndLoadBean(t, &models.TeamUser{}).(*models.TeamUser)
  18. team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team)
  19. user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)
  20. session := loginUser(t, user.Name)
  21. token := getTokenForLoggedInUser(t, session)
  22. req := NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamUser.TeamID)
  23. resp := session.MakeRequest(t, req, http.StatusOK)
  24. var apiTeam api.Team
  25. DecodeJSON(t, resp, &apiTeam)
  26. assert.EqualValues(t, team.ID, apiTeam.ID)
  27. assert.Equal(t, team.Name, apiTeam.Name)
  28. // non team member user will not access the teams details
  29. teamUser2 := models.AssertExistsAndLoadBean(t, &models.TeamUser{ID: 3}).(*models.TeamUser)
  30. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser2.UID}).(*models.User)
  31. session = loginUser(t, user2.Name)
  32. token = getTokenForLoggedInUser(t, session)
  33. req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamUser.TeamID)
  34. _ = session.MakeRequest(t, req, http.StatusForbidden)
  35. req = NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID)
  36. _ = session.MakeRequest(t, req, http.StatusUnauthorized)
  37. // Get an admin user able to create, update and delete teams.
  38. user = models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  39. session = loginUser(t, user.Name)
  40. token = getTokenForLoggedInUser(t, session)
  41. org := models.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User)
  42. // Create team.
  43. teamToCreate := &api.CreateTeamOption{
  44. Name: "team1",
  45. Description: "team one",
  46. IncludesAllRepositories: true,
  47. Permission: "write",
  48. Units: []string{"repo.code", "repo.issues"},
  49. }
  50. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", org.Name, token), teamToCreate)
  51. resp = session.MakeRequest(t, req, http.StatusCreated)
  52. DecodeJSON(t, resp, &apiTeam)
  53. checkTeamResponse(t, &apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
  54. teamToCreate.Permission, teamToCreate.Units)
  55. checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
  56. teamToCreate.Permission, teamToCreate.Units)
  57. teamID := apiTeam.ID
  58. // Edit team.
  59. teamToEdit := &api.EditTeamOption{
  60. Name: "teamone",
  61. Description: "team 1",
  62. IncludesAllRepositories: false,
  63. Permission: "admin",
  64. Units: []string{"repo.code", "repo.pulls", "repo.releases"},
  65. }
  66. req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit)
  67. resp = session.MakeRequest(t, req, http.StatusOK)
  68. DecodeJSON(t, resp, &apiTeam)
  69. checkTeamResponse(t, &apiTeam, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories,
  70. teamToEdit.Permission, teamToEdit.Units)
  71. checkTeamBean(t, apiTeam.ID, teamToEdit.Name, teamToEdit.Description, teamToEdit.IncludesAllRepositories,
  72. teamToEdit.Permission, teamToEdit.Units)
  73. // Read team.
  74. teamRead := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
  75. req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID)
  76. resp = session.MakeRequest(t, req, http.StatusOK)
  77. DecodeJSON(t, resp, &apiTeam)
  78. checkTeamResponse(t, &apiTeam, teamRead.Name, teamRead.Description, teamRead.IncludesAllRepositories,
  79. teamRead.Authorize.String(), teamRead.GetUnitNames())
  80. // Delete team.
  81. req = NewRequestf(t, "DELETE", "/api/v1/teams/%d?token="+token, teamID)
  82. session.MakeRequest(t, req, http.StatusNoContent)
  83. models.AssertNotExistsBean(t, &models.Team{ID: teamID})
  84. }
  85. func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string, includesAllRepositories bool, permission string, units []string) {
  86. assert.Equal(t, name, apiTeam.Name, "name")
  87. assert.Equal(t, description, apiTeam.Description, "description")
  88. assert.Equal(t, includesAllRepositories, apiTeam.IncludesAllRepositories, "includesAllRepositories")
  89. assert.Equal(t, permission, apiTeam.Permission, "permission")
  90. sort.StringSlice(units).Sort()
  91. sort.StringSlice(apiTeam.Units).Sort()
  92. assert.EqualValues(t, units, apiTeam.Units, "units")
  93. }
  94. func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string) {
  95. team := models.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team)
  96. assert.NoError(t, team.GetUnits(), "GetUnits")
  97. checkTeamResponse(t, convert.ToTeam(team), name, description, includesAllRepositories, permission, units)
  98. }
  99. type TeamSearchResults struct {
  100. OK bool `json:"ok"`
  101. Data []*api.Team `json:"data"`
  102. }
  103. func TestAPITeamSearch(t *testing.T) {
  104. defer prepareTestEnv(t)()
  105. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  106. org := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
  107. var results TeamSearchResults
  108. session := loginUser(t, user.Name)
  109. req := NewRequestf(t, "GET", "/api/v1/orgs/%s/teams/search?q=%s", org.Name, "_team")
  110. resp := session.MakeRequest(t, req, http.StatusOK)
  111. DecodeJSON(t, resp, &results)
  112. assert.NotEmpty(t, results.Data)
  113. assert.Equal(t, 1, len(results.Data))
  114. assert.Equal(t, "test_team", results.Data[0].Name)
  115. // no access if not organization member
  116. user5 := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
  117. session = loginUser(t, user5.Name)
  118. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/teams/search?q=%s", org.Name, "team")
  119. resp = session.MakeRequest(t, req, http.StatusForbidden)
  120. }