-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path33room-members.pl
706 lines (533 loc) · 18.8 KB
/
33room-members.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use Future 0.33; # then catch semantics
use Future::Utils qw( fmap );
use List::UtilsBy qw( partition_by );
my $creator_fixture = local_user_fixture();
# This provides $room_id *AND* $room_alias
my $room_fixture = fixture(
requires => [ $creator_fixture, room_alias_name_fixture() ],
setup => sub {
my ( $user, $room_alias_name ) = @_;
matrix_create_room( $user,
room_alias_name => $room_alias_name,
);
},
);
test "POST /rooms/:room_id/join can join a room",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
do => sub {
my ( $user, $room_id, undef ) = @_;
do_request_json_for( $user,
method => "POST",
uri => "/r0/rooms/$room_id/join",
content => {},
)->then( sub {
my ( $body ) = @_;
$body->{room_id} eq $room_id or
die "Expected 'room_id' to be $room_id";
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $user, $room_id,
type => "m.room.member",
state_key => $user->user_id,
)->then( sub {
my ( $body ) = @_;
$body->{membership} eq "join" or
die "Expected membership to be 'join'";
Future->done(1);
})
}
});
};
push our @EXPORT, qw( matrix_join_room );
sub matrix_join_room
{
my ( $user, $room, %opts ) = @_;
is_User( $user ) or croak "Expected a User; got $user";
my %content;
my %params;
defined $opts{server_name} and $params{server_name} = $opts{server_name};
defined $opts{third_party_signed} and $content{third_party_signed} = $opts{third_party_signed};
do_request_json_for( $user,
method => "POST",
uri => "/r0/join/$room",
params => \%params,
content => \%content,
)->then( sub {
my ( $body ) = @_;
my $user_id = $user->user_id;
log_if_fail "User $user_id joined room", $body;
Future->done( $body->{room_id} )
});
}
test "POST /join/:room_alias can join a room",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
proves => [qw( can_join_room_by_alias )],
do => sub {
my ( $user, $room_id, $room_alias ) = @_;
do_request_json_for( $user,
method => "POST",
uri => "/r0/join/$room_alias",
content => {},
)->then( sub {
my ( $body ) = @_;
$body->{room_id} eq $room_id or
die "Expected 'room_id' to be $room_id";
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $user, $room_id,
type => "m.room.member",
state_key => $user->user_id,
)->then( sub {
my ( $body ) = @_;
$body->{membership} eq "join" or
die "Expected membership to be 'join'";
Future->done(1);
})
}
});
};
test "POST /join/:room_id can join a room",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
do => sub {
my ( $user, $room_id, undef ) = @_;
do_request_json_for( $user,
method => "POST",
uri => "/r0/join/$room_id",
content => {},
)->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( room_id ));
$body->{room_id} eq $room_id or
die "Expected 'room_id' to be $room_id";
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $user, $room_id,
type => "m.room.member",
state_key => $user->user_id,
)->then( sub {
my ( $body ) = @_;
$body->{membership} eq "join" or
die "Expected membership to be 'join'";
Future->done(1);
})
}
});
};
test "POST /join/:room_id can join a room with custom content",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
do => sub {
my ( $user, $room_id, undef ) = @_;
do_request_json_for( $user,
method => "POST",
uri => "/r0/join/$room_id",
content => { "foo" => "bar" },
)->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( room_id ) );
assert_eq( $body->{room_id}, $room_id );
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $user, $room_id,
type => "m.room.member",
state_key => $user->user_id,
)->then( sub {
my ( $body ) = @_;
log_if_fail "body", $body;
assert_json_keys( $body, qw( foo membership ) );
assert_eq( $body->{foo}, "bar" );
assert_eq( $body->{membership}, "join" );
Future->done(1);
})
}
});
};
test "POST /join/:room_alias can join a room with custom content",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
do => sub {
my ( $user, $room_id, $room_alias ) = @_;
do_request_json_for( $user,
method => "POST",
uri => "/r0/join/$room_alias",
content => { "foo" => "bar" },
)->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( room_id ) );
assert_eq( $body->{room_id}, $room_id );
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $user, $room_id,
type => "m.room.member",
state_key => $user->user_id,
)->then( sub {
my ( $body ) = @_;
log_if_fail "body", $body;
assert_json_keys( $body, qw( foo membership ) );
assert_eq( $body->{foo}, "bar" );
assert_eq( $body->{membership}, "join" );
Future->done(1);
})
}
});
};
test "POST /rooms/:room_id/leave can leave a room",
requires => [ local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
do => sub {
my ( $joiner_to_leave, $room_id, undef ) = @_;
matrix_join_room( $joiner_to_leave, $room_id )
->then( sub {
do_request_json_for( $joiner_to_leave,
method => "POST",
uri => "/r0/rooms/$room_id/leave",
content => {},
)
})->then( sub {
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $joiner_to_leave, $room_id,
type => "m.room.member",
state_key => $joiner_to_leave->user_id,
)->then( sub { # then
my ( $body ) = @_;
$body->{membership} eq "join" and
die "Expected membership not to be 'join'";
Future->done(1);
},
http => sub { # catch
my ( $failure, undef, $response ) = @_;
Future->fail( @_ ) unless $response->code == 403;
# We're expecting a 403 so that's fine
Future->done(1);
})
}
});
};
push @EXPORT, qw( matrix_leave_room );
sub matrix_leave_room
{
my ( $user, $room_id ) = @_;
is_User( $user ) or croak "Expected a User; got $user";
do_request_json_for( $user,
method => "POST",
uri => "/r0/rooms/$room_id/leave",
content => {},
)->then_done(1);
}
test "POST /rooms/:room_id/invite can send an invite",
requires => [ $creator_fixture, local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
proves => [qw( can_invite_room )],
do => sub {
my ( $creator, $invited_user, $room_id, undef ) = @_;
do_request_json_for( $creator,
method => "POST",
uri => "/r0/rooms/$room_id/invite",
content => { user_id => $invited_user->user_id },
)->then( sub {
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $creator, $room_id,
type => "m.room.member",
state_key => $invited_user->user_id,
)->then( sub {
my ( $body ) = @_;
$body->{membership} eq "invite" or
die "Expected membership to be 'invite'";
Future->done(1);
})
}
});
};
push @EXPORT, qw( matrix_invite_user_to_room );
sub matrix_invite_user_to_room
{
my ( $user, $invitee, $room_id ) = @_;
is_User( $user ) or croak "Expected a User; got $user";
( defined $room_id and !ref $room_id ) or croak "Expected a room ID; got $room_id";
my $invitee_id;
if( is_User( $invitee ) ) {
$invitee_id = $invitee->user_id;
}
elsif( defined $invitee and !ref $invitee ) {
$invitee_id = $invitee;
}
else {
croak "Expected invitee to be a User struct or plain string; got $invitee";
}
do_request_json_for( $user,
method => "POST",
uri => "/r0/rooms/$room_id/invite",
content => { user_id => $invitee_id }
)->then( sub {
my ( $body ) = @_;
log_if_fail "Invited user $invitee_id to $room_id", $body;
Future->done(1);
});
}
test "POST /rooms/:room_id/ban can ban a user",
requires => [ $creator_fixture, local_user_fixture(), $room_fixture,
qw( can_get_room_membership )],
proves => [qw( can_ban_room )],
do => sub {
my ( $creator, $banned_user, $room_id, undef ) = @_;
do_request_json_for( $creator,
method => "POST",
uri => "/r0/rooms/$room_id/ban",
content => {
user_id => $banned_user->user_id,
reason => "Just testing",
},
)->then( sub {
# Retry getting the state a few times, as it may take some time to
# propagate in a multi-process homeserver
retry_until_success {
matrix_get_room_state( $creator, $room_id,
type => "m.room.member",
state_key => $banned_user->user_id,
)->then( sub {
my ( $body ) = @_;
$body->{membership} eq "ban" or
die "Expecting membership to be 'ban'";
Future->done(1);
})
}
});
};
my $next_alias = 1;
sub _invite_users
{
my ( $creator, $room_id, @other_members ) = @_;
Future->needs_all(
( map {
my $user = $_;
matrix_invite_user_to_room( $creator, $user, $room_id );
} @other_members)
);
}
=head2 matrix_create_and_join_room
matrix_create_and_join_room( [ $creator, $user2, ... ], %opts )->then( sub {
my ( $room_id ) = @_;
});
matrix_create_and_join_room( [ $creator, $user2, ... ],
with_alias => 1, %opts,
)->then( sub {
my ( $room_id, $room_alias ) = @_;
});
Create a new room, and have a list of users join it.
The following may be passed as optional parametrs:
=over
=item with_alias => SCALAR
Make this truthy to return the newly created alias
=item with_invite => SCALAR
Make this truthy to send invites to the other users before they join.
=item (everything else)
Other parameters are passed into C<matrix_create_room>, whence they are
passed on to the server.
=back
=cut
push @EXPORT, qw( matrix_create_and_join_room );
sub matrix_create_and_join_room
{
my ( $members, %options ) = @_;
my ( $creator, @other_members ) = @$members;
is_User( $creator ) or croak "Expected a User for creator; got $creator";
is_User( $_ ) or croak "Expected a User for a member; got $_"
for @other_members;
my $room_id;
my $n_joiners = scalar @other_members;
my $creator_server_name = $creator->http->server_name;
my $room_alias_name = sprintf "test-%s-%d", $TEST_RUN_ID, $next_alias++;
my $room_alias_fullname =
sprintf "#%s:%s", $room_alias_name, $creator_server_name;
my $with_invite = delete $options{with_invite};
my $with_alias = delete $options{with_alias};
matrix_create_room( $creator,
%options,
room_alias_name => $room_alias_name,
)->then( sub {
( $room_id ) = @_;
log_if_fail "room_id=$room_id";
( $with_invite ?
_invite_users( $creator, $room_id, @other_members ) :
Future->done() )
})->then( sub {
# Best not to join remote users concurrently because of
# https://matrix.org/jira/browse/SYN-318
my %members_by_server = partition_by { $_->http } @other_members;
my @local_members = @{ delete $members_by_server{ $creator->http } // [] };
my @remote_members = map { @$_ } values %members_by_server;
Future->needs_all(
( fmap {
my $user = shift;
matrix_join_room_synced( $user, $room_alias_fullname )
} foreach => \@remote_members ),
map {
my $user = $_;
matrix_join_room_synced( $user, $room_alias_fullname )
} @local_members,
)
})->then( sub {
log_if_fail "Created room_id=$room_id";
Future->done( $room_id,
( $with_alias ? ( $room_alias_fullname ) : () )
);
});
}
=head2 room_fixture
$fixture = room_fixture( $user_fixture, %opts );
Returns a Fixture, which when provisioned will create a new room on the user's
server and return the room id.
C<$user_fixture> should be a Fixture which will provide a User when
provisioned.
C<opts> can contain a boolean key called C<synced>, which determines whether to
perform a user /sync request after creating the room. This involves inserting
and checking for a 'm.room.test' event in the room state, which may be
undesirable in some cases. Defaults to 1.
Any other options are passed into C<matrix_create_room>, whence they are passed
on to the server.
It is generally easier to use C<local_user_and_room_fixtures>.
=cut
push @EXPORT, qw( room_fixture );
sub room_fixture
{
my ( $user_fixture, %args ) = @_;
fixture(
requires => [ $user_fixture ],
setup => sub {
my ( $user ) = @_;
# Determine whether to create room synced or not. Default is synced
#
# This will insert a m.room.test event into room state which
# some tests may not want
my $sync = 1;
if ( exists( $args{synced} ) && $args{synced} == 0 ) {
$sync = 0;
}
( $sync ? matrix_create_room_synced( $user, %args ) : matrix_create_room( $user, %args ) )->then( sub {
my ( $room_id ) = @_;
# matrix_create_room returns the room_id and the room_alias if
# one was set. However we only want to return the room_id
# because our callers only expect the room_id to be passed to
# their setup code.
Future->done( $room_id );
});
}
);
}
=head2 magic_room_fixture
$fixture = magic_room_fixture(
requires_users => [ $creator_fixture, $other_user_fixture ],
%opts
);
Returns a Fixture, which when provisioned will create a new room on the user's
server and return the room id (and optionally the room alias).
The following may be passed as named parameters:
=over
=item requires_users => LIST
This should contain a list of user fixtures giving members of the new room. The
first user is the creator of the room; the others are joined to the room after creation.
=item with_alias => SCALAR
This is passed into C<matrix_create_and_join_room>, and will make the result include
the room alias as well as the room id.
=item (everything else)
Other parameters are passed into C<matrix_create_and_join_room>.
=back
=cut
push @EXPORT, qw( magic_room_fixture );
sub magic_room_fixture
{
my %args = @_;
fixture(
requires => delete $args{requires_users},
setup => sub {
my @members = @_;
matrix_create_and_join_room( \@members, %args );
}
);
}
=head2 local_user_and_room_fixtures
( $user_fixture, $room_fixture ) = local_user_and_room_fixtures( %opts );
Returns a pair of Fixtures, which when provisioned will respectively create a
new user on the main test server (returning the User object), and use that
user to create a new room (returning the room id).
The following can be passed as optional parameters:
=over
=item user_opts => HASH
Options to use when creating the user, such as C<displayname>. These are passed
through to C<setup_user>.
=item room_opts => HASH
Options to use when creating the room. These are passed into
C<matrix_create_room>, whence they are passed on to the server.
=back
=cut
push @EXPORT, qw( local_user_and_room_fixtures );
sub local_user_and_room_fixtures
{
my %args = @_;
my $user_opts = $args{user_opts} // {};
my $room_opts = $args{room_opts} // {};
my $user_fixture = local_user_fixture( %$user_opts );
return (
$user_fixture,
room_fixture( $user_fixture, %$room_opts ),
);
}
push @EXPORT, qw(
matrix_join_room_synced
matrix_leave_room_synced matrix_invite_user_to_room_synced
);
sub matrix_join_room_synced
{
my ( $user, $room_id_or_alias, %params ) = @_;
matrix_do_and_wait_for_sync( $user,
do => sub {
retry_until_success { matrix_join_room( $user, $room_id_or_alias, %params ) }
},
check => sub { exists $_[0]->{rooms}{join}{$_[1]} },
);
}
sub matrix_leave_room_synced
{
my ( $user, $room_id, %params ) = @_;
matrix_do_and_wait_for_sync( $user,
do => sub {
matrix_leave_room( $user, $room_id, %params );
},
check => sub { exists $_[0]->{rooms}{leave}{$room_id} },
);
}
sub matrix_invite_user_to_room_synced
{
my ( $inviter, $invitee, $room_id, %params ) = @_;
matrix_do_and_wait_for_sync( $inviter,
do => sub {
matrix_do_and_wait_for_sync( $invitee,
do => sub {
matrix_invite_user_to_room(
$inviter, $invitee, $room_id, %params
);
},
check => sub { exists $_[0]->{rooms}{invite}{$room_id} },
);
},
check => sub {
sync_timeline_contains( $_[0], $room_id, sub {
$_[0]->{type} eq "m.room.member"
and $_[0]->{state_key} eq $invitee->user_id
and $_[0]->{content}{membership} eq "invite"
});
},
);
}