Skip to content

Commit

Permalink
Use temporary federation server for invite rejections
Browse files Browse the repository at this point in the history
to avoid our server getting blacklisted and affecting later tests.
  • Loading branch information
richvdh committed Apr 24, 2017
1 parent 34b1d27 commit a7e9d38
Showing 1 changed file with 44 additions and 72 deletions.
116 changes: 44 additions & 72 deletions tests/50federation/35room-invite.pl
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,65 @@ sub invite_server
);
}

foreach my $error_code (403, 500) {
test "Inbound federation can receive invite and reject when remote replies with a $error_code",
requires => [ local_user_fixture(), $main::INBOUND_SERVER,
federation_user_id_fixture() ],

foreach my $error_code (403, 500, -1) {
# a temporary federation server which is shut down at the end of the test.
# we use a temporary server because otherwise the remote server ends up on the
# backoff list and subsequent tests fail.
my $temp_federation_server_fixture = fixture(
setup => sub {
create_federation_server()
},
teardown => sub {
my ($server) = @_;
$server -> close();
}
);

test "Inbound federation can receive invite and reject when "
. ($error_code >= 0 ? "remote replies with a $error_code" : "is unreachable" ),
requires => [ local_user_fixture(), $temp_federation_server_fixture ],

do => sub {
my ( $user, $inbound_server, $creator_id ) = @_;
my ( $user, $federation_server ) = @_;

my $creator_id = '@__ANON__:' . $federation_server->server_name;

my $datastore = $inbound_server->datastore;
my $datastore = $federation_server->datastore;

my $room = SyTest::Federation::Room->new(
datastore => $datastore,
);

$room->create_initial_events(
server => $inbound_server,
server => $federation_server,
creator => $creator_id,
);

my $room_id = $room->room_id;

invite_server( $room, $creator_id, $user, $inbound_server )
invite_server( $room, $creator_id, $user, $federation_server )
->then( sub {
Future->needs_all(
$inbound_server->await_request_make_leave( $room_id, $user->user_id )->then( sub {
my ( $req, undef ) = @_;

assert_eq( $req->method, "GET", 'request method' );

$req->respond_json( {}, code => $error_code );

Future->done;
}),
matrix_leave_room( $user, $room_id )
)
if ($error_code < 0) {
# now shut down the remote server, so that we get an 'unreachable'
# error on make_leave
$federation_server->close();

return matrix_leave_room( $user, $room_id );
} else {
Future->needs_all(
$federation_server->await_request_make_leave( $room_id, $user->user_id )->then( sub {
my ( $req, undef ) = @_;

assert_eq( $req->method, "GET", 'request method' );

$req->respond_json( {}, code => $error_code );

Future->done;
}),
matrix_leave_room( $user, $room_id )
);
}
})->then( sub {
matrix_sync( $user );
})->then( sub {
Expand All @@ -189,55 +213,3 @@ sub invite_server
});
};
}

# a temporary federation server which is shut down at the end of the test.
my $temp_federation_server_fixture = fixture(
setup => sub {
create_federation_server()
},
teardown => sub {
my ($server) = @_;
# in case the test failed early, shut down the server here anyway.
$server -> close();
}
);

test "Inbound federation can receive invite and reject when remote is unreachable",
requires => [ local_user_fixture(), $temp_federation_server_fixture ],

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

my $creator_id = '@__ANON__:' . $federation_server->server_name;

my $datastore = $federation_server->datastore;

my $room = SyTest::Federation::Room->new(
datastore => $datastore,
);

$room->create_initial_events(
server => $federation_server,
creator => $creator_id,
);

my $room_id = $room->room_id;

invite_server( $room, $creator_id, $user, $federation_server )
->then( sub {
# now shut down the remote server, so that we get an 'unreachable'
# error on make_leave
$federation_server->close();

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

log_if_fail "Sync body", $body;
assert_json_object( $body->{rooms}{invite} );
keys %{ $body->{rooms}{invite} } and die "Expected empty dictionary";
Future->done(1);
});
};

0 comments on commit a7e9d38

Please sign in to comment.