예제 #1
0
    def post(self, local_data=None):
        """Endpoint for posting a new message to communications table

        :param json_string local_data: This should be used in place of the POST body that would be used through HTTP, used for local calls.

        :returns: response_object, status_code
        :rtype: tuple (dict, int)
        """
        response_object = {'status': 'fail', 'message': 'Invalid payload'}

        if not local_data:
            post_data = request.get_json()
        else:
            post_data = json.loads(local_data)

        # NOTE: why do we do this
        # TODO: also we need a validator...
        post_data.update(
            {'timestamp': datetime.datetime.now(datetime.timezone.utc)})

        #TODO: assertion checks for proper data types (in validation.py)

        message = Communications(**post_data)
        db.session.add(message)
        db.session.commit()

        response_object = {
            'status': 'success',
            'message': f'message {message.message} was sent!',
            'data': message.to_json()
        }

        return response_object, 201
예제 #2
0
    def get(self, message_id):
        """Endpoint for getting a specific message

        :param int message_id: The message id

        :returns: response_object, status_code
        :rtype: tuple (dict, int)
        """
        response_object = {
            'status': 'fail',
            'message': 'message does not exist'
        }

        # get a single message via it's ID
        message = Communications.filter_by(id=message_id).first() # should only be one

        if not message: # query of Communications returned nothing
            return response_object, 404
        else: # there is a message in Communications with message_id
            response_object = {
                'status': 'success',
                'data': message.to_json()
            }

            return response_object, 200
예제 #3
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'])
예제 #4
0
    def post(self, local_data=None):
        """Endpoint for posting a new message to communications table

        :param json_string local_data: This should be used in place of the POST body that would be used through HTTP, used for local calls.

        :returns: response_object, status_code
        :rtype: tuple (dict, int)
        """
        response_object = {
            'status': 'fail',
            'message': 'Invalid payload'
        }

        if not local_data:
            post_data = request.get_json()
        else:
            post_data = json.loads(local_data)

        # NOTE: why do we do this
        # TODO: also we need a validator...
        post_data.update({'timestamp': datetime.datetime.now(datetime.timezone.utc)})

        #TODO: assertion checks for proper data types

        # message should look like:
        # dict {
        # command : string of the form "COMMAND_NAME ARG1 ARG2 ... ARGN" or a string with a response from the satellite
        # timestamp : autogenerated time for when function was called
        # sender : string
        # receiver : string
        # }
        message = Communications(**post_data)
        db.session.add(message)
        db.session.commit()

        response_object = {
            'status': 'success',
            'message': f'message {message.message} was sent!',
            'data': message.to_json()
        }

        return response_object, 201
예제 #5
0
파일: utils.py 프로젝트: AlbertaSat/_ex2_
def add_message_to_communications(timestamp, message, receiver, sender):
    """Add a new message to the communications table
    """
    message = Communications(timestamp=timestamp,
                             message=message,
                             receiver=receiver,
                             sender=sender)

    db.session.add(message)
    db.session.commit()
    return message
예제 #6
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'])
예제 #7
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'])
예제 #8
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'])