Skip to content

Commit

Permalink
Implement test for GetOwnerToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Farries authored and roboquat committed May 17, 2022
1 parent dc80c45 commit a645a8c
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions components/public-api-server/pkg/apiv1/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,72 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) {
}

func TestWorkspaceService_GetOwnerToken(t *testing.T) {
const (
bearerToken = "bearer-token-for-tests"
foundWorkspaceID = "easycz-seer-xl8o1zacpyw"
ownerToken = "some-owner-token"
)

srv := baseserver.NewForTests(t,
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
)
var connPool *FakeServerConnPool

connPool := &FakeServerConnPool{
api: &FakeGitpodAPI{
ownertokens: map[string]string{foundWorkspaceID: ownerToken},
},
}
v1.RegisterWorkspacesServiceServer(srv.GRPC(), NewWorkspaceService(connPool))
baseserver.StartServerForTests(t, srv)

conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)

client := v1.NewWorkspacesServiceClient(conn)
ctx := context.Background()
ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", bearerToken)

actualOwnerId, err := client.GetOwnerToken(ctx, &v1.GetOwnerTokenRequest{WorkspaceId: "some-workspace-id"})
require.NoError(t, err)
type Expectation struct {
Code codes.Code
Response *v1.GetOwnerTokenResponse
}

scenarios := []struct {
name string
WorkspaceID string
Expect Expectation
}{
{
name: "returns an owner token when workspace is found by ID",
WorkspaceID: foundWorkspaceID,
Expect: Expectation{
Code: codes.OK,
Response: &v1.GetOwnerTokenResponse{
Token: ownerToken,
},
},
},
{
name: "not found when workspace is not found by ID",
WorkspaceID: "some-not-found-workspace-id",
Expect: Expectation{
Code: codes.NotFound,
},
},
}

require.Equal(t, "some-owner-token", actualOwnerId.Token)
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
resp, err := client.GetOwnerToken(ctx, &v1.GetOwnerTokenRequest{
WorkspaceId: scenario.WorkspaceID,
})
if diff := cmp.Diff(scenario.Expect, Expectation{
Code: status.Code(err),
Response: resp,
}, protocmp.Transform()); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
}
})
}
}

type FakeServerConnPool struct {
Expand All @@ -145,7 +193,8 @@ func (f *FakeServerConnPool) Get(ctx context.Context, token string) (gitpod.APII
}

type FakeGitpodAPI struct {
workspaces map[string]*gitpod.WorkspaceInfo
workspaces map[string]*gitpod.WorkspaceInfo
ownertokens map[string]string
}

func (f *FakeGitpodAPI) GetWorkspace(ctx context.Context, id string) (res *gitpod.WorkspaceInfo, err error) {
Expand All @@ -158,7 +207,11 @@ func (f *FakeGitpodAPI) GetWorkspace(ctx context.Context, id string) (res *gitpo
}

func (f *FakeGitpodAPI) GetOwnerToken(ctx context.Context, workspaceID string) (res string, err error) {
panic("implement me")
w, ok := f.ownertokens[workspaceID]
if !ok {
return "", errors.New("code 404")
}
return w, nil
}

func (f *FakeGitpodAPI) AdminBlockUser(ctx context.Context, req *gitpod.AdminBlockUserRequest) (err error) {
Expand Down

0 comments on commit a645a8c

Please sign in to comment.