Пример #1
0
    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))
Пример #2
0
    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])
Пример #3
0
    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))
Пример #4
0
    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))
Пример #5
0
    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))
Пример #6
0
    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))
Пример #7
0
    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))
Пример #8
0
    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))
Пример #9
0
    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))
Пример #10
0
    def test_fails_without_id(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))
        res_save = json.loads(ws.outgoing_messages[0])
        self.assertEqual(res_save['code'], 'success')

        update_no_id = {
            'target': 'company',
            'type': 'update',
            'data': {
                'name': 'Hairdresser',
            }
        }

        ws.mock_incoming_message(update_no_id)
        g.switch()

        self.assertEqual(2, len(ws.outgoing_messages))
        res_update = json.loads(ws.outgoing_messages[1])

        self.assertDictEqual(
            {
                'type': 'update',
                'code': 'error',
                'message': 'No id given',
            }, res_update)
Пример #11
0
    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)
Пример #12
0
    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))
Пример #13
0
    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)
Пример #14
0
    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)
Пример #15
0
    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'])
Пример #16
0
    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)
Пример #17
0
    def test_update_model(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))
        res_save = json.loads(ws.outgoing_messages[0])
        self.assertEqual(res_save['code'], 'success')

        ws.mock_incoming_message(update_company)
        g.switch()

        self.assertEqual(2, len(ws.outgoing_messages))
        res_update = json.loads(ws.outgoing_messages[1])

        self.assertEqual('update', res_update['type'])
        self.assertEqual('company', res_update['target'])
        self.assertEqual('success', res_update['code'])
        self.assertEqual('Hairdresser', res_update['data']['name'])
        self.assertEqual(1, res_update['data']['id'])
Пример #18
0
    def test_stop_receiving_publishes(self):
        ws1 = MockWebSocket()
        ws2 = MockWebSocket()
        ws1.mock_incoming_message(subscribe_company)

        g1 = greenlet(self.client.open_connection)
        g1.switch(ws1, app=self.client.app)

        ws2.mock_incoming_message(save_company)
        g2 = greenlet(self.client.open_connection)
        g2.switch(ws2, app=self.client.app)

        # check ws1 receives publish
        self.assertEqual(2, len(ws1.outgoing_messages))
        self.assertDictEqual(
            {
                'type': 'publish',
                'target': 'company',
                'requestId': '1234',
                'data': {
                    'add': [{
                        'id': 1,
                        'name': 'Garage',
                        'type': 'notIT'
                    }]
                }
            }, json.loads(ws1.outgoing_messages[1]))

        ws1.mock_incoming_message(unsubscribe_company)
        g1.switch()

        ws2.mock_incoming_message(update_company)
        g2.switch()
        self.assertEqual(2, len(ws2.outgoing_messages))
        self.assertEqual('success',
                         json.loads(ws2.outgoing_messages[1])['code'])

        # check ws1 does no receives publish
        self.assertEqual(3, len(ws1.outgoing_messages))
        self.assertDictEqual(
            {
                'type': 'unsubscribe',
                'code': 'success',
                'requestId': '1234',
            }, json.loads(ws1.outgoing_messages[2]))