Skip to content

Commit

Permalink
Merge pull request #127 from matrix-org/paul/room-alias-fixture
Browse files Browse the repository at this point in the history
Fixtures for allocating room aliases (and localpart names)
  • Loading branch information
leonerd committed Jan 5, 2016
2 parents 1337730 + 74e41a8 commit 938650b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 42 deletions.
65 changes: 65 additions & 0 deletions tests/10apidoc/30room-create.pl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,68 @@ sub matrix_create_room
Future->done( $body->{room_id}, $body->{room_alias} );
});
}

push @EXPORT, qw( room_alias_name_fixture room_alias_fixture );

my $next_alias_name = 0;

=head2 room_alias_name_fixture
$fixture = room_alias_name_fixture( prefix => $prefix )
Returns a new Fixture, which when provisioned will allocate a new room alias
name (i.e. localpart, before the homeserver domain name, and return it as a
string.
An optional prefix string can be provided, which will be prepended onto the
alias name.
=cut

sub room_alias_name_fixture
{
my %args = @_;

my $prefix = $args{prefix} // "";

return fixture(
setup => sub {
my ( $info ) = @_;

my $alias_name = sprintf "%s__ANON__-%d", $prefix, $next_alias_name++;

Future->done( $alias_name );
},
);
}

=head2 room_alias_fixture
$fixture = room_alias_fixture( prefix => $prefix )
Returns a new Fixture, which when provisioned will allocate a name for a new
room alias on the first homeserver, and return it as a string. Note that this
does not actually create the alias on the server itself, it simply suggests a
new unique name for one.
An optional prefix string can be provided, which will be prepended onto the
alias name.
=cut

sub room_alias_fixture
{
my %args = @_;

return fixture(
requires => [
room_alias_name_fixture( prefix => $args{prefix} ), $main::HOMESERVER_INFO[0],
],

setup => sub {
my ( $alias_name, $info ) = @_;

Future->done( sprintf "#%s:%s", $alias_name, $info->server_name );
},
);
}
6 changes: 3 additions & 3 deletions tests/10apidoc/31room-state.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

# This provides $room_id *AND* $room_alias
my $room_fixture = fixture(
requires => [ $user_fixture ],
requires => [ $user_fixture, room_alias_name_fixture() ],

setup => sub {
my ( $user ) = @_;
my ( $user, $room_alias_name ) = @_;

matrix_create_room( $user,
room_alias_name => "31room-state",
room_alias_name => $room_alias_name,
);
},
);
Expand Down
17 changes: 7 additions & 10 deletions tests/10apidoc/32room-alias.pl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
my $alias_localpart = "#another-alias";

my $user_fixture = local_user_fixture();

my $room_fixture = fixture(
Expand All @@ -8,19 +6,20 @@
setup => sub {
my ( $user ) = @_;

matrix_create_room( $user );
matrix_create_room( $user )->then( sub {
my ( $room_id, undef ) = @_;
Future->done( $room_id ); # Don't return the alias
});
},
);

test "PUT /directory/room/:room_alias creates alias",
requires => [ $user_fixture, $room_fixture ],
requires => [ $user_fixture, $room_fixture, room_alias_fixture() ],

proves => [qw( can_create_room_alias can_lookup_room_alias )],

do => sub {
my ( $user, $room_id ) = @_;
my $server_name = $user->http->server_name;
my $room_alias = "${alias_localpart}:$server_name";
my ( $user, $room_id, $room_alias ) = @_;

do_request_json_for( $user,
method => "PUT",
Expand All @@ -33,9 +32,7 @@
},

check => sub {
my ( $user, $room_id ) = @_;
my $server_name = $user->http->server_name;
my $room_alias = "${alias_localpart}:$server_name";
my ( $user, $room_id, $room_alias ) = @_;

do_request_json_for( $user,
method => "GET",
Expand Down
6 changes: 3 additions & 3 deletions tests/10apidoc/33room-members.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

# This provides $room_id *AND* $room_alias
my $room_fixture = fixture(
requires => [ $creator_fixture ],
requires => [ $creator_fixture, room_alias_name_fixture() ],

setup => sub {
my ( $user ) = @_;
my ( $user, $room_alias_name ) = @_;

matrix_create_room( $user,
room_alias_name => "33room-members",
room_alias_name => $room_alias_name,
);
},
);
Expand Down
12 changes: 6 additions & 6 deletions tests/30rooms/03members-remote.pl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
my $remote_user_fixture = remote_user_fixture();

my $room_fixture = fixture(
requires => [ $creator_fixture ],
requires => [ $creator_fixture, room_alias_name_fixture() ],

setup => sub {
my ( $user ) = @_;
my ( $user, $room_alias_name ) = @_;

matrix_create_room( $user,
room_alias_name => "03members-remote"
room_alias_name => $room_alias_name,
);
},
);
Expand Down Expand Up @@ -208,14 +208,14 @@
};

test "Remote users may not join unfederated rooms",
requires => [ local_user_fixture(), remote_user_fixture(),
requires => [ local_user_fixture(), remote_user_fixture(), room_alias_name_fixture(),
qw( can_create_room_with_creation_content )],

check => sub {
my ( $creator, $remote_user ) = @_;
my ( $creator, $remote_user, $room_alias_name ) = @_;

matrix_create_room( $creator,
room_alias_name => "unfederated",
room_alias_name => $room_alias_name,
creation_content => {
"m.federate" => JSON::false,
},
Expand Down
6 changes: 3 additions & 3 deletions tests/30rooms/05aliases.pl
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@
};

multi_test "Canonical alias can be set",
requires => [ local_user_fixture() ],
requires => [ local_user_fixture(), room_alias_name_fixture() ],

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

my ( $room_id, $room_alias );

matrix_create_room( $user,
room_alias_name => "is-this-canonical",
room_alias_name => $room_alias_name,
)->then( sub {
( $room_id, $room_alias ) = @_;

Expand Down
6 changes: 3 additions & 3 deletions tests/50federation/11query-directory.pl
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
# TODO(paul): technically this doesn't need local_user_fixture(), if we had
# some user we could assert can perform media/directory/etc... operations
# but doesn't mutate any of its own state, or join rooms, etc...
requires => [ $main::OUTBOUND_CLIENT, $main::HOMESERVER_INFO[0], local_user_fixture(),
requires => [ $main::OUTBOUND_CLIENT, $main::HOMESERVER_INFO[0],
local_user_fixture(), room_alias_fixture(),
qw( can_create_room_alias )],

do => sub {
my ( $outbound_client, $info, $user ) = @_;
my ( $outbound_client, $info, $user, $room_alias ) = @_;
my $first_home_server = $info->server_name;

my $room_id;
my $room_alias = "#50federation-11query-directory:$first_home_server";

matrix_create_room( $user )
->then( sub {
Expand Down
21 changes: 11 additions & 10 deletions tests/60app-services/01as-create.pl
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@
};

test "AS can make room aliases",
requires => [ $main::AS_USER, $main::AS_USER_INFO, $room_fixture,
qw( can_create_room_alias )],
requires => [
$main::AS_USER, $main::AS_USER_INFO, $room_fixture,
room_alias_fixture( prefix => "astest-" ),
qw( can_create_room_alias ),
],

do => sub {
my ( $as_user, $as_user_info, $room_id ) = @_;
my $server_name = $as_user->http->server_name;
my $room_alias = "#astest-01create-1:$server_name";
my ( $as_user, $as_user_info, $room_id, $room_alias ) = @_;

Future->needs_all(
await_as_event( "m.room.aliases" )->then( sub {
Expand Down Expand Up @@ -127,13 +128,13 @@
};

test "Regular users cannot create room aliases within the AS namespace",
requires => [ $user_fixture, $room_fixture,
qw( can_create_room_alias )],
requires => [
$user_fixture, $room_fixture, room_alias_fixture( prefix => "astest-" ),
qw( can_create_room_alias ),
],

do => sub {
my ( $user, $room_id ) = @_;
my $server_name = $user->http->server_name;
my $room_alias = "#astest-01create-2:$server_name";
my ( $user, $room_id, $room_alias ) = @_;

do_request_json_for( $user,
method => "PUT",
Expand Down
6 changes: 2 additions & 4 deletions tests/60app-services/03passive.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@

multi_test "Accesing an AS-hosted room alias asks the AS server",
requires => [ $main::AS_USER, local_user_fixture(), $room_fixture,
room_alias_fixture( prefix => "astest-" ),

qw( can_join_room_by_alias )],

do => sub {
my ( $as_user, $local_user, $room_id ) = @_;
my $server_name = $as_user->http->server_name;

my $room_alias = "#astest-03passive-1:$server_name";
my ( $as_user, $local_user, $room_id, $room_alias ) = @_;

require_stub await_http_request( "/appserv/rooms/$room_alias", sub { 1 } )
->then( sub {
Expand Down

0 comments on commit 938650b

Please sign in to comment.