Akron | abdf9a9 | 2021-01-12 19:06:57 +0100 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Mojo::File 'path'; |
| 3 | use Test::More; |
| 4 | use Test::Mojo; |
| 5 | |
| 6 | ##################### |
| 7 | # Start Fake server # |
| 8 | ##################### |
| 9 | my $mount_point = '/realapi/'; |
| 10 | $ENV{KALAMAR_API} = $mount_point; |
| 11 | $ENV{QUERY_REF_MOCKUP} = 1; |
| 12 | |
| 13 | # Test app |
| 14 | my $t = Test::Mojo->new('Kalamar' => { |
| 15 | Kalamar => { |
| 16 | plugins => ['Auth','QueryReference'] |
Akron | 7fb78d6 | 2021-06-10 12:51:13 +0200 | [diff] [blame] | 17 | }, |
| 18 | 'Kalamar-Auth' => { |
| 19 | client_id => 2, |
| 20 | client_secret => 'k414m4r-s3cr3t', |
| 21 | oauth2 => 1 |
Akron | abdf9a9 | 2021-01-12 19:06:57 +0100 | [diff] [blame] | 22 | } |
| 23 | }); |
| 24 | |
| 25 | # Mount fake backend |
| 26 | # Get the fixture path |
| 27 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, '..', 'server'); |
| 28 | my $fake_backend = $t->app->plugin( |
| 29 | Mount => { |
| 30 | $mount_point => |
| 31 | $fixtures_path->child('mock.pl') |
| 32 | } |
| 33 | ); |
| 34 | # Configure fake backend |
| 35 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 36 | |
| 37 | $t->put_ok('/query/baum') |
| 38 | ->status_is(400) |
| 39 | ->json_like('/errors/0/message', qr!unable!i) |
| 40 | ; |
| 41 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 42 | ->status_is(201) |
| 43 | ->content_is('') |
| 44 | ; |
| 45 | $t->put_ok('/query/frage?q=[orth=Frage]&desc=Eine Frage-Query&ql=poliqarp') |
| 46 | ->status_is(201) |
| 47 | ->content_is('') |
| 48 | ; |
| 49 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 50 | ->status_is(400) |
| 51 | ->json_like('/errors/0/message', qr!unable!i) |
| 52 | ; |
| 53 | |
| 54 | $t->get_ok('/query') |
| 55 | ->status_is(200) |
| 56 | ->json_is('/0/name', 'baum') |
| 57 | ->json_is('/1/name', 'frage') |
| 58 | ; |
| 59 | |
| 60 | $t->get_ok('/query/frage') |
| 61 | ->status_is(200) |
| 62 | ->json_is('/name', 'frage') |
| 63 | ->json_is('/description', 'Eine Frage-Query') |
| 64 | ->json_is('/koralQuery', '[orth=Frage]') |
| 65 | ->json_is('/q', '[orth=Frage]') |
| 66 | ->json_is('/ql', 'poliqarp') |
| 67 | ; |
| 68 | $t->delete_ok('/query/frage') |
| 69 | ->status_is(200) |
| 70 | ->content_is('') |
| 71 | ; |
| 72 | $t->delete_ok('/query/frage2') |
| 73 | ->status_is(200) |
| 74 | ->content_is('') |
| 75 | ; |
| 76 | $t->get_ok('/query') |
| 77 | ->status_is(200) |
| 78 | ->json_is('/0/name', 'baum') |
| 79 | ->json_hasnt('/1') |
| 80 | ; |
| 81 | $t->get_ok('/query/frage') |
| 82 | ->status_is(404) |
| 83 | ->json_is('/errors/0/message', 'Query reference not found') |
| 84 | ; |
| 85 | $t->get_ok('/query/baum') |
| 86 | ->status_is(200) |
| 87 | ->json_is('/name', 'baum') |
| 88 | ->json_is('/description', 'Eine Baum-Query') |
| 89 | ->json_is('/koralQuery', '[orth=Baum]') |
| 90 | ->json_is('/q', '[orth=Baum]') |
| 91 | ->json_is('/ql', 'poliqarp') |
| 92 | ; |
| 93 | |
| 94 | ## REALAPI |
| 95 | $ENV{QUERY_REF_MOCKUP} = 0; |
| 96 | |
| 97 | $t->get_ok('/realapi/v1.0') |
| 98 | ->status_is(200) |
| 99 | ->content_is('Fake server available') |
| 100 | ; |
| 101 | |
| 102 | # Login |
| 103 | my $csrf = $t->get_ok('/') |
| 104 | ->status_is(200) |
| 105 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 106 | ; |
| 107 | |
| 108 | $t->post_ok('/user/login' => form => { |
Akron | 9fa7cc5 | 2022-05-12 11:17:20 +0200 | [diff] [blame] | 109 | handle_or_email => 'test', |
Akron | abdf9a9 | 2021-01-12 19:06:57 +0100 | [diff] [blame] | 110 | pwd => 'pass', |
| 111 | csrf_token => $csrf |
| 112 | }) |
| 113 | ->status_is(302) |
| 114 | ->content_is('') |
| 115 | ->header_is('Location' => '/'); |
| 116 | |
| 117 | $t->put_ok('/query/baum') |
| 118 | ->status_is(400) |
| 119 | ->json_like('/errors/0/message', qr!unable!i) |
| 120 | ; |
| 121 | |
| 122 | $t->put_ok('/query/baum?q=[orth=Baum]') |
| 123 | ->status_is(201) |
| 124 | ->content_is('') |
| 125 | ; |
| 126 | |
| 127 | $t->put_ok('/query/frage?q=[orth=Frage]&desc=Eine Frage-Query&ql=poliqarp') |
| 128 | ->status_is(201) |
| 129 | ->content_is('') |
| 130 | ; |
| 131 | |
| 132 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 133 | ->status_is(400) |
| 134 | ->json_like('/errors/0/message', qr!unable!i) |
| 135 | ; |
| 136 | |
| 137 | $t->get_ok('/query') |
| 138 | ->status_is(200) |
| 139 | ->json_is('/refs/0/name', 'baum') |
| 140 | ->json_is('/refs/1/name', 'frage') |
| 141 | ; |
| 142 | |
| 143 | $t->get_ok('/query/frage') |
| 144 | ->status_is(200) |
| 145 | ->json_is('/description', 'Eine Frage-Query') |
| 146 | ->json_is('/type', 'PRIVATE') |
| 147 | ->json_is('/queryType', 'QUERY') |
| 148 | ->json_is('/query', '[orth=Frage]') |
| 149 | ->json_is('/queryLanguage', 'poliqarp') |
| 150 | ->json_is('/koralQuery/@type', 'Okay') |
| 151 | ; |
| 152 | |
| 153 | $t->delete_ok('/query/frage') |
| 154 | ->status_is(200) |
| 155 | ->content_is('') |
| 156 | ; |
| 157 | |
| 158 | $t->delete_ok('/query/frage2') |
| 159 | ->status_is(200) |
| 160 | ->content_is('') |
| 161 | ; |
| 162 | |
| 163 | $t->get_ok('/query') |
| 164 | ->status_is(200) |
| 165 | ->json_is('/refs/0/name', 'baum') |
| 166 | ->json_hasnt('/1') |
| 167 | ; |
| 168 | |
| 169 | $t->get_ok('/query/frage') |
| 170 | ->status_is(404) |
| 171 | ->json_is('/errors/0/message', 'Query reference not found') |
| 172 | ; |
| 173 | |
| 174 | $t->get_ok('/query/baum') |
| 175 | ->status_is(200) |
| 176 | ->json_is('/name', 'baum') |
| 177 | ->json_hasnt('/description') |
| 178 | ->json_is('/koralQuery/@type', 'Okay') |
| 179 | ->json_is('/query', '[orth=Baum]') |
| 180 | ->json_is('/queryLanguage', 'poliqarp') |
| 181 | ; |
| 182 | |
| 183 | |
| 184 | done_testing(); |