Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pulltheflower committed Dec 11, 2024
1 parent 8bfc4d1 commit f24ad07
Showing 1 changed file with 7 additions and 162 deletions.
169 changes: 7 additions & 162 deletions component/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ func TestRepoComponent_UpdateTags(t *testing.T) {

}

func TestRepoComponent_checkCurrentUserPermission(t *testing.T) {
func TestRepoComponent_CheckCurrentUserPermission(t *testing.T) {

t.Run("can read self-owned", func(t *testing.T) {
ctx := context.TODO()
Expand Down Expand Up @@ -1673,161 +1673,6 @@ func TestRepoComponent_Tree(t *testing.T) {

}

func TestRepoComponent_CheckCurrentUserPermission(t *testing.T) {
t.Run("check current user permission when repo is user's personal repo", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)

repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "user_name").Return(database.Namespace{
ID: 1,
Path: "user_name",
NamespaceType: "user",
UserID: 1,
Mirrored: false,
}, nil)

repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
}, nil)

allow, err := repoComp.CheckCurrentUserPermission(ctx, "user_name", "user_name", membership.RoleRead)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "user_name", membership.RoleWrite)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "user_name", membership.RoleAdmin)
require.NoError(t, err)
require.True(t, allow)
})

t.Run("check current user permission when user is admin", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "org_name").Return(database.Namespace{
ID: 1,
Path: "org_name",
NamespaceType: "organization",
UserID: 1,
Mirrored: false,
}, nil)

repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
RoleMask: "admin",
}, nil)

allow, err := repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleRead)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleWrite)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleAdmin)
require.NoError(t, err)
require.True(t, allow)
})

t.Run("check current user permission when user is reader of organization", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "org_name").Return(database.Namespace{
ID: 1,
Path: "org_name",
NamespaceType: "organization",
UserID: 1,
Mirrored: false,
}, nil)

repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
}, nil)

repoComp.mocks.userSvcClient.EXPECT().GetMemberRole(ctx, "org_name", "user_name").Return(membership.RoleRead, nil)

allow, err := repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleRead)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleWrite)
require.NoError(t, err)
require.False(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleAdmin)
require.NoError(t, err)
require.False(t, allow)
})

t.Run("check current user permission when user is writer of organization", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "org_name").Return(database.Namespace{
ID: 1,
Path: "org_name",
NamespaceType: "organization",
UserID: 1,
Mirrored: false,
}, nil)

repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
}, nil)

repoComp.mocks.userSvcClient.EXPECT().GetMemberRole(ctx, "org_name", "user_name").Return(membership.RoleWrite, nil)

allow, err := repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleRead)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleWrite)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleAdmin)
require.NoError(t, err)
require.False(t, allow)
})

t.Run("check current user permission when user is admin of organization", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "org_name").Return(database.Namespace{
ID: 1,
Path: "org_name",
NamespaceType: "organization",
UserID: 1,
Mirrored: false,
}, nil)

repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
}, nil)

repoComp.mocks.userSvcClient.EXPECT().GetMemberRole(ctx, "org_name", "user_name").Return(membership.RoleAdmin, nil)

allow, err := repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleRead)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleWrite)
require.NoError(t, err)
require.True(t, allow)

allow, err = repoComp.CheckCurrentUserPermission(ctx, "user_name", "org_name", membership.RoleAdmin)
require.NoError(t, err)
require.True(t, allow)
})
}

func TestRepoComponent_AllowReadAccess(t *testing.T) {
t.Run("should return false if repo find return error", func(t *testing.T) {
ctx := context.TODO()
Expand All @@ -1849,8 +1694,8 @@ func TestRepoComponent_AllowWriteAccess(t *testing.T) {
Path: "namespace/name",
Private: false,
}, nil)
allow, _ := repoComp.AllowWriteAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, ErrUserNotFound)
allow, err := repoComp.AllowWriteAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, err, ErrUserNotFound)
require.False(t, allow)
})

Expand All @@ -1859,7 +1704,7 @@ func TestRepoComponent_AllowWriteAccess(t *testing.T) {
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{}, errors.New("error"))
allow, err := repoComp.AllowWriteAccess(ctx, types.ModelRepo, "namespace", "name", "user_name")
require.Error(t, fmt.Errorf("failed to find repo, error: %w", err))
require.Error(t, err, fmt.Errorf("failed to find repo, error: %w", err))
require.False(t, allow)
})
}
Expand All @@ -1874,8 +1719,8 @@ func TestRepoComponent_AllowAdminAccess(t *testing.T) {
Path: "namespace/name",
Private: false,
}, nil)
allow, _ := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, ErrUserNotFound)
allow, err := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, err, ErrUserNotFound)
require.False(t, allow)
})

Expand All @@ -1884,7 +1729,7 @@ func TestRepoComponent_AllowAdminAccess(t *testing.T) {
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{}, errors.New("error"))
allow, err := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "user_name")
require.Error(t, fmt.Errorf("failed to find repo, error: %w", err))
require.Error(t, err, fmt.Errorf("failed to find repo, error: %w", err))
require.False(t, allow)
})
}
Expand Down

0 comments on commit f24ad07

Please sign in to comment.