Files
forge/tests/integration/api_repo_collaborator_test.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

151 lines
6.3 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
auth_model "github.com/hanzoai/git/models/auth"
"github.com/hanzoai/git/models/perm"
repo_model "github.com/hanzoai/git/models/repo"
"github.com/hanzoai/git/models/unittest"
user_model "github.com/hanzoai/git/models/user"
api "github.com/hanzoai/git/modules/structs"
"github.com/hanzoai/git/tests"
"github.com/stretchr/testify/assert"
)
func TestAPIRepoCollaboratorPermission(t *testing.T) {
defer tests.PrepareTestEnv(t)()
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
repo2Owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo2.OwnerID})
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
user10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10})
user11 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 11})
user34 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 34})
testCtx := NewAPITestContext(t, repo2Owner.Name, repo2.Name, auth_model.AccessTokenScopeWriteRepository)
t.Run("RepoOwnerShouldBeOwner", func(t *testing.T) {
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, repo2Owner.Name).
AddTokenAuth(testCtx.Token)
resp := MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameOwner, repoPermission.Permission)
})
t.Run("CollaboratorWithReadAccess", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithReadAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeRead))
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user4.Name).
AddTokenAuth(testCtx.Token)
resp := MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
})
t.Run("CollaboratorWithWriteAccess", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithWriteAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeWrite))
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user4.Name).
AddTokenAuth(testCtx.Token)
resp := MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameWrite, repoPermission.Permission)
})
t.Run("CollaboratorWithAdminAccess", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithAdminAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeAdmin))
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user4.Name).
AddTokenAuth(testCtx.Token)
resp := MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameAdmin, repoPermission.Permission)
})
t.Run("CollaboratorNotFound", func(t *testing.T) {
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, "non-existent-user").
AddTokenAuth(testCtx.Token)
MakeRequest(t, req, http.StatusNotFound)
})
t.Run("CollaboratorBlocked", func(t *testing.T) {
ctx := NewAPITestContext(t, repo2Owner.Name, repo2.Name, auth_model.AccessTokenScopeWriteRepository)
ctx.ExpectedCode = http.StatusForbidden
doAPIAddCollaborator(ctx, user34.Name, perm.AccessModeAdmin)(t)
})
t.Run("CollaboratorCanQueryItsPermissions", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithReadAccess", doAPIAddCollaborator(testCtx, user5.Name, perm.AccessModeRead))
_session := loginUser(t, user5.Name)
_testCtx := NewAPITestContext(t, user5.Name, repo2.Name, auth_model.AccessTokenScopeReadRepository)
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user5.Name).
AddTokenAuth(_testCtx.Token)
resp := _session.MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
t.Run("CollaboratorCanReadOwnPermission", func(t *testing.T) {
session := loginUser(t, user5.Name)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
req = NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user5.Name).AddTokenAuth(token)
resp = MakeRequest(t, req, http.StatusOK)
repoCollPerm := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameRead, repoCollPerm.Permission)
})
})
t.Run("CollaboratorCanQueryItsPermissions", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithReadAccess", doAPIAddCollaborator(testCtx, user5.Name, perm.AccessModeRead))
_session := loginUser(t, user5.Name)
_testCtx := NewAPITestContext(t, user5.Name, repo2.Name, auth_model.AccessTokenScopeReadRepository)
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user5.Name).
AddTokenAuth(_testCtx.Token)
resp := _session.MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
})
t.Run("RepoAdminCanQueryACollaboratorsPermissions", func(t *testing.T) {
t.Run("AddUserAsCollaboratorWithAdminAccess", doAPIAddCollaborator(testCtx, user10.Name, perm.AccessModeAdmin))
t.Run("AddUserAsCollaboratorWithReadAccess", doAPIAddCollaborator(testCtx, user11.Name, perm.AccessModeRead))
_session := loginUser(t, user10.Name)
_testCtx := NewAPITestContext(t, user10.Name, repo2.Name, auth_model.AccessTokenScopeReadRepository)
req := NewRequestf(t, "GET", "/v1/repos/%s/%s/collaborators/%s/permission", repo2Owner.Name, repo2.Name, user11.Name).
AddTokenAuth(_testCtx.Token)
resp := _session.MakeRequest(t, req, http.StatusOK)
repoPermission := DecodeJSON(t, resp, &api.RepoCollaboratorPermission{})
assert.Equal(t, api.AccessLevelNameRead, repoPermission.Permission)
})
}