2020-12-16 02:10:37 +01:00
|
|
|
import os
|
2018-01-04 18:29:03 +01:00
|
|
|
import unittest
|
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
from . import BaseHttpTest, conf_dir
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2020-12-16 02:10:37 +01:00
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
class TestHttp(BaseHttpTest):
|
|
|
|
"""
|
|
|
|
Tests the full flow of a request/response on the HTTP backend.
|
|
|
|
Runs a remote command over HTTP via shell.exec plugin and gets the output.
|
|
|
|
"""
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
config_file = os.path.join(conf_dir, 'test_http_config.yaml')
|
2020-12-16 02:10:37 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-02-27 15:01:25 +01:00
|
|
|
super(TestHttp, self).__init__(*args, **kwargs)
|
2020-12-16 02:10:37 +01:00
|
|
|
|
|
|
|
def test_http_flow(self):
|
|
|
|
# An /execute request performed before any user is registered should redirect to the registration page.
|
2021-02-27 15:01:25 +01:00
|
|
|
expected_registration_redirect = '{base_url}/register?redirect={base_url}/execute'.format(
|
|
|
|
base_url=self.base_url)
|
|
|
|
expected_login_redirect = '{base_url}/login?redirect={base_url}/execute'.format(
|
|
|
|
base_url=self.base_url)
|
|
|
|
|
|
|
|
response = self.send_request(authenticate=False, parse_response=False)
|
|
|
|
self.assertEqual(expected_registration_redirect, response.url,
|
2020-12-16 02:10:37 +01:00
|
|
|
'No users registered, but the application did not redirect us to the registration page')
|
|
|
|
|
|
|
|
# Emulate a first user registration through form and get the session_token.
|
|
|
|
response = self.register_user()
|
|
|
|
self.assertGreater(len(response.history), 0, 'Redirect missing from the history')
|
|
|
|
self.assertTrue('session_token' in response.history[0].cookies, 'No session_token returned upon registration')
|
|
|
|
self.assertEqual('{base_url}/'.format(base_url=self.base_url), response.url,
|
|
|
|
'The registration form did not redirect to the main panel')
|
|
|
|
|
|
|
|
# After a first user has been registered any unauthenticated call to /execute should redirect to /login.
|
2021-02-27 15:01:25 +01:00
|
|
|
response = self.send_request(authenticate=False, parse_response=False)
|
|
|
|
self.assertEqual(expected_login_redirect, response.url,
|
2020-12-16 02:10:37 +01:00
|
|
|
'An unauthenticated request after user registration should result in a login redirect')
|
|
|
|
|
|
|
|
# A request authenticated with user/pass should succeed.
|
2021-02-27 15:01:25 +01:00
|
|
|
response = self.send_request(authenticate=True)
|
2020-12-16 02:10:37 +01:00
|
|
|
self.assertEqual(response.output.strip(), 'ping', 'The request did not return the expected output')
|
|
|
|
|
|
|
|
# A request with the wrong user/pass should fail.
|
2021-02-27 15:01:25 +01:00
|
|
|
response = self.send_request(authenticate=False, auth=('wrong', 'wrong'), parse_response=False)
|
|
|
|
self.assertEqual(expected_login_redirect, response.url, 'A request with wrong credentials should fail')
|
2018-01-04 18:29:03 +01:00
|
|
|
|
2020-12-16 02:10:37 +01:00
|
|
|
def send_request(self, **kwargs):
|
2021-02-27 15:01:25 +01:00
|
|
|
return super().send_request('shell.exec', args={'cmd': 'echo ping'}, **kwargs)
|
2018-01-04 18:29:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
2019-07-15 16:28:44 +02:00
|
|
|
# vim:sw=4:ts=4:et:
|