class TestSave(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_save_model(self): ws = MockWebSocket() ws.mock_incoming_message(save_company) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'save', 'target': 'company', 'code': 'success', 'data': { 'id': 1, 'name': 'Snackbar', 'type': 'notIT', } }, json.loads(m))
class TestSub(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_delete(self): ws = MockWebSocket() c = Company({ 'name': 'Slagerij Henk', }) db.session.add(c) db.session.commit() delete_message = { 'requestId': '1234', 'target': 'company', 'type': 'delete', 'data': { 'id': c.id, } } ws.mock_incoming_message(subscribe_company) ws.mock_incoming_message(delete_message) self.client.open_connection(ws) self.assertEqual(3, len(ws.outgoing_messages)) r1 = json.loads(ws.outgoing_messages[0]) r2 = json.loads(ws.outgoing_messages[1]) r3 = json.loads(ws.outgoing_messages[2]) self.assertEqual('publish', r1['type']) self.assertEqual(1, len(r1['data']['add'])) self.assertEqual('success', r1['code']) self.assertEqual('delete', r3['type']) self.assertEqual('success', r3['code']) self.assertDictEqual( { 'type': 'publish', 'target': 'company', 'requestId': '1234', 'data': { 'remove': [{ 'id': c.id, 'name': c.name, 'type': 'notIT', }] }, }, r2)
class TestPing(TestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_pong(self): ws = MockWebSocket() ws.mock_incoming_message('ping') self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) # Whoops our response is a json encoded string self.assertEqual('pong', ws.outgoing_messages[0])
class TestDelete(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_delete(self): ws = MockWebSocket() c = Company({ 'name': 'Slagerij Henk', }) db.session.add(c) db.session.commit() delete_message = { 'requestId': '1234', 'target': 'company', 'type': 'delete', 'data': { 'id': c.id, } } ws.mock_incoming_message(delete_message) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'delete', 'target': 'company', 'requestId': '1234', 'data': { 'id': c.id, 'name': c.name, 'type': 'notIT', }, 'code': 'success', }, json.loads(m))
class TestNotAuth(TestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_company(self): ws = MockWebSocket() ws.mock_incoming_message({ 'target': 'company', 'type': 'ask_for_raise', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual({ 'code': 'error', 'message': 'Unauthorized', 'type': 'ask_for_raise', }, json.loads(m))
class TestSub(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_save(self): ws = MockWebSocket() ws.mock_incoming_message(subscribe_company) ws.mock_incoming_message(save_company) self.client.open_connection(ws) self.assertEqual(3, len(ws.outgoing_messages)) r1 = json.loads(ws.outgoing_messages[0]) r2 = json.loads(ws.outgoing_messages[1]) r3 = json.loads(ws.outgoing_messages[2]) self.assertEqual('publish', r1['type']) self.assertEqual('success', r1['code']) self.assertEqual('save', r3['type']) self.assertEqual('success', r3['code']) self.assertDictEqual( { 'type': 'publish', 'target': 'company', 'requestId': '1234', 'data': { 'add': [{ 'id': 1, 'name': 'Snackbar', 'type': 'notIT', }] }, }, r2)
def setUp(self): self.client = Client(app, db) super().setUp()
class TestAuth(TestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_unauth_request(self): ws = MockWebSocket() ws.mock_incoming_message(subscribe_company) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'subscribe', 'message': 'Unauthorized', 'code': 'error', }, json.loads(m)) def test_google_oauth(self): def mock_oauth(request, **kwargs): return MockResponse({'id_token': create_fake_google_id_token()}, 200) self.client.set_mock_api(mock_oauth) ws = MockWebSocket() ws.mock_incoming_message(authenticate) g = greenlet(self.client.open_connection) g.switch(ws, app=self.client.app) self.assertEqual(1, requests.post.call_count) call_params = requests.post.call_args_list[0][1]['params'] self.assertTrue('client_id' in call_params) self.assertTrue('client_secret' in call_params) self.assertTrue('redirect_uri' in call_params) self.assertTrue(call_params['code'], authenticate['data']['code']) self.assertTrue(call_params['grant_type'], 'authorization_code') users = self.client.db.session.query(User).all() self.assertEqual(1, len(users)) self.assertEqual('*****@*****.**', users[0].email) self.assertEqual(1, len(ws.outgoing_messages)) m = json.loads(ws.outgoing_messages[0]) self.assertEqual('authenticate', m['type']) self.assertEqual('success', m['code']) self.assertTrue('authorization' in m) # Now check that the response auth code actually works req_bootstrap = { 'type': 'bootstrap', 'authorization': m['authorization'], } ws.mock_incoming_message(req_bootstrap) g.switch() self.assertEqual(2, len(ws.outgoing_messages)) m = json.loads(ws.outgoing_messages[1]) self.assertEqual('bootstrap', m['type']) self.assertEqual('success', m['code']) self.assertEqual('Henk de Vries', m['data']['username'])
class TestRecon(TestCase): ''' In the Router we have some sanity checks These sanitychecks may only return useful information if the user is authenticated ''' def setUp(self): self.client = Client(app, db) super().setUp() def test_no_target(self): ws = MockWebSocket() ws.mock_incoming_message({ 'type': 'subscribe', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'subscribe', 'message': 'Unauthorized', 'code': 'error', }, json.loads(m)) def test_invalid_target(self): ws = MockWebSocket() ws.mock_incoming_message({ 'type': 'subscribe', 'target': "Robert';) DROP TABLE Students; --" }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'subscribe', 'message': 'Unauthorized', 'code': 'error', }, json.loads(m)) def test_invalid_type(self): ws = MockWebSocket() ws.mock_incoming_message({ 'type': 'PUT', 'target': 'company', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'PUT', 'message': 'Unauthorized', 'code': 'error', }, json.loads(m))
class TestCustom(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_company(self): ws = MockWebSocket() ws.mock_incoming_message({ 'target': 'company', 'type': 'ask_for_raise', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual({ 'answer': 'no', 'code': 'error', }, json.loads(m)) def test_db_connection(self): c = Company(name='Post office') db.session.add(c) e1 = Employee(name='Henk') e2 = Employee(name='Peter') e1.company = c e2.company = c db.session.add(e1, e2) db.session.commit() ws = MockWebSocket() ws.mock_incoming_message({ 'target': 'company', 'data': {'id': 1}, 'type': 'list_employees', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual({ 'code': 'success', 'data': ['Henk', 'Peter'], 'type': 'list_employees', }, json.loads(m)) def test_exception(self): ws = MockWebSocket() ws.mock_incoming_message({ 'target': 'company', 'data': {'id': 1}, 'type': 'list_employees_error', }) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) m = ws.outgoing_messages[0] self.assertDictEqual({ 'code': 'error', 'type': 'list_employees_error', 'message': "'CompanyController' object has no attribute 'type'", }, json.loads(m))
class TestSub(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_update_into_scope(self): ws = MockWebSocket() c = Company(name='Butcher') db.session.add(c) db.session.commit() ws.mock_incoming_message(subscribe_company) ws.mock_incoming_message(update_company) self.client.open_connection(ws) self.assertEqual(3, len(ws.outgoing_messages)) r1 = json.loads(ws.outgoing_messages[0]) r2 = json.loads(ws.outgoing_messages[1]) r3 = json.loads(ws.outgoing_messages[2]) self.assertEqual('publish', r1['type']) self.assertEqual(0, len(r1['data']['add'])) self.assertEqual('success', r1['code']) self.assertEqual('update', r3['type']) self.assertEqual('success', r3['code']) self.assertDictEqual( { 'type': 'publish', 'target': 'company', 'requestId': '1234', 'data': { 'add': [{ 'id': c.id, 'name': c.name, 'type': 'notIT', }] }, }, r2) def test_update_out_of_scope(self): ws = MockWebSocket() c = Company(name='Butcher') db.session.add(c) db.session.commit() subscribe_company = { 'requestId': '9012', 'target': 'company', 'scope': { 'name': 'Butcher', }, 'type': 'subscribe', } ws.mock_incoming_message(subscribe_company) ws.mock_incoming_message(update_company) self.client.open_connection(ws) self.assertEqual(3, len(ws.outgoing_messages)) r1 = json.loads(ws.outgoing_messages[0]) r2 = json.loads(ws.outgoing_messages[1]) r3 = json.loads(ws.outgoing_messages[2]) self.assertEqual('publish', r1['type']) self.assertEqual(1, len(r1['data']['add'])) self.assertEqual('success', r1['code']) self.assertEqual('update', r3['type']) self.assertEqual('success', r3['code']) self.assertDictEqual( { 'type': 'publish', 'target': 'company', 'requestId': '9012', 'data': { 'remove': [{ 'id': c.id, 'name': c.name, 'type': 'notIT', }] }, }, r2) def test_update_already_in_scope(self): ws = MockWebSocket() c = Company(name='Butcher') db.session.add(c) db.session.commit() subscribe_company = { 'requestId': '5678', 'target': 'company', 'data': { 'id': 1, }, 'type': 'subscribe', } ws.mock_incoming_message(subscribe_company) ws.mock_incoming_message(update_company) self.client.open_connection(ws) self.assertEqual(3, len(ws.outgoing_messages)) r1 = json.loads(ws.outgoing_messages[0]) r2 = json.loads(ws.outgoing_messages[1]) r3 = json.loads(ws.outgoing_messages[2]) self.assertEqual('publish', r1['type']) self.assertEqual(1, len(r1['data']['add'])) self.assertEqual('success', r1['code']) self.assertEqual('update', r3['type']) self.assertEqual('success', r3['code']) self.assertDictEqual( { 'type': 'publish', 'target': 'company', 'requestId': '5678', 'data': { 'update': [{ 'id': c.id, 'name': c.name, 'type': 'notIT', }] }, }, r2)
class TestParse(LoggedInTestCase): def setUp(self): self.client = Client(app, db) super().setUp() def test_fk_date_datetime(self): ws = MockWebSocket() ws.mock_incoming_message(save_company) g = greenlet(self.client.open_connection) g.switch(ws, app=self.client.app) self.assertEqual(1, len(ws.outgoing_messages)) c = json.loads(ws.outgoing_messages[0])['data'] ws.mock_incoming_message({ 'target': 'employee', 'type': 'save', 'data': { 'name': 'Henk', 'company': c['id'], 'started_at': '2017-09-16T15:59:36+02:00', 'birthday': '1993-03-21', }, }) g.switch() self.assertEqual(2, len(ws.outgoing_messages)) r2 = json.loads(ws.outgoing_messages[1]) self.assertDictEqual( { 'target': 'employee', 'type': 'save', 'code': 'success', 'data': { 'id': 1, 'name': 'Henk', 'company': c['id'], 'started_at': '2017-09-16T13:59:36+00:00', 'birthday': '1993-03-21', }, }, r2) c = Company.query.get(1) e = Employee.query.get(1) self.assertEqual(e.company, c) self.assertEqual(c.employees.first(), e) def test_enum(self): ws = MockWebSocket() ws.mock_incoming_message(save_company) self.client.open_connection(ws) self.assertEqual(1, len(ws.outgoing_messages)) res = ws.outgoing_messages[0] self.assertDictEqual( { 'type': 'save', 'target': 'company', 'code': 'success', 'data': { 'id': 1, 'name': 'Developers', 'type': 'IT', } }, json.loads(res))