Skip to content

Commit

Permalink
Revert "Merge branch 'matthew/lazy_load_messages_api' into matthew/ro…
Browse files Browse the repository at this point in the history
…om_summary"

This reverts commit f95ede9, reversing
changes made to 080dfed.
  • Loading branch information
ara4n committed Jul 27, 2018
1 parent 995231b commit 2b069b5
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 226 deletions.
2 changes: 1 addition & 1 deletion lib/SyTest/HTTPClient.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ sub do_request
my $message = $response->message;
$message =~ s/\r?\n?$//; # because HTTP::Response doesn't do this

return Future->fail( "HTTP Request failed ( ${\$response->code} $message $uri )",
return Future->fail( "HTTP Request failed (${\$response->code} $message)",
http => $response, $response->request );
}

Expand Down
96 changes: 71 additions & 25 deletions tests/10apidoc/01register.pl
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ sub matrix_register_user
});
}

shared_secret_tests( "/r0/admin/register", \&matrix_admin_register_user_via_secret);
shared_secret_tests( "/v1/register", \&matrix_v1_register_user_via_secret);
shared_secret_tests( "/r0/register", \&matrix_r0_register_user_via_secret);

sub matrix_admin_register_user_via_secret
sub matrix_v1_register_user_via_secret
{
my ( $http, $uid, %opts ) = @_;

Expand All @@ -239,30 +240,23 @@ sub matrix_admin_register_user_via_secret
defined $uid or
croak "Require UID for matrix_register_user_via_secret";

$http->do_request_json(
method => "GET",
uri => "/r0/admin/register",
)->then( sub{
my ( $nonce ) = @_;

my $mac = hmac_sha1_hex(
join( "\0", $nonce->{nonce}, $uid, $password, $is_admin ? "admin" : "notadmin" ),
"reg_secret"
);
my $mac = hmac_sha1_hex(
join( "\0", $uid, $password, $is_admin ? "admin" : "notadmin" ),
"reg_secret"
);

return $http->do_request_json(
method => "POST",
uri => "/r0/admin/register",
$http->do_request_json(
method => "POST",
uri => "/api/v1/register",

content => {
nonce => $nonce->{nonce},
username => $uid,
password => $password,
admin => $is_admin ? JSON::true : JSON::false,
mac => $mac,
},
)
})->then( sub {
content => {
type => "org.matrix.login.shared_secret",
user => $uid,
password => $password,
admin => $is_admin ? JSON::true : JSON::false,
mac => $mac,
},
)->then( sub {
my ( $body ) = @_;

assert_json_keys( $body, qw( user_id access_token ));
Expand All @@ -284,6 +278,58 @@ sub matrix_admin_register_user_via_secret
});
}

sub matrix_r0_register_user_via_secret
{
my ( $http, $uid, %opts ) = @_;

my $password = $opts{password} // "an0th3r s3kr1t";
my $is_admin = $opts{is_admin} // 0;

defined $uid or
croak "Require UID for matrix_register_user_via_secret";

# for some reason the /r0/register endpoint only includes the
# uid in the hash. AFAICT it's equally valid, but one has to
# wonder why it is different to the /v1/ endpoint.
# (https://github.com/matrix-org/synapse/issues/2664)
my $mac = hmac_sha1_hex(
$uid,
"reg_secret"
);

$http->do_request_json(
method => "POST",
uri => "/r0/register",

content => {
type => "org.matrix.login.shared_secret",
username => $uid,
password => $password,
admin => $is_admin ? JSON::true : JSON::false,
mac => $mac,
},
)->then( sub {
my ( $body ) = @_;

assert_json_keys( $body, qw( user_id access_token device_id ));

my $uid = $body->{user_id};

log_if_fail "Registered new user (via secret) $uid";

my $access_token = $body->{access_token};

my $user = new_User(
http => $http,
user_id => $uid,
device_id => $body->{device_id},
password => $password,
access_token => $access_token,
);
return Future->done( $user );
});
}

sub shared_secret_tests {
my ( $ep_name, $register_func ) = @_;

Expand Down Expand Up @@ -382,7 +428,7 @@ sub local_admin_fixture
setup => sub {
my ( $http, $localpart ) = @_;

matrix_admin_register_user_via_secret( $http, $localpart, is_admin => 1, %args );
matrix_v1_register_user_via_secret( $http, $localpart, is_admin => 1, %args );
},
);
}
Expand Down
40 changes: 0 additions & 40 deletions tests/10apidoc/34room-messages.pl
Original file line number Diff line number Diff line change
Expand Up @@ -158,46 +158,6 @@ sub matrix_send_room_text_message
});
};

test "GET /rooms/:room_id/messages lazy loads members correctly",
requires => [ local_user_and_room_fixtures(),
qw( can_send_message )],

proves => [qw( can_get_messages )],

check => sub {
my ( $user, $room_id ) = @_;

matrix_send_room_text_message( $user, $room_id,
body => "Here is the message content",
)->then( sub {
do_request_json_for( $user,
method => "GET",
uri => "/r0/rooms/$room_id/messages",

# With no params this does "forwards from END"; i.e. nothing useful
params => {
dir => "b",
filter => '{ "lazy_load_members" : true }',
},
)
})->then( sub {
my ( $body ) = @_;

assert_json_keys( $body, qw( start end state chunk ));
assert_json_list( $body->{chunk} );
assert_json_list( $body->{state} );

assert_eq( scalar @{$body->{state}}, 1);
assert_eq( $body->{state}[0]{type}, 'm.room.member');
assert_eq( $body->{state}[0]{state_key}, $user->user_id);

scalar @{ $body->{chunk} } > 0 or
die "Expected some messages but got none at all\n";

Future->done(1);
});
};

push @EXPORT, qw(
matrix_get_room_messages matrix_send_room_text_message_synced
matrix_send_room_message_synced matrix_send_filler_messages_synced
Expand Down
23 changes: 10 additions & 13 deletions tests/14account/02deactivate.pl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use JSON qw( decode_json );

my $password = "my secure password";

sub matrix_deactivate_account
{
my ( $user, %opts ) = @_;

# use the user's password unless one was given in opts
my $password = (delete $opts{password}) // $user->password;
my ( $user, $password ) = @_;

do_request_json_for( $user,
method => "POST",
Expand All @@ -16,28 +15,26 @@ sub matrix_deactivate_account
user => $user->user_id,
password => $password,
},
%opts,
},
);
}
push our @EXPORT, qw( matrix_deactivate_account );

test "Can deactivate account",
requires => [ local_user_fixture() ],
requires => [ local_user_fixture( password => $password ) ],

check => sub {
my ( $user ) = @_;

matrix_deactivate_account( $user );
matrix_deactivate_account( $user, $password );
};

test "Can't deactivate account with wrong password",
requires => [ local_user_fixture() ],
requires => [ local_user_fixture( password => $password ) ],

check => sub {
my ( $user ) = @_;

matrix_deactivate_account( $user, password=>"wrong password" )
matrix_deactivate_account( $user, "wrong password" )
->main::expect_http_401->then( sub {
my ( $resp ) = @_;

Expand All @@ -55,20 +52,20 @@ sub matrix_deactivate_account
};

test "After deactivating account, can't log in with password",
requires => [ local_user_fixture() ],
requires => [ local_user_fixture( password => $password ) ],

check => sub {
my ( $user ) = @_;

matrix_deactivate_account( $user )
matrix_deactivate_account( $user, $password )
->then( sub {
do_request_json_for( $user,
method => "POST",
uri => "/r0/login",
content => {
type => "m.login.password",
user => $user->user_id,
password => $user->password,
password => $password,
}
# We don't mandate the exact failure code here
# (that should be done in the login test if
Expand Down
62 changes: 61 additions & 1 deletion tests/30rooms/31forget.pl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
})->main::expect_http_4xx;
};

test "Can re-join room if re-invited",
test "Can re-join room if re-invited - history_visibility = shared",
requires => [ local_user_fixture(), local_user_fixture() ],

do => sub {
Expand Down Expand Up @@ -183,6 +183,66 @@
});
};

test "Can re-join room if re-invited - history_visibility joined",
requires => [ local_user_fixture(), local_user_fixture() ],

do => sub {
my ( $creator, $user ) = @_;

my ( $room_id );

matrix_create_room( $creator, invite => [ $user->user_id ] )->then( sub {
( $room_id ) = @_;

log_if_fail "room_id", $room_id;

matrix_put_room_state( $creator, $room_id,
type => "m.room.join_rules",
state_key => "",
content => {
join_rule => "invite",
}
)
})->then( sub {
matrix_set_room_history_visibility( $creator, $room_id, "joined");
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_send_room_text_message( $creator, $room_id, body => "before leave" );
})->then( sub {
matrix_leave_room( $user, $room_id );
})->then( sub {
matrix_forget_room( $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id )->main::expect_http_403;
})->then( sub {
matrix_invite_user_to_room( $creator, $user, $room_id );
})->then( sub {
matrix_join_room( $user, $room_id );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 100 );
})->then( sub {
my ( $body ) = @_;

none { $_->{type} eq "m.room.message" } @{ $body->{chunk} }
or die "Should not have seen any m.room.message events";

matrix_send_room_text_message( $creator, $room_id, body => "after rejoin" );
})->then( sub {
matrix_get_room_messages( $user, $room_id, limit => 1 );
})->then( sub {
my ( $body ) = @_;

log_if_fail "body", $body;

@{ $body->{chunk} } == 1 or die "Expected event";
$body->{chunk}[0]->{type} eq "m.room.message" && $body->{chunk}[0]->{content}{body} eq "after rejoin"
or die "Got wrong event";

Future->done( 1 );
});
};

push our @EXPORT, qw( matrix_forget_room );

sub matrix_forget_room
Expand Down
Loading

0 comments on commit 2b069b5

Please sign in to comment.