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.
83 lines
3.0 KiB
83 lines
3.0 KiB
4 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package integration
|
||
4 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unit"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
4 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
4 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestAPIRepoTeams(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
4 years ago
|
|
||
|
// publicOrgRepo = user3/repo21
|
||
2 years ago
|
publicOrgRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 32})
|
||
4 years ago
|
// user4
|
||
2 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
||
4 years ago
|
session := loginUser(t, user.Name)
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
|
||
|
// ListTeams
|
||
|
url := fmt.Sprintf("/api/v1/repos/%s/teams?token=%s", publicOrgRepo.FullName(), token)
|
||
|
req := NewRequest(t, "GET", url)
|
||
|
res := session.MakeRequest(t, req, http.StatusOK)
|
||
|
var teams []*api.Team
|
||
|
DecodeJSON(t, res, &teams)
|
||
|
if assert.Len(t, teams, 2) {
|
||
|
assert.EqualValues(t, "Owners", teams[0].Name)
|
||
3 years ago
|
assert.True(t, teams[0].CanCreateOrgRepo)
|
||
3 years ago
|
assert.True(t, util.IsEqualSlice(unit.AllUnitKeyNames(), teams[0].Units), fmt.Sprintf("%v == %v", unit.AllUnitKeyNames(), teams[0].Units))
|
||
4 years ago
|
assert.EqualValues(t, "owner", teams[0].Permission)
|
||
|
|
||
|
assert.EqualValues(t, "test_team", teams[1].Name)
|
||
4 years ago
|
assert.False(t, teams[1].CanCreateOrgRepo)
|
||
4 years ago
|
assert.EqualValues(t, []string{"repo.issues"}, teams[1].Units)
|
||
|
assert.EqualValues(t, "write", teams[1].Permission)
|
||
|
}
|
||
|
|
||
|
// IsTeam
|
||
|
url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "Test_Team", token)
|
||
|
req = NewRequest(t, "GET", url)
|
||
|
res = session.MakeRequest(t, req, http.StatusOK)
|
||
|
var team *api.Team
|
||
|
DecodeJSON(t, res, &team)
|
||
|
assert.EqualValues(t, teams[1], team)
|
||
|
|
||
|
url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "NonExistingTeam", token)
|
||
|
req = NewRequest(t, "GET", url)
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusNotFound)
|
||
4 years ago
|
|
||
|
// AddTeam with user4
|
||
|
url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "team1", token)
|
||
|
req = NewRequest(t, "PUT", url)
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusForbidden)
|
||
4 years ago
|
|
||
|
// AddTeam with user2
|
||
2 years ago
|
user = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||
4 years ago
|
session = loginUser(t, user.Name)
|
||
|
token = getTokenForLoggedInUser(t, session)
|
||
|
url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "team1", token)
|
||
|
req = NewRequest(t, "PUT", url)
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusNoContent)
|
||
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity) // test duplicate request
|
||
4 years ago
|
|
||
|
// DeleteTeam
|
||
|
url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "team1", token)
|
||
|
req = NewRequest(t, "DELETE", url)
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusNoContent)
|
||
|
session.MakeRequest(t, req, http.StatusUnprocessableEntity) // test duplicate request
|
||
4 years ago
|
}
|