From f3f383db83504fd6e921b7899bbe893575625d45 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 4 Aug 2018 18:52:16 -0300 Subject: Add tests --- t/20-client.t | 64 ++++++++++++++++++++++++++++++++++ t/30-room.t | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ t/40-response.t | 25 +++++++++++++ t/50-exception.t | 12 +++++++ 4 files changed, 205 insertions(+) create mode 100644 t/20-client.t create mode 100644 t/30-room.t create mode 100644 t/40-response.t create mode 100644 t/50-exception.t (limited to 't') diff --git a/t/20-client.t b/t/20-client.t new file mode 100644 index 0000000..3fd6739 --- /dev/null +++ b/t/20-client.t @@ -0,0 +1,64 @@ +use lib 'lib'; +use Test; +use Matrix::Client; +plan 4; + +unless %*ENV { + skip-rest 'No test server setted'; + exit; +} + +my $home-server = %*ENV; +my $username = %*ENV; +my $password = %*ENV; +my $device-id = %*ENV; +my $access-token; +my Matrix::Client $client; + +subtest 'creation' => { + plan 2; + $client .= new(:$home-server, :$device-id); + isnt $client.home-server, '', 'home server isnt empty'; + isnt $client.device-id, '', 'device-id isnt empty'; + note $client.base-url; +} + +subtest 'login' => { + plan 4; + throws-like { + $client.login(:username, :password) + }, X::Matrix::Response, message => /M_FORBIDDEN/; + lives-ok { $client.login($username, $password) }, 'can logging with right data'; + + isnt $client.access-token, '', 'access-token setted'; + $access-token = $client.access-token; + + my Matrix::Client $access-token-client .= new(:$home-server, + :$device-id, + :$access-token); + ok $access-token-client.whoami.starts-with("@$username"), + 'client with access-token can do authorized calls'; +} + +subtest 'User data' => { + plan 2; + isa-ok $client.profile, Hash, '.profile returns a Hash?'; + + subtest 'display name' => { + plan 3; + is $client.display-name, $username, 'get default display-name'; + ok $client.change-display-name('testing'), 'change display-name'; + is $client.display-name, 'testing', 'get new display-name'; + $client.change-display-name($username); + } +} + +subtest 'sync' => { + plan 3; + isa-ok $client.sync(), Matrix::Response::Sync, + 'sync without params is a Response'; + isa-ok $client.sync(:sync-filter('{"room": { "timeline": { "limit": 1 } } }')), + Matrix::Response::Sync, 'sync with Str sync-filter'; + isa-ok $client.sync(:sync-filter(room => timeline => limit => 1)), + Matrix::Response::Sync, 'sync wit Hash sync-filter'; +} diff --git a/t/30-room.t b/t/30-room.t new file mode 100644 index 0000000..e12a17c --- /dev/null +++ b/t/30-room.t @@ -0,0 +1,104 @@ +use lib 'lib'; +use Test; +use Matrix::Client; +plan 10; + +unless %*ENV { + skip-rest 'No test server setted'; + exit; +} + +my $home-server = %*ENV; +my $username = %*ENV; +my $password = %*ENV; +my $device-id = %*ENV; +my $public-room-id = %*ENV // '!cYTYddgfJTLTzdiDBP:localhost'; +my Matrix::Client $client .= new(:$home-server); + +$client.login(:$username, :$password); + +my $room-alias = 'Room' ~ (^10).map({('a'…'z').pick}).join; +my $main-room; + +lives-ok { + $main-room = $client.create-room( + :public, + :preset, + :room_alias_name($room-alias), + :name("The Grand Duke Pub"), + :topic("All about happy hour"), + :creation_content({ + "m.federate" => False + }) + ); +}, 'Can create room'; + +isa-ok $main-room, Matrix::Client::Room; + +my $room-id = $main-room.id; + +lives-ok { + $main-room.leave; +}, 'Can leave room'; + +lives-ok { + $main-room.join; +}, 'Can join a room'; + +lives-ok { + $client.join-room($public-room-id) +}, 'Can join public room'; + +my @rooms = $client.joined-rooms; +my $public-room = @rooms.first(-> $room { $room.id eq $public-room-id }); + +isa-ok $public-room, Matrix::Client::Room; +isa-ok $public-room.send('hi'), Str; + +subtest 'states' => { + plan 2; + isa-ok $main-room.state(), Seq; + my @states = $main-room.state(); + isa-ok @states.first(), Matrix::Response::StateEvent; +}; + +subtest 'creation' => { + plan 3; + my $new-room = Matrix::Client::Room.new( + :id($public-room.id), + :$home-server, + :access-token($client.access-token) + ); + + ok $new-room, 'Can .new a room with the id of the public room'; + isa-ok $new-room, Matrix::Client::Room; + is $new-room.id, $public-room.id, 'The id is the same as the public room'; +}; + +subtest 'name' => { + plan 4; + + my $name = "Name room test"; + my $test-room = $client.create-room: + :creation_content({ + "m.federate" => False + }); + + throws-like { + $test-room.name() + }, X::Matrix::Response, + message => /M_NOT_FOUND/, + '.name without name gives event not found'; + + lives-ok { + $test-room.send-state('m.room.name', :name($name)) + }, 'Can change name to an unnamed room'; + + lives-ok { + $test-room.name() + }, '.name with a name set dont die'; + + is $test-room.name, $name, 'The name is set correctly'; + + $test-room.leave; +}; diff --git a/t/40-response.t b/t/40-response.t new file mode 100644 index 0000000..61dd635 --- /dev/null +++ b/t/40-response.t @@ -0,0 +1,25 @@ +use lib 'lib'; +use Test; +use JSON::Tiny; +use Matrix::Response; +plan 7; + +my $test-file = 'sync.json'; + +unless $test-file.IO.f { + skip-rest 'Missing sync.json to test'; + exit; +} + +my $data = from-json($test-file.IO.slurp); + +ok $data; +lives-ok { Matrix::Response::Sync.new($test-file.IO.slurp) }; +lives-ok { Matrix::Response::Sync.new($data) }; + +my $res = Matrix::Response::Sync.new($data); +can-ok $res, 'joined-rooms'; +can-ok $res, 'presence'; + +isa-ok $res.joined-rooms, List; +isa-ok $res.presence, List; diff --git a/t/50-exception.t b/t/50-exception.t new file mode 100644 index 0000000..d252153 --- /dev/null +++ b/t/50-exception.t @@ -0,0 +1,12 @@ +use lib 'lib'; +use Test; +use Matrix::Client; +use Matrix::Client::Exception; + +plan 1; + +my $c = Matrix::Client.new(:home-server); + +throws-like { + $c.get('/unknown-endpoint'); +}, X::Matrix::Response, 'Unknown endpoint throws exception'; -- cgit v1.2.3-70-g09d2