Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repo access check tests #208

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 142 additions & 1 deletion 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 @@ -1672,3 +1672,144 @@ func TestRepoComponent_Tree(t *testing.T) {
}

}

func TestRepoComponent_AllowReadAccess(t *testing.T) {
t.Run("should return false if repo find return error", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{}, errors.New("error"))
allow, err := repoComp.AllowReadAccess(ctx, types.ModelRepo, "namespace", "name", "user_name")
require.Error(t, fmt.Errorf("failed to find repo, error: %w", err))
require.False(t, allow)
})
}

func TestRepoComponent_AllowWriteAccess(t *testing.T) {
t.Run("should return false if username is empty", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: false,
}, nil)
allow, err := repoComp.AllowWriteAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, err, ErrUserNotFound)
require.False(t, allow)
})

t.Run("should return false if repo find return error", func(t *testing.T) {
ctx := context.TODO()
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, err, fmt.Errorf("failed to find repo, error: %w", err))
require.False(t, allow)
})

t.Run("should return false if user has no write access for public repo", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: false,
}, nil)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "namespace").Return(database.Namespace{
ID: 1,
Path: "namespace",
NamespaceType: database.UserNamespace,
}, nil)
repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
Email: "[email protected]",
RoleMask: "",
}, nil)
allow, err := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "user_name")
require.NoError(t, err)
require.False(t, allow)
})
}

func TestRepoComponent_AllowAdminAccess(t *testing.T) {
t.Run("should return false if username is empty", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: false,
}, nil)
allow, err := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "")
require.Error(t, err, ErrUserNotFound)
require.False(t, allow)
})

t.Run("should return false if repo find return error", func(t *testing.T) {
ctx := context.TODO()
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, err, fmt.Errorf("failed to find repo, error: %w", err))
require.False(t, allow)
})

t.Run("should return false if user has no admin access for public repo", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)
repoComp.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "namespace", "name").Return(&database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: false,
}, nil)
repoComp.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "namespace").Return(database.Namespace{
ID: 1,
Path: "namespace",
NamespaceType: database.UserNamespace,
}, nil)
repoComp.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user_name").Return(database.User{
ID: 1,
Username: "user_name",
Email: "[email protected]",
RoleMask: "",
}, nil)
allow, err := repoComp.AllowAdminAccess(ctx, types.ModelRepo, "namespace", "name", "user_name")
require.NoError(t, err)
require.False(t, allow)
})
}

func TestRepoComponent_AllowReadAccessRepo(t *testing.T) {
t.Run("should return true if repo is public", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)

allow, err := repoComp.AllowReadAccessRepo(ctx, &database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: false,
}, "user_name")
require.NoError(t, err)
require.True(t, allow)
})

t.Run("should return false if repo is private and username is empty", func(t *testing.T) {
ctx := context.TODO()
repoComp := initializeTestRepoComponent(ctx, t)

allow, err := repoComp.AllowReadAccessRepo(ctx, &database.Repository{
ID: 1,
Name: "name",
Path: "namespace/name",
Private: true,
}, "")
require.Error(t, err, ErrUserNotFound)
require.False(t, allow)
})
}
Loading