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'] |
| 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 | $t->put_ok('/query/baum') |
| 33 | ->status_is(400) |
| 34 | ->json_like('/errors/0/message', qr!unable!i) |
| 35 | ; |
| 36 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 37 | ->status_is(201) |
| 38 | ->content_is('') |
| 39 | ; |
| 40 | $t->put_ok('/query/frage?q=[orth=Frage]&desc=Eine Frage-Query&ql=poliqarp') |
| 41 | ->status_is(201) |
| 42 | ->content_is('') |
| 43 | ; |
| 44 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 45 | ->status_is(400) |
| 46 | ->json_like('/errors/0/message', qr!unable!i) |
| 47 | ; |
| 48 | |
| 49 | $t->get_ok('/query') |
| 50 | ->status_is(200) |
| 51 | ->json_is('/0/name', 'baum') |
| 52 | ->json_is('/1/name', 'frage') |
| 53 | ; |
| 54 | |
| 55 | $t->get_ok('/query/frage') |
| 56 | ->status_is(200) |
| 57 | ->json_is('/name', 'frage') |
| 58 | ->json_is('/description', 'Eine Frage-Query') |
| 59 | ->json_is('/koralQuery', '[orth=Frage]') |
| 60 | ->json_is('/q', '[orth=Frage]') |
| 61 | ->json_is('/ql', 'poliqarp') |
| 62 | ; |
| 63 | $t->delete_ok('/query/frage') |
| 64 | ->status_is(200) |
| 65 | ->content_is('') |
| 66 | ; |
| 67 | $t->delete_ok('/query/frage2') |
| 68 | ->status_is(200) |
| 69 | ->content_is('') |
| 70 | ; |
| 71 | $t->get_ok('/query') |
| 72 | ->status_is(200) |
| 73 | ->json_is('/0/name', 'baum') |
| 74 | ->json_hasnt('/1') |
| 75 | ; |
| 76 | $t->get_ok('/query/frage') |
| 77 | ->status_is(404) |
| 78 | ->json_is('/errors/0/message', 'Query reference not found') |
| 79 | ; |
| 80 | $t->get_ok('/query/baum') |
| 81 | ->status_is(200) |
| 82 | ->json_is('/name', 'baum') |
| 83 | ->json_is('/description', 'Eine Baum-Query') |
| 84 | ->json_is('/koralQuery', '[orth=Baum]') |
| 85 | ->json_is('/q', '[orth=Baum]') |
| 86 | ->json_is('/ql', 'poliqarp') |
| 87 | ; |
| 88 | |
| 89 | ## REALAPI |
| 90 | $ENV{QUERY_REF_MOCKUP} = 0; |
| 91 | |
| 92 | $t->get_ok('/realapi/v1.0') |
| 93 | ->status_is(200) |
| 94 | ->content_is('Fake server available') |
| 95 | ; |
| 96 | |
| 97 | # Login |
| 98 | my $csrf = $t->get_ok('/') |
| 99 | ->status_is(200) |
| 100 | ->tx->res->dom->at('input[name=csrf_token]')->attr('value') |
| 101 | ; |
| 102 | |
| 103 | $t->post_ok('/user/login' => form => { |
| 104 | handle => 'test', |
| 105 | pwd => 'pass', |
| 106 | csrf_token => $csrf |
| 107 | }) |
| 108 | ->status_is(302) |
| 109 | ->content_is('') |
| 110 | ->header_is('Location' => '/'); |
| 111 | |
| 112 | $t->put_ok('/query/baum') |
| 113 | ->status_is(400) |
| 114 | ->json_like('/errors/0/message', qr!unable!i) |
| 115 | ; |
| 116 | |
| 117 | $t->put_ok('/query/baum?q=[orth=Baum]') |
| 118 | ->status_is(201) |
| 119 | ->content_is('') |
| 120 | ; |
| 121 | |
| 122 | $t->put_ok('/query/frage?q=[orth=Frage]&desc=Eine Frage-Query&ql=poliqarp') |
| 123 | ->status_is(201) |
| 124 | ->content_is('') |
| 125 | ; |
| 126 | |
| 127 | $t->put_ok('/query/baum?q=[orth=Baum]&desc=Eine Baum-Query') |
| 128 | ->status_is(400) |
| 129 | ->json_like('/errors/0/message', qr!unable!i) |
| 130 | ; |
| 131 | |
| 132 | $t->get_ok('/query') |
| 133 | ->status_is(200) |
| 134 | ->json_is('/refs/0/name', 'baum') |
| 135 | ->json_is('/refs/1/name', 'frage') |
| 136 | ; |
| 137 | |
| 138 | $t->get_ok('/query/frage') |
| 139 | ->status_is(200) |
| 140 | ->json_is('/description', 'Eine Frage-Query') |
| 141 | ->json_is('/type', 'PRIVATE') |
| 142 | ->json_is('/queryType', 'QUERY') |
| 143 | ->json_is('/query', '[orth=Frage]') |
| 144 | ->json_is('/queryLanguage', 'poliqarp') |
| 145 | ->json_is('/koralQuery/@type', 'Okay') |
| 146 | ; |
| 147 | |
| 148 | $t->delete_ok('/query/frage') |
| 149 | ->status_is(200) |
| 150 | ->content_is('') |
| 151 | ; |
| 152 | |
| 153 | $t->delete_ok('/query/frage2') |
| 154 | ->status_is(200) |
| 155 | ->content_is('') |
| 156 | ; |
| 157 | |
| 158 | $t->get_ok('/query') |
| 159 | ->status_is(200) |
| 160 | ->json_is('/refs/0/name', 'baum') |
| 161 | ->json_hasnt('/1') |
| 162 | ; |
| 163 | |
| 164 | $t->get_ok('/query/frage') |
| 165 | ->status_is(404) |
| 166 | ->json_is('/errors/0/message', 'Query reference not found') |
| 167 | ; |
| 168 | |
| 169 | $t->get_ok('/query/baum') |
| 170 | ->status_is(200) |
| 171 | ->json_is('/name', 'baum') |
| 172 | ->json_hasnt('/description') |
| 173 | ->json_is('/koralQuery/@type', 'Okay') |
| 174 | ->json_is('/query', '[orth=Baum]') |
| 175 | ->json_is('/queryLanguage', 'poliqarp') |
| 176 | ; |
| 177 | |
| 178 | |
| 179 | done_testing(); |