TCMS.t 5.7 KB

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