From d4fcd3f05604b98fe20b20e74b6d3a375f871c76 Mon Sep 17 00:00:00 2001 From: Eric Barbour Date: Jun 16 2016 14:42:05 +0000 Subject: Add tests for /api/hub/* Still skipping tests that check logged out behavior until issues can be worked out with testing redirecting --- diff --git a/hubs/app.py b/hubs/app.py index df33459..c283490 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -641,6 +641,12 @@ def hub_leave(hub): @app.route('/api/fedmsg/markup', methods=['GET']) def markup_fedmsg(): + ''' + This is a temporary endpoint to create a human-readable form of a message. + For now it serves as a development tool + + This route will be removed once its functionality is integrated into FMN + ''' try: data = flask.request.args['message'] plugin = flask.request.args['plugin'] diff --git a/hubs/tests/__init__.py b/hubs/tests/__init__.py index 7c9da28..bafe0a1 100644 --- a/hubs/tests/__init__.py +++ b/hubs/tests/__init__.py @@ -88,28 +88,16 @@ class APPTest(unittest.TestCase): @contextmanager -def user_set(APP, user): - """ Set the provided user as fas_user in the provided application.""" - # Hack used to remove the before_request function set by - # flask.ext.fas_openid.FAS which otherwise kills our effort to set a - # flask.g.fas_user. - from flask import appcontext_pushed, g - - def handler(sender, **kwargs): - g.fas_user = user - g.fas_session_id = b'123' - - with appcontext_pushed.connected_to(handler, APP): - yield - - -@contextmanager def auth_set(APP, auth): - """ Set the provided user as fas_user in the provided application.""" + """ + Set the provided user as g.auth in the provided application. + :param APP: A Flask instance. Cannot be a FlaskClient. + :param auth: A FakeAuthorization instance to set as g.auth. + """ # Hack used to remove the before_request function set by # flask.ext.fas_openid.FAS which otherwise kills our effort to set a - # flask.g.fas_user. + # flask.g.auth. from flask import appcontext_pushed, g APP.before_request_funcs[None] = [] @@ -123,7 +111,7 @@ def auth_set(APP, auth): class FakeUser(object): - """ Fake user used to test the fedocallib library. """ + """ Fake user to be attached to an Authorization. """ def __init__(self, username='username'): """ Constructor. @@ -131,7 +119,7 @@ class FakeUser(object): supposed to be. """ self.username = username - self.openid = username + 'id.fedoraproject.org' + self.openid = username + '.id.fedoraproject.org' self.booksmarks = [] def __getitem__(self, key): @@ -139,7 +127,7 @@ class FakeUser(object): class FakeAuthorization(object): - """ Fake user used to test the fedocallib library. """ + """ Fake Authorization used to set as flask.g.auth. """ def __init__(self, username='username'): """ Constructor. @@ -149,10 +137,11 @@ class FakeAuthorization(object): self.logged_in = True self.fullname = 'fullname: ' + username self.email = 'email: ' + username - self.openid = username + 'id.fedoraproject.org' + self.openid = username + '.id.fedoraproject.org' self.user = FakeUser(username) self.avatar = 'avatar_src_url' self.nickname = username + self.username = username def __getitem__(self, key): return self.dic[key] diff --git a/hubs/tests/test_api/test_fedmsg.py b/hubs/tests/test_api/test_fedmsg.py index c708073..ef3b6db 100644 --- a/hubs/tests/test_api/test_fedmsg.py +++ b/hubs/tests/test_api/test_fedmsg.py @@ -2,6 +2,7 @@ import json import hubs.tests import hubs.models +from hubs.app import app class TestFeed(hubs.tests.APPTest): @@ -271,12 +272,12 @@ class TestFeed(hubs.tests.APPTest): def test_returns_400_when_no_message(self): payload = {'plugin': 'feed'} response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 400 + self.assertEqual(response.status_code, 400) def test_returns_400_when_no_plugin(self): payload = {'message': json.dumps(self.message_involved)} response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 400 + self.assertEqual(response.status_code, 400) def test_returns_400_when_bad_plugin_name(self): payload = { @@ -284,17 +285,16 @@ class TestFeed(hubs.tests.APPTest): 'plugin': 'notarealplugin' } response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 400 + self.assertEqual(response.status_code, 400) def test_returns_403_when_not_logged_in(self): payload = { 'message': json.dumps(self.message_involved), 'plugin': 'feed' } - with hubs.tests.user_set(self.app, self.user): - response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 403 + response = self.app.get('/api/fedmsg/markup', query_string=payload) + self.assertEqual(response.status_code, 403) def test_returns_match_if_involved(self): payload = { @@ -305,13 +305,13 @@ class TestFeed(hubs.tests.APPTest): sess['openid'] = 'atelic@fedoraproject.org' sess['nickname'] = 'atelic' - with hubs.tests.user_set(self.app, self.user): + with hubs.tests.auth_set(app, self.user): response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 200, response.status_code + self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertTrue(data) - assert isinstance(data[0], dict) + self.assertTrue(isinstance(data[0], dict)) def test_returns_no_match_if_not_involved(self): payload = { @@ -322,9 +322,9 @@ class TestFeed(hubs.tests.APPTest): sess['openid'] = 'atelic@fedoraproject.org' sess['nickname'] = 'atelic' - with hubs.tests.user_set(self.app, self.user): + with hubs.tests.auth_set(app, self.user): response = self.app.get('/api/fedmsg/markup', query_string=payload) - assert response.status_code == 200, response.status_code + self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertFalse(data) diff --git a/hubs/tests/test_api/test_hub.py b/hubs/tests/test_api/test_hub.py new file mode 100644 index 0000000..ee749d9 --- /dev/null +++ b/hubs/tests/test_api/test_hub.py @@ -0,0 +1,150 @@ +import flask +import unittest + +import hubs.tests +import hubs.models +from hubs.app import app + + +def usernames(collection): return [u.username for u in collection] + + +class TestHubSubscribe(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_subscribe_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/subscribe'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_subscribe_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/subscribe'.format(hub.name), + follow_redirects=True) + self.assertEqual(resp.status_code, 200) + # Need to find the Hub again to avoid DetachedInstanceError + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username in usernames(h.subscribers)) + + +class TestHubUnsubscribe(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_unsubscribe_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/unsubscribe'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_unsubscribe_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + # Need a real user model to subscribe to the hub + User = hubs.models.User.by_username(self.session, self.user.username) + hub.subscribe(self.session, User) + + self.assertTrue(self.user.username in usernames(hub.subscribers)) + + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/unsubscribe'.format(hub.name), + follow_redirects=True) + self.assertEqual(resp.status_code, 200) + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username not in usernames(h.subscribers)) + + +class TestHubStar(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_star_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/star'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_star_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/star'.format(hub.name), + follow_redirects=True) + + self.assertEqual(resp.status_code, 200) + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username in usernames(h.stargazers)) + + +class TestHubUnstar(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_unstar_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/unstar'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_unstar_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + # Need a real user model to subscribe to the hub + User = hubs.models.User.by_username(self.session, self.user.username) + hub.subscribe(self.session, User, role='stargazer') + + self.assertTrue(self.user.username in [u.username for + u in hub.stargazers]) + + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/unstar'.format(hub.name), + follow_redirects=True) + self.assertEqual(resp.status_code, 200) + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username not in usernames(h.stargazers)) + + +class TestHubJoin(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_join_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/join'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_join_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/join'.format(hub.name), + follow_redirects=True) + + self.assertEqual(resp.status_code, 200) + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username in usernames(h.members)) + + +class TestHubLeave(hubs.tests.APPTest): + user = hubs.tests.FakeAuthorization('decause') + + @unittest.skip('Need to handle redirects') + def test_leave_redirects_when_logged_out(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + resp = self.app.post('/api/hub/{}/leave'.format(hub.idx)) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.location, flask.url_for('fedora.login')) + + def test_star_when_logged_in(self): + hub = hubs.models.Hub.by_name(self.session, 'infra') + # Need a real user model to subscribe to the hub + User = hubs.models.User.by_username(self.session, self.user.username) + hub.subscribe(self.session, User, role='member') + + with hubs.tests.auth_set(app, self.user): + resp = self.app.post('/api/hub/{}/leave'.format(hub.name), + follow_redirects=True) + + self.assertEqual(resp.status_code, 200) + h = hubs.models.Hub.by_name(self.session, 'infra') + self.assertTrue(self.user.username not in usernames(h.members))