Skip to content

Commit

Permalink
Merge branch 'feature/component_tests' into 'main'
Browse files Browse the repository at this point in the history
Add some component tests

See merge request product/starhub/starhub-server!714
  • Loading branch information
yiling.ji authored and Yiling-J committed Dec 12, 2024
1 parent 691f8fd commit f501546
Show file tree
Hide file tree
Showing 17 changed files with 1,155 additions and 203 deletions.
58 changes: 0 additions & 58 deletions _mocks/opencsg.com/csghub-server/component/mock_TagComponent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions common/tests/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type MockStores struct {
Org database.OrgStore
MultiSync database.MultiSyncStore
File database.FileStore
SSH database.SSHKeyStore
}

func NewMockStores(t interface {
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewMockStores(t interface {
Org: mockdb.NewMockOrgStore(t),
MultiSync: mockdb.NewMockMultiSyncStore(t),
File: mockdb.NewMockFileStore(t),
SSH: mockdb.NewMockSSHKeyStore(t),
}
}

Expand Down Expand Up @@ -232,3 +234,7 @@ func (s *MockStores) MultiSyncMock() *mockdb.MockMultiSyncStore {
func (s *MockStores) FileMock() *mockdb.MockFileStore {
return s.File.(*mockdb.MockFileStore)
}

func (s *MockStores) SSHMock() *mockdb.MockSSHKeyStore {
return s.SSH.(*mockdb.MockSSHKeyStore)
}
28 changes: 19 additions & 9 deletions component/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

pb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"opencsg.com/csghub-server/builder/git"
"opencsg.com/csghub-server/builder/git/gitserver"
"opencsg.com/csghub-server/builder/git/gitserver/gitaly"
"opencsg.com/csghub-server/builder/store/database"
Expand All @@ -17,10 +18,13 @@ import (
)

type internalComponentImpl struct {
config *config.Config
sshKeyStore database.SSHKeyStore
repoStore database.RepoStore
*repoComponentImpl
config *config.Config
sshKeyStore database.SSHKeyStore
repoStore database.RepoStore
tokenStore database.AccessTokenStore
namespaceStore database.NamespaceStore
repoComponent RepoComponent
gitServer gitserver.GitServer
}

type InternalComponent interface {
Expand All @@ -37,11 +41,17 @@ func NewInternalComponent(config *config.Config) (InternalComponent, error) {
c.config = config
c.sshKeyStore = database.NewSSHKeyStore()
c.repoStore = database.NewRepoStore()
c.repoComponentImpl, err = NewRepoComponentImpl(config)
c.repoComponent, err = NewRepoComponentImpl(config)
c.tokenStore = database.NewAccessTokenStore()
c.namespaceStore = database.NewNamespaceStore()
if err != nil {
return nil, err
}
git, err := git.NewGitServer(config)
if err != nil {
return nil, fmt.Errorf("failed to create git server: %w", err)
}
c.gitServer = git
return c, nil
}

Expand Down Expand Up @@ -70,7 +80,7 @@ func (c *internalComponentImpl) SSHAllowed(ctx context.Context, req types.SSHAll
return nil, fmt.Errorf("failed to find ssh key by id, err: %v", err)
}
if req.Action == "git-receive-pack" {
allowed, err := c.AllowWriteAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
allowed, err := c.repoComponent.AllowWriteAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
if err != nil {
return nil, ErrUnauthorized
}
Expand All @@ -79,7 +89,7 @@ func (c *internalComponentImpl) SSHAllowed(ctx context.Context, req types.SSHAll
}
} else if req.Action == "git-upload-pack" {
if repo.Private {
allowed, err := c.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
allowed, err := c.repoComponent.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
if err != nil {
return nil, ErrUnauthorized
}
Expand Down Expand Up @@ -133,7 +143,7 @@ func (c *internalComponentImpl) GetCommitDiff(ctx context.Context, req types.Get
if repo == nil {
return nil, errors.New("repo not found")
}
diffs, err := c.git.GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{
diffs, err := c.gitServer.GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{
Namespace: req.Namespace,
Name: req.Name,
RepoType: req.RepoType,
Expand Down Expand Up @@ -165,7 +175,7 @@ func (c *internalComponentImpl) LfsAuthenticate(ctx context.Context, req types.L
return nil, fmt.Errorf("failed to find ssh key by id, err: %v", err)
}
if repo.Private {
allowed, err := c.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
allowed, err := c.repoComponent.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username)
if err != nil {
return nil, ErrUnauthorized
}
Expand Down
153 changes: 153 additions & 0 deletions component/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package component

import (
"context"
"testing"

"github.com/stretchr/testify/require"
pb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"opencsg.com/csghub-server/builder/git/gitserver"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/types"
)

func TestInternalComponent_Allowed(t *testing.T) {
ctx := context.TODO()
ic := initializeTestInternalComponent(ctx, t)

allowed, err := ic.Allowed(ctx)
require.Nil(t, err)
require.True(t, allowed)
}

func TestInternalComponent_SSHAllowed(t *testing.T) {
ctx := context.TODO()
ic := initializeTestInternalComponent(ctx, t)

ic.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "ns").Return(database.Namespace{
ID: 321,
}, nil)
ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return(
&database.Repository{ID: 123, Private: true}, nil,
)
ic.mocks.stores.SSHMock().EXPECT().FindByID(ctx, int64(1)).Return(&database.SSHKey{
ID: 111,
User: &database.User{ID: 11, Username: "user"},
}, nil)
ic.mocks.components.repo.EXPECT().AllowWriteAccess(
ctx, types.ModelRepo, "ns", "n", "user",
).Return(true, nil)
ic.mocks.components.repo.EXPECT().AllowReadAccess(
ctx, types.ModelRepo, "ns", "n", "user",
).Return(true, nil)

req := types.SSHAllowedReq{
RepoType: types.ModelRepo,
Namespace: "ns",
Name: "n",
KeyID: "1",
Action: "git-receive-pack",
}
resp, err := ic.SSHAllowed(ctx, req)
require.Nil(t, err)
expected := &types.SSHAllowedResp{
Success: true,
Message: "allowed",
Repo: req.Repo,
UserID: "11",
KeyType: "ssh",
KeyID: 111,
ProjectID: 123,
RootNamespaceID: 321,
GitConfigOptions: []string{"uploadpack.allowFilter=true", "uploadpack.allowAnySHA1InWant=true"},
Gitaly: types.Gitaly{
Repo: pb.Repository{
RelativePath: "models_ns/n.git",
GlRepository: "models/ns/n",
},
},
StatusCode: 200,
}

require.Equal(t, expected, resp)

req.Action = "git-upload-pack"
resp, err = ic.SSHAllowed(ctx, req)
require.Nil(t, err)
require.Equal(t, expected, resp)

}

func TestInternalComponent_GetAuthorizedKeys(t *testing.T) {
ctx := context.TODO()
ic := initializeTestInternalComponent(ctx, t)

ic.mocks.stores.SSHMock().EXPECT().FindByFingerpringSHA256(
ctx, "dUQ5GwtKsCPC8Scv1OLnOEvIW0QWULVSWyj5bZwQHwM",
).Return(&database.SSHKey{}, nil)
key, err := ic.GetAuthorizedKeys(ctx, "foobar")
require.Nil(t, err)
require.Equal(t, &database.SSHKey{}, key)
}

func TestInternalComponent_GetCommitDiff(t *testing.T) {
ctx := context.TODO()
ic := initializeTestInternalComponent(ctx, t)

req := types.GetDiffBetweenTwoCommitsReq{
Namespace: "ns",
Name: "n",
RepoType: types.ModelRepo,
Ref: "main",
LeftCommitId: "l",
RightCommitId: "r",
}
ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return(
&database.Repository{}, nil,
)
ic.mocks.gitServer.EXPECT().GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{
Namespace: req.Namespace,
Name: req.Name,
RepoType: req.RepoType,
Ref: req.Ref,
LeftCommitId: req.LeftCommitId,
RightCommitId: req.RightCommitId,
}).Return(&types.GiteaCallbackPushReq{Ref: "main"}, nil)

resp, err := ic.GetCommitDiff(ctx, req)
require.Nil(t, err)
require.Equal(t, &types.GiteaCallbackPushReq{Ref: "main"}, resp)
}

func TestInternalComponent_LfsAuthenticate(t *testing.T) {
ctx := context.TODO()
ic := initializeTestInternalComponent(ctx, t)

ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return(
&database.Repository{Private: true}, nil,
)
ic.mocks.stores.SSHMock().EXPECT().FindByID(ctx, int64(1)).Return(&database.SSHKey{
ID: 111,
User: &database.User{ID: 11, Username: "user"},
}, nil)
ic.mocks.components.repo.EXPECT().AllowReadAccess(
ctx, types.ModelRepo, "ns", "n", "user",
).Return(true, nil)
ic.mocks.stores.AccessTokenMock().EXPECT().GetUserGitToken(ctx, "user").Return(
&database.AccessToken{Token: "token"}, nil,
)

resp, err := ic.LfsAuthenticate(ctx, types.LfsAuthenticateReq{
Namespace: "ns",
Name: "n",
RepoType: types.ModelRepo,
KeyID: "1",
})
require.Nil(t, err)
require.Equal(t, &types.LfsAuthenticateResp{
Username: "user",
LfsToken: "token",
RepoPath: "/models/ns/n.git",
}, resp)

}
20 changes: 10 additions & 10 deletions component/mirror_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type mirrorSourceComponentImpl struct {
msStore database.MirrorSourceStore
userStore database.UserStore
mirrorSourceStore database.MirrorSourceStore
userStore database.UserStore
}

type MirrorSourceComponent interface {
Expand All @@ -25,8 +25,8 @@ type MirrorSourceComponent interface {

func NewMirrorSourceComponent(config *config.Config) (MirrorSourceComponent, error) {
return &mirrorSourceComponentImpl{
msStore: database.NewMirrorSourceStore(),
userStore: database.NewUserStore(),
mirrorSourceStore: database.NewMirrorSourceStore(),
userStore: database.NewUserStore(),
}, nil
}

Expand All @@ -41,7 +41,7 @@ func (c *mirrorSourceComponentImpl) Create(ctx context.Context, req types.Create
}
ms.SourceName = req.SourceName
ms.InfoAPIUrl = req.InfoAPiUrl
res, err := c.msStore.Create(ctx, &ms)
res, err := c.mirrorSourceStore.Create(ctx, &ms)
if err != nil {
return nil, fmt.Errorf("failed to create mirror source, error: %w", err)
}
Expand All @@ -56,7 +56,7 @@ func (c *mirrorSourceComponentImpl) Get(ctx context.Context, id int64, currentUs
if !user.CanAdmin() {
return nil, errors.New("user does not have admin permission")
}
ms, err := c.msStore.Get(ctx, id)
ms, err := c.mirrorSourceStore.Get(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to get mirror source, error: %w", err)
}
Expand All @@ -71,7 +71,7 @@ func (c *mirrorSourceComponentImpl) Index(ctx context.Context, currentUser strin
if !user.CanAdmin() {
return nil, errors.New("user does not have admin permission")
}
ms, err := c.msStore.Index(ctx)
ms, err := c.mirrorSourceStore.Index(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get mirror source, error: %w", err)
}
Expand All @@ -89,7 +89,7 @@ func (c *mirrorSourceComponentImpl) Update(ctx context.Context, req types.Update
ms.ID = req.ID
ms.SourceName = req.SourceName
ms.InfoAPIUrl = req.InfoAPiUrl
err = c.msStore.Update(ctx, &ms)
err = c.mirrorSourceStore.Update(ctx, &ms)
if err != nil {
return nil, fmt.Errorf("failed to update mirror source, error: %w", err)
}
Expand All @@ -104,11 +104,11 @@ func (c *mirrorSourceComponentImpl) Delete(ctx context.Context, id int64, curren
if !user.CanAdmin() {
return errors.New("user does not have admin permission")
}
ms, err := c.msStore.Get(ctx, id)
ms, err := c.mirrorSourceStore.Get(ctx, id)
if err != nil {
return fmt.Errorf("failed to find mirror source, error: %w", err)
}
err = c.msStore.Delete(ctx, ms)
err = c.mirrorSourceStore.Delete(ctx, ms)
if err != nil {
return fmt.Errorf("failed to delete mirror source, error: %w", err)
}
Expand Down
Loading

0 comments on commit f501546

Please sign in to comment.