TCMS.t 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env perl
  2. # The OpenAPI document that TCMS.pm hands tPSGI, made from tCMS's own routing
  3. # table.
  4. use 5.040;
  5. use re '/aasx';
  6. use warnings FATAL => 'all';
  7. use FindBin;
  8. use FindBin::libs;
  9. use Test::More;
  10. use Test::MockModule qw{strict};
  11. use JSON::MaybeXS ();
  12. use JSON::Validator::Schema::OpenAPIv3 ();
  13. our ( $REPO, $ROOT, $OLDCWD );
  14. BEGIN {
  15. require Cwd;
  16. require File::Copy;
  17. require File::Path;
  18. require File::Temp;
  19. $REPO = Cwd::abs_path("$FindBin::Bin/..");
  20. $OLDCWD = Cwd::getcwd();
  21. $ROOT = File::Temp::tempdir( 'tcms-routes-XXXXXX', TMPDIR => 1, CLEANUP => 1 );
  22. # TCMS.pm builds its document at file scope, against whatever cwd is
  23. # current when it loads, and every path in tCMS is relative to that.
  24. $ENV{HOME} = $ROOT;
  25. File::Path::make_path( map { "$ROOT/$_" } qw{config schema data/files logs totp www/statics www/themes} );
  26. File::Copy::copy( "$REPO/schema/$_", "$ROOT/schema/$_" ) or die $! for qw{auth.schema flatfile.schema log.schema};
  27. open( my $fh, '>', "$ROOT/config/default.cfg" ) or die $!;
  28. print {$fh} "[general]\n data_model=FlatFile\n hostname=routes.test.test\n";
  29. close($fh) or die $!;
  30. chdir($ROOT) or die "could not chdir to the sandbox: $!";
  31. }
  32. END {
  33. chdir($OLDCWD) if $OLDCWD;
  34. }
  35. # The routes of posts, as the tag index of a FlatFile site gives them: by the
  36. # name of their callback.
  37. require_ok('Trog::SQLite::TagIndex') or BAIL_OUT("Can't load the tag index");
  38. my $index = Test::MockModule->new('Trog::SQLite::TagIndex');
  39. $index->redefine(
  40. routes => sub {
  41. return (
  42. '/posts/p-one' => { method => 'GET', callback => 'Trog::Routes::HTML::posts' },
  43. '/posts/{curly}' => { method => 'GET', callback => 'Trog::Routes::HTML::posts' },
  44. );
  45. }
  46. );
  47. # What a post's route runs, which the routes of posts name.
  48. require_ok('Trog::Routes::HTML') or BAIL_OUT("Can't load the routes");
  49. my $html = Test::MockModule->new('Trog::Routes::HTML');
  50. $html->redefine( posts => sub { return [ 200, [], ['the post'] ] } );
  51. require_ok('TCMS') or BAIL_OUT("Can't load the dispatcher");
  52. my %openapi = do { no warnings 'once'; %TCMS::openapi };
  53. # Checked as JSON reads it, since tPSGI is not a prerequisite of tCMS: perl's
  54. # booleans become JSON's, and a callback becomes null.
  55. my $json = JSON::MaybeXS->new( allow_unknown => 1, canonical => 1 );
  56. my $schema = JSON::Validator::Schema::OpenAPIv3->new( $json->decode( $json->encode( \%openapi ) ) );
  57. my $paths = $openapi{paths};
  58. subtest 'the document' => sub {
  59. is_deeply( $schema->errors, [], 'is valid OpenAPI 3' ) or diag explain $schema->errors;
  60. ok( !exists $paths->{default}, 'and has no path for the setup route, which is not one' );
  61. ok( !( grep { m/[(]/ } keys(%$paths) ), 'nor any regex left' ) or diag explain [ grep { m/[(]/ } keys(%$paths) ];
  62. };
  63. subtest 'an operation' => sub {
  64. my $manual = $paths->{'/lib/{module+}'}{get};
  65. ok( $manual, 'the manual of a module is a GET of /lib/{module+}' );
  66. is( ref $manual->{'x-tpsgi-callback'}, 'CODE', 'with a callback' );
  67. is( $manual->{'x-tcms-auth'}, 1, 'and its auth as x-tcms-auth' );
  68. is_deeply(
  69. $manual->{parameters},
  70. [ { name => 'module+', in => 'path', required => true, schema => { type => 'string' } } ],
  71. 'and each variable of the template as a required path parameter'
  72. );
  73. my $totp = $paths->{'/totp'}{get};
  74. is( $totp->{'x-tcms-totp_exempt'}, 1, 'totp_exempt is x-tcms-totp_exempt' );
  75. is_deeply( $paths->{'/sitemap/{map}.xml'}{get}{'x-tpsgi-data'}, { xml => 1 }, 'and data is x-tpsgi-data' );
  76. ok( $paths->{'/auth'}{post} && !$paths->{'/auth'}{get}, 'the method is the key of the operation' );
  77. };
  78. subtest 'the routes of posts' => sub {
  79. is( ref $paths->{'/posts/p-one'}{get}{'x-tpsgi-callback'}, 'CODE', 'a post named by its callback is routed' );
  80. ok( $paths->{'/secure/posts/p-one'}{get}, 'and so is its /secure copy' );
  81. # The flag that says setup is done, or the default route answers instead.
  82. open( my $fh, '>', 'config/setup' ) or die $!;
  83. close($fh) or die $!;
  84. my $res = $paths->{'/posts/p-one'}{get}{'x-tpsgi-callback'}->( { log_dir => "$ROOT/logs", verbose => 0 }, { route => '/posts/p-one', dispatcher => $paths->{'/posts/p-one'}{get} } );
  85. is_deeply( $res->[2], ['the post'], 'and it runs the sub that the name names' );
  86. ok( !exists $paths->{'/posts/{curly}'}, 'but a post whose path has a brace is not, since it would be a template' );
  87. };
  88. subtest 'the scripts of a route' => sub {
  89. my $post = $paths->{'/posts/p-one'}{get};
  90. is_deeply( $post->{'x-tcms-scripts'}, [qw{highlight.min.js reveal.min.js}], 'a post page has the scripts of one' );
  91. is_deeply( $post->{'x-tcms-authenticated_scripts'}, [qw{post.js post_relations.js}], 'and the editing scripts for a reader who is logged in' );
  92. is_deeply( $paths->{'/secure/posts/p-one'}{get}{'x-tcms-scripts'}, $post->{'x-tcms-scripts'}, 'and so does its /secure copy' );
  93. is_deeply( $paths->{'/metrics'}{get}{'x-tcms-scripts'}, ['chart.min.js'], 'a route that names its own has those' );
  94. ok( !exists $paths->{'/metrics'}{get}{'x-tcms-authenticated_scripts'}, 'and no others' );
  95. ok( !exists $paths->{'/login'}{get}{'x-tcms-scripts'}, 'a route that names none has none' );
  96. no warnings qw{once};
  97. my %declared = %Trog::Bundle::declared;
  98. ok( $declared{'highlight.min+reveal.min'}, 'the bundle of a post page is declared at startup' );
  99. ok( $declared{'highlight.min+reveal.min+post+post_relations'}, 'and the one for a reader who is logged in' );
  100. ok( $declared{'chart.min'}, 'and the bundle of every other route' );
  101. ok( !$declared{'reveal.min+highlight.min'}, 'but no other list' );
  102. };
  103. subtest 'validation' => sub {
  104. my $token = '0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0';
  105. is_deeply( [ $schema->validate_request( [ get => '/api/auth_change_request/{token}' ], { path => { token => $token } } ) ], [], 'a token that is a UUID is taken' );
  106. ok( scalar $schema->validate_request( [ get => '/api/auth_change_request/{token}' ], { path => { token => "$token|x" } } ), 'and one with more after it is not' );
  107. is_deeply( [ $schema->validate_request( [ get => '/api/requests_per' ], { query => { period => 'week', num_periods => 2 } } ) ], [], 'a period that requests_per knows is taken' );
  108. ok( scalar $schema->validate_request( [ get => '/api/requests_per' ], { query => { period => 'fortnight' } } ), 'and one it does not is refused' );
  109. ok( scalar $schema->validate_request( [ get => '/api/requests_per' ], { query => { num_periods => 'many' } } ), 'as is a count that is not a number' );
  110. };
  111. subtest 'robots' => sub {
  112. my $banned;
  113. my $renderer = Test::MockModule->new('Trog::Renderer');
  114. $renderer->redefine( render => sub { my ( $class, %args ) = @_; $banned = $args{data}{banned}; return [ 200, [], [''] ] } );
  115. TCMS::robots( { domain => 'routes.test.test' } );
  116. ok( ( grep { $_ eq '/avatar/*' } @$banned ), 'a template with no robot_name is disallowed with * for its variable' );
  117. ok( ( grep { $_ eq '/guest/reprovision/log/' } @$banned ), 'and one with a robot_name by that' );
  118. ok( !( grep { m/[{}]/ } @$banned ), 'and no template is written as it is' );
  119. };
  120. done_testing();