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.

160 lines
6.6 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. editDescription := "team 1"
  60. editFalse := false
  61. teamToEdit := &api.EditTeamOption{
  62. Name: "teamone",
  63. Description: &editDescription,
  64. Permission: "admin",
  65. IncludesAllRepositories: &editFalse,
  66. Units: []string{"repo.code", "repo.pulls", "repo.releases"},
  67. }
  68. req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit)
  69. resp = session.MakeRequest(t, req, http.StatusOK)
  70. DecodeJSON(t, resp, &apiTeam)
  71. checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
  72. teamToEdit.Permission, teamToEdit.Units)
  73. checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEdit.Description, *teamToEdit.IncludesAllRepositories,
  74. teamToEdit.Permission, teamToEdit.Units)
  75. // Edit team Description only
  76. editDescription = "first team"
  77. teamToEditDesc := api.EditTeamOption{Description: &editDescription}
  78. req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEditDesc)
  79. resp = session.MakeRequest(t, req, http.StatusOK)
  80. DecodeJSON(t, resp, &apiTeam)
  81. checkTeamResponse(t, &apiTeam, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
  82. teamToEdit.Permission, teamToEdit.Units)
  83. checkTeamBean(t, apiTeam.ID, teamToEdit.Name, *teamToEditDesc.Description, *teamToEdit.IncludesAllRepositories,
  84. teamToEdit.Permission, teamToEdit.Units)
  85. // Read team.
  86. teamRead := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
  87. req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID)
  88. resp = session.MakeRequest(t, req, http.StatusOK)
  89. DecodeJSON(t, resp, &apiTeam)
  90. checkTeamResponse(t, &apiTeam, teamRead.Name, *teamToEditDesc.Description, teamRead.IncludesAllRepositories,
  91. teamRead.Authorize.String(), teamRead.GetUnitNames())
  92. // Delete team.
  93. req = NewRequestf(t, "DELETE", "/api/v1/teams/%d?token="+token, teamID)
  94. session.MakeRequest(t, req, http.StatusNoContent)
  95. models.AssertNotExistsBean(t, &models.Team{ID: teamID})
  96. }
  97. func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string, includesAllRepositories bool, permission string, units []string) {
  98. assert.Equal(t, name, apiTeam.Name, "name")
  99. assert.Equal(t, description, apiTeam.Description, "description")
  100. assert.Equal(t, includesAllRepositories, apiTeam.IncludesAllRepositories, "includesAllRepositories")
  101. assert.Equal(t, permission, apiTeam.Permission, "permission")
  102. sort.StringSlice(units).Sort()
  103. sort.StringSlice(apiTeam.Units).Sort()
  104. assert.EqualValues(t, units, apiTeam.Units, "units")
  105. }
  106. func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string) {
  107. team := models.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team)
  108. assert.NoError(t, team.GetUnits(), "GetUnits")
  109. checkTeamResponse(t, convert.ToTeam(team), name, description, includesAllRepositories, permission, units)
  110. }
  111. type TeamSearchResults struct {
  112. OK bool `json:"ok"`
  113. Data []*api.Team `json:"data"`
  114. }
  115. func TestAPITeamSearch(t *testing.T) {
  116. defer prepareTestEnv(t)()
  117. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  118. org := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
  119. var results TeamSearchResults
  120. session := loginUser(t, user.Name)
  121. req := NewRequestf(t, "GET", "/api/v1/orgs/%s/teams/search?q=%s", org.Name, "_team")
  122. resp := session.MakeRequest(t, req, http.StatusOK)
  123. DecodeJSON(t, resp, &results)
  124. assert.NotEmpty(t, results.Data)
  125. assert.Equal(t, 1, len(results.Data))
  126. assert.Equal(t, "test_team", results.Data[0].Name)
  127. // no access if not organization member
  128. user5 := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
  129. session = loginUser(t, user5.Name)
  130. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/teams/search?q=%s", org.Name, "team")
  131. resp = session.MakeRequest(t, req, http.StatusForbidden)
  132. }