Path: blob/main/components/public-api-server/pkg/apiv1/user_test.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package apiv156import (7"context"8"net/http"9"net/http/httptest"10"testing"1112"github.com/bufbuild/connect-go"13"github.com/gitpod-io/gitpod/components/public-api/go/config"14v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"15"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"16protocol "github.com/gitpod-io/gitpod/gitpod-protocol"17"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"18"github.com/gitpod-io/gitpod/public-api-server/pkg/jws"19"github.com/gitpod-io/gitpod/public-api-server/pkg/jws/jwstest"20"github.com/golang/mock/gomock"21"github.com/google/uuid"22"github.com/stretchr/testify/require"23)2425func TestUserService_GetAuthenticatedUser(t *testing.T) {26t.Run("proxies request to server", func(t *testing.T) {27serverMock, client := setupUserService(t)2829user := newUser(&protocol.User{30Name: "John",31})3233serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil)3435retrieved, err := client.GetAuthenticatedUser(context.Background(), connect.NewRequest(&v1.GetAuthenticatedUserRequest{}))36require.NoError(t, err)37requireEqualProto(t, &v1.GetAuthenticatedUserResponse{38User: userToAPIResponse(user),39}, retrieved.Msg)40})41}4243func TestUserService_ListSSHKeys(t *testing.T) {44t.Run("proxies request to server", func(t *testing.T) {45serverMock, client := setupUserService(t)4647var keys []*protocol.UserSSHPublicKeyValue48keys = append(keys, newSSHKey(&protocol.UserSSHPublicKeyValue{49Name: "test key",50}))5152serverMock.EXPECT().GetSSHPublicKeys(gomock.Any()).Return(keys, nil)5354var expected []*v1.SSHKey55for _, k := range keys {56expected = append(expected, sshKeyToAPIResponse(k))57}5859retrieved, err := client.ListSSHKeys(context.Background(), connect.NewRequest(&v1.ListSSHKeysRequest{}))60require.NoError(t, err)61requireEqualProto(t, &v1.ListSSHKeysResponse{62Keys: expected,63}, retrieved.Msg)64})65}6667func TestUserService_GetGitToken(t *testing.T) {68t.Run("proxies request to server", func(t *testing.T) {69serverMock, client := setupUserService(t)7071token := newGitToken(&protocol.Token{72Username: "John",73})7475serverMock.EXPECT().GetToken(gomock.Any(), &protocol.GetTokenSearchOptions{Host: "github.com"}).Return(token, nil)7677retrieved, err := client.GetGitToken(context.Background(), connect.NewRequest(&v1.GetGitTokenRequest{Host: "github.com"}))78require.NoError(t, err)79requireEqualProto(t, &v1.GetGitTokenResponse{80Token: gitTokenToAPIResponse(token),81}, retrieved.Msg)82})83}8485func setupUserService(t *testing.T) (*protocol.MockAPIInterface, v1connect.UserServiceClient) {86t.Helper()8788ctrl := gomock.NewController(t)89t.Cleanup(ctrl.Finish)9091serverMock := protocol.NewMockAPIInterface(ctrl)9293svc := NewUserService(&FakeServerConnPool{94api: serverMock,95})9697keyset := jwstest.GenerateKeySet(t)98rsa256, err := jws.NewRSA256(keyset)99require.NoError(t, err)100101_, handler := v1connect.NewUserServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor(config.SessionConfig{102Issuer: "unitetest.com",103Cookie: config.CookieConfig{104Name: "cookie_jwt",105},106}, rsa256)))107108srv := httptest.NewServer(handler)109t.Cleanup(srv.Close)110111client := v1connect.NewUserServiceClient(http.DefaultClient, srv.URL, connect.WithInterceptors(112auth.NewClientInterceptor("auth-token"),113))114115return serverMock, client116}117118func newUser(t *protocol.User) *protocol.User {119result := &protocol.User{120ID: uuid.New().String(),121Name: "John",122AvatarURL: "https://avatars.yolo/first.png",123CreationDate: "2022-10-10T10:10:10.000Z",124}125126if t.ID != "" {127result.ID = t.ID128}129130if t.Name != "" {131result.Name = t.Name132}133134if t.CreationDate != "" {135result.CreationDate = t.CreationDate136}137138return result139}140141func newSSHKey(t *protocol.UserSSHPublicKeyValue) *protocol.UserSSHPublicKeyValue {142result := &protocol.UserSSHPublicKeyValue{143ID: uuid.New().String(),144Name: "John",145Key: "ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAACAQDCnrN9UdK1bNGPmZfenTW",146Fingerprint: "ykjP/b5aqoa3envmXzWpPMCGgEFMu3QvubfSTNrJCMA=",147CreationTime: "2022-10-10T10:10:10.000Z",148LastUsedTime: "2022-10-10T10:10:10.000Z",149}150151if t.ID != "" {152result.ID = t.ID153}154155if t.Name != "" {156result.Name = t.Name157}158159if t.Key != "" {160result.Key = t.Key161}162163if t.CreationTime != "" {164result.CreationTime = t.CreationTime165}166167return result168}169170func newGitToken(t *protocol.Token) *protocol.Token {171result := &protocol.Token{172ExpiryDate: "2022-10-10T10:10:10.000Z",173IDToken: uuid.New().String(),174RefreshToken: "",175Scopes: []string{"public_repo", "repo", "user:email"},176UpdateDate: "2022-10-10T10:10:10.000Z",177Username: "john",178Value: "gh_abcdefg123456789",179}180181if t.IDToken != "" {182result.IDToken = t.IDToken183}184185if t.Username != "" {186result.Username = t.Username187}188189if len(t.Scopes) != 0 {190result.Scopes = t.Scopes191}192193return result194}195196197