From 38e552673990c80231749a8ac812441802c6f66f Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 22 Jan 2019 10:46:35 +0000 Subject: [PATCH 1/4] Add tests for transfer of tags and dm status on room upgrade --- tests/30rooms/60version_upgrade.pl | 74 ++++++++++++++++++++++++++++++ tests/42tags.pl | 41 +++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/tests/30rooms/60version_upgrade.pl b/tests/30rooms/60version_upgrade.pl index b415479aa..1b735aa3f 100644 --- a/tests/30rooms/60version_upgrade.pl +++ b/tests/30rooms/60version_upgrade.pl @@ -15,6 +15,8 @@ use Future::Utils qw( repeat ); use List::Util qw( all first ); +push our @EXPORT, qw ( upgrade_room_synced $TEST_NEW_VERSION ); + # TODO: switch this to '2' once that is released my $TEST_NEW_VERSION = 'vdh-test-version'; @@ -59,6 +61,45 @@ sub upgrade_room { }); } +=head2 is_direct_room + + is_direct_room( $user, $room_id )->then( sub { + my ( $is_direct ) = @_; + }) + +Check if a room ID is considered direct by the given user. + +=cut + +sub is_direct_room { + my ( $user, $room_id ) = @_; + + # Download account data events from sync + matrix_sync( $user )->then( sub { + my ( $sync_body ) = @_; + + # Should only have the m.direct event in account_data + my $account_data = $sync_body->{account_data}{events}; + + # Check if the room_id is in the list of direct rooms + foreach my $event ( @$account_data ) { + if ( $event->{type} eq "m.direct" ) { + my $room_ids = $event->{content}{$user->user_id}; + + # Return whether the given room ID is in the response + foreach my $rid (@$room_ids) { + if ( $rid eq $room_id ) { + return Future->done( 1 ); + } + } + } + } + + # Didn't find an m.direct event with our room ID + Future->done( 0 ); + }); +} + =head2 upgrade_room_synced upgrade_room_synced( $user, $room_id, %opts )->then( sub { @@ -474,6 +515,39 @@ sub upgrade_room_synced { }); }; +test "/upgrade preserves direct room state", + requires => [ + local_user_and_room_fixtures(), + qw( can_upgrade_room_version ), + ], + + do => sub { + my ( $creator, $room_id ) = @_; + + my $new_room_id; + my $user_id = $creator->user_id; + + do_request_json_for( + $creator, + method => "PUT", + uri => "/r0/user/$user_id/account_data/m.direct", + content => { $user_id => [$room_id] }, + )->then( sub { + upgrade_room_synced( + $creator, $room_id, + new_version => $TEST_NEW_VERSION, + ); + })->then( sub { + ( $new_room_id ) = @_; + + is_direct_room( $creator, $new_room_id ); + })->then( sub { + my ( $is_direct_room ) = @_; + + $is_direct_room == 1 or die "Expected upgraded room to be a direct room"; + Future->done( 1 ); + }); + }; test "/upgrade restricts power levels in the old room", requires => [ diff --git a/tests/42tags.pl b/tests/42tags.pl index e7df893b0..572081245 100644 --- a/tests/42tags.pl +++ b/tests/42tags.pl @@ -407,3 +407,44 @@ sub check_account_data { Future->done( 1 ); }); }; + +test "/upgrade copies user tags to the new room", + requires => [ + local_user_and_room_fixtures(), + qw( can_upgrade_room_version can_add_tag ), + ], + + do => sub { + my ( $user, $room_id ) = @_; + my ( $new_room_id ); + + matrix_add_tag( + $user, $room_id, "test_tag", {} + )->then( sub { + matrix_sync( $user ); + })->then( sub { + upgrade_room_synced( + $user, $room_id, + new_version => $main::TEST_NEW_VERSION, + ); + })->then( sub { + ( $new_room_id, ) = @_; + + matrix_sync_again( $user ); + })->then( sub { + my ( $sync_body ) = @_; + + log_if_fail "sync body", $sync_body; + + my $room = $sync_body->{rooms}{join}{$new_room_id}; + + matrix_list_tags( $user, $new_room_id ); + })->then( sub { + my ( $tags ) = @_; + + keys %{ $tags } == 1 or die "Expected one tag in the upgraded room"; + defined $tags->{test_tag} or die "Unexpected tag"; + + Future->done(1); + }); + }; From 7bd71fdc4bc989d158a748def0d66d37daf54399 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 14:31:27 +0000 Subject: [PATCH 2/4] Use GET account data endpoint instead of syncing --- tests/30rooms/60version_upgrade.pl | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/tests/30rooms/60version_upgrade.pl b/tests/30rooms/60version_upgrade.pl index 1b735aa3f..8b0a6e326 100644 --- a/tests/30rooms/60version_upgrade.pl +++ b/tests/30rooms/60version_upgrade.pl @@ -75,27 +75,25 @@ sub is_direct_room { my ( $user, $room_id ) = @_; # Download account data events from sync - matrix_sync( $user )->then( sub { - my ( $sync_body ) = @_; - + matrix_get_account_data( $user, "m.direct" )->then( sub { # Should only have the m.direct event in account_data - my $account_data = $sync_body->{account_data}{events}; + my ( $data ) = @_; + + log_if_fail "m.direct account data", $data; # Check if the room_id is in the list of direct rooms - foreach my $event ( @$account_data ) { - if ( $event->{type} eq "m.direct" ) { - my $room_ids = $event->{content}{$user->user_id}; - - # Return whether the given room ID is in the response - foreach my $rid (@$room_ids) { - if ( $rid eq $room_id ) { - return Future->done( 1 ); - } + foreach my $user_id ( keys %{ $data } ) { + my $room_ids = $data->{$user_id}; + + # Return whether the given room ID is in the response + foreach my $room (@$room_ids) { + if ( $room eq $room_id ) { + return Future->done( 1 ); } } } - # Didn't find an m.direct event with our room ID + # Didn't find an direct romo with our room ID Future->done( 0 ); }); } From fc4163eac0ee8e9c929c5daf0170b5bcd53c52ff Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 28 Jan 2019 14:12:19 +0000 Subject: [PATCH 3/4] Update tests/30rooms/60version_upgrade.pl Co-Authored-By: anoadragon453 <1342360+anoadragon453@users.noreply.github.com> --- tests/30rooms/60version_upgrade.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/30rooms/60version_upgrade.pl b/tests/30rooms/60version_upgrade.pl index 8b0a6e326..1620ff827 100644 --- a/tests/30rooms/60version_upgrade.pl +++ b/tests/30rooms/60version_upgrade.pl @@ -67,7 +67,7 @@ =head2 is_direct_room my ( $is_direct ) = @_; }) -Check if a room ID is considered direct by the given user. +Check if a room ID is considered to be a direct chat by the given user. =cut From 2793a51029b7e756d02d0cae8a98c9f70856ec1b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 28 Jan 2019 14:12:33 +0000 Subject: [PATCH 4/4] Fix typo --- tests/30rooms/60version_upgrade.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/30rooms/60version_upgrade.pl b/tests/30rooms/60version_upgrade.pl index 8b0a6e326..274095e76 100644 --- a/tests/30rooms/60version_upgrade.pl +++ b/tests/30rooms/60version_upgrade.pl @@ -93,7 +93,7 @@ sub is_direct_room { } } - # Didn't find an direct romo with our room ID + # Didn't find a direct room with our room ID Future->done( 0 ); }); }