From e9b017368593cac123ac9e6ffd63134c5ae46bd5 Mon Sep 17 00:00:00 2001 From: zhzhang Date: Wed, 11 Dec 2024 11:43:26 +0800 Subject: [PATCH] Fix tests --- component/repo_test.go | 167 ++--------------------------------------- 1 file changed, 6 insertions(+), 161 deletions(-) diff --git a/component/repo_test.go b/component/repo_test.go index b2b598f3..cb1fe626 100644 --- a/component/repo_test.go +++ b/component/repo_test.go @@ -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() @@ -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() @@ -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) }) @@ -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) }) } @@ -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) })