2017-04-29 06:33:25 +02:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
2017-10-10 03:23:29 +02:00
|
|
|
"fmt"
|
2019-03-15 15:19:09 +01:00
|
|
|
"io/ioutil"
|
2017-04-29 06:33:25 +02:00
|
|
|
"net/http"
|
2019-03-15 15:19:09 +01:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-04-29 06:33:25 +02:00
|
|
|
"testing"
|
2017-07-10 13:07:39 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-07-10 13:07:39 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-04-29 06:33:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIUserReposNotLogin(t *testing.T) {
|
2017-06-25 11:34:30 +02:00
|
|
|
prepareTestEnv(t)
|
2017-07-10 13:07:39 +02:00
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
|
|
|
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/users/%s/repos", user.Name)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2017-04-29 06:33:25 +02:00
|
|
|
|
2017-07-10 13:07:39 +02:00
|
|
|
var apiRepos []api.Repository
|
|
|
|
DecodeJSON(t, resp, &apiRepos)
|
|
|
|
expectedLen := models.GetCount(t, models.Repository{OwnerID: user.ID},
|
|
|
|
models.Cond("is_private = ?", false))
|
|
|
|
assert.Len(t, apiRepos, expectedLen)
|
|
|
|
for _, repo := range apiRepos {
|
|
|
|
assert.EqualValues(t, user.ID, repo.Owner.ID)
|
|
|
|
assert.False(t, repo.Private)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 03:23:29 +02:00
|
|
|
func TestAPISearchRepo(t *testing.T) {
|
2017-06-25 11:34:30 +02:00
|
|
|
prepareTestEnv(t)
|
2017-07-10 13:07:39 +02:00
|
|
|
const keyword = "test"
|
|
|
|
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/repos/search?q=%s", keyword)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2017-04-30 07:08:09 +02:00
|
|
|
|
2017-09-20 11:24:38 +02:00
|
|
|
var body api.SearchResults
|
2017-07-10 13:07:39 +02:00
|
|
|
DecodeJSON(t, resp, &body)
|
2017-09-20 11:24:38 +02:00
|
|
|
assert.NotEmpty(t, body.Data)
|
|
|
|
for _, repo := range body.Data {
|
|
|
|
assert.Contains(t, repo.Name, keyword)
|
|
|
|
assert.False(t, repo.Private)
|
2017-07-10 13:07:39 +02:00
|
|
|
}
|
2017-10-10 03:23:29 +02:00
|
|
|
|
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User)
|
|
|
|
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User)
|
2017-10-17 17:20:22 +02:00
|
|
|
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 18}).(*models.User)
|
2017-10-26 23:16:13 +02:00
|
|
|
user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 20}).(*models.User)
|
2017-10-10 03:23:29 +02:00
|
|
|
orgUser := models.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User)
|
|
|
|
|
|
|
|
// Map of expected results, where key is user for login
|
|
|
|
type expectedResults map[*models.User]struct {
|
|
|
|
count int
|
|
|
|
repoOwnerID int64
|
|
|
|
repoName string
|
|
|
|
includesPrivate bool
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name, requestURL string
|
|
|
|
expectedResults
|
|
|
|
}{
|
2019-05-15 17:24:39 +02:00
|
|
|
{name: "RepositoriesMax50", requestURL: "/api/v1/repos/search?limit=50&private=false", expectedResults: expectedResults{
|
2019-04-17 07:31:08 +02:00
|
|
|
nil: {count: 21},
|
|
|
|
user: {count: 21},
|
|
|
|
user2: {count: 21}},
|
2017-10-10 03:23:29 +02:00
|
|
|
},
|
2019-05-15 17:24:39 +02:00
|
|
|
{name: "RepositoriesMax10", requestURL: "/api/v1/repos/search?limit=10&private=false", expectedResults: expectedResults{
|
2017-10-10 03:23:29 +02:00
|
|
|
nil: {count: 10},
|
|
|
|
user: {count: 10},
|
|
|
|
user2: {count: 10}},
|
|
|
|
},
|
2019-05-15 17:24:39 +02:00
|
|
|
{name: "RepositoriesDefaultMax10", requestURL: "/api/v1/repos/search?default&private=false", expectedResults: expectedResults{
|
2017-10-10 03:23:29 +02:00
|
|
|
nil: {count: 10},
|
|
|
|
user: {count: 10},
|
|
|
|
user2: {count: 10}},
|
|
|
|
},
|
2019-05-15 17:24:39 +02:00
|
|
|
{name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/v1/repos/search?q=%s&private=false", "big_test_"), expectedResults: expectedResults{
|
2017-10-26 23:16:13 +02:00
|
|
|
nil: {count: 7, repoName: "big_test_"},
|
|
|
|
user: {count: 7, repoName: "big_test_"},
|
|
|
|
user2: {count: 7, repoName: "big_test_"}},
|
2017-10-10 03:23:29 +02:00
|
|
|
},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
|
2019-05-15 17:24:39 +02:00
|
|
|
nil: {count: 5},
|
|
|
|
user: {count: 9, includesPrivate: true},
|
|
|
|
user2: {count: 5, includesPrivate: true}},
|
2017-10-10 03:23:29 +02:00
|
|
|
},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1},
|
2019-05-15 17:24:39 +02:00
|
|
|
user: {count: 2, includesPrivate: true},
|
|
|
|
user2: {count: 2, includesPrivate: true},
|
|
|
|
user4: {count: 1}},
|
2017-10-10 03:23:29 +02:00
|
|
|
},
|
2017-10-17 17:20:22 +02:00
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser3", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user3.ID), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1},
|
2019-05-15 17:24:39 +02:00
|
|
|
user: {count: 4, includesPrivate: true},
|
|
|
|
user2: {count: 2, includesPrivate: true},
|
2017-10-17 17:20:22 +02:00
|
|
|
user3: {count: 4, includesPrivate: true}},
|
|
|
|
},
|
2017-10-10 03:23:29 +02:00
|
|
|
{name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1, repoOwnerID: orgUser.ID},
|
|
|
|
user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
|
|
|
|
user2: {count: 1, repoOwnerID: orgUser.ID}},
|
|
|
|
},
|
2017-10-26 23:16:13 +02:00
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user4.ID), expectedResults: expectedResults{
|
|
|
|
nil: {count: 3},
|
2019-05-15 17:24:39 +02:00
|
|
|
user: {count: 4, includesPrivate: true},
|
|
|
|
user4: {count: 7, includesPrivate: true}}},
|
2017-10-26 23:16:13 +02:00
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeSource", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "source"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 0},
|
2019-05-15 17:24:39 +02:00
|
|
|
user: {count: 1, includesPrivate: true},
|
|
|
|
user4: {count: 1, includesPrivate: true}}},
|
2017-10-26 23:16:13 +02:00
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "fork"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1},
|
|
|
|
user: {count: 1},
|
|
|
|
user4: {count: 2, includesPrivate: true}}},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork/Exclusive", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "fork"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1},
|
|
|
|
user: {count: 1},
|
|
|
|
user4: {count: 2, includesPrivate: true}}},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "mirror"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 2},
|
|
|
|
user: {count: 2},
|
|
|
|
user4: {count: 4, includesPrivate: true}}},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror/Exclusive", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "mirror"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 1},
|
|
|
|
user: {count: 1},
|
|
|
|
user4: {count: 2, includesPrivate: true}}},
|
|
|
|
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeCollaborative", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "collaborative"), expectedResults: expectedResults{
|
|
|
|
nil: {count: 0},
|
2019-05-15 17:24:39 +02:00
|
|
|
user: {count: 1, includesPrivate: true},
|
|
|
|
user4: {count: 1, includesPrivate: true}}},
|
2017-10-10 03:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
for userToLogin, expected := range testCase.expectedResults {
|
|
|
|
var session *TestSession
|
|
|
|
var testName string
|
2017-10-26 23:16:13 +02:00
|
|
|
var userID int64
|
2018-09-10 18:15:52 +02:00
|
|
|
var token string
|
2017-10-10 03:23:29 +02:00
|
|
|
if userToLogin != nil && userToLogin.ID > 0 {
|
|
|
|
testName = fmt.Sprintf("LoggedUser%d", userToLogin.ID)
|
|
|
|
session = loginUser(t, userToLogin.Name)
|
2018-09-10 18:15:52 +02:00
|
|
|
token = getTokenForLoggedInUser(t, session)
|
2017-10-26 23:16:13 +02:00
|
|
|
userID = userToLogin.ID
|
2017-10-10 03:23:29 +02:00
|
|
|
} else {
|
|
|
|
testName = "AnonymousUser"
|
|
|
|
session = emptyTestSession(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
2018-09-10 18:15:52 +02:00
|
|
|
request := NewRequest(t, "GET", testCase.requestURL+"&token="+token)
|
2017-10-10 03:23:29 +02:00
|
|
|
response := session.MakeRequest(t, request, http.StatusOK)
|
|
|
|
|
|
|
|
var body api.SearchResults
|
|
|
|
DecodeJSON(t, response, &body)
|
|
|
|
|
2019-05-15 17:24:39 +02:00
|
|
|
repoNames := make([]string, 0, len(body.Data))
|
|
|
|
for _, repo := range body.Data {
|
|
|
|
repoNames = append(repoNames, fmt.Sprintf("%d:%s:%t", repo.ID, repo.FullName, repo.Private))
|
|
|
|
}
|
|
|
|
assert.Len(t, repoNames, expected.count)
|
2017-10-10 03:23:29 +02:00
|
|
|
for _, repo := range body.Data {
|
2017-10-26 23:16:13 +02:00
|
|
|
r := getRepo(t, repo.ID)
|
2018-11-28 12:26:14 +01:00
|
|
|
hasAccess, err := models.HasAccess(userID, r)
|
2019-05-15 17:24:39 +02:00
|
|
|
assert.NoError(t, err, "Error when checking if User: %d has access to %s: %v", userID, repo.FullName, err)
|
|
|
|
assert.True(t, hasAccess, "User: %d does not have access to %s", userID, repo.FullName)
|
2017-10-26 23:16:13 +02:00
|
|
|
|
2017-10-10 03:23:29 +02:00
|
|
|
assert.NotEmpty(t, repo.Name)
|
2019-05-15 17:24:39 +02:00
|
|
|
assert.Equal(t, repo.Name, r.Name)
|
2017-10-10 03:23:29 +02:00
|
|
|
|
|
|
|
if len(expected.repoName) > 0 {
|
|
|
|
assert.Contains(t, repo.Name, expected.repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected.repoOwnerID > 0 {
|
|
|
|
assert.Equal(t, expected.repoOwnerID, repo.Owner.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expected.includesPrivate {
|
2019-05-15 17:24:39 +02:00
|
|
|
assert.False(t, repo.Private, "User: %d not expecting private repository: %s", userID, repo.FullName)
|
2017-10-10 03:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-04-30 07:08:09 +02:00
|
|
|
}
|
2017-07-12 03:23:41 +02:00
|
|
|
|
2017-10-26 23:16:13 +02:00
|
|
|
var repoCache = make(map[int64]*models.Repository)
|
|
|
|
|
|
|
|
func getRepo(t *testing.T, repoID int64) *models.Repository {
|
|
|
|
if _, ok := repoCache[repoID]; !ok {
|
|
|
|
repoCache[repoID] = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
|
|
|
|
}
|
|
|
|
return repoCache[repoID]
|
|
|
|
}
|
|
|
|
|
2017-07-12 03:23:41 +02:00
|
|
|
func TestAPIViewRepo(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1")
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var repo api.Repository
|
|
|
|
DecodeJSON(t, resp, &repo)
|
|
|
|
assert.EqualValues(t, 1, repo.ID)
|
|
|
|
assert.EqualValues(t, "repo1", repo.Name)
|
|
|
|
}
|
2017-07-13 13:14:15 +02:00
|
|
|
|
|
|
|
func TestAPIOrgRepos(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
2018-11-23 22:23:27 +01:00
|
|
|
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
|
|
|
|
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
|
2017-07-13 13:14:15 +02:00
|
|
|
// User3 is an Org. Check their repos.
|
|
|
|
sourceOrg := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
|
|
|
|
|
2018-11-23 22:23:27 +01:00
|
|
|
expectedResults := map[*models.User]struct {
|
|
|
|
count int
|
|
|
|
includesPrivate bool
|
|
|
|
}{
|
|
|
|
nil: {count: 1},
|
|
|
|
user: {count: 2, includesPrivate: true},
|
|
|
|
user2: {count: 3, includesPrivate: true},
|
|
|
|
user3: {count: 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
for userToLogin, expected := range expectedResults {
|
|
|
|
var session *TestSession
|
|
|
|
var testName string
|
|
|
|
var token string
|
|
|
|
if userToLogin != nil && userToLogin.ID > 0 {
|
|
|
|
testName = fmt.Sprintf("LoggedUser%d", userToLogin.ID)
|
|
|
|
session = loginUser(t, userToLogin.Name)
|
|
|
|
token = getTokenForLoggedInUser(t, session)
|
|
|
|
} else {
|
|
|
|
testName = "AnonymousUser"
|
|
|
|
session = emptyTestSession(t)
|
|
|
|
}
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/orgs/%s/repos?token="+token, sourceOrg.Name)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var apiRepos []*api.Repository
|
|
|
|
DecodeJSON(t, resp, &apiRepos)
|
|
|
|
assert.Len(t, apiRepos, expected.count)
|
|
|
|
for _, repo := range apiRepos {
|
|
|
|
if !expected.includesPrivate {
|
|
|
|
assert.False(t, repo.Private)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-07-13 13:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 03:13:33 +02:00
|
|
|
|
|
|
|
func TestAPIGetRepoByIDUnauthorized(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User)
|
2018-09-10 18:15:52 +02:00
|
|
|
session := loginUser(t, user.Name)
|
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/repositories/2?token="+token)
|
|
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
2017-07-30 03:13:33 +02:00
|
|
|
}
|
2018-07-05 00:45:15 +02:00
|
|
|
|
|
|
|
func TestAPIRepoMigrate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
ctxUserID, userID int64
|
|
|
|
cloneURL, repoName string
|
|
|
|
expectedStatus int
|
|
|
|
}{
|
|
|
|
{ctxUserID: 1, userID: 2, cloneURL: "https://github.com/go-gitea/git.git", repoName: "git-admin", expectedStatus: http.StatusCreated},
|
|
|
|
{ctxUserID: 2, userID: 2, cloneURL: "https://github.com/go-gitea/git.git", repoName: "git-own", expectedStatus: http.StatusCreated},
|
|
|
|
{ctxUserID: 2, userID: 1, cloneURL: "https://github.com/go-gitea/git.git", repoName: "git-bad", expectedStatus: http.StatusForbidden},
|
|
|
|
{ctxUserID: 2, userID: 3, cloneURL: "https://github.com/go-gitea/git.git", repoName: "git-org", expectedStatus: http.StatusCreated},
|
|
|
|
{ctxUserID: 2, userID: 6, cloneURL: "https://github.com/go-gitea/git.git", repoName: "git-bad-org", expectedStatus: http.StatusForbidden},
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareTestEnv(t)
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
|
|
|
|
session := loginUser(t, user.Name)
|
2018-09-10 18:15:52 +02:00
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOption{
|
2018-07-05 00:45:15 +02:00
|
|
|
CloneAddr: testCase.cloneURL,
|
|
|
|
UID: int(testCase.userID),
|
|
|
|
RepoName: testCase.repoName,
|
|
|
|
})
|
|
|
|
session.MakeRequest(t, req, testCase.expectedStatus)
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 01:51:02 +02:00
|
|
|
|
2019-03-15 15:19:09 +01:00
|
|
|
func TestAPIRepoMigrateConflict(t *testing.T) {
|
|
|
|
onGiteaRun(t, testAPIRepoMigrateConflict)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
|
|
|
|
username := "user2"
|
|
|
|
baseAPITestContext := NewAPITestContext(t, username, "repo1")
|
|
|
|
|
|
|
|
u.Path = baseAPITestContext.GitPath()
|
|
|
|
|
|
|
|
t.Run("Existing", func(t *testing.T) {
|
|
|
|
httpContext := baseAPITestContext
|
|
|
|
|
|
|
|
httpContext.Reponame = "repo-tmp-17"
|
|
|
|
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dstPath)
|
|
|
|
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
|
|
|
|
|
|
|
|
user, err := models.GetUserByName(httpContext.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
userID := user.ID
|
|
|
|
|
|
|
|
cloneURL := "https://github.com/go-gitea/git.git"
|
|
|
|
|
|
|
|
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+httpContext.Token,
|
|
|
|
&api.MigrateRepoOption{
|
|
|
|
CloneAddr: cloneURL,
|
|
|
|
UID: int(userID),
|
|
|
|
RepoName: httpContext.Reponame,
|
|
|
|
})
|
|
|
|
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
|
|
|
|
respJSON := map[string]string{}
|
|
|
|
DecodeJSON(t, resp, &respJSON)
|
|
|
|
assert.Equal(t, respJSON["message"], "The repository with the same name already exists.")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-05 01:51:02 +02:00
|
|
|
func TestAPIOrgRepoCreate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
ctxUserID int64
|
|
|
|
orgName, repoName string
|
|
|
|
expectedStatus int
|
|
|
|
}{
|
|
|
|
{ctxUserID: 1, orgName: "user3", repoName: "repo-admin", expectedStatus: http.StatusCreated},
|
|
|
|
{ctxUserID: 2, orgName: "user3", repoName: "repo-own", expectedStatus: http.StatusCreated},
|
|
|
|
{ctxUserID: 2, orgName: "user6", repoName: "repo-bad-org", expectedStatus: http.StatusForbidden},
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareTestEnv(t)
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
|
|
|
|
session := loginUser(t, user.Name)
|
2018-09-10 18:15:52 +02:00
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/org/%s/repos?token="+token, testCase.orgName), &api.CreateRepoOption{
|
2018-07-05 01:51:02 +02:00
|
|
|
Name: testCase.repoName,
|
|
|
|
})
|
|
|
|
session.MakeRequest(t, req, testCase.expectedStatus)
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 15:19:09 +01:00
|
|
|
|
|
|
|
func TestAPIRepoCreateConflict(t *testing.T) {
|
|
|
|
onGiteaRun(t, testAPIRepoCreateConflict)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAPIRepoCreateConflict(t *testing.T, u *url.URL) {
|
|
|
|
username := "user2"
|
|
|
|
baseAPITestContext := NewAPITestContext(t, username, "repo1")
|
|
|
|
|
|
|
|
u.Path = baseAPITestContext.GitPath()
|
|
|
|
|
|
|
|
t.Run("Existing", func(t *testing.T) {
|
|
|
|
httpContext := baseAPITestContext
|
|
|
|
|
|
|
|
httpContext.Reponame = "repo-tmp-17"
|
|
|
|
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dstPath)
|
|
|
|
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
|
|
|
|
|
|
|
|
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+httpContext.Token,
|
|
|
|
&api.CreateRepoOption{
|
|
|
|
Name: httpContext.Reponame,
|
|
|
|
})
|
|
|
|
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
|
|
|
|
respJSON := map[string]string{}
|
|
|
|
DecodeJSON(t, resp, &respJSON)
|
|
|
|
assert.Equal(t, respJSON["message"], "The repository with the same name already exists.")
|
|
|
|
})
|
|
|
|
}
|