1
1
use crate :: {
2
- add_team_to_crate, app ,
2
+ add_team_to_crate,
3
3
builders:: { CrateBuilder , PublishBuilder } ,
4
- new_team, new_user , req , sign_in_as ,
4
+ new_team,
5
5
util:: RequestHelper ,
6
6
TestApp ,
7
7
} ;
8
8
use cargo_registry:: {
9
- models:: { Crate , NewCrateOwnerInvitation } ,
10
- schema:: crate_owner_invitations,
11
- views:: {
12
- EncodableCrateOwnerInvitation , EncodableOwner , EncodablePublicUser , InvitationResponse ,
13
- } ,
9
+ models:: Crate ,
10
+ views:: { EncodableCrateOwnerInvitation , EncodableOwner , InvitationResponse } ,
14
11
} ;
15
12
16
- use conduit:: { Handler , Method } ;
17
13
use diesel:: prelude:: * ;
18
14
19
15
#[ derive( Deserialize ) ]
@@ -56,6 +52,31 @@ impl crate::util::MockCookieUser {
56
52
assert_eq ! ( crate_owner_invite. crate_owner_invitation. crate_id, krate_id) ;
57
53
}
58
54
55
+ /// As the currently logged in user, decline an invitation to become an owner of the named
56
+ /// crate.
57
+ fn decline_ownership_invitation ( & self , krate_name : & str , krate_id : i32 ) {
58
+ let body = json ! ( {
59
+ "crate_owner_invite" : {
60
+ "invited_by_username" : "" ,
61
+ "crate_name" : krate_name,
62
+ "crate_id" : krate_id,
63
+ "created_at" : "" ,
64
+ "accepted" : false
65
+ }
66
+ } ) ;
67
+
68
+ #[ derive( Deserialize ) ]
69
+ struct CrateOwnerInvitation {
70
+ crate_owner_invitation : InvitationResponse ,
71
+ }
72
+
73
+ let url = format ! ( "/api/v1/me/crate_owner_invitations/{}" , krate_id) ;
74
+ let crate_owner_invite: CrateOwnerInvitation =
75
+ self . put ( & url, body. to_string ( ) . as_bytes ( ) ) . good ( ) ;
76
+ assert ! ( !crate_owner_invite. crate_owner_invitation. accepted) ;
77
+ assert_eq ! ( crate_owner_invite. crate_owner_invitation. crate_id, krate_id) ;
78
+ }
79
+
59
80
/// As the currently logged in user, list my pending invitations.
60
81
fn list_invitations ( & self ) -> InvitationListResponse {
61
82
self . get ( "/api/v1/me/crate_owner_invitations" ) . good ( )
@@ -228,76 +249,23 @@ fn invitations_list() {
228
249
*/
229
250
#[ test]
230
251
fn test_accept_invitation ( ) {
231
- # [ derive ( Deserialize ) ]
232
- struct Q {
233
- users : Vec < EncodablePublicUser > ,
234
- }
252
+ let ( app , anon , owner , owner_token ) = TestApp :: init ( ) . with_token ( ) ;
253
+ let owner = owner . as_model ( ) ;
254
+ let invited_user = app . db_new_user ( "user_bar" ) ;
255
+ let krate = app . db ( |conn| CrateBuilder :: new ( "accept_invitation" , owner . id ) . expect_build ( conn ) ) ;
235
256
236
- #[ derive( Deserialize ) ]
237
- struct T {
238
- crate_owner_invitation : InvitationResponse ,
239
- }
257
+ // Invite a new owner
258
+ owner_token. add_user_owner ( "accept_invitation" , invited_user. as_model ( ) ) ;
240
259
241
- let ( app, middle) = app ( ) ;
242
- let mut req = req ( Method :: Get , "/api/v1/me/crate_owner_invitations" ) ;
243
- let ( krate, user) = {
244
- let conn = app. diesel_database . get ( ) . unwrap ( ) ;
245
- let owner = new_user ( "inviting_user" ) . create_or_update ( & conn) . unwrap ( ) ;
246
- let user = new_user ( "invited_user" ) . create_or_update ( & conn) . unwrap ( ) ;
247
- let krate = CrateBuilder :: new ( "invited_crate" , owner. id ) . expect_build ( & conn) ;
248
-
249
- // This should be replaced by an actual call to the route that `owner --add` hits once
250
- // that route creates an invitation.
251
- diesel:: insert_into ( crate_owner_invitations:: table)
252
- . values ( & NewCrateOwnerInvitation {
253
- invited_by_user_id : owner. id ,
254
- invited_user_id : user. id ,
255
- crate_id : krate. id ,
256
- } )
257
- . execute ( & * conn)
258
- . unwrap ( ) ;
259
- ( krate, user)
260
- } ;
261
- sign_in_as ( & mut req, & user) ;
262
-
263
- let body = json ! ( {
264
- "crate_owner_invite" : {
265
- "invited_by_username" : "inviting_user" ,
266
- "crate_name" : "invited_crate" ,
267
- "crate_id" : krate. id,
268
- "created_at" : "" ,
269
- "accepted" : true
270
- }
271
- } ) ;
260
+ // New owner accepts the invitation
261
+ invited_user. accept_ownership_invitation ( & krate. name , krate. id ) ;
272
262
273
- // first check that response from inserting new crate owner
274
- // and deleting crate_owner_invitation is okay
275
- let mut response = ok_resp ! ( middle. call(
276
- req. with_path( & format!( "api/v1/me/crate_owner_invitations/{}" , krate. id) )
277
- . with_method( Method :: Put )
278
- . with_body( body. to_string( ) . as_bytes( ) ) ,
279
- ) ) ;
280
-
281
- let json: T = crate :: json ( & mut response) ;
282
- assert_eq ! ( json. crate_owner_invitation. accepted, true ) ;
283
- assert_eq ! ( json. crate_owner_invitation. crate_id, krate. id) ;
284
-
285
- // then check to make sure that accept_invite did what it
286
- // was supposed to
287
- // crate_owner_invitation was deleted
288
- let mut response = ok_resp ! ( middle. call(
289
- req. with_path( "api/v1/me/crate_owner_invitations" )
290
- . with_method( Method :: Get )
291
- ) ) ;
292
- let json: InvitationListResponse = crate :: json ( & mut response) ;
263
+ // New owner's invitation list should now be empty
264
+ let json = invited_user. list_invitations ( ) ;
293
265
assert_eq ! ( json. crate_owner_invitations. len( ) , 0 ) ;
294
266
295
- // new crate owner was inserted
296
- let mut response = ok_resp ! ( middle. call(
297
- req. with_path( "/api/v1/crates/invited_crate/owners" )
298
- . with_method( Method :: Get )
299
- ) ) ;
300
- let json: Q = crate :: json ( & mut response) ;
267
+ // New owner is now listed as an owner, so the crate has two owners
268
+ let json = anon. show_crate_owners ( "accept_invitation" ) ;
301
269
assert_eq ! ( json. users. len( ) , 2 ) ;
302
270
}
303
271
@@ -308,75 +276,22 @@ fn test_accept_invitation() {
308
276
*/
309
277
#[ test]
310
278
fn test_decline_invitation ( ) {
311
- # [ derive ( Deserialize ) ]
312
- struct Q {
313
- users : Vec < EncodablePublicUser > ,
314
- }
279
+ let ( app , anon , owner , owner_token ) = TestApp :: init ( ) . with_token ( ) ;
280
+ let owner = owner . as_model ( ) ;
281
+ let invited_user = app . db_new_user ( "user_bar" ) ;
282
+ let krate = app . db ( |conn| CrateBuilder :: new ( "decline_invitation" , owner . id ) . expect_build ( conn ) ) ;
315
283
316
- #[ derive( Deserialize ) ]
317
- struct T {
318
- crate_owner_invitation : InvitationResponse ,
319
- }
284
+ // Invite a new owner
285
+ owner_token. add_user_owner ( "decline_invitation" , invited_user. as_model ( ) ) ;
320
286
321
- let ( app, middle) = app ( ) ;
322
- let mut req = req ( Method :: Get , "/api/v1/me/crate_owner_invitations" ) ;
323
- let ( krate, user) = {
324
- let conn = app. diesel_database . get ( ) . unwrap ( ) ;
325
- let owner = new_user ( "inviting_user" ) . create_or_update ( & conn) . unwrap ( ) ;
326
- let user = new_user ( "invited_user" ) . create_or_update ( & conn) . unwrap ( ) ;
327
- let krate = CrateBuilder :: new ( "invited_crate" , owner. id ) . expect_build ( & conn) ;
328
-
329
- // This should be replaced by an actual call to the route that `owner --add` hits once
330
- // that route creates an invitation.
331
- diesel:: insert_into ( crate_owner_invitations:: table)
332
- . values ( & NewCrateOwnerInvitation {
333
- invited_by_user_id : owner. id ,
334
- invited_user_id : user. id ,
335
- crate_id : krate. id ,
336
- } )
337
- . execute ( & * conn)
338
- . unwrap ( ) ;
339
- ( krate, user)
340
- } ;
341
- sign_in_as ( & mut req, & user) ;
342
-
343
- let body = json ! ( {
344
- "crate_owner_invite" : {
345
- "invited_by_username" : "inviting_user" ,
346
- "crate_name" : "invited_crate" ,
347
- "crate_id" : krate. id,
348
- "created_at" : "" ,
349
- "accepted" : false
350
- }
351
- } ) ;
287
+ // Invited user declines the invitation
288
+ invited_user. decline_ownership_invitation ( & krate. name , krate. id ) ;
352
289
353
- // first check that response from deleting
354
- // crate_owner_invitation is okay
355
- let mut response = ok_resp ! ( middle. call(
356
- req. with_path( & format!( "api/v1/me/crate_owner_invitations/{}" , krate. id) )
357
- . with_method( Method :: Put )
358
- . with_body( body. to_string( ) . as_bytes( ) ) ,
359
- ) ) ;
360
-
361
- let json: T = crate :: json ( & mut response) ;
362
- assert_eq ! ( json. crate_owner_invitation. accepted, false ) ;
363
- assert_eq ! ( json. crate_owner_invitation. crate_id, krate. id) ;
364
-
365
- // then check to make sure that decline_invite did what it
366
- // was supposed to
367
- // crate_owner_invitation was deleted
368
- let mut response = ok_resp ! ( middle. call(
369
- req. with_path( "api/v1/me/crate_owner_invitations" )
370
- . with_method( Method :: Get )
371
- ) ) ;
372
- let json: InvitationListResponse = crate :: json ( & mut response) ;
290
+ // Invited user's invitation list should now be empty
291
+ let json = invited_user. list_invitations ( ) ;
373
292
assert_eq ! ( json. crate_owner_invitations. len( ) , 0 ) ;
374
293
375
- // new crate owner was not inserted
376
- let mut response = ok_resp ! ( middle. call(
377
- req. with_path( "/api/v1/crates/invited_crate/owners" )
378
- . with_method( Method :: Get )
379
- ) ) ;
380
- let json: Q = crate :: json ( & mut response) ;
294
+ // Invited user is NOT listed as an owner, so the crate still only has one owner
295
+ let json = anon. show_crate_owners ( "decline_invitation" ) ;
381
296
assert_eq ! ( json. users. len( ) , 1 ) ;
382
297
}
0 commit comments