def test_retrieve_message_raises_error(self): """retrieves message from when db does not exist""" with app.app_context(): database.db.drop_all() with current_app.test_request_context(): with self.assertRaises(InternalServerError): Retriever().retrieve_message(1, 'internal.21345')
def test_retrieve_message_list_raises_error(self): """retrieves messages from when db does not exist""" with app.app_context(): database.db.drop_all() with current_app.test_request_context(): with self.assertRaises(InternalServerError): Retriever().retrieve_message_list(1, MESSAGE_QUERY_LIMIT)
def test_msg_returned_with_msg_id_returns_404(self): """retrieves message using id that doesn't exist""" message_id = 1 with app.app_context(): with current_app.test_request_context(): with self.assertRaises(NotFound): Retriever().retrieve_message(message_id, 'internal.21345')
def test_archived_label_is_removed_from_message(self): """testing message is added to database with archived label removed and inbox and read is added instead""" self.populate_database(1) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) message_service = Retriever() message = message_service.retrieve_message( msg_id, 'respondent.21345') modifier = Modifier() modifier.add_archived(message, 'respondent.21345') message = message_service.retrieve_message( msg_id, 'respondent.21345') modifier.del_archived( message, 'respondent.21345', ) message = message_service.retrieve_message( msg_id, 'respondent.21345') self.assertCountEqual(message['labels'], ['SENT'])
def test_archive_is_removed_for_both_respondent_and_internal(self): """testing archive label is removed after being added to both respondent and internal""" self.populate_database(2) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) message_service = Retriever() modifier = Modifier() message = message_service.retrieve_message( msg_id, 'respondent.21345') modifier.add_archived(message, 'respondent.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') modifier.add_archived(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'respondent.21345') modifier.del_archived(message, 'respondent.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') modifier.del_archived(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') self.assertCountEqual(message['labels'], ['UNREAD', 'INBOX']) message = message_service.retrieve_message( msg_id, 'internal.21345') self.assertCountEqual(message['labels'], ['UNREAD', 'INBOX'])
def test_read_date_is_not_reset(self): """testing message read_date is not reset when unread label is removed again""" self.populate_database(1) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) message_service = Retriever() modifier = Modifier() message = message_service.retrieve_message( msg_id, 'internal.21345') modifier.del_unread(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') read_date_set = message['read_date'] modifier.add_unread(message, 'internal.21345') modifier.del_unread(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') self.assertEqual(message['read_date'], read_date_set)
def test_db_connection_test_fails(self): """runs check_db_connection function after dropping database""" with app.app_context(): database.db.drop_all() with current_app.test_request_context(): response = Retriever().check_db_connection() self.assertEqual(response.status_code, 500)
def test_msg_returned_with_msg_id_msg_not_in_database(self): """retrieves message using id""" message_id = 21 self.populate_database(20) with app.app_context(): with current_app.test_request_context(): with self.assertRaises(NotFound): Retriever().retrieve_message(message_id, 'internal.21345')
def test_0_msg_returned_when_db_empty_true(self): """retrieves messages from empty database""" with app.app_context(): with current_app.test_request_context(): response = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT)[1] msg = [] for message in response.items: msg.append(message.serialize) self.assertEqual(msg, [])
def get(message_id): """Get message by id""" # res = authenticate(request) user_urn = request.headers.get( 'user_urn') # getting user urn from header request # check user is authorised to view message message_service = Retriever() # pass msg_id and user urn resp = message_service.retrieve_message(message_id, user_urn) return jsonify(resp)
def test_15_msg_returned_when_db_greater_than_limit(self): """retrieves x messages when database has greater than x entries""" self.populate_database(MESSAGE_QUERY_LIMIT + 5) with app.app_context(): with current_app.test_request_context(): status, response = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT) msg = [] for message in response.items: msg.append(message.serialize) self.assertEqual(len(msg), MESSAGE_QUERY_LIMIT)
def test_paginated_to_json_does_not_return_next_page(self): """turns paginated result list to json checking next page is not returned if not needed""" self.populate_database(MESSAGE_QUERY_LIMIT - 1) with app.app_context(): with current_app.test_request_context(): resp = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT)[1] json_data = MessageList()._paginated_list_to_json( resp, 1, MESSAGE_QUERY_LIMIT, "http://localhost:5050/", 'respondent.21345') data = json.loads(json_data.get_data()) self.assertFalse('next' in data['_links'])
def test_paginated_to_json_returns_prev_page(self): """turns paginated result list to json checking prev page is returned if needed""" self.populate_database(MESSAGE_QUERY_LIMIT * 2) with app.app_context(): with current_app.test_request_context(): resp = Retriever().retrieve_message_list( 2, MESSAGE_QUERY_LIMIT)[1] json_data = MessageList()._paginated_list_to_json( resp, 2, MESSAGE_QUERY_LIMIT, "http://localhost:5050/", 'respondent.21345') data = json.loads(json_data.get_data()) self.assertTrue('prev' in data['_links'])
def test_all_msg_returned_when_db_less_than_limit(self): """retrieves messages from database with less entries than retrieval amount""" self.populate_database(5) with app.app_context(): with current_app.test_request_context(): response = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT)[1] msg = [] for message in response.items: msg.append(message.serialize) self.assertEqual(len(msg), 5)
def test_paginated_to_json_returns_correct_messages_len(self): """turns paginated result list to json checking correct amount of messages are given""" self.populate_database(MESSAGE_QUERY_LIMIT - 1) with app.app_context(): with current_app.test_request_context(): resp = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT)[1] json_data = MessageList()._paginated_list_to_json( resp, 1, MESSAGE_QUERY_LIMIT, "http://localhost:5050/", 'respondent.21345') data = json.loads(json_data.get_data()) self.assertEqual(len(data['messages']), (MESSAGE_QUERY_LIMIT - 1))
def test_paginated_to_json_has_correct_self_link(self): """turns paginated result list to json checking correct self link has been added for list""" self.populate_database(MESSAGE_QUERY_LIMIT - 1) with app.app_context(): with current_app.test_request_context(): resp = Retriever().retrieve_message_list( 1, MESSAGE_QUERY_LIMIT)[1] json_data = MessageList()._paginated_list_to_json( resp, 1, MESSAGE_QUERY_LIMIT, "http://localhost:5050/", 'respondent.21345') data = json.loads(json_data.get_data()) self.assertEqual( data['_links']['self']['href'], "{0}?page={1}&limit={2}".format(self.MESSAGE_LIST_ENDPOINT, 1, MESSAGE_QUERY_LIMIT))
def test_msg_returned_with_msg_id_true(self): """retrieves message using id""" # message_id = "" self.populate_database(20) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) response = Retriever().retrieve_message( msg_id, 'internal.21345') self.assertEqual(response['msg_id'], str(names[0]))
def test_correct_labels_returned_external(self): """retrieves message using id and checks the labels are correct""" self.populate_database(1) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) response = Retriever().retrieve_message( msg_id, 'respondent.21345') labels = ['SENT'] self.assertCountEqual(response['labels'], labels)
def test_correct_to_and_from_returned(self): """retrieves message using id and checks the to and from urns are correct""" self.populate_database(1) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) response = Retriever().retrieve_message( msg_id, 'respondent.21345') urn_to = ['SurveyType'] self.assertEqual(response['urn_to'], urn_to) self.assertEqual(response['urn_from'], 'respondent.21345')
def get(): # res = authenticate(request) res = {'status': "ok"} if res == {'status': "ok"}: page = 1 limit = MESSAGE_QUERY_LIMIT if request.args.get('limit') and request.args.get('page'): page = int(request.args.get('page')) limit = int(request.args.get('limit')) message_service = Retriever() status, result = message_service.retrieve_message_list(page, limit) if status: resp = MessageList._paginated_list_to_json( result, page, limit, request.host_url, request.headers.get('user_urn')) resp.status_code = 200 return resp else: return res
def test_add_archive_is_added_to_internal(self): """testing message is added to database with archived label attached""" self.populate_database(1) with self.engine.connect() as con: query = 'SELECT msg_id FROM secure_message LIMIT 1' query_x = con.execute(query) names = [] for row in query_x: names.append(row[0]) with app.app_context(): with current_app.test_request_context(): msg_id = str(names[0]) message_service = Retriever() message = message_service.retrieve_message( msg_id, 'internal.21345') Modifier.del_archived(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') Modifier.add_archived(message, 'internal.21345') message = message_service.retrieve_message( msg_id, 'internal.21345') self.assertCountEqual(message['labels'], ['UNREAD', 'INBOX', 'ARCHIVE'])
def put(message_id): """Update message by status""" user_urn = request.headers.get('user_urn') request_data = request.get_json() action, label = ModifyById.validate_request(request_data) # pass msg_id and user urn message = Retriever().retrieve_message(message_id, user_urn) if label == Labels.UNREAD.value: resp = ModifyById.modify_unread(action, message, user_urn) else: resp = ModifyById.modify_label(action, message, user_urn, label) if resp: res = jsonify({'status': 'ok'}) res.status_code = 200 else: res = jsonify({'status': 'error'}) res.status_code = 400 return res
def get(): return Retriever().check_db_connection()