2021-03-06 16:21:28 +01:00
|
|
|
import pytest
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
from .utils import register_user, send_request as _send_request
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def expected_registration_redirect(base_url):
|
|
|
|
yield '{base_url}/register?redirect={base_url}/execute'.format(base_url=base_url)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def expected_login_redirect(base_url):
|
|
|
|
yield '{base_url}/login?redirect={base_url}/execute'.format(base_url=base_url)
|
|
|
|
|
|
|
|
|
|
|
|
def send_request(**kwargs):
|
|
|
|
return _send_request('shell.exec', args={'cmd': 'echo ping'}, **kwargs)
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_request_with_no_registered_users(base_url, expected_registration_redirect):
|
|
|
|
"""
|
|
|
|
An /execute request performed before any user is registered should redirect to the registration page.
|
|
|
|
"""
|
|
|
|
response = send_request(authenticate=False, parse_json=False)
|
|
|
|
assert expected_registration_redirect == response.url, \
|
|
|
|
'No users registered, but the application did not redirect us to the registration page'
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_first_user_registration(base_url):
|
|
|
|
"""
|
|
|
|
Emulate a first user registration through form and get the session_token.
|
|
|
|
"""
|
|
|
|
response = register_user()
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
assert len(response.history) > 0, 'Redirect missing from the history'
|
|
|
|
assert 'session_token' in response.history[0].cookies, 'No session_token returned upon registration'
|
|
|
|
assert '{base_url}/'.format(base_url=base_url) == response.url, \
|
|
|
|
'The registration form did not redirect to the main panel'
|
2020-12-16 02:10:37 +01:00
|
|
|
|
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_unauthorized_request_with_registered_user(base_url, expected_login_redirect):
|
|
|
|
"""
|
|
|
|
After a first user has been registered any unauthenticated call to /execute should redirect to /login.
|
|
|
|
"""
|
|
|
|
response = send_request(authenticate=False, parse_json=False)
|
|
|
|
assert expected_login_redirect == response.url, \
|
|
|
|
'An unauthenticated request after user registration should result in a login redirect'
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_authorized_request_with_registered_user(base_url):
|
|
|
|
# A request authenticated with user/pass should succeed.
|
|
|
|
response = send_request(authenticate=True)
|
|
|
|
assert response.output.strip() == 'ping', 'The request did not return the expected output'
|
2018-01-04 18:29:03 +01:00
|
|
|
|
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
def test_request_with_wrong_credentials(base_url, expected_login_redirect):
|
|
|
|
# A request with the wrong user/pass should fail.
|
|
|
|
response = send_request(authenticate=False, auth=('wrong', 'wrong'), parse_json=False)
|
|
|
|
assert expected_login_redirect == response.url, 'A request with wrong credentials should fail'
|
2018-01-04 18:29:03 +01:00
|
|
|
|
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
pytest.main()
|
|
|
|
|
|
|
|
|
2019-07-15 16:28:44 +02:00
|
|
|
# vim:sw=4:ts=4:et:
|