|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +"""Test authorisation and authentication HTTP response codes.""" |
| 17 | + |
| 18 | +from functools import partial |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from tornado.httpclient import HTTPClientError |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def jp_server_config(jp_template_dir): |
| 27 | + """Config to turn the CylcUIServer extension on.""" |
| 28 | + config = { |
| 29 | + "ServerApp": { |
| 30 | + "jpserver_extensions": { |
| 31 | + 'cylc.uiserver': True |
| 32 | + }, |
| 33 | + } |
| 34 | + } |
| 35 | + return config |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def patch_conf_files(monkeypatch): |
| 40 | + """Auto-patches the CylcUIServer to prevent it loading config files.""" |
| 41 | + monkeypatch.setattr( |
| 42 | + 'cylc.uiserver.app.CylcUIServer.config_file_paths', [] |
| 43 | + ) |
| 44 | + yield |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.integration |
| 48 | +async def test_jp_handler(patch_conf_files, jp_fetch): |
| 49 | + resp = await jp_fetch( |
| 50 | + '/', method='GET' |
| 51 | + ) |
| 52 | + assert resp.code == 200 |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.integration |
| 56 | +async def test_cylc_handler(patch_conf_files, jp_fetch): |
| 57 | + resp = await jp_fetch( |
| 58 | + 'cylc', 'userprofile', method='GET' |
| 59 | + ) |
| 60 | + assert resp.code == 200 |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.integration |
| 64 | +@pytest.mark.usefixtures("mock_authentication") |
| 65 | +@pytest.mark.parametrize( |
| 66 | + 'endpoint,code,message,body', |
| 67 | + [ |
| 68 | + pytest.param( |
| 69 | + ('cylc', 'graphql'), |
| 70 | + 400, |
| 71 | + None, |
| 72 | + b'Must provide query string', |
| 73 | + id='cylc/graphql', |
| 74 | + ), |
| 75 | + pytest.param( |
| 76 | + ('cylc', 'subscriptions'), |
| 77 | + 400, |
| 78 | + None, |
| 79 | + b'WebSocket', |
| 80 | + id='cylc/subscriptions', |
| 81 | + ), |
| 82 | + pytest.param( |
| 83 | + ('cylc', 'userprofile'), |
| 84 | + 200, |
| 85 | + None, |
| 86 | + b'"name":', |
| 87 | + id='cylc/userprofile', |
| 88 | + ) |
| 89 | + ] |
| 90 | +) |
| 91 | +async def test_authorised_and_authenticated( |
| 92 | + patch_conf_files, |
| 93 | + jp_fetch, |
| 94 | + endpoint, |
| 95 | + code, |
| 96 | + message, |
| 97 | + body |
| 98 | +): |
| 99 | + await _test(jp_fetch, endpoint, code, message, body) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.integration |
| 103 | +@pytest.mark.usefixtures("mock_authentication_none") |
| 104 | +@pytest.mark.parametrize( |
| 105 | + 'endpoint,code,message,body', |
| 106 | + [ |
| 107 | + # pytest.param( |
| 108 | + # ('cylc', 'graphql'), |
| 109 | + # 403, |
| 110 | + # 'Forbidden', |
| 111 | + # None, |
| 112 | + # id='cylc/graphql', |
| 113 | + # ), |
| 114 | + pytest.param( |
| 115 | + ('cylc', 'subscriptions'), |
| 116 | + 403, |
| 117 | + 'Forbidden', |
| 118 | + None, |
| 119 | + id='cylc/subscriptions', |
| 120 | + ), |
| 121 | + # pytest.param( |
| 122 | + # ('cylc', 'userprofile'), |
| 123 | + # 403, |
| 124 | + # 'Forbidden', |
| 125 | + # None, |
| 126 | + # id='cylc/userprofile', |
| 127 | + # ) |
| 128 | + ] |
| 129 | +) |
| 130 | +async def test_unauthenticated( |
| 131 | + patch_conf_files, |
| 132 | + jp_fetch, |
| 133 | + endpoint, |
| 134 | + code, |
| 135 | + message, |
| 136 | + body |
| 137 | +): |
| 138 | + await _test(jp_fetch, endpoint, code, message, body) |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.integration |
| 142 | +@pytest.mark.usefixtures("mock_authentication_yossarian") |
| 143 | +@pytest.mark.parametrize( |
| 144 | + 'endpoint,code,message,body', |
| 145 | + [ |
| 146 | + # pytest.param( |
| 147 | + # ('cylc', 'graphql'), |
| 148 | + # 403, |
| 149 | + # 'authorisation insufficient', |
| 150 | + # None, |
| 151 | + # id='cylc/graphql', |
| 152 | + # ), |
| 153 | + # pytest.param( |
| 154 | + # ('cylc', 'subscriptions'), |
| 155 | + # 403, |
| 156 | + # 'authorisation insufficient', |
| 157 | + # None, |
| 158 | + # id='cylc/subscriptions', |
| 159 | + # ), |
| 160 | + # pytest.param( |
| 161 | + # ('cylc', 'userprofile'), |
| 162 | + # 403, |
| 163 | + # 'authorisation insufficient', |
| 164 | + # None, |
| 165 | + # id='cylc/userprofile', |
| 166 | + # ) |
| 167 | + ] |
| 168 | +) |
| 169 | +async def test_unauthorised( |
| 170 | + patch_conf_files, |
| 171 | + jp_fetch, |
| 172 | + endpoint, |
| 173 | + code, |
| 174 | + message, |
| 175 | + body |
| 176 | +): |
| 177 | + await _test(jp_fetch, endpoint, code, message, body) |
| 178 | + |
| 179 | + |
| 180 | +async def _test(jp_fetch, endpoint, code, message, body): |
| 181 | + """Test 400 HTTP response (upgrade to websocket).""" |
| 182 | + fetch = partial(jp_fetch, *endpoint) |
| 183 | + if code != 200: |
| 184 | + with pytest.raises(HTTPClientError) as exc_ctx: |
| 185 | + await fetch() |
| 186 | + exc = exc_ctx.value |
| 187 | + assert exc.code == code |
| 188 | + if message: |
| 189 | + assert exc.message == message |
| 190 | + if body: |
| 191 | + assert body in exc.response.body |
| 192 | + else: |
| 193 | + response = await fetch() |
| 194 | + assert code == response.code |
| 195 | + if message: |
| 196 | + assert response.reason == message |
| 197 | + if body: |
| 198 | + assert body in response.body |
0 commit comments