2016-12-02 09:18:22 -05:00
|
|
|
"""
|
|
|
|
Functional tests for the managing sites.
|
|
|
|
|
2017-10-24 12:04:00 -04:00
|
|
|
:copyright: Copyright 2013-2017, Dorian Puła
|
|
|
|
<dorian.pula@amber-penguin-software.ca>
|
2016-12-02 09:18:22 -05:00
|
|
|
:license: AGPL v3+
|
|
|
|
"""
|
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
import copy
|
2017-04-05 17:43:24 -04:00
|
|
|
import http
|
2017-10-19 09:00:53 -04:00
|
|
|
from unittest import mock
|
2017-04-05 17:43:24 -04:00
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
import pytest
|
2016-12-02 09:18:22 -05:00
|
|
|
import requests
|
|
|
|
|
2017-10-24 17:49:27 -04:00
|
|
|
from rookeries.sites import models
|
2017-03-14 08:46:54 -04:00
|
|
|
from tests import utils
|
2016-12-02 09:18:22 -05:00
|
|
|
|
|
|
|
|
2017-04-26 08:34:48 -04:00
|
|
|
# TODO: Rework site management and testing.
|
2017-05-02 08:35:12 -04:00
|
|
|
# TODO: Rework modification to only allow changes to layout and children...
|
2017-10-23 08:32:22 -04:00
|
|
|
# TODO: Add a test if the site does not exist for some reason.
|
2017-04-05 17:43:24 -04:00
|
|
|
|
2016-12-02 09:18:22 -05:00
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def valid_user(db):
|
|
|
|
user, password = utils.generate_test_user(db)
|
|
|
|
return user.username, password
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def auth_token_headers(valid_user, api_base_uri):
|
|
|
|
username, password = valid_user
|
|
|
|
token = requests.post(
|
|
|
|
url=f'{api_base_uri}/auth',
|
|
|
|
json={
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
}
|
|
|
|
).json()['access_token']
|
|
|
|
|
|
|
|
return {'Authorization': f'JWT {token}'}
|
|
|
|
|
|
|
|
|
2017-12-14 08:20:22 -05:00
|
|
|
def test_visitor_can_access_site(api_base_uri, test_site):
|
2017-12-13 17:50:01 -05:00
|
|
|
response = requests.get(
|
|
|
|
url=f'{api_base_uri}/api/site',
|
|
|
|
headers={},
|
|
|
|
)
|
|
|
|
assert_site_response(response)
|
|
|
|
|
|
|
|
|
2017-12-14 08:20:22 -05:00
|
|
|
def test_authenticated_user_can_access_site(
|
|
|
|
auth_token_headers, api_base_uri, test_site):
|
2017-12-13 17:50:01 -05:00
|
|
|
response = requests.get(
|
|
|
|
url=f'{api_base_uri}/api/site',
|
|
|
|
headers=auth_token_headers,
|
|
|
|
)
|
|
|
|
assert_site_response(response)
|
|
|
|
|
|
|
|
|
2017-12-13 20:07:10 -05:00
|
|
|
def test_authenticated_user_cannot_modify_site_using_bad_request(
|
2017-12-14 08:20:22 -05:00
|
|
|
auth_token_headers, api_base_uri, test_site):
|
2017-12-13 17:50:01 -05:00
|
|
|
response = requests.put(
|
2017-05-02 08:35:12 -04:00
|
|
|
url=f'{api_base_uri}/api/site',
|
2017-04-05 17:43:24 -04:00
|
|
|
data='',
|
|
|
|
headers=auth_token_headers,
|
|
|
|
)
|
2017-12-13 17:50:01 -05:00
|
|
|
assert_bad_request_response(response)
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
|
2017-12-13 20:07:10 -05:00
|
|
|
def test_authenticated_user_cannot_modify_site_using_empty_request(
|
2017-12-14 08:20:22 -05:00
|
|
|
auth_token_headers, api_base_uri, test_site):
|
2017-12-13 17:50:01 -05:00
|
|
|
response = requests.put(
|
2017-05-02 08:35:12 -04:00
|
|
|
url=f'{api_base_uri}/api/site',
|
2017-04-05 17:43:24 -04:00
|
|
|
json={},
|
|
|
|
headers=auth_token_headers,
|
|
|
|
)
|
2017-12-13 17:50:01 -05:00
|
|
|
assert_bad_request_response(response)
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
|
2017-12-13 20:07:10 -05:00
|
|
|
def test_authenticated_user_can_modify_site_using_proper_request(
|
|
|
|
auth_token_headers, api_base_uri, test_site, db):
|
2017-12-13 17:50:01 -05:00
|
|
|
|
|
|
|
original_site = get_site_request(auth_token_headers, api_base_uri)
|
|
|
|
modify_site = copy.deepcopy(original_site)
|
|
|
|
modify_site['name'] = 'Modified Test Site'
|
|
|
|
|
|
|
|
response = requests.put(
|
2017-05-02 08:35:12 -04:00
|
|
|
url=f'{api_base_uri}/api/site',
|
2017-12-13 17:50:01 -05:00
|
|
|
json=modify_site,
|
2017-04-05 17:43:24 -04:00
|
|
|
headers=auth_token_headers,
|
|
|
|
)
|
2017-12-13 17:50:01 -05:00
|
|
|
assert_updated_site_response(test_site, response, db)
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
def get_site_request(auth_token_headers, api_base_uri):
|
|
|
|
return requests.get(
|
|
|
|
url=f'{api_base_uri}/api/site',
|
|
|
|
headers=auth_token_headers,
|
|
|
|
).json()
|
2017-04-05 17:43:24 -04:00
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
|
|
|
|
def assert_site_response(response):
|
|
|
|
assert response.status_code == http.HTTPStatus.OK
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
expected_json = {
|
2017-10-23 08:58:59 -04:00
|
|
|
'name': 'Test Site',
|
|
|
|
'links': {'self': '/api/site'},
|
2017-10-18 23:52:43 -04:00
|
|
|
'config': mock.ANY,
|
|
|
|
'landingPage': mock.ANY,
|
2017-10-24 20:31:08 -04:00
|
|
|
'menu': mock.ANY,
|
2017-11-21 08:39:37 -05:00
|
|
|
'header': '',
|
|
|
|
'footer': '',
|
2017-04-05 17:43:24 -04:00
|
|
|
}
|
2017-12-13 17:50:01 -05:00
|
|
|
assert expected_json == response.json()
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
def assert_unauthorized_response(expected_url, response):
|
|
|
|
expected_response_json = {
|
|
|
|
'error': {
|
|
|
|
'status_code': http.HTTPStatus.UNAUTHORIZED.value,
|
|
|
|
'message': 'Not authorized to access this resource.',
|
|
|
|
'resource': expected_url,
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 17:43:24 -04:00
|
|
|
|
2017-12-13 17:50:01 -05:00
|
|
|
assert response.status_code == http.HTTPStatus.UNAUTHORIZED
|
|
|
|
assert response.json() == expected_response_json
|
|
|
|
|
|
|
|
|
|
|
|
def assert_bad_request_response(response):
|
|
|
|
expected_response_json = {
|
|
|
|
'error': {
|
|
|
|
'status_code': http.HTTPStatus.BAD_REQUEST.value,
|
|
|
|
'message': mock.ANY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert response.json() == expected_response_json
|
|
|
|
assert response.status_code == http.HTTPStatus.BAD_REQUEST
|
2017-04-05 17:43:24 -04:00
|
|
|
|
|
|
|
|
2017-10-24 17:49:27 -04:00
|
|
|
def assert_updated_site_response(test_site, api_response, db):
|
2017-04-05 20:59:19 -04:00
|
|
|
actual_response = utils.extract_response(api_response)
|
2017-04-05 17:43:24 -04:00
|
|
|
assert actual_response.status_code == http.HTTPStatus.OK
|
|
|
|
|
2017-10-24 08:27:19 -04:00
|
|
|
expected_json = test_site.to_api_json()
|
|
|
|
expected_json['name'] = 'Modified Test Site'
|
2017-04-05 17:43:24 -04:00
|
|
|
assert expected_json == actual_response.json()
|
2017-10-24 08:27:19 -04:00
|
|
|
assert '/api/site' == actual_response.json()['links']['self']
|
|
|
|
|
|
|
|
# TODO: Remember to reset the state of the site to its original here.
|
2017-10-24 17:49:27 -04:00
|
|
|
updated_site = models.Site.find_in_db(db, {})
|
|
|
|
updated_site.name = test_site.name
|
|
|
|
updated_site.config = test_site.config
|
|
|
|
updated_site.menu = test_site.menu
|
|
|
|
updated_site.landing_page = test_site.landing_page
|
|
|
|
db.create_or_update_document(
|
|
|
|
updated_site.to_db_json(), updated_site._id, updated_site._rev)
|