Skip to content

Commit 08a0caa

Browse files
committed
fix test
1 parent 236e660 commit 08a0caa

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

models/user/user.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,13 @@ func (u *User) MaxCreationLimit() int {
249249
}
250250

251251
// CanCreateRepoIn checks whether the doer(u) can create a repository in the owner
252-
// NOTE: functions calling this assume a failure due to repository count limit; it ONLY checks the repo number LIMIT, if new checks are added, those functions should be revised
252+
// NOTE: functions calling this assume a failure due to repository count limit, or the owner is not a real user.
253+
// It ONLY checks the repo number LIMIT or whether owner user is real. If new checks are added, those functions should be revised.
254+
// TODO: the callers can only return ErrReachLimitOfRepo, need to fine tune to support other error types in the future.
253255
func (u *User) CanCreateRepoIn(owner *User) bool {
256+
if u.ID <= 0 {
257+
return false // fake user like Ghost or Actions user
258+
}
254259
if u.IsAdmin {
255260
return true
256261
}

routers/api/v1/repo/repo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
repo_module "code.gitea.io/gitea/modules/repository"
2929
"code.gitea.io/gitea/modules/setting"
3030
api "code.gitea.io/gitea/modules/structs"
31+
"code.gitea.io/gitea/modules/util"
3132
"code.gitea.io/gitea/modules/validation"
3233
"code.gitea.io/gitea/modules/web"
3334
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -270,6 +271,8 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
270271
db.IsErrNamePatternNotAllowed(err) ||
271272
label.IsErrTemplateLoad(err) {
272273
ctx.APIError(http.StatusUnprocessableEntity, err)
274+
} else if errors.Is(err, util.ErrPermissionDenied) {
275+
ctx.APIError(http.StatusForbidden, err)
273276
} else {
274277
ctx.APIErrorInternal(err)
275278
}

services/lfs/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho
556556
return perm.CanAccess(accessMode, unit.TypeCode)
557557
}
558558

559-
// ctx.IsSigned is unnecessary here, this will be checked in perm.CanAccess
559+
// it works for both anonymous request and signed-in user, then perm.CanAccess will do the permission check
560560
perm, err := access_model.GetUserRepoPermission(ctx, repository, ctx.Doer)
561561
if err != nil {
562562
log.Error("Unable to GetUserRepoPermission for user %-v in repo %-v Error: %v", ctx.Doer, repository, err)
@@ -571,6 +571,8 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho
571571
}
572572

573573
// now, either sign-in is required or the ctx.Doer cannot access, check the LFS token
574+
// however, "ctx.Doer exists but cannot access then check LFS token" should not really happen:
575+
// * why a request can be sent with both valid user session and valid LFS token then use LFS token to access?
574576
user, err := parseToken(ctx, authorization, repository, accessMode)
575577
if err != nil {
576578
// Most of these are Warn level - the true internal server errors are logged in parseToken already

tests/integration/actions_job_token_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/db"
1515
"code.gitea.io/gitea/models/unittest"
1616
"code.gitea.io/gitea/modules/structs"
17+
"code.gitea.io/gitea/modules/util"
1718

1819
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
@@ -53,9 +54,7 @@ func testActionsJobTokenAccess(u *url.URL, isFork bool) func(t *testing.T) {
5354
require.Equal(t, "user5", r.Owner.UserName)
5455
}))
5556

56-
if isFork {
57-
context.ExpectedCode = 403
58-
}
57+
context.ExpectedCode = util.Iif(isFork, http.StatusForbidden, http.StatusCreated)
5958
t.Run("API Create File", doAPICreateFile(context, "test.txt", &structs.CreateFileOptions{
6059
FileOptions: structs.FileOptions{
6160
NewBranchName: "new-branch",
@@ -64,10 +63,10 @@ func testActionsJobTokenAccess(u *url.URL, isFork bool) func(t *testing.T) {
6463
ContentBase64: base64.StdEncoding.EncodeToString([]byte(`This is a test file created using job token.`)),
6564
}))
6665

67-
context.ExpectedCode = 500
66+
context.ExpectedCode = http.StatusForbidden
6867
t.Run("Fail to Create Repository", doAPICreateRepository(context, true))
6968

70-
context.ExpectedCode = 403
69+
context.ExpectedCode = http.StatusForbidden
7170
t.Run("Fail to Delete Repository", doAPIDeleteRepository(context))
7271

7372
t.Run("Fail to Create Organization", doAPICreateOrganization(context, &structs.CreateOrgOption{

0 commit comments

Comments
 (0)