-
Notifications
You must be signed in to change notification settings - Fork 55
/
02ghost.pl
292 lines (216 loc) · 8.47 KB
/
02ghost.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
my $user_fixture = local_user_fixture( with_events => 1 );
multi_test "AS-ghosted users can use rooms via AS",
requires => [ as_ghost_fixture(), $main::AS_USER[0], $user_fixture, $main::APPSERV[0],
room_fixture( $user_fixture ),
qw( can_receive_room_message_locally )],
do => sub {
my ( $ghost, $as_user, $creator, $appserv, $room_id ) = @_;
Future->needs_all(
$appserv->await_event( "m.room.member" )->then( sub {
my ( $event ) = @_;
log_if_fail "AS event", $event;
assert_json_keys( $event, qw( content room_id ));
$event->{room_id} eq $room_id or
die "Expected room_id to be $room_id";
$event->{state_key} eq $ghost->user_id or
die "Expected state_key to be ${\$ghost->user_id}";
assert_json_keys( my $content = $event->{content}, qw( membership ) );
$content->{membership} eq "join" or
die "Expected membership to be 'join'";
Future->done;
}),
do_request_json_for( $as_user,
method => "POST",
uri => "/v3/rooms/$room_id/join",
params => {
user_id => $ghost->user_id,
},
content => {},
)
)->SyTest::pass_on_done( "User joined room via AS" )
->then( sub {
Future->needs_all(
$appserv->await_event( "m.room.message" )->then( sub {
my ( $event ) = @_;
log_if_fail "AS event", $event;
assert_json_keys( $event, qw( room_id sender ));
$event->{room_id} eq $room_id or
die "Expected room_id to be $room_id";
$event->{sender} eq $ghost->user_id or
die "Expected sender user_id to be ${\$ghost->user_id}";
Future->done;
}),
do_request_json_for( $as_user,
method => "POST",
uri => "/v3/rooms/$room_id/send/m.room.message",
params => {
user_id => $ghost->user_id,
},
content => { msgtype => "m.text", body => "Message from AS directly" },
)
)
})->SyTest::pass_on_done( "User posted message via AS" )
->then( sub {
await_sync_timeline_contains( $creator, $room_id, check => sub {
my ( $event ) = @_;
return unless $event->{type} eq "m.room.message";
log_if_fail "Event", $event;
my $content = $event->{content};
$content->{body} eq "Message from AS directly" or
die "Expected 'body' as 'Message from AS directly'";
$event->{sender} eq $ghost->user_id or
die "Expected sender user_id as ${\$ghost->user_id}";
return 1;
})->on_done( sub { "Creator received user's message" } )
})->then_done(1);
};
multi_test "AS-ghosted users can use rooms themselves",
requires => [ as_ghost_fixture(), $user_fixture, $main::APPSERV[0],
room_fixture( $user_fixture ),
qw( can_receive_room_message_locally can_send_message )],
do => sub {
my ( $ghost, $creator, $appserv, $room_id ) = @_;
Future->needs_all(
$appserv->await_event( "m.room.member" )->then( sub {
my ( $event ) = @_;
log_if_fail "AS event", $event;
assert_json_keys( $event, qw( content room_id ));
$event->{room_id} eq $room_id or
die "Expected room_id to be $room_id";
assert_json_keys( my $content = $event->{content}, qw( membership ) );
$content->{membership} eq "join" or
die "Expected membership to be 'join'";
Future->done;
}),
matrix_join_room_synced( $ghost, $room_id )
)->SyTest::pass_on_done( "Ghost joined room themselves" )
->then( sub {
Future->needs_all(
$appserv->await_event( "m.room.message" )->then( sub {
my ( $event ) = @_;
log_if_fail "AS event", $event;
assert_json_keys( $event, qw( room_id sender ));
$event->{room_id} eq $room_id or
die "Expected room_id to be $room_id";
$event->{sender} eq $ghost->user_id or
die "Expected sender user_id to be ${\$ghost->user_id}";
Future->done;
}),
matrix_send_room_text_message_synced( $ghost, $room_id,
body => "Message from AS Ghost",
)
)
})->SyTest::pass_on_done( "Ghost posted message themselves" )
->then( sub {
await_sync_timeline_contains( $creator, $room_id, check => sub {
my ( $event ) = @_;
return unless $event->{type} eq "m.room.message";
log_if_fail "Event", $event;
my $content = $event->{content};
$content->{body} eq "Message from AS Ghost" or
die "Expected 'body' as 'Message from AS Ghost'";
$event->{sender} eq $ghost->user_id or
die "Expected sender user_id as ${\$ghost->user_id}";
return 1;
})->SyTest::pass_on_done( "Creator received ghost's message" )
})->then_done(1);
};
my $unregistered_as_user_localpart = "astest-02ghost-1";
test "Ghost user must register before joining room",
requires => [ $main::AS_USER[0], local_user_and_room_fixtures(), $main::HOMESERVER_INFO[0] ],
check => sub {
my ( $as_user, undef, $room_id, $hs_info ) = @_;
do_request_json_for( $as_user,
method => "POST",
uri => "/v3/rooms/$room_id/join",
params => {
user_id => "@".$unregistered_as_user_localpart.":".$hs_info->server_name,
},
content => {},
);
},
do => sub {
my ( $as_user, undef, $room_id ) = @_;
do_request_json_for( $as_user,
method => "POST",
uri => "/v3/register",
content => {
username => $unregistered_as_user_localpart,
type => "m.login.application_service",
},
);
};
my $avatar_url = "http://somewhere/my-pic.jpg";
test "AS can set avatar for ghosted users",
requires => [ as_ghost_fixture(), $main::AS_USER[0],
qw( can_get_avatar_url ) ],
check => sub {
my ( $ghost, $as_user ) = @_;
my $user_id = $ghost->user_id;
my $http = $as_user->http;
$http->do_request_json(
method => "GET",
uri => "/v3/profile/$user_id/avatar_url",
)->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( avatar_url ));
assert_eq( $body->{avatar_url}, $avatar_url, 'avatar_url' );
Future->done(1);
});
},
do => sub {
my ( $ghost, $as_user ) = @_;
my $user_id = $ghost->user_id;
do_request_json_for(
$as_user,
method => "PUT",
uri => "/v3/profile/$user_id/avatar_url",
params => {
user_id => $user_id,
},
content => { avatar_url => $avatar_url },
);
};
my $displayname = "Ghost user's new name";
test "AS can set displayname for ghosted users",
requires => [ as_ghost_fixture(), $main::AS_USER[0],
qw( can_get_displayname ) ],
check => sub {
my ( $ghost, $as_user ) = @_;
my $user_id = $ghost->user_id;
my $http = $as_user->http;
$http->do_request_json(
method => "GET",
uri => "/v3/profile/$user_id/displayname",
)->then( sub {
my ( $body ) = @_;
assert_json_keys( $body, qw( displayname ));
assert_eq( $body->{displayname}, $displayname, 'displayname' );
Future->done(1);
});
},
do => sub {
my ( $ghost, $as_user ) = @_;
as_set_displayname_for_user( $as_user, $ghost, $displayname );
};
test "AS can't set displayname for random users",
requires => [ $main::AS_USER[0], $user_fixture ],
do => sub {
my ( $as_user, $regular_user ) = @_;
as_set_displayname_for_user(
$as_user, $regular_user, $displayname
)->main::expect_http_403;
};
sub as_set_displayname_for_user {
my ( $as_user, $target_user, $displayname ) = @_;
my $user_id = $target_user->user_id;
do_request_json_for(
$as_user,
method => "PUT",
uri => "/v3/profile/$user_id/displayname",
content => { displayname => $displayname },
params => {
user_id => $user_id,
},
);
}