| |
@@ -0,0 +1,121 @@
|
| |
+ import json
|
| |
+ from urlparse import urlparse
|
| |
+
|
| |
+ import hubs.tests
|
| |
+ import hubs.models
|
| |
+ from hubs.app import app
|
| |
+
|
| |
+
|
| |
+ class TestGetNotifications(hubs.tests.APPTest):
|
| |
+ user = hubs.tests.FakeAuthorization('ralph')
|
| |
+
|
| |
+ def test_get_notifications_invalid_name(self):
|
| |
+ name = 'notarealfasuser'
|
| |
+
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.get('/{}/notifications/'.format(name))
|
| |
+ self.assertEqual(resp.status_code, 200)
|
| |
+ data = json.loads(resp.data)
|
| |
+ self.assertEqual(data, [])
|
| |
+
|
| |
+ def test_get_notifications_valid_name(self):
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.get('/{}/notifications/'.format(
|
| |
+ self.user.username))
|
| |
+
|
| |
+ self.assertEqual(resp.status_code, 200)
|
| |
+ data = json.loads(resp.data)
|
| |
+ self.assertEqual(len(data), 1)
|
| |
+
|
| |
+ for saved in data:
|
| |
+ self.assertEqual(saved['markup'], 'foo')
|
| |
+ self.assertEqual(saved['link'], 'bar')
|
| |
+
|
| |
+
|
| |
+ class TestPostNotifications(hubs.tests.APPTest):
|
| |
+ user = hubs.tests.FakeAuthorization('ralph')
|
| |
+ valid_payload = {
|
| |
+ 'username': user.username,
|
| |
+ 'markup': 'foobar',
|
| |
+ 'link': 'baz',
|
| |
+ 'secondary_icon': 'http://placekitten.com/g/200/300',
|
| |
+ 'dom_id': 'reallyuniqueuid'
|
| |
+ }
|
| |
+
|
| |
+ invalid_payload = {
|
| |
+ 'username': user.username,
|
| |
+ }
|
| |
+
|
| |
+ def test_post_notification_invalid_user(self):
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.post(
|
| |
+ '/{}/notifications/'.format('notarealfasuser'),
|
| |
+ data=json.dumps(self.valid_payload))
|
| |
+ self.assertEqual(resp.status_code, 400)
|
| |
+
|
| |
+ def test_post_notification_invalid_payload(self):
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.post(
|
| |
+ '/{}/notifications/'.format(self.user.username),
|
| |
+ data=json.dumps(self.invalid_payload))
|
| |
+ self.assertEqual(resp.status_code, 400)
|
| |
+
|
| |
+ def test_post_notification_valid_payload(self):
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.post(
|
| |
+ '/{}/notifications/'.format(self.user.username),
|
| |
+ data=json.dumps(self.valid_payload))
|
| |
+
|
| |
+ self.assertEqual(resp.status_code, 200)
|
| |
+ data = json.loads(resp.data)
|
| |
+ self.assertTrue(isinstance(data, dict))
|
| |
+
|
| |
+ notification = data['notification']
|
| |
+ self.assertEqual(notification['markup'], 'foobar')
|
| |
+ self.assertEqual(notification['link'], 'baz')
|
| |
+
|
| |
+ all_saved = hubs.models.SavedNotification.by_username(
|
| |
+ hubs.app.session, self.user.username)
|
| |
+ self.assertEqual(len(all_saved), 2)
|
| |
+ all_saved = [s.__json__() for s in all_saved]
|
| |
+ self.assertTrue(any(str(s['markup']) == self.valid_payload['markup']
|
| |
+ for s in all_saved))
|
| |
+ self.assertTrue(any(str(s['link']) == self.valid_payload['link']
|
| |
+ for s in all_saved))
|
| |
+
|
| |
+
|
| |
+ class TestDeleteNotifications(hubs.tests.APPTest):
|
| |
+ user = hubs.tests.FakeAuthorization('ralph')
|
| |
+ notification = hubs.models.SavedNotification(
|
| |
+ username='ralph',
|
| |
+ markup='foo',
|
| |
+ link='bar',
|
| |
+ secondary_icon='baz',
|
| |
+ dom_id='qux'
|
| |
+ )
|
| |
+
|
| |
+ def test_delete_notification(self):
|
| |
+ self.session.add(self.notification)
|
| |
+ self.session.commit()
|
| |
+ idx = self.notification.idx
|
| |
+
|
| |
+ self.assertIsNotNone(self.notification)
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.delete(
|
| |
+ '/{}/notifications/{}/'.format(self.user.username, idx)
|
| |
+ )
|
| |
+
|
| |
+ self.assertEqual(resp.status_code, 200)
|
| |
+ notification = self.session.query(
|
| |
+ hubs.models.SavedNotification).filter_by(idx=idx).first()
|
| |
+
|
| |
+ self.assertIsNone(notification)
|
| |
+
|
| |
+ def test_404_on_bad_idx(self):
|
| |
+ idx = 'thisisastringnotanint'
|
| |
+
|
| |
+ with hubs.tests.auth_set(app, self.user):
|
| |
+ resp = self.app.delete(
|
| |
+ '/{}/notifications/{}/'.format(self.user.username, idx)
|
| |
+ )
|
| |
+ self.assertEqual(resp.status_code, 404)
|
| |
Should we keep this file if dropdown doesnt work?