Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::More; |
| 3 | use Test::Mojo; |
| 4 | use Mojo::File qw/path/; |
| 5 | use Data::Dumper; |
| 6 | |
| 7 | |
| 8 | ##################### |
| 9 | # Start Fake server # |
| 10 | ##################### |
| 11 | my $mount_point = '/realapi/'; |
| 12 | $ENV{KALAMAR_API} = $mount_point; |
| 13 | |
| 14 | my $t = Test::Mojo->new('Kalamar' => { |
| 15 | Kalamar => { |
| 16 | plugins => ['Auth'] |
| 17 | } |
| 18 | }); |
| 19 | |
| 20 | # Mount fake backend |
| 21 | # Get the fixture path |
| 22 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server'); |
| 23 | my $fake_backend = $t->app->plugin( |
| 24 | Mount => { |
| 25 | $mount_point => |
| 26 | $fixtures_path->child('mock.pl') |
| 27 | } |
| 28 | ); |
| 29 | # Configure fake backend |
| 30 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 31 | |
| 32 | # Globally set server |
| 33 | $t->app->ua->server->app($t->app); |
| 34 | |
| 35 | my $r = $t->app->routes; |
| 36 | |
| 37 | # API proxy route |
| 38 | $r->any('/api/v#apiv' => [apiv => ['1.0']])->to('Proxy#pass'); |
| 39 | $r->any('/api/v#apiv/*path' => [apiv => ['1.0']])->to('Proxy#pass'); |
| 40 | |
| 41 | $t->get_ok('/realapi/v1.0') |
| 42 | ->status_is(200) |
| 43 | ->content_is('Fake server available') |
| 44 | ; |
| 45 | |
| 46 | $t->get_ok('/api/v1.0/') |
| 47 | ->status_is(200) |
| 48 | ->content_is('Fake server available') |
| 49 | ; |
| 50 | |
| 51 | $t->get_ok('/api/v1.0/search?ql=cosmas3') |
| 52 | ->status_is(400) |
| 53 | ->json_is('/errors/0/0','307') |
| 54 | ; |
| 55 | |
| 56 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 57 | username => 'test', |
| 58 | password => 'pass' |
| 59 | }) |
| 60 | ->status_is(400) |
| 61 | ->json_is('/errors/0/1','Grant Type unknown') |
| 62 | ; |
| 63 | |
| 64 | $t->post_ok('/api/v1.0/oauth2/token' => {} => form => { |
| 65 | grant_type => 'password', |
| 66 | client_id => 2, |
| 67 | client_secret => 'k414m4r-s3cr3t', |
| 68 | username => 'test', |
| 69 | password => 'pass' |
| 70 | }) |
| 71 | ->status_is(200) |
| 72 | ->json_is('/token_type', 'Bearer') |
| 73 | ; |
| 74 | |
| 75 | |
| 76 | done_testing; |
| 77 | __END__ |