From 4823babb29e8d2c2f65b775e72210aac7a2175c5 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 17 May 2020 20:09:38 -0300 Subject: Add Matrix::Client::MediaStore class This is to wrap all the endpoints pointing to the media store. --- t/10-use.t | 2 ++ 1 file changed, 2 insertions(+) (limited to 't') diff --git a/t/10-use.t b/t/10-use.t index df4130b..1c6f97e 100644 --- a/t/10-use.t +++ b/t/10-use.t @@ -6,4 +6,6 @@ use-ok 'Matrix::Client::Room'; use-ok 'Matrix::Client::Requester'; use-ok 'Matrix::Response'; use-ok 'Matrix::Client::Exception'; +use-ok 'Matrix::Client::MediaStore'; + done-testing; -- cgit v1.2.3-54-g00ecf From c1851c5a391f3c72cc8de3cfb166fbdb8fee3ae5 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 17 May 2020 20:26:17 -0300 Subject: Add MediaStore tests --- t/60-media.t | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 t/60-media.t (limited to 't') diff --git a/t/60-media.t b/t/60-media.t new file mode 100644 index 0000000..602b0fc --- /dev/null +++ b/t/60-media.t @@ -0,0 +1,49 @@ +use lib 'lib'; +use Test; +use Matrix::Client::MediaStore; +use Matrix::Client::Exception; + +plan 6; + +my $path = $*TMPDIR.add('matrix-client-test'); +LEAVE { unlink $path; } +$path.spurt("") unless $path.f; + +# Mock the post-bin method of Matrix::Client::Requester. +my $media = Matrix::Client::MediaStore.new(:home-server("1234")) but role { + has %.called-with; + method post-bin(Str $path, Buf $buf, :$content-type) { + %.called-with = $path; + %.called-with = $buf; + %.called-with = $content-type; + + class { method content { '{"content_uri": "bla" }' } } + } +} + + +$media.upload($path, "something"); +is $media.called-with, '/upload?filename=something', "Send filename pass by parameter"; + +$media.upload($path, "something with spaces"); +is $media.called-with, '/upload?filename=something%20with%20spaces', "Escaped filename"; + +$media.upload($path); +is $media.called-with, "/upload?filename={$path.basename}", "Send file basename if not set"; +is $media.called-with, 'image/png', "By default use `image/png` MIME"; + +$media.upload($path, content-type => 'application/pdf'); +is $media.called-with, 'application/pdf', "Can change the content type"; + +subtest { + plan 2; + my $media = Matrix::Client::MediaStore.new(:home-server("1234")); + is $media.parse-mxc("mxc://matrix.org/123456.pdf"), { + server-name => "matrix.org", media-id => "123456.pdf" + }, 'Simple parsing'; + + throws-like { + $media.parse-mxc("http://matrix.org/123456.pdf") + }, X::Matrix::MXCParse, 'Dies with HTTP URI'; + +}, 'mxc parsing'; -- cgit v1.2.3-54-g00ecf From 80d50c14866f6e58ee57792a383920a7e8a1274a Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 6 Sep 2020 19:38:12 -0300 Subject: Add media store config method --- lib/Matrix/Client/MediaStore.rakumod | 10 ++++-- lib/Matrix/Response.pm6 | 9 ++++- t/40-response.t | 44 +++++++++++++++-------- t/60-media.t | 69 ++++++++++++++++++++++++------------ 4 files changed, 92 insertions(+), 40 deletions(-) (limited to 't') diff --git a/lib/Matrix/Client/MediaStore.rakumod b/lib/Matrix/Client/MediaStore.rakumod index 7086083..7afa9e3 100644 --- a/lib/Matrix/Client/MediaStore.rakumod +++ b/lib/Matrix/Client/MediaStore.rakumod @@ -1,7 +1,9 @@ use JSON::Fast; +use URI::Escape; + use Matrix::Client::Requester; use Matrix::Client::Exception; -use URI::Escape; +use Matrix::Response; unit class Matrix::Client::MediaStore does Matrix::Client::Requester; @@ -16,7 +18,6 @@ submethod TWEAK { $!client-endpoint = "/_matrix/media/r0"; } -# https://matrix.deprecated.org/_matrix/media/r0/thumbnail/matrix.org/TKTUVTAazFocrTjezhiXZiIe?width=25&height=25&method=crop method parse-mxc(Str $uri) { if $uri ~~ m/"mxc://" $ = [.*] "/" $ = [ .* ]/ { return { @@ -68,3 +69,8 @@ multi method download(Str $server-name, Str $media-id, Bool :$allow-remote = Tru content => $response.content ) } + +method config(--> Matrix::Response::MediaStore::Config) { + my $response = $.get("/config"); + Matrix::Response::MediaStore::Config.new(from-json($response.content)) +} diff --git a/lib/Matrix/Response.pm6 b/lib/Matrix/Response.pm6 index 268be84..ec93cf3 100644 --- a/lib/Matrix/Response.pm6 +++ b/lib/Matrix/Response.pm6 @@ -123,7 +123,6 @@ class Tag { } } - class Matrix::Response::Device { has Str $.device-id; has $.display-name; @@ -137,3 +136,11 @@ class Matrix::Response::Device { :last_seen_ts(:$!last-seen-ts)? ) { } } + +class Matrix::Response::MediaStore::Config { + has Int $.upload-size; + + method new(%config) { + self.bless(:upload-size(%config // Int)); + } +} diff --git a/t/40-response.t b/t/40-response.t index cc219b2..8bc3569 100644 --- a/t/40-response.t +++ b/t/40-response.t @@ -2,24 +2,38 @@ use lib 'lib'; use Test; use JSON::Fast; use Matrix::Response; -plan 7; +plan 2; -my $test-file = 'sync.json'; +subtest 'Sync', { + plan 7; + my $test-file = 'sync.json'; + my $data; -unless $test-file.IO.f { - skip-rest 'Missing sync.json to test'; - exit; -} + if $test-file.IO.f { + $data = from-json($test-file.IO.slurp); + lives-ok { Matrix::Response::Sync.new($test-file.IO.slurp) }, 'Sync.new accepts Str'; + lives-ok { Matrix::Response::Sync.new($data) }, 'Sync.new accepts Associative'; -my $data = from-json($test-file.IO.slurp); + my $res = Matrix::Response::Sync.new($data); + can-ok $res, 'joined-rooms', 'can .joined-rooms'; + can-ok $res, 'presence', 'can .presence'; -ok $data; -lives-ok { Matrix::Response::Sync.new($test-file.IO.slurp) }; -lives-ok { Matrix::Response::Sync.new($data) }; + isa-ok $res.joined-rooms, List, '.joined-rooms returns a List'; + isa-ok $res.presence, List, '.presence returns a List'; -my $res = Matrix::Response::Sync.new($data); -can-ok $res, 'joined-rooms'; -can-ok $res, 'presence'; + } else { + skip 'Missing sync.json to test', 7; + } -isa-ok $res.joined-rooms, List; -isa-ok $res.presence, List; +}; + +subtest 'MediaStore', { + plan 4; + my %config = 'm.upload.size' => 5000; + my $config-response = Matrix::Response::MediaStore::Config.new(|%config); + + isa-ok $config-response, Matrix::Response::MediaStore::Config; + can-ok $config-response, 'upload-size'; + isa-ok $config-response.upload-size, Int; + is $config-response.upload-size, %config, 'correct upload size'; +}; diff --git a/t/60-media.t b/t/60-media.t index 602b0fc..5601d8a 100644 --- a/t/60-media.t +++ b/t/60-media.t @@ -3,39 +3,40 @@ use Test; use Matrix::Client::MediaStore; use Matrix::Client::Exception; -plan 6; - my $path = $*TMPDIR.add('matrix-client-test'); LEAVE { unlink $path; } $path.spurt("") unless $path.f; -# Mock the post-bin method of Matrix::Client::Requester. -my $media = Matrix::Client::MediaStore.new(:home-server("1234")) but role { - has %.called-with; - method post-bin(Str $path, Buf $buf, :$content-type) { - %.called-with = $path; - %.called-with = $buf; - %.called-with = $content-type; +subtest 'Upload', { + plan 5; + # Mock the post-bin method of Matrix::Client::Requester. + my $media = Matrix::Client::MediaStore.new(:home-server("1234")) but role { + has %.called-with; + method post-bin(Str $path, Buf $buf, :$content-type) { + %.called-with = $path; + %.called-with = $buf; + %.called-with = $content-type; - class { method content { '{"content_uri": "bla" }' } } + class { method content { '{"content_uri": "bla" }' } } + } } -} -$media.upload($path, "something"); -is $media.called-with, '/upload?filename=something', "Send filename pass by parameter"; + $media.upload($path, "something"); + is $media.called-with, '/upload?filename=something', "Send filename pass by parameter"; -$media.upload($path, "something with spaces"); -is $media.called-with, '/upload?filename=something%20with%20spaces', "Escaped filename"; + $media.upload($path, "something with spaces"); + is $media.called-with, '/upload?filename=something%20with%20spaces', "Escaped filename"; -$media.upload($path); -is $media.called-with, "/upload?filename={$path.basename}", "Send file basename if not set"; -is $media.called-with, 'image/png', "By default use `image/png` MIME"; + $media.upload($path); + is $media.called-with, "/upload?filename={$path.basename}", "Send file basename if not set"; + is $media.called-with, 'image/png', "By default use `image/png` MIME"; -$media.upload($path, content-type => 'application/pdf'); -is $media.called-with, 'application/pdf', "Can change the content type"; + $media.upload($path, content-type => 'application/pdf'); + is $media.called-with, 'application/pdf', "Can change the content type"; +} -subtest { +subtest 'mxc parsing', { plan 2; my $media = Matrix::Client::MediaStore.new(:home-server("1234")); is $media.parse-mxc("mxc://matrix.org/123456.pdf"), { @@ -46,4 +47,28 @@ subtest { $media.parse-mxc("http://matrix.org/123456.pdf") }, X::Matrix::MXCParse, 'Dies with HTTP URI'; -}, 'mxc parsing'; +}; + +subtest 'config', { + # Mock the post-bin method of Matrix::Client::Requester. + plan 5; + my $media = Matrix::Client::MediaStore.new(:home-server("1234")) but role { + method get(Str $path) { + class { method content { '{"m.upload.size": 5000 }' } } + } + } + + isa-ok $media.config, Matrix::Response::MediaStore::Config, 'Can load Associative'; + is $media.config.upload-size, 5000, 'correct upload size'; + + my $empty-media = Matrix::Client::MediaStore.new(:home-server("1234")) but role { + method get(Str $path) { + class { method content { '{}' } } + } + } + + isa-ok $empty-media.config, Matrix::Response::MediaStore::Config, 'Can load empty configuration'; + is $empty-media.config.upload-size, Int, 'Unknown upload-size'; + nok $empty-media.config.upload-size.defined, 'upload-size not defined'; + +} -- cgit v1.2.3-54-g00ecf From 346f058b08e3c0b911243f59996cd0ef635ae211 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 23 Nov 2020 19:52:11 -0300 Subject: Fix tests --- t/40-response.t | 2 +- t/60-media.t | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 't') diff --git a/t/40-response.t b/t/40-response.t index 8bc3569..c401378 100644 --- a/t/40-response.t +++ b/t/40-response.t @@ -30,7 +30,7 @@ subtest 'Sync', { subtest 'MediaStore', { plan 4; my %config = 'm.upload.size' => 5000; - my $config-response = Matrix::Response::MediaStore::Config.new(|%config); + my $config-response = Matrix::Response::MediaStore::Config.new(%config); isa-ok $config-response, Matrix::Response::MediaStore::Config; can-ok $config-response, 'upload-size'; diff --git a/t/60-media.t b/t/60-media.t index 5601d8a..2417cb1 100644 --- a/t/60-media.t +++ b/t/60-media.t @@ -7,6 +7,8 @@ my $path = $*TMPDIR.add('matrix-client-test'); LEAVE { unlink $path; } $path.spurt("") unless $path.f; +plan 3; + subtest 'Upload', { plan 5; # Mock the post-bin method of Matrix::Client::Requester. -- cgit v1.2.3-54-g00ecf