Esempio n. 1
0
 def test_local_post_with_auth(self):
     current_app.config.update(BYPASS_AUTH=False)
     test_message = fake_message_as_dict(sender='Bob')
     test_message['timestamp'] = str(test_message['timestamp'])
     endpoint = CommunicationList()
     response = endpoint.post(local_data=json.dumps(test_message))
     self.assertEqual(response[1], 201)
     self.assertIn('success', response[0]['status'])
Esempio n. 2
0
    def test_get_communications_with_query_params(self):
        test_message_1 = fake_message_as_dict()
        test_message_2 = fake_message_as_dict(message='test 2')

        test_message_1 = Communications(**test_message_1)
        test_message_2 = Communications(**test_message_2)

        db.session.add(test_message_1)
        db.session.add(test_message_2)
        db.session.commit()

        with self.client:
            response = self.client.get('/api/communications?last_id=1&receiver=tester2')
            data = json.loads(response.data.decode())

            self.assertEqual(response.status_code, 200)
            self.assertEqual(data['status'], 'success')
            self.assertEqual(len(data['data']['messages']), 1)
            self.assertIn('test 2', data['data']['messages'][0]['message'])
Esempio n. 3
0
    def test_get_all_communications(self):
        test_message_1 = fake_message_as_dict()
        test_message_2 = fake_message_as_dict(message='test 2')

        test_message_1 = Communications(**test_message_1)
        test_message_2 = Communications(**test_message_2)

        db.session.add(test_message_1)
        db.session.add(test_message_2)
        db.session.commit()

        with self.client:
            response=self.client.get('/api/communications')
            data=json.loads(response.data.decode())
            # print(data)

            self.assertEqual(response.status_code, 200)
            self.assertEqual(data['status'], 'success')
            self.assertEqual(len(data['data']['messages']), 2)
            self.assertIn('test', data['data']['messages'][0]['message'])
            self.assertIn('test 2', data['data']['messages'][1]['message'])
Esempio n. 4
0
    def test_get_communication_with_max_id_non_empty_db(self):
        test_message_1 = fake_message_as_dict()
        test_message_2 = fake_message_as_dict(message='test 2')

        test_message_1 = Communications(**test_message_1)
        test_message_2 = Communications(**test_message_2)

        db.session.add(test_message_1)
        db.session.add(test_message_2)
        db.session.commit()

        correct_max_id = test_message_2.id

        with self.client:
            response = self.client.get('/api/communications?max=true')
            data = json.loads(response.data.decode())

            self.assertEqual(response.status_code, 200)
            self.assertEqual(data['status'], 'success')
            self.assertEqual(len(data['data']['messages']), 1)
            self.assertEqual(correct_max_id, data['data']['messages'][0]['message_id'])
Esempio n. 5
0
 def test_post_as_unauthenticated_user(self):
     current_app.config.update(BYPASS_AUTH=False)
     user = add_user('Bob', 'password', is_admin=False)
     auth_token = user.encode_auth_token_by_id().decode()
     test_message = fake_message_as_dict(sender='Bob')
     test_message['timestamp'] = str(test_message['timestamp'])
     with self.client:
         response = self.client.post(
             '/api/communications',
             data=json.dumps(test_message),
             content_type='application/json'
         )
         data=json.loads(response.data.decode())
         self.assertEqual(response.status_code, 401)
Esempio n. 6
0
 def test_post_with_invalid_token(self):
     current_app.config.update(BYPASS_AUTH=False)
     user = add_user('Bob', 'password', is_admin=False)
     auth_token = "uydbisjanxsifbinewkrnieuwd"
     test_message = fake_message_as_dict(sender='Bob')
     test_message['timestamp'] = str(test_message['timestamp'])
     with self.client:
         response = self.client.post(
             '/api/communications',
             headers={'Authorization': f'Bearer {auth_token}'},
             data=json.dumps(test_message),
             content_type='application/json'
         )
         data=json.loads(response.data.decode())
         self.assertEqual(response.status_code, 401)
Esempio n. 7
0
    def test_get_all_communications_newest_first(self):
        messages = []
        for i in range(10):
            test_message = Communications(**fake_message_as_dict(message=f'test message {i}'))
            messages.append(test_message)
            db.session.add(test_message)

        db.session.commit()

        messages.sort(key=lambda obj : -1 * obj.id)

        with self.client:
            response=self.client.get('/api/communications?newest-first=true')
            data=json.loads(response.data.decode())
            # print(data)
            for resp_message_idx in range(len(data['data']['messages'])):
                self.assertEqual(messages[resp_message_idx].id, data['data']['messages'][resp_message_idx]['message_id'])
Esempio n. 8
0
    def test_post_valid_communication(self):
        # service = CommunicationsList()
        test_message = fake_message_as_dict()
        test_message['timestamp'] = str(test_message['timestamp'])
        # response = service.post()

        with self.client:
            response = self.client.post(
                '/api/communications',
                data=json.dumps(test_message),
                content_type='application/json'
            )
            data=json.loads(response.data.decode())
            self.assertEqual(response.status_code, 201)
            # print(test_message)
            msg = test_message['message']
            self.assertEqual(
                f'message {msg} was sent!',
                data['message']
            )
            self.assertEqual('success', data['status'])