|
5 | 5 | package integrations
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "fmt" |
8 | 9 | "net/http"
|
| 10 | + "sort" |
9 | 11 | "testing"
|
10 | 12 |
|
11 | 13 | "code.gitea.io/gitea/models"
|
| 14 | + "code.gitea.io/gitea/routers/api/v1/convert" |
12 | 15 | api "code.gitea.io/sdk/gitea"
|
13 | 16 |
|
14 | 17 | "github.com/stretchr/testify/assert"
|
@@ -42,4 +45,65 @@ func TestAPITeam(t *testing.T) {
|
42 | 45 |
|
43 | 46 | req = NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID)
|
44 | 47 | resp = session.MakeRequest(t, req, http.StatusUnauthorized)
|
| 48 | + |
| 49 | + // Get an admin user able to create, update and delete teams. |
| 50 | + user = models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) |
| 51 | + session = loginUser(t, user.Name) |
| 52 | + token = getTokenForLoggedInUser(t, session) |
| 53 | + |
| 54 | + org := models.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User) |
| 55 | + |
| 56 | + // Create team. |
| 57 | + teamToCreate := &api.CreateTeamOption{ |
| 58 | + Name: "team1", |
| 59 | + Description: "team one", |
| 60 | + Permission: "write", |
| 61 | + Units: []string{"repo.code", "repo.issues"}, |
| 62 | + } |
| 63 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", org.Name, token), teamToCreate) |
| 64 | + resp = session.MakeRequest(t, req, http.StatusCreated) |
| 65 | + DecodeJSON(t, resp, &apiTeam) |
| 66 | + checkTeamResponse(t, &apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.Permission, teamToCreate.Units) |
| 67 | + checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.Permission, teamToCreate.Units) |
| 68 | + teamID := apiTeam.ID |
| 69 | + |
| 70 | + // Edit team. |
| 71 | + teamToEdit := &api.EditTeamOption{ |
| 72 | + Name: "teamone", |
| 73 | + Description: "team 1", |
| 74 | + Permission: "admin", |
| 75 | + Units: []string{"repo.code", "repo.pulls", "repo.releases"}, |
| 76 | + } |
| 77 | + req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d?token=%s", teamID, token), teamToEdit) |
| 78 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 79 | + DecodeJSON(t, resp, &apiTeam) |
| 80 | + checkTeamResponse(t, &apiTeam, teamToEdit.Name, teamToEdit.Description, teamToEdit.Permission, teamToEdit.Units) |
| 81 | + checkTeamBean(t, apiTeam.ID, teamToEdit.Name, teamToEdit.Description, teamToEdit.Permission, teamToEdit.Units) |
| 82 | + |
| 83 | + // Read team. |
| 84 | + teamRead := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) |
| 85 | + req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID) |
| 86 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 87 | + DecodeJSON(t, resp, &apiTeam) |
| 88 | + checkTeamResponse(t, &apiTeam, teamRead.Name, teamRead.Description, teamRead.Authorize.String(), teamRead.GetUnitNames()) |
| 89 | + |
| 90 | + // Delete team. |
| 91 | + req = NewRequestf(t, "DELETE", "/api/v1/teams/%d?token="+token, teamID) |
| 92 | + session.MakeRequest(t, req, http.StatusNoContent) |
| 93 | + models.AssertNotExistsBean(t, &models.Team{ID: teamID}) |
| 94 | +} |
| 95 | + |
| 96 | +func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string, permission string, units []string) { |
| 97 | + assert.Equal(t, name, apiTeam.Name, "name") |
| 98 | + assert.Equal(t, description, apiTeam.Description, "description") |
| 99 | + assert.Equal(t, permission, apiTeam.Permission, "permission") |
| 100 | + sort.StringSlice(units).Sort() |
| 101 | + sort.StringSlice(apiTeam.Units).Sort() |
| 102 | + assert.EqualValues(t, units, apiTeam.Units, "units") |
| 103 | +} |
| 104 | + |
| 105 | +func checkTeamBean(t *testing.T, id int64, name, description string, permission string, units []string) { |
| 106 | + team := models.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team) |
| 107 | + assert.NoError(t, team.GetUnits(), "GetUnits") |
| 108 | + checkTeamResponse(t, convert.ToTeam(team), name, description, permission, units) |
45 | 109 | }
|
0 commit comments