blob: 2e2f9223f568a9a34871d2f9f1b53903de702d41 [file] [log] [blame]
Akron63d963b2019-07-05 15:35:51 +02001use Mojo::Base -strict;
2use Test::More;
3use Test::Mojo;
4use Mojo::File qw/path/;
5use Data::Dumper;
6
7
8#####################
9# Start Fake server #
10#####################
11my $mount_point = '/realapi/';
12$ENV{KALAMAR_API} = $mount_point;
13
14my $t = Test::Mojo->new('Kalamar' => {
15 Kalamar => {
Akronb7876a82019-07-18 13:09:00 +020016 plugins => ['Auth'],
17 experimental_proxy => 1
Akron63d963b2019-07-05 15:35:51 +020018 }
19});
20
21# Mount fake backend
22# Get the fixture path
23my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
24my $fake_backend = $t->app->plugin(
25 Mount => {
26 $mount_point =>
27 $fixtures_path->child('mock.pl')
28 }
29);
30# Configure fake backend
31$fake_backend->pattern->defaults->{app}->log($t->app->log);
32
33# Globally set server
34$t->app->ua->server->app($t->app);
35
Akrona130fa52019-08-07 20:12:26 +020036my $rendered = 0;
37$t->app->hook(
38 after_render => sub {
39 $rendered++;
40 }
41);
42
43$t->get_ok('/doc')
44 ->status_is(200)
45 ->text_like('title', qr!KorAP!)
46 ;
47
48is($rendered, 1);
49
Akron63d963b2019-07-05 15:35:51 +020050$t->get_ok('/realapi/v1.0')
51 ->status_is(200)
52 ->content_is('Fake server available')
53 ;
54
Akrona130fa52019-08-07 20:12:26 +020055is($rendered, 1);
56
Akron63d963b2019-07-05 15:35:51 +020057$t->get_ok('/api/v1.0/')
58 ->status_is(200)
59 ->content_is('Fake server available')
60 ;
61
Akrona130fa52019-08-07 20:12:26 +020062# Proxy renders
63is($rendered, 2);
64
Akron63d963b2019-07-05 15:35:51 +020065$t->get_ok('/api/v1.0/search?ql=cosmas3')
66 ->status_is(400)
67 ->json_is('/errors/0/0','307')
Akron56b3d0d2019-07-10 17:13:31 +020068 ->header_is('connection', 'close')
Akron63d963b2019-07-05 15:35:51 +020069 ;
70
Akrona130fa52019-08-07 20:12:26 +020071# Proxy renders
72is($rendered, 3);
73
Akron63d963b2019-07-05 15:35:51 +020074$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
75 username => 'test',
76 password => 'pass'
77})
78 ->status_is(400)
79 ->json_is('/errors/0/1','Grant Type unknown')
80 ;
81
82$t->post_ok('/api/v1.0/oauth2/token' => {} => form => {
83 grant_type => 'password',
84 client_id => 2,
85 client_secret => 'k414m4r-s3cr3t',
86 username => 'test',
87 password => 'pass'
88})
89 ->status_is(200)
90 ->json_is('/token_type', 'Bearer')
91 ;
92
93
94done_testing;
95__END__