Improve testing situation.

This commit is contained in:
Dorian 2017-10-23 08:32:22 -04:00
parent 852389b031
commit 9768794412
4 changed files with 45 additions and 44 deletions

View File

@ -5,8 +5,10 @@ Support for CouchDB 2.x.
:license: AGPL v3+
"""
import copy
import enum
import http
import json
import logging
import os
from urllib import parse
@ -102,6 +104,22 @@ class CouchDB(object):
)
class InvalidQueryError(Exception):
pass
class NoMatchingDocumentFoundError(Exception):
def __init__(self, db: CouchDB, query: dict=None):
self.db = db
self.query = query
def __str__(self):
query_string = json.dumps(self.query, indent=4, sort_keys=True)
db_url = self.db.connection()
message = f'No document in "{db_url}" matches "{query_string}"'
return message
# TODO: Test and document properly.
class DatabaseModel(object):
def __init__(self, **kwargs):
@ -112,6 +130,19 @@ class DatabaseModel(object):
def doc_type():
raise NotImplementedError
@classmethod
def find_in_db(cls, db: CouchDB, query: dict):
if not query or not isinstance(query, dict):
raise InvalidQueryError
doc_query = copy.deepcopy(query)
doc_query['type'] = cls.doc_type()
found_model = db.find_first_document(doc_query)
if not found_model:
raise NoMatchingDocumentFoundError(db, query)
return cls(**found_model)
def to_api_json(self):
raise NotImplementedError

View File

@ -30,17 +30,12 @@ def serve_page(slug=''):
# TODO: Attach DB to Flask app context.
db = database.CouchDB.configure_from_env()
found_page = db.find_first_document({
'type': models.Page.doc_type(),
'slug': slug,
})
if not found_page:
try:
page = models.Page.find_in_db(db, {'slug': slug})
return flask.jsonify(page.to_api_json())
except database.NoMatchingDocumentFoundError:
flask.abort(http.HTTPStatus.NOT_FOUND)
page = models.Page(**found_page)
return flask.jsonify(page.to_api_json())
@rookeries_app.route('/api/pages/<slug>', methods=['POST', 'PUT'])
@flask_jwt.jwt_required()

View File

@ -13,8 +13,9 @@ Scenario: User cannot modify a site using an empty request
And I try to modify the site with an empty request
Then I get a bad request response
Scenario: User can modify a site
Given I am a valid user
And I get the site
And I try to modify the site
Then my updates are reflected in the site
# TODO: Enable once site modification endpoint ready.
#Scenario: User can modify a site
# Given I am a valid user
# And I get the site
# And I try to modify the site
# Then my updates are reflected in the site

View File

@ -16,9 +16,10 @@ from tests import utils
# TODO: Rework site management and testing.
bdd.scenarios('site_access.feature')
# TODO: Rework modification to only allow changes to layout and children...
# bdd.scenarios('site_modification.feature')
# TODO: Add a test if the site does not exist for some reason.
bdd.scenarios('site_access.feature')
bdd.scenarios('site_modification.feature')
@bdd.given(parsers.parse('I try to modify the site with a non-json request'), target_fixture='api_response')
@ -30,7 +31,7 @@ def invalid_non_json_modify_site_response(api_response, auth_token_headers, api_
)
@bdd.given(parsers.parse('I try to modify the site with an empty retest_sitequest'), target_fixture='api_response')
@bdd.given(parsers.parse('I try to modify the site with an empty request'), target_fixture='api_response')
def invalid_empty_modify_site_response(api_response, auth_token_headers, api_base_uri):
return requests.put(
url=f'{api_base_uri}/api/site',
@ -78,18 +79,6 @@ def modify_site_response(test_site, api_response, auth_token_headers, api_base_u
return utils.ResponseSampleBundle(response, test_site)
@bdd.given(parsers.parse('I try to modify a site that does not exist'), target_fixture='api_response')
def modify_non_existent_site_request(test_site, auth_token_headers, api_base_uri):
site_modification_request = {
}
return requests.put(
url=f'{api_base_uri}/api/site',
json=site_modification_request,
headers=auth_token_headers,
)
@bdd.then(parsers.parse('my updates are reflected in the site'))
def assert_updated_site_response(api_response, api_base_uri):
actual_response = utils.extract_response(api_response)
@ -98,18 +87,3 @@ def assert_updated_site_response(api_response, api_base_uri):
expected_json = {}
assert expected_json == actual_response.json()
assert f'{api_base_uri}/api/site' == actual_response.json()['urls']['self']
@bdd.then(parsers.parse('I get a site can not be found message'))
def assert_resource_not_found_response(api_response):
actual_response = utils.extract_response(api_response)
expected_response_json = {
'error': {
'status_code': http.HTTPStatus.NOT_FOUND.value,
'message': 'Resource not found.',
'resource': actual_response.request.url,
},
}
assert actual_response.status_code == http.HTTPStatus.NOT_FOUND
assert actual_response.json() == expected_response_json