Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for setting account data (SYT-30) #82

Merged
merged 1 commit into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/10apidoc/03events-initial.pl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ sub matrix_sync
)->on_done( sub {
my ( $body ) = @_;

assert_json_keys( $body, qw( rooms presence next_batch ) );
assert_json_keys( $body, qw( account_data rooms presence next_batch ) );
assert_json_keys( $body->{presence}, qw( events ));
assert_json_keys( $body->{rooms}, qw( join invite leave ) );
});
Expand Down
269 changes: 269 additions & 0 deletions tests/44account_data.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
push our @EXPORT, qw( matrix_add_account_data );

=head2 matrix_add_account_data

matrix_add_account_data( $user, $type, $content )->get;

Add account data for the user.

=cut

sub matrix_add_account_data
{
my ( $user, $type, $content ) = @_;

do_request_json_for( $user,
method => "PUT",
uri => "/v2_alpha/user/:user_id/account_data/$type",
content => $content
);
}

=head2 matrix_add_room_account_data

matrix_add_account_data( $user, $room_id, $type, $content )->get;

Add account data for the user for a room.

=cut

sub matrix_add_room_account_data
{
my ( $user, $room_id, $type, $content ) = @_;

do_request_json_for( $user,
method => "PUT",
uri => "/v2_alpha/user/:user_id/rooms/$room_id/account_data/$type",
content => $content
);
}


test "Can add account data",
requires => [ local_user_fixture() ],

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

matrix_add_account_data( $user, "my.test.type", {} );
};


test "Can add account data to room",
requires => [ local_user_and_room_fixtures() ],

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

matrix_add_room_account_data( $user, $room_id, "my.test.type", {} );
};


sub check_one_account_data_event
{
my ( $account_data_events, $expected_type, $cats_or_rats ) = @_;

log_if_fail "account data", $account_data_events;

@{ $account_data_events } == 1 or die "Expected only one event";

my $event = $account_data_events->[0];
assert_json_keys($event, qw( type content ));

$event->{type} eq $expected_type
or die "Unexpected event type, wanted $expected_type";

$event->{content}{cats_or_rats} eq $cats_or_rats
or die "Unexpected event content, wanted $cats_or_rats";
}


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

Future->needs_all(
matrix_add_account_data( $user, "my.test.type", {
cats_or_rats => "frogs",
}),
matrix_add_room_account_data( $user, $room_id, "my.test.type", {
cats_or_rats => "dogs",
}),
)->then( sub {
Future->needs_all(
matrix_add_account_data( $user, "my.test.type", {
cats_or_rats => "cats",
}),
matrix_add_room_account_data( $user, $room_id, "my.test.type", {
cats_or_rats => "rats",
}),
);
});
}


test "Latest account data comes down in /initialSync",
requires => [ local_user_and_room_fixtures() ],

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

setup_account_data( $user, $room_id )->then( sub {
matrix_initialsync( $user );
})->then( sub {
my ( $body ) = @_;

check_one_account_data_event(
$body->{account_data}, "my.test.type", "cats"
);

check_one_account_data_event(
$body->{rooms}[0]{account_data}, "my.test.type", "rats"
);

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


test "Latest account data comes down in room initialSync",
requires => [ local_user_and_room_fixtures() ],

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

setup_account_data( $user, $room_id )->then( sub {
matrix_initialsync_room( $user, $room_id );
})->then( sub {
my ( $body ) = @_;

check_one_account_data_event(
$body->{account_data}, "my.test.type", "rats"
);

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


test "Account data appears in v1 /events stream",
requires => [ local_user_fixture() ],

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

Future->needs_all(
await_event_for( $user, filter => sub {
my ( $event ) = @_;

return $event->{type} eq "my.test.type"
and $event->{content}{cats_or_rats} eq "cats";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wants a terminating return 1; at the end.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a dropped "unless" :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahyes, that would be neater

}),
matrix_add_account_data( $user, "my.test.type", {
cats_or_rats => "cats",
}),
);
};


test "Room account data appears in v1 /events stream",
requires => [ local_user_and_room_fixtures() ],

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

Future->needs_all(
await_event_for( $user, filter => sub {
my ( $event ) = @_;

return $event->{type} eq "my.test.type"
and $event->{content}{cats_or_rats} eq "rats"
and $event->{room_id} eq $room_id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto return 1;

}),
matrix_add_room_account_data( $user, $room_id, "my.test.type", {
cats_or_rats => "rats",
}),
);
};


test "Latest account data appears in v2 /sync",
requires => [ local_user_and_room_fixtures() ],

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

setup_account_data( $user, $room_id )->then( sub {
matrix_sync( $user );
})->then( sub {
my ( $body ) = @_;

check_one_account_data_event(
$body->{account_data}{events}, "my.test.type", "cats"
);

check_one_account_data_event(
$body->{rooms}{join}{$room_id}{account_data}{events},
"my.test.type", "rats"
);

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

sub setup_incremental_account_data
{
my ( $user, $room_id, $type, $top_animal, $room_animal ) = @_;

Future->needs_all(
matrix_add_account_data( $user, $type, {
cats_or_rats => $top_animal,
}),
matrix_add_room_account_data( $user, $room_id, $type, {
cats_or_rats => $room_animal,
})
);
}

test "New account data appears in incremental v2 /sync",
requires => [ local_user_and_room_fixtures() ],

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

my ( $next_batch );

Future->needs_all(
setup_incremental_account_data(
$user, $room_id, "my.unchanging.type", "lions", "tigers"
),
setup_incremental_account_data(
$user, $room_id, "my.changing.type", "dogs", "frogs"
),
)->then( sub {
matrix_sync( $user );
})->then( sub {
my ( $body ) = @_;

$next_batch = $body->{next_batch};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would possibly Future->done( $body->{next_batch} ) and grab it in the next sub, rather than having it scoped as a variable outside the futures. But possibly also I would do exactly what you've done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generally-prevailing style around most tests is some shared captured variables at the top of the test block, that get set in some stage and read later. For consistency it's probably best to stick to that style.

Neither of them is overly nice - ideally we'd want to use the async/await syntax for a lot of these tests instead, but I haven't got around to implementing that yet... ;)


setup_incremental_account_data(
$user, $room_id, "my.changing.type", "cats", "rats"
),
})->then( sub {
matrix_sync( $user, since => $next_batch );
})->then( sub {
my ( $body ) = @_;

check_one_account_data_event(
$body->{account_data}{events}, "my.changing.type", "cats"
);

check_one_account_data_event(
$body->{rooms}{join}{$room_id}{account_data}{events},
"my.changing.type", "rats"
);

Future->done(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check that my.unchanging.type isn't present?

});
};