| |
@@ -0,0 +1,285 @@
|
| |
+ import unittest
|
| |
+ from urlparse import urlparse
|
| |
+
|
| |
+ from flask import json
|
| |
+ from os.path import dirname
|
| |
+ import vcr
|
| |
+ from werkzeug.datastructures import ImmutableMultiDict
|
| |
+
|
| |
+ import hubs
|
| |
+ from hubs import tests
|
| |
+ from hubs.app import app
|
| |
+
|
| |
+ import hubs.models
|
| |
+
|
| |
+ import fedmsg.config
|
| |
+
|
| |
+ cassette_dir = dirname(dirname(__file__)) + '/vcr-request-data/'
|
| |
+
|
| |
+
|
| |
+ class HubsAPITest(hubs.tests.APPTest):
|
| |
+ def test_index_logged_out(self):
|
| |
+ result = self.app.get('/', follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 302)
|
| |
+ self.assertEqual(urlparse(result.location).path, "/login/fedora")
|
| |
+
|
| |
+ def test_index_logged_in(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/', follow_redirects=True)
|
| |
+ # its trying to redirect to login id.fedoraproject.org/openid
|
| |
+ # assert the status code of the response
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ self.assertFalse('Not logged in. Click to <a href="'
|
| |
+ '/login/fedora">login</a>' in result.data)
|
| |
+
|
| |
+ def test_hub_logged_out(self):
|
| |
+ with app.test_request_context('/ralph'):
|
| |
+ # need to manually call the @app.before_request
|
| |
+ # since unittest don't call it
|
| |
+ hubs.app.check_auth()
|
| |
+ result = self.app.get('/ralph', follow_redirects=True)
|
| |
+ # assert the status code of the response
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ str_expected = 'Not logged in. Click to ' \
|
| |
+ '<a href="/login/fedora">login</a>'
|
| |
+ self.assertTrue(str_expected in result.data)
|
| |
+
|
| |
+ def test_groups_logged_out(self):
|
| |
+ result = self.app.get('/groups', follow_redirects=False)
|
| |
+ # assert the status code of the response
|
| |
+ self.assertEqual(result.status_code, 302)
|
| |
+ # this will redirect to fedora.login
|
| |
+ self.assertEqual(urlparse(result.location).path, "/login/fedora")
|
| |
+
|
| |
+ def test_groups_logged_in(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/groups', follow_redirects=True)
|
| |
+ # assert the status code of the response
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ self.assertTrue("ZOMG - is the Hub Of The Month!" in result.data)
|
| |
+
|
| |
+ def test_hub_logged_in(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/ralph', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ self.assertFalse('Not logged in. Click to <a href="'
|
| |
+ '/login/fedora">login</a>' in result.data)
|
| |
+
|
| |
+ def test_hub_json(self):
|
| |
+ result = self.app.get('/ralph/json', follow_redirects=True)
|
| |
+ # assert the status code of the response
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ data = {
|
| |
+ "avatar": "https://seccdn.libravatar.org/avatar/"
|
| |
+ "9c9f7784935381befc302fe3c814f9136e7a339"
|
| |
+ "53d0318761669b8643f4df55c?s=312&d=retro",
|
| |
+ "left_width": 8,
|
| |
+ "members": ["ralph"],
|
| |
+ "name": "ralph",
|
| |
+ "owners": ["ralph"],
|
| |
+ "subscribers": [],
|
| |
+ "summary": "Ralph",
|
| |
+ "widgets": [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 51]
|
| |
+ }
|
| |
+ self.assertDictEqual(data, json.loads(result.data))
|
| |
+
|
| |
+ @unittest.skip("Authorization layer not present yet")
|
| |
+ def test_hub_edit_get_logged_out(self):
|
| |
+ # FAILING: Authorization layer not present yet
|
| |
+ with tests.auth_set(app, None):
|
| |
+ result = self.app.get('/ralph/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 403) # failing right
|
| |
+ # We do not have the authorization layer in place yet
|
| |
+
|
| |
+ @unittest.skip("Authorization layer not present yet")
|
| |
+ def test_hub_edit_get_logged_in_not_owner(self):
|
| |
+ # FAILING: Authorization layer not present yet
|
| |
+ user = tests.FakeAuthorization('not_ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/ralph/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 403) # failing right
|
| |
+ # We do not have the authorization layer in place yet
|
| |
+
|
| |
+ def test_hub_edit_get_logged_in_owner(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/ralph/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ # We do not have the authorization layer so everyone can edit
|
| |
+
|
| |
+ @unittest.skip("Authorization layer not present yet")
|
| |
+ def test_hub_edit_post_logged_out(self):
|
| |
+ # FAILING: Authorization layer not present yet
|
| |
+ with tests.auth_set(app, None):
|
| |
+ result = self.app.post('/ralph/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 403) # failing right
|
| |
+ # We do not have the authorization layer in place yet
|
| |
+
|
| |
+ @unittest.skip("Authorization layer not present yet")
|
| |
+ def test_hub_edit_post_logged_in_not_owner(self):
|
| |
+ # FAILING: Authorization layer not present yet
|
| |
+ user = tests.FakeAuthorization('not_ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.post('/ralph/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 403) # failing right
|
| |
+ # We do not have the authorization layer in place yet
|
| |
+
|
| |
+ def test_hub_edit_post_logged_in_owner_empty_data(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.post('/ralph/edit', data={},
|
| |
+ follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ self.assertFalse('Not logged in. Click to <a href="'
|
| |
+ '/login/fedora">login</a>' in result.data)
|
| |
+ self.assertTrue('<a href="#" class="dropdown-item">Full Name: '
|
| |
+ 'fullname: ralph</a>' in result.data)
|
| |
+
|
| |
+ def test_hub_edit_post_logged_in_owner_valid_data(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ data = ImmutableMultiDict(
|
| |
+ [('right_indexes[]', u'0'), ('right_indexes[]', u'1'),
|
| |
+ ('right_indexes[]', u'2'), ('right_indexes[]', u'3'),
|
| |
+ ('right_indexes[]', u'4'), ('right_indexes[]', u'5'),
|
| |
+ ('right_indexes[]', u'6'), ('right_indexes[]', u'7'),
|
| |
+ ('right_indexes[]', u'8'), ('right_widgets[]', u'32'),
|
| |
+ ('right_widgets[]', u'33'), ('right_widgets[]', u'34'),
|
| |
+ ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),
|
| |
+ ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),
|
| |
+ ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),
|
| |
+ ('js', u'true'), ('left_indexes[]', u'0'),
|
| |
+ ('left_indexes[]', u'1'), ('left_widgets[]', u'31'),
|
| |
+ ('left_widgets[]', u'32')])
|
| |
+ result = self.app.post('/ralph/edit', data=data,
|
| |
+ follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ self.assertEqual(result.data, 'ok')
|
| |
+
|
| |
+ def test_hub_edit_post_logged_in_owner_invalid_data_1(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ # some indexes and widgets are not integers
|
| |
+ data = ImmutableMultiDict(
|
| |
+ [('right_indexes[]', u'0a'), ('right_indexes[]', u'1'),
|
| |
+ ('right_indexes[]', u'2'), ('right_indexes[]', u'3'),
|
| |
+ ('right_indexes[]', u'4'), ('right_indexes[]', u'5'),
|
| |
+ ('right_indexes[]', u'6'), ('right_indexes[]', u'7'),
|
| |
+ ('right_indexes[]', u'8'), ('right_widgets[]', u'32'),
|
| |
+ ('right_widgets[]', u'33a'), ('right_widgets[]', u'34'),
|
| |
+ ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),
|
| |
+ ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),
|
| |
+ ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),
|
| |
+ ('js', u'true'), ('left_indexes[]', u'0a'),
|
| |
+ ('left_indexes[]', u'1'), ('left_widgets[]', u'31a'),
|
| |
+ ('left_widgets[]', u'32')])
|
| |
+ result = self.app.post('/ralph/edit', data=data,
|
| |
+ follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 400)
|
| |
+
|
| |
+ def test_hub_edit_post_logged_in_owner_invalid_data_2(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ # indexes len don't match widgets len
|
| |
+ data = ImmutableMultiDict(
|
| |
+ [('right_indexes[]', u'1'), ('right_indexes[]', u'2'),
|
| |
+ ('right_indexes[]', u'3'), ('right_indexes[]', u'4'),
|
| |
+ ('right_indexes[]', u'5'), ('right_indexes[]', u'6'),
|
| |
+ ('right_indexes[]', u'7'), ('right_indexes[]', u'8'),
|
| |
+ ('right_widgets[]', u'32'), ('right_widgets[]', u'34'),
|
| |
+ ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),
|
| |
+ ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),
|
| |
+ ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),
|
| |
+ ('js', u'true'), ('left_indexes[]', u'0'),
|
| |
+ ('left_indexes[]', u'1'), ('left_widgets[]', u'32')])
|
| |
+ result = self.app.post('/ralph/edit', data=data,
|
| |
+ follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 400)
|
| |
+
|
| |
+ def test_login_already_loggedin(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/login', follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 302)
|
| |
+ self.assertEqual(urlparse(result.location).path, "/")
|
| |
+
|
| |
+ def test_hub_add_widget_get_no_args(self):
|
| |
+ result = self.app.get('/ralph/add', follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 400)
|
| |
+ expected_str = 'Invalid position provided'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_add_widget_get_with_args(self):
|
| |
+ result = self.app.get('/ralph/add?position=right',
|
| |
+ follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ expected_str = 'Adding widget to hub: ralph'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+ expected_str = "url: 'add/' + $('#widget').val() + '?position=right',"
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_add_widget_post_no_widget_name(self):
|
| |
+ data = {}
|
| |
+ result = self.app.post('/ralph/add', data=data, follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 400)
|
| |
+ expected_str = 'Invalid request sent'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_add_widget_post_invalid_widget_name(self):
|
| |
+ data = {'widget_name': 'invalid_widget_name'}
|
| |
+ result = self.app.post('/ralph/add', data=data, follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 404)
|
| |
+ expected_str = 'Unknown widget called'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_add_widget_post_valid_widget_name_no_args(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ data = {'widget_name': 'about'}
|
| |
+ result = self.app.post('/ralph/add', data=data,
|
| |
+ follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ expected_str = '<h6 class="dropdown-header">Account Information</h6>'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+ expected_str = '<a href="#" class="dropdown-item">Full Name: fullname: ralph</a>'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_add_widget_post_valid_widget_name_with_args(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ data = {'widget_name': 'about', 'text': 'text of widget'}
|
| |
+ result = self.app.post('/ralph/add', data=data,
|
| |
+ follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ expected_str = '<h6 class="dropdown-header">Account Information</h6>'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+ expected_str = '<a href="#" class="dropdown-item">Full Name: fullname: ralph</a>'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ def test_hub_edit_widget_get_logged_in(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ result = self.app.get('/ralph/31/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 200)
|
| |
+ expected_str = '<input id="fmn_context" class="form-control" type="text"'
|
| |
+ self.assertTrue(expected_str in result.data)
|
| |
+
|
| |
+ @unittest.skip("Authorization layer not present yet")
|
| |
+ def test_hub_edit_widget_get_logged_out(self):
|
| |
+ result = self.app.get('/ralph/31/edit', follow_redirects=True)
|
| |
+ self.assertEqual(result.status_code, 403)
|
| |
+
|
| |
+ def test_hub_edit_widget_post_empty_data_logged_in(self):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ data = {}
|
| |
+ url = '/ralph/31/edit'
|
| |
+ result = self.app.post(url, data=data, follow_redirects=False)
|
| |
+ self.assertEqual(result.status_code, 302)
|
| |
+ self.assertEqual(urlparse(result.location).path, '/ralph/')
|
| |
+
|
| |
+ if __name__ == '__main__':
|
| |
+ unittest.main()
|
| |
If we turn on running the tests on jenkins, we should consider using faitout: http://faitout.fedorainfracloud.org/
This is how I handle it in pagure to keep sqlite/memory DB for local tests and use faitout on jenkins: https://pagure.io/pagure/blob/master/f/tests/__init__.py#_40-53
(This can wait for another PR though)