Trog-Routes-JSON.t 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Test::More;
  6. use JSON::Validator ();
  7. use JSON::MaybeXS ();
  8. use Test::MockModule qw{strict};
  9. use FindBin::libs;
  10. require_ok('Trog::Routes::JSON') or BAIL_OUT("Can't find SUT");
  11. subtest 'the change request token is a uuid, and nothing else' => sub {
  12. no warnings 'once';
  13. my ($token) = @{ $Trog::Routes::JSON::routes{'/api/auth_change_request/{token}'}{parameters} };
  14. my $jv = JSON::Validator->new->schema( $token->{schema} );
  15. my $valid = sub ($value) { return !$jv->validate($value) };
  16. ok( $valid->('5bed1745-4201-40dd-b5dd-65acc0316604'), 'a uuid is a token' );
  17. ok( !$valid->(q{}), 'nothing is not' );
  18. ok( !$valid->('x;drop table user'), 'and neither is text with a hex digit in it' );
  19. ok( !$valid->("5bed1745-4201\n"), 'nor a token with something after it' );
  20. ok( !$valid->("5bed1745-4201-40dd-b5dd-65acc0316604\n"), 'nor a uuid with a newline after it' );
  21. ok( !$valid->('a|b'), 'nor a pipe, which the class used to hold' );
  22. };
  23. subtest 'webmanifest links each icon from the theme or the stock icons' => sub {
  24. my $themes = Test::MockModule->new('Trog::Themes');
  25. $themes->redefine( themed => sub ($resource) { $resource eq 'img/icon/favicon-192.png' ? "www/themes/spec/$resource" : "www/$resource" } );
  26. my $res = Trog::Routes::JSON::webmanifest();
  27. my $manifest = JSON::MaybeXS::decode_json( join( '', @{ $res->[2] } ) );
  28. my %src = map { $_->{sizes} => $_->{src} } @{ $manifest->{icons} };
  29. is( $src{'192x192'}, '/themes/spec/img/icon/favicon-192.png', "an icon that the theme has is linked from the theme" );
  30. is( $src{'48x48'}, '/img/icon/favicon-48.png', "and one that it has not is linked from the stock icons" );
  31. is( scalar( keys %src ), 6, "every size is listed" );
  32. };
  33. done_testing();