Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 15, 2024
1 parent 69396b5 commit 6664f3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
9 changes: 3 additions & 6 deletions models/perm/access/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
}
}()

if err := repo.LoadUnits(ctx); err != nil {
perm.UnitsMode = make(map[unit.Type]perm_model.AccessMode) // always initialize UnitsMode to avoid nil panic
if err = repo.LoadUnits(ctx); err != nil {
return perm, err
}
perm.Units = repo.Units
perm.UnitsMode = make(map[unit.Type]perm_model.AccessMode)

// anonymous user visit private repo.
// TODO: anonymous user visit public unit of private repo???
Expand All @@ -163,7 +163,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
}
}

if err := repo.LoadOwner(ctx); err != nil {
if err = repo.LoadOwner(ctx); err != nil {
return perm, err
}

Expand Down Expand Up @@ -192,9 +192,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
return perm, err
}

if err := repo.LoadOwner(ctx); err != nil {
return perm, err
}
if !repo.Owner.IsOrganization() {
return perm, nil
}
Expand Down
35 changes: 35 additions & 0 deletions models/perm/access/repo_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,38 @@ func TestApplyDefaultUserRepoPermission(t *testing.T) {
applyDefaultUserRepoPermission(&user_model.User{ID: 1}, &perm)
assert.True(t, perm.CanRead(unit.TypeWiki))
}

func TestUnitAccessMode(t *testing.T) {
perm := Permission{
AccessMode: perm_model.AccessModeNone,
}
assert.Equal(t, perm_model.AccessModeNone, perm.UnitAccessMode(unit.TypeWiki), "no unit or map, use AccessMode")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
Units: []*repo_model.RepoUnit{
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
},
UnitsMode: map[unit.Type]perm_model.AccessMode{},
}
assert.Equal(t, perm_model.AccessModeOwner, perm.UnitAccessMode(unit.TypeWiki), "only unit no map, use AccessMode")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
UnitsMode: map[unit.Type]perm_model.AccessMode{
unit.TypeWiki: perm_model.AccessModeRead,
},
}
assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeWiki), "no unit only map, use map")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
Units: []*repo_model.RepoUnit{
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeWrite},
},
UnitsMode: map[unit.Type]perm_model.AccessMode{
unit.TypeWiki: perm_model.AccessModeRead,
},
}
assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeWiki), "has unit and map, use map")
}

0 comments on commit 6664f3c

Please sign in to comment.