-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from olinlibrary/osteele/test-events
Test /events and /labels, especially auth
- Loading branch information
Showing
5 changed files
with
206 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from unittest import skip | ||
|
||
import flask | ||
import isodate | ||
|
||
from . import abe_unittest | ||
from .context import abe # noqa: F401 | ||
|
||
# These imports have to happen after .context sets the environment variables | ||
import abe.app # isort:skip | ||
from abe import sample_data # isort:skip | ||
|
||
|
||
class EventsTestCase(abe_unittest.TestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.app = abe.app.app.test_client() | ||
sample_data.load_data(self.db) | ||
|
||
def test_get_events(self): | ||
with self.subTest("a six-month query returns some events"): | ||
response = self.app.get('/events/?start=2017-01-01&end=2017-07-01') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(flask.json.loads(response.data)), 25) | ||
|
||
with self.subTest("a one-year query returns all events"): | ||
response = self.app.get('/events/?start=2017-01-01&end=2018-01-01') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(flask.json.loads(response.data)), 69) | ||
|
||
with self.subTest("a two-year query is too long"): | ||
response = self.app.get('/events/?start=2017-01-01&end=2019-01-01') | ||
self.assertEqual(response.status_code, 404) | ||
|
||
with self.subTest("a one-year query works for leap years"): | ||
response = self.app.get('/events/?start=2020-01-01&end=2021-01-01') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
with self.subTest("an unauthenticated sender retrieves only public events"): | ||
event_response = self.app.get('/events/?start=2017-01-01&end=2017-07-01') | ||
# TODO: change the following when #75 is implemented | ||
self.assertEqual(len(flask.json.loads(event_response.data)), 25) | ||
|
||
def test_post(self): | ||
event = { | ||
'title': 'test_post', | ||
'start': isodate.parse_datetime('2018-05-04T09:00:00') | ||
} | ||
|
||
with self.subTest("succeeds when required fields are present"): | ||
response = self.app.post( | ||
'/events/', | ||
data=flask.json.dumps(event), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, 201) | ||
|
||
with self.subTest("fails when fields are missing"): | ||
evt = event.copy() | ||
del evt['title'] | ||
response = self.app.post( | ||
'/events/', | ||
data=flask.json.dumps(evt), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, 400) | ||
self.assertRegex(flask.json.loads(response.data)['error_message'], r"^ValidationError.*'title'") | ||
|
||
evt = event.copy() | ||
del evt['start'] | ||
response = self.app.post( | ||
'/events/', | ||
data=flask.json.dumps(evt), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, 400) | ||
self.assertRegex(flask.json.loads(response.data)['error_message'], r"^ValidationError.*'start'") | ||
|
||
with self.subTest("fails when the client is not authorized"): | ||
response = self.app.post( | ||
'/events/', | ||
data=flask.json.dumps(event), | ||
content_type='application/json', | ||
headers={'X-Forwarded-For': '192.168.1.1'} | ||
) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
@skip("Unimplemented test") | ||
def test_put(self): | ||
# TODO: test success | ||
# TODO: test invalid id | ||
# TODO: test invalid data | ||
# TODO: test unauthorized user | ||
pass | ||
|
||
@skip("Unimplemented test") | ||
def test_delete(self): | ||
# TODO: test success | ||
# TODO: test invalid id | ||
# TODO: test invalid data | ||
# TODO: test unauthorized user | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import flask | ||
|
||
from . import abe_unittest | ||
from .context import abe # noqa: F401 | ||
|
||
# These imports have to happen after .context sets the environment variables | ||
import abe.app # isort:skip | ||
from abe import sample_data # isort:skip | ||
|
||
|
||
class LabelsTestCase(abe_unittest.TestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.app = abe.app.app.test_client() | ||
sample_data.load_data(self.db) | ||
|
||
def test_get(self): | ||
response = self.app.get('/labels/') | ||
self.assertEqual(response.status_code, 200) | ||
labels = flask.json.loads(response.data) | ||
self.assertEqual(len(labels), 15) | ||
label = next((l for l in labels if l['name'] == 'library'), None) | ||
self.assertIsNotNone(label) | ||
self.assertEqual(label['name'], 'library') | ||
self.assertEqual(label['color'], '#26AAA5') | ||
self.assertRegex(label['description'], r'relating to the Olin Library') | ||
self.assertEqual(label['url'], 'http://library.olin.edu/') | ||
|
||
def test_post(self): | ||
label1 = { | ||
'name': 'label-test', | ||
} | ||
with self.subTest("succeeds when required fields are present"): | ||
response = self.app.post( | ||
'/labels/', | ||
data=flask.json.dumps(label1), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, 201) | ||
|
||
# FIXME: | ||
# with self.subTest("fails on a duplicate label name"): | ||
# response = self.app.post( | ||
# '/labels/', | ||
# data=flask.json.dumps(label1), | ||
# content_type='application/json' | ||
# ) | ||
# self.assertEqual(response.status_code, 400) | ||
|
||
with self.subTest("fails when fields are missing"): | ||
response = self.app.post( | ||
'/labels/', | ||
data=flask.json.dumps({}), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, 400) | ||
self.assertRegex(flask.json.loads(response.data)['error_message'], r"^ValidationError.*'name'") | ||
|
||
with self.subTest("fails when the client is not authorized"): | ||
label = { | ||
'name': 'label-test-2', | ||
} | ||
response = self.app.post( | ||
'/labels/', | ||
data=flask.json.dumps(label), | ||
content_type='application/json', | ||
headers={'X-Forwarded-For': '192.168.1.1'} | ||
) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_put(self): | ||
# TODO: test success | ||
# TODO: test invalid id | ||
# TODO: test invalid data | ||
with self.subTest("fails when the client is not authorized"): | ||
response = self.app.put( | ||
'/labels/library', | ||
data=flask.json.dumps({'description': 'new description'}), | ||
content_type='application/json', | ||
headers={'X-Forwarded-For': '192.168.1.1'} | ||
) | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_delete(self): | ||
# TODO: test success | ||
# TODO: test invalid id | ||
# TODO: test invalid data | ||
with self.subTest("fails when the client is not authorized"): | ||
response = self.app.delete( | ||
'/labels/library', | ||
headers={'X-Forwarded-For': '192.168.1.1'} | ||
) | ||
self.assertEqual(response.status_code, 401) |