Skip to content

Commit

Permalink
add and update tests for new behaviour with empty backups (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhoreg authored Nov 5, 2018
1 parent 4369cfc commit 546e580
Showing 1 changed file with 80 additions and 21 deletions.
101 changes: 80 additions & 21 deletions tests/41end-to-end-keys/07-backup.pl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
my $fixture = local_user_fixture();

my $current_version; # FIXME: is there a better way of passing the backup version between tests?

test "Can create backup version",
requires => [ $fixture ],

Expand All @@ -14,15 +12,15 @@

matrix_create_key_backup( $user )->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Create backup: ", $content;

assert_json_keys( $content, "version" );
$version = $content->{version};

matrix_get_key_backup_info( $user );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Get backup info: ", $content;

assert_json_keys( $content, "algorithm" );

Expand All @@ -40,6 +38,51 @@
});
};

test "Responds correctly when backup is empty",
requires => [ $fixture, qw( can_create_backup_version ) ],

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

matrix_get_key_backup_info( $user )->then( sub {
my ( $content ) = @_;

log_if_fail "Get backup info: ", $content;

$version = $content->{version};

# check that asking for a specific session that does not exist returns
# an M_NOT_FOUND
matrix_get_backup_key( $user, $version, '!notaroom', 'notassession' );
})->main::expect_m_not_found
->then( sub {
# check that asking for all the keys in a room returns an empty
# response rather than an error when nothing has been backed up yet
matrix_get_backup_key( $user, $version, '!notaroom' );
})->then( sub {
my ( $content ) = @_;

log_if_fail "Get keys from room: ", $content;

assert_deeply_eq( $content, { "sessions" => {} } );

# check that asking for all the keys returns an empty response rather
# than an error when nothing has been backed up yet
matrix_get_backup_key( $user, $version );
})->then( sub {
my ( $content ) = @_;

log_if_fail "Get all keys: ", $content;

assert_deeply_eq( $content, { "rooms" => {} } );

# check that asking for a nonexistent backup version returns an
# M_NOT_FOUND
matrix_get_backup_key( $user, 'bogusversion' );
})->main::expect_m_not_found;
};

test "Can backup keys",
requires => [ $fixture, qw( can_create_backup_version ) ],

Expand All @@ -63,12 +106,12 @@
);
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Back up session: ", $content;

matrix_get_backup_key( $user, '!abcd', '1234', $version );
matrix_get_backup_key( $user, $version, '!abcd', '1234' );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Get session backup: ", $content;

assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) );

Expand Down Expand Up @@ -115,12 +158,12 @@
);
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Back up session: ", $content;

matrix_get_backup_key( $user, '!abcd', '1234', $version );
matrix_get_backup_key( $user, $version, '!abcd', '1234' );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Get session backup: ", $content;

assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) );

Expand Down Expand Up @@ -167,12 +210,12 @@
);
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Back up session: ", $content;

matrix_get_backup_key( $user, '!abcd', '1234', $version );
matrix_get_backup_key( $user, $version, '!abcd', '1234' );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Get session backup: ", $content;

assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) );

Expand Down Expand Up @@ -239,7 +282,7 @@
matrix_get_key_backup_info( $user );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Get backup: ", $content;

my $new_version = $content->{version};

Expand All @@ -259,7 +302,7 @@

matrix_create_key_backup( $user )->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Create backup: ", $content;

assert_json_keys( $content, "version" );

Expand All @@ -280,7 +323,7 @@
matrix_delete_key_backup( $user, $version );
})->then( sub {
my ( $content ) = @_;
log_if_fail "Content", $content;
log_if_fail "Delete backup: ", $content;

matrix_create_key_backup( $user );
})->then( sub {
Expand All @@ -295,7 +338,13 @@
version => $content->{version},
}
);
})->main::expect_http_404;
})->then( sub {
my ( $content ) = @_;

assert_deeply_eq($content, {"rooms" => {}}, "Expected new backup to be empty");

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


Expand Down Expand Up @@ -384,18 +433,28 @@ sub matrix_backup_keys {

=head2 matrix_get_backup_key
matrix_get_backup_key( $user, $room_id, $session_id, $version )
matrix_get_backup_key( $user, $version, $room_id, $session_id )
Send keys to a given key backup version
Get keys from a given key backup version
=cut

sub matrix_get_backup_key {
my ( $user, $room_id, $session_id, $version ) = @_;
my ( $user, $version, $room_id, $session_id ) = @_;

my $uri;

if ( defined $session_id ) {
$uri = "/unstable/room_keys/keys/$room_id/$session_id";
} elsif ( defined $room_id ) {
$uri = "/unstable/room_keys/keys/$room_id";
} else {
$uri = "/unstable/room_keys/keys";
}

do_request_json_for( $user,
method => "GET",
uri => "/unstable/room_keys/keys/$room_id/$session_id",
uri => $uri,
params => {
version => $version,
},
Expand Down

0 comments on commit 546e580

Please sign in to comment.