Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/apiv1/user_test.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package apiv1
6
7
import (
8
"context"
9
"net/http"
10
"net/http/httptest"
11
"testing"
12
13
"github.com/bufbuild/connect-go"
14
"github.com/gitpod-io/gitpod/components/public-api/go/config"
15
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
16
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
17
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
18
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
19
"github.com/gitpod-io/gitpod/public-api-server/pkg/jws"
20
"github.com/gitpod-io/gitpod/public-api-server/pkg/jws/jwstest"
21
"github.com/golang/mock/gomock"
22
"github.com/google/uuid"
23
"github.com/stretchr/testify/require"
24
)
25
26
func TestUserService_GetAuthenticatedUser(t *testing.T) {
27
t.Run("proxies request to server", func(t *testing.T) {
28
serverMock, client := setupUserService(t)
29
30
user := newUser(&protocol.User{
31
Name: "John",
32
})
33
34
serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil)
35
36
retrieved, err := client.GetAuthenticatedUser(context.Background(), connect.NewRequest(&v1.GetAuthenticatedUserRequest{}))
37
require.NoError(t, err)
38
requireEqualProto(t, &v1.GetAuthenticatedUserResponse{
39
User: userToAPIResponse(user),
40
}, retrieved.Msg)
41
})
42
}
43
44
func TestUserService_ListSSHKeys(t *testing.T) {
45
t.Run("proxies request to server", func(t *testing.T) {
46
serverMock, client := setupUserService(t)
47
48
var keys []*protocol.UserSSHPublicKeyValue
49
keys = append(keys, newSSHKey(&protocol.UserSSHPublicKeyValue{
50
Name: "test key",
51
}))
52
53
serverMock.EXPECT().GetSSHPublicKeys(gomock.Any()).Return(keys, nil)
54
55
var expected []*v1.SSHKey
56
for _, k := range keys {
57
expected = append(expected, sshKeyToAPIResponse(k))
58
}
59
60
retrieved, err := client.ListSSHKeys(context.Background(), connect.NewRequest(&v1.ListSSHKeysRequest{}))
61
require.NoError(t, err)
62
requireEqualProto(t, &v1.ListSSHKeysResponse{
63
Keys: expected,
64
}, retrieved.Msg)
65
})
66
}
67
68
func TestUserService_GetGitToken(t *testing.T) {
69
t.Run("proxies request to server", func(t *testing.T) {
70
serverMock, client := setupUserService(t)
71
72
token := newGitToken(&protocol.Token{
73
Username: "John",
74
})
75
76
serverMock.EXPECT().GetToken(gomock.Any(), &protocol.GetTokenSearchOptions{Host: "github.com"}).Return(token, nil)
77
78
retrieved, err := client.GetGitToken(context.Background(), connect.NewRequest(&v1.GetGitTokenRequest{Host: "github.com"}))
79
require.NoError(t, err)
80
requireEqualProto(t, &v1.GetGitTokenResponse{
81
Token: gitTokenToAPIResponse(token),
82
}, retrieved.Msg)
83
})
84
}
85
86
func setupUserService(t *testing.T) (*protocol.MockAPIInterface, v1connect.UserServiceClient) {
87
t.Helper()
88
89
ctrl := gomock.NewController(t)
90
t.Cleanup(ctrl.Finish)
91
92
serverMock := protocol.NewMockAPIInterface(ctrl)
93
94
svc := NewUserService(&FakeServerConnPool{
95
api: serverMock,
96
})
97
98
keyset := jwstest.GenerateKeySet(t)
99
rsa256, err := jws.NewRSA256(keyset)
100
require.NoError(t, err)
101
102
_, handler := v1connect.NewUserServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor(config.SessionConfig{
103
Issuer: "unitetest.com",
104
Cookie: config.CookieConfig{
105
Name: "cookie_jwt",
106
},
107
}, rsa256)))
108
109
srv := httptest.NewServer(handler)
110
t.Cleanup(srv.Close)
111
112
client := v1connect.NewUserServiceClient(http.DefaultClient, srv.URL, connect.WithInterceptors(
113
auth.NewClientInterceptor("auth-token"),
114
))
115
116
return serverMock, client
117
}
118
119
func newUser(t *protocol.User) *protocol.User {
120
result := &protocol.User{
121
ID: uuid.New().String(),
122
Name: "John",
123
AvatarURL: "https://avatars.yolo/first.png",
124
CreationDate: "2022-10-10T10:10:10.000Z",
125
}
126
127
if t.ID != "" {
128
result.ID = t.ID
129
}
130
131
if t.Name != "" {
132
result.Name = t.Name
133
}
134
135
if t.CreationDate != "" {
136
result.CreationDate = t.CreationDate
137
}
138
139
return result
140
}
141
142
func newSSHKey(t *protocol.UserSSHPublicKeyValue) *protocol.UserSSHPublicKeyValue {
143
result := &protocol.UserSSHPublicKeyValue{
144
ID: uuid.New().String(),
145
Name: "John",
146
Key: "ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAACAQDCnrN9UdK1bNGPmZfenTW",
147
Fingerprint: "ykjP/b5aqoa3envmXzWpPMCGgEFMu3QvubfSTNrJCMA=",
148
CreationTime: "2022-10-10T10:10:10.000Z",
149
LastUsedTime: "2022-10-10T10:10:10.000Z",
150
}
151
152
if t.ID != "" {
153
result.ID = t.ID
154
}
155
156
if t.Name != "" {
157
result.Name = t.Name
158
}
159
160
if t.Key != "" {
161
result.Key = t.Key
162
}
163
164
if t.CreationTime != "" {
165
result.CreationTime = t.CreationTime
166
}
167
168
return result
169
}
170
171
func newGitToken(t *protocol.Token) *protocol.Token {
172
result := &protocol.Token{
173
ExpiryDate: "2022-10-10T10:10:10.000Z",
174
IDToken: uuid.New().String(),
175
RefreshToken: "",
176
Scopes: []string{"public_repo", "repo", "user:email"},
177
UpdateDate: "2022-10-10T10:10:10.000Z",
178
Username: "john",
179
Value: "gh_abcdefg123456789",
180
}
181
182
if t.IDToken != "" {
183
result.IDToken = t.IDToken
184
}
185
186
if t.Username != "" {
187
result.Username = t.Username
188
}
189
190
if len(t.Scopes) != 0 {
191
result.Scopes = t.Scopes
192
}
193
194
return result
195
}
196
197