5
5
*/
6
6
7
7
import { TypeORM } from "@gitpod/gitpod-db/lib" ;
8
- import { Organization , User } from "@gitpod/gitpod-protocol" ;
8
+ import { AuthProviderInfo , Organization , User } from "@gitpod/gitpod-protocol" ;
9
9
import { Experiments } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server" ;
10
10
import * as chai from "chai" ;
11
11
import { Container } from "inversify" ;
@@ -25,6 +25,7 @@ const expect = chai.expect;
25
25
26
26
describe ( "AuthProviderService" , async ( ) => {
27
27
let service : AuthProviderService ;
28
+ let userService : UserService ;
28
29
let container : Container ;
29
30
let owner : User ;
30
31
let org : Organization ;
@@ -94,13 +95,24 @@ describe("AuthProviderService", async () => {
94
95
oauth : { ...expectedOrgEntry ( ) . oauth , clientSecret : "secret-123" } ,
95
96
} ;
96
97
98
+ const addBuiltInProvider = ( host : string = "github.com" ) => {
99
+ const config = container . get < Config > ( Config ) ;
100
+ config . builtinAuthProvidersConfigured = true ;
101
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
102
+ config . authProviderConfigs . push ( ( < Partial < AuthProviderParams > > {
103
+ host,
104
+ id : "Public-GitHub" ,
105
+ verified : true ,
106
+ } ) as any ) ;
107
+ } ;
108
+
97
109
beforeEach ( async ( ) => {
98
110
container = createTestContainer ( ) ;
99
111
Experiments . configureTestingClient ( {
100
112
centralizedPermissions : true ,
101
113
} ) ;
102
114
service = container . get ( AuthProviderService ) ;
103
- const userService = container . get < UserService > ( UserService ) ;
115
+ userService = container . get < UserService > ( UserService ) ;
104
116
owner = await userService . createUser ( {
105
117
identity : {
106
118
authId : "gh-user-1" ,
@@ -130,12 +142,7 @@ describe("AuthProviderService", async () => {
130
142
} ) ;
131
143
132
144
it ( "should fail in case of conflict with built-in provider" , async ( ) => {
133
- const config = container . get < Config > ( Config ) ;
134
- config . builtinAuthProvidersConfigured = true ;
135
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
136
- config . authProviderConfigs . push ( {
137
- host : "github.com" ,
138
- } as any ) ;
145
+ addBuiltInProvider ( ) ;
139
146
140
147
const providersAtStart = await service . getAllAuthProviderParams ( ) ;
141
148
expect ( providersAtStart ) . to . be . empty ;
@@ -198,8 +205,68 @@ describe("AuthProviderService", async () => {
198
205
const created = await service . createOrgAuthProvider ( owner . id , newOrgEntry ( ) ) ;
199
206
200
207
const retrieved = await service . getAuthProvider ( owner . id , created . id ) ;
201
- console . log ( JSON . stringify ( retrieved ) ) ;
202
208
expect ( retrieved ) . to . deep . include ( expectedOrgEntry ( ) ) ;
203
209
} ) ;
210
+ it ( "should find user-level provider" , async ( ) => {
211
+ const providersAtStart = await service . getAllAuthProviderParams ( ) ;
212
+ expect ( providersAtStart ) . to . be . empty ;
213
+
214
+ const created = await service . createAuthProviderOfUser ( owner . id , newEntry ( ) ) ;
215
+
216
+ const retrieved = await service . getAuthProvider ( owner . id , created . id ) ;
217
+ expect ( retrieved ) . to . deep . include ( expectedEntry ( ) ) ;
218
+ } ) ;
219
+ it ( "should not find org-level provider for non-members" , async ( ) => {
220
+ const providersAtStart = await service . getAllAuthProviderParams ( ) ;
221
+ expect ( providersAtStart ) . to . be . empty ;
222
+
223
+ const created = await service . createOrgAuthProvider ( owner . id , newOrgEntry ( ) ) ;
224
+
225
+ const nonMember = await userService . createUser ( {
226
+ identity : {
227
+ authId : "gh-user-2" ,
228
+ authName : "user2" ,
229
+ authProviderId : "public-github" ,
230
+ } ,
231
+ } ) ;
232
+
233
+ // expecting 404, as Orgs shall not be enumerable to non-members
234
+ await expectError ( ErrorCodes . NOT_FOUND , service . getAuthProvider ( nonMember . id , created . id ) ) ;
235
+ } ) ;
236
+ } ) ;
237
+
238
+ describe . only ( "getAuthProviderDescriptionsUnauthenticated" , async ( ) => {
239
+ it ( "should find built-in provider" , async ( ) => {
240
+ addBuiltInProvider ( ) ;
241
+
242
+ const providers = await service . getAuthProviderDescriptionsUnauthenticated ( ) ;
243
+ expect ( providers ) . to . has . lengthOf ( 1 ) ;
244
+ expect ( providers [ 0 ] . authProviderId ) . to . be . equal ( "Public-GitHub" ) ;
245
+ } ) ;
246
+ it ( "should find only built-in providers but no user-level providers" , async ( ) => {
247
+ addBuiltInProvider ( "localhost" ) ;
248
+
249
+ const created = await service . createAuthProviderOfUser ( owner . id , newEntry ( ) ) ;
250
+ await service . markAsVerified ( { userId : owner . id , id : created . id } ) ;
251
+
252
+ const providers = await service . getAuthProviderDescriptionsUnauthenticated ( ) ;
253
+ expect ( providers ) . to . has . lengthOf ( 1 ) ;
254
+ expect ( providers [ 0 ] . host ) . to . be . equal ( "localhost" ) ;
255
+ } ) ;
256
+ it . only ( "should find user-level providers if no built-in providers present" , async ( ) => {
257
+ const created = await service . createAuthProviderOfUser ( owner . id , newEntry ( ) ) ;
258
+ await service . markAsVerified ( { userId : owner . id , id : created . id } ) ;
259
+
260
+ const providers = await service . getAuthProviderDescriptionsUnauthenticated ( ) ;
261
+ expect ( providers ) . to . has . lengthOf ( 1 ) ;
262
+ expect ( providers [ 0 ] ) . to . deep . include ( < Partial < AuthProviderInfo > > {
263
+ authProviderId : created . id ,
264
+ authProviderType : created . type ,
265
+ host : created . host ,
266
+ } ) ;
267
+
268
+ const oauthProperty : keyof AuthProviderEntry = "oauth" ;
269
+ expect ( providers [ 0 ] ) . to . not . haveOwnProperty ( oauthProperty ) ;
270
+ } ) ;
204
271
} ) ;
205
272
} ) ;
0 commit comments