Skip to content

Commit

Permalink
Fix usage of /messages API
Browse files Browse the repository at this point in the history
The test depended on a bug in Synapse where prev_batch is set
to an incorrect value in case /sync is called with since parameter.

Signed-off-by: Kurt Roeckx <[email protected]>
  • Loading branch information
kroeckx committed Aug 10, 2021
1 parent 5077771 commit 97d1228
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions tests/10apidoc/34room-messages.pl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ sub matrix_send_room_text_message
)
}

# This first sends a message, then does a /sync without since parameter.
# That /sync will most likely return the whole timeline for the room
# so we can't use it's prev_batch in the /messages API. So we send
# a 2nd message, and then do a /sync with since set to the previous
# /sync's next_batch. We can then fetch messages with the 2nd /sync's
# prev_batch that are before first /sync.
test "GET /rooms/:room_id/messages returns a message",
requires => [ local_user_and_room_fixtures(),
qw( can_send_message )],
Expand All @@ -138,12 +144,18 @@ sub matrix_send_room_text_message
my ( $user, $room_id ) = @_;

matrix_send_room_text_message( $user, $room_id,
body => "Here is the message content",
body => "Here is the first message",
)->then( sub {
matrix_sync( $user )
matrix_sync( $user );
})->then( sub {
matrix_send_room_text_message( $user, $room_id,
body => "Here is a second message",
)
})->then( sub {
matrix_sync_again( $user );
})->then( sub {
my ( $sync_body ) = @_;
my $token = $sync_body->{rooms}->{join}->{$room_id}->{timeline}->{prev_batch};
my $prev_batch = $sync_body->{rooms}->{join}->{$room_id}->{timeline}->{prev_batch};

do_request_json_for( $user,
method => "GET",
Expand All @@ -152,7 +164,7 @@ sub matrix_send_room_text_message
# With no params this does "forwards from END"; i.e. nothing useful
params => {
dir => "b",
from => $token,
from => $prev_batch,
},
)
})->then( sub {
Expand All @@ -164,6 +176,10 @@ sub matrix_send_room_text_message
scalar @{ $body->{chunk} } > 0 or
die "Expected some messages but got none at all\n";

assert_eq( $body->{chunk}[0]{type}, 'm.room.message');
assert_eq( $body->{chunk}[0]{content}{msgtype}, 'm.text');
assert_eq( $body->{chunk}[0]{content}{body}, 'Here is the first message');

Future->done(1);
});
};
Expand All @@ -176,12 +192,18 @@ sub matrix_send_room_text_message
my ( $user, $room_id ) = @_;

matrix_send_room_text_message( $user, $room_id,
body => "Here is the message content",
body => "Here is the first message",
)->then( sub {
matrix_sync( $user )
matrix_sync( $user );
})->then( sub {
matrix_send_room_text_message( $user, $room_id,
body => "Here is a second message",
)
})->then( sub {
matrix_sync_again( $user );
})->then( sub {
my ( $sync_body ) = @_;
my $token = $sync_body->{rooms}->{join}->{$room_id}->{timeline}->{prev_batch};
my $prev_batch = $sync_body->{rooms}->{join}->{$room_id}->{timeline}->{prev_batch};

do_request_json_for( $user,
method => "GET",
Expand All @@ -190,7 +212,7 @@ sub matrix_send_room_text_message
params => {
dir => "b",
filter => '{ "lazy_load_members" : true }',
from => $token,
from => $prev_batch,
},
)
})->then( sub {
Expand Down

0 comments on commit 97d1228

Please sign in to comment.