TCMS.t 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # It answers with where the request would be sent on, when it says.
  51. $html->redefine( posts => sub ($query) { return [ 200, [], [ exists $query->{to} ? $query->{to} : 'the post' ] ] } );
  52. require_ok('TCMS') or BAIL_OUT("Can't load the dispatcher");
  53. my %openapi = do { no warnings 'once'; %TCMS::openapi };
  54. # Checked as JSON reads it, since tPSGI is not a prerequisite of tCMS: perl's
  55. # booleans become JSON's, and a callback becomes null.
  56. my $json = JSON::MaybeXS->new( allow_unknown => 1, canonical => 1 );
  57. my $schema = JSON::Validator::Schema::OpenAPIv3->new( $json->decode( $json->encode( \%openapi ) ) );
  58. my $paths = $openapi{paths};
  59. subtest 'the document' => sub {
  60. is_deeply( $schema->errors, [], 'is valid OpenAPI 3' ) or diag explain $schema->errors;
  61. ok( !exists $paths->{default}, 'and has no path for the setup route, which is not one' );
  62. ok( !( grep { m/[(]/ } keys(%$paths) ), 'nor any regex left' ) or diag explain [ grep { m/[(]/ } keys(%$paths) ];
  63. };
  64. subtest 'an operation' => sub {
  65. my $manual = $paths->{'/lib/{module+}'}{get};
  66. ok( $manual, 'the manual of a module is a GET of /lib/{module+}' );
  67. is( ref $manual->{'x-tpsgi-callback'}, 'CODE', 'with a callback' );
  68. is( $manual->{'x-tcms-auth'}, 1, 'and its auth as x-tcms-auth' );
  69. is_deeply(
  70. $manual->{parameters},
  71. [ { name => 'module+', in => 'path', required => true, schema => { type => 'string' } } ],
  72. 'and each variable of the template as a required path parameter'
  73. );
  74. my $totp = $paths->{'/totp'}{get};
  75. is( $totp->{'x-tcms-totp_exempt'}, 1, 'totp_exempt is x-tcms-totp_exempt' );
  76. is_deeply( $paths->{'/sitemap/{map}.xml'}{get}{'x-tpsgi-data'}, { xml => 1 }, 'and data is x-tpsgi-data' );
  77. ok( $paths->{'/auth'}{post} && !$paths->{'/auth'}{get}, 'the method is the key of the operation' );
  78. };
  79. subtest 'the routes of posts' => sub {
  80. is( ref $paths->{'/posts/p-one'}{get}{'x-tpsgi-callback'}, 'CODE', 'a post named by its callback is routed' );
  81. ok( $paths->{'/secure/posts/p-one'}{get}, 'and so is its /secure copy' );
  82. # The flag that says setup is done, or the default route answers instead.
  83. open( my $fh, '>', 'config/setup' ) or die $!;
  84. close($fh) or die $!;
  85. 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} } );
  86. is_deeply( $res->[2], ['the post'], 'and it runs the sub that the name names' );
  87. ok( !exists $paths->{'/posts/{curly}'}, 'but a post whose path has a brace is not, since it would be a template' );
  88. };
  89. subtest 'where a redirect may send you' => sub {
  90. my $operation = $paths->{'/posts/p-one'}{get};
  91. my $to = sub ($to) {
  92. return $operation->{'x-tpsgi-callback'}->( { log_dir => "$ROOT/logs", verbose => 0 }, { route => '/posts/p-one', dispatcher => $operation, to => $to } )->[2][0];
  93. };
  94. is( $to->('/config'), '/config', 'a path on this site is kept' );
  95. is( $to->('https://bogus.test/x'), '/x', 'the path of another site is all that is kept of it' );
  96. is( $to->('https://bogus.test'), '/', 'and another site with no path is the root, not the other site' );
  97. is( $to->('//bogus.test'), '/', 'even without a scheme' );
  98. is( $to->('////bogus.test'), '/bogus.test', 'and a path that starts with two slashes, which names a site, starts with one' );
  99. };
  100. subtest 'the scripts of a route' => sub {
  101. my $post = $paths->{'/posts/p-one'}{get};
  102. is_deeply( $post->{'x-tcms-scripts'}, [qw{highlight.min.js reveal.min.js}], 'a post page has the scripts of one' );
  103. is_deeply( $post->{'x-tcms-authenticated_scripts'}, [qw{post.js post_relations.js}], 'and the editing scripts for a reader who is logged in' );
  104. is_deeply( $paths->{'/secure/posts/p-one'}{get}{'x-tcms-scripts'}, $post->{'x-tcms-scripts'}, 'and so does its /secure copy' );
  105. is_deeply( $paths->{'/metrics'}{get}{'x-tcms-scripts'}, ['chart.min.js'], 'a route that names its own has those' );
  106. ok( !exists $paths->{'/metrics'}{get}{'x-tcms-authenticated_scripts'}, 'and no others' );
  107. ok( !exists $paths->{'/login'}{get}{'x-tcms-scripts'}, 'a route that names none has none' );
  108. no warnings qw{once};
  109. my %declared = %Trog::Bundle::declared;
  110. ok( $declared{'highlight.min+reveal.min'}, 'the bundle of a post page is declared at startup' );
  111. ok( $declared{'highlight.min+reveal.min+post+post_relations'}, 'and the one for a reader who is logged in' );
  112. ok( $declared{'chart.min'}, 'and the bundle of every other route' );
  113. ok( !$declared{'reveal.min+highlight.min'}, 'but no other list' );
  114. };
  115. subtest 'validation' => sub {
  116. my $token = '0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0';
  117. is_deeply( [ $schema->validate_request( [ get => '/api/auth_change_request/{token}' ], { path => { token => $token } } ) ], [], 'a token that is a UUID is taken' );
  118. ok( scalar $schema->validate_request( [ get => '/api/auth_change_request/{token}' ], { path => { token => "$token|x" } } ), 'and one with more after it is not' );
  119. is_deeply( [ $schema->validate_request( [ get => '/api/requests_per' ], { query => { period => 'week', num_periods => 2 } } ) ], [], 'a period that requests_per knows is taken' );
  120. ok( scalar $schema->validate_request( [ get => '/api/requests_per' ], { query => { period => 'fortnight' } } ), 'and one it does not is refused' );
  121. ok( scalar $schema->validate_request( [ get => '/api/requests_per' ], { query => { num_periods => 'many' } } ), 'as is a count that is not a number' );
  122. };
  123. subtest 'robots' => sub {
  124. my $banned;
  125. my $renderer = Test::MockModule->new('Trog::Renderer');
  126. $renderer->redefine( render => sub { my ( $class, %args ) = @_; $banned = $args{data}{banned}; return [ 200, [], [''] ] } );
  127. TCMS::robots( { domain => 'routes.test.test' } );
  128. ok( ( grep { $_ eq '/avatar/*' } @$banned ), 'a template with no robot_name is disallowed with * for its variable' );
  129. ok( ( grep { $_ eq '/guest/reprovision/log/' } @$banned ), 'and one with a robot_name by that' );
  130. ok( !( grep { m/[{}]/ } @$banned ), 'and no template is written as it is' );
  131. };
  132. done_testing();