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_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_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_draft_label_is_deleted(self): """Check draft label is deleted for message""" with app.app_context(): with current_app.test_request_context(): self.test_message = { 'msg_id': 'test123', 'urn_to': 'richard', 'urn_from': 'respondent.richard', 'subject': 'MyMessage', 'body': 'hello', 'thread_id': '', 'collection_case': 'ACollectionCase', 'reporting_unit': 'AReportingUnit', 'survey': 'ACollectionInstrument' } modifier = Modifier() with self.engine.connect() as con: add_draft = ( "INSERT INTO status (label, msg_id, actor) " "VALUES ('{0}', 'test123', 'respondent.richard')" ).format(Labels.DRAFT.value) con.execute(add_draft) modifier.del_draft(self.test_message['msg_id']) with self.engine.connect() as con: request = con.execute( "SELECT * FROM status WHERE msg_id='{0}' AND actor='{1}'" .format('test123', 'respondent.richard')) for row in request: self.assertTrue(row is None) break else: pass
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_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 setUp(self): """setup test environment""" self.app = application.app.test_client() app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/messages.db' self.engine = create_engine('sqlite:////tmp/messages.db', echo=True) AlertUser.alert_method = mock.Mock(AlertViaGovNotify) self.url = "http://localhost:5050/draft/save" self.headers = {'Content-Type': 'application/json', 'user_urn': ''} self.test_message = { 'urn_to': 'richard', 'urn_from': 'torrance', 'subject': 'MyMessage', 'body': 'hello', 'thread_id': '', 'collection_case': 'ACollectionCase', 'reporting_unit': 'AReportingUnit', 'survey': 'ACollectionInstrument' } with app.app_context(): database.db.init_app(current_app) database.db.drop_all() database.db.create_all() self.db = database.db
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_save_message_rasies_message_save_exception_on_db_error(self): """Tests exception is logged if message save fails""" mock_session = mock.Mock(db.session) mock_session.commit.side_effect = Exception("Not Saved") with app.app_context(): with current_app.test_request_context(): with self.assertRaises(MessageSaveException): Saver().save_message(self.test_message, mock_session)
def setUp(self): """setup test environment""" self.app = application.app.test_client() app.testing = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/messages.db' with app.app_context(): database.db.init_app(current_app) database.db.drop_all() database.db.create_all() self.db = database.db
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 test_save_msg_status_raises_message_save_exception_on_db_error(self): """Tests MessageSaveException generated if db commit fails saving message""" mock_session = mock.Mock(db.session) mock_session.commit.side_effect = Exception("Not Saved") message_status = {'msg_id': 'AMsgId', 'actor': 'Tej'} with app.app_context(): with current_app.test_request_context(): with self.assertRaises(MessageSaveException): Saver().save_msg_status(message_status['msg_id'], message_status['actor'], 'INBOX', mock_session)
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_save_msg_audit_raises_message_save_exception_on_db_error(self): """Tests MessageSaveException generated if db commit fails saving message audit""" mock_session = mock.Mock(db.session) mock_session.commit.side_effect = Exception("Not Saved") message_audit = {'msg_id': 'MsgId', 'msg_urn': 'Tej'} with app.app_context(): with current_app.test_request_context(): with self.assertRaises(MessageSaveException): Saver().save_msg_audit(message_audit['msg_id'], message_audit['msg_urn'], mock_session)
def test_msg_audit_has_been_saved(self): """Tests message audit is saved to database""" message_audit = {'msg_id': 'MsgId', 'msg_urn': 'Tej'} with app.app_context(): with current_app.test_request_context(): Saver().save_msg_audit(message_audit['msg_id'], message_audit['msg_urn']) with self.engine.connect() as con: request = con.execute('SELECT * FROM internal_sent_audit') for row in request: self.assertTrue(row is not None)
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 setUp(self): """setup test environment""" app.testing = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/messages.db' self.engine = create_engine('sqlite:////tmp/messages.db', echo=True) self.MESSAGE_LIST_ENDPOINT = "http://localhost:5050/messages" self.MESSAGE_BY_ID_ENDPOINT = "http://localhost:5050/message/" with app.app_context(): database.db.init_app(current_app) database.db.drop_all() database.db.create_all() self.db = database.db
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_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_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_saved_msg_status_has_been_saved(self): """retrieves message status from database""" message_status = {'msg_id': 'AMsgId', 'actor': 'Tej'} with app.app_context(): with current_app.test_request_context(): Saver().save_msg_status(message_status['msg_id'], message_status['actor'], 'INBOX, UNREAD') with self.engine.connect() as con: request = con.execute('SELECT * FROM status') for row in request: self.assertTrue(row is not None)
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 before_scenario(context): AlertUser.alert_method = mock.Mock(AlertViaGovNotify) with app.app_context(): database.db.init_app(current_app) database.db.drop_all() database.db.create_all() data.update({ 'urn_to': 'test', 'urn_from': 'respondent.test', 'subject': 'Hello World', 'body': 'Test', 'thread_id': '', 'collection_case': 'collection case1', 'reporting_unit': 'reporting case1', 'survey': 'survey'})
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 setUp(self): """setup test environment""" app.testing = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/messages.db' self.engine = create_engine('sqlite:////tmp/messages.db', echo=True) self.test_message = Message( **{ 'urn_to': 'tej', 'urn_from': 'gemma', 'subject': 'MyMessage', 'body': 'hello', 'thread_id': "" }) with app.app_context(): database.db.init_app(current_app) database.db.drop_all() database.db.create_all() self.db = database.db
def test_saved_message_has_not_saved_sent_date(self): message = Message( **{ 'msg_id': 'Amsgid', 'urn_to': 'tej', 'urn_from': 'gemma', 'subject': 'MyMessage', 'body': 'hello', 'thread_id': "" }) with app.app_context(): with current_app.test_request_context(): Saver().save_message(message) with self.engine.connect() as con: request = con.execute( "SELECT * FROM secure_message WHERE msg_id='Amsgid'") for row in request: data = {"sent_date": row['sent_date']} self.assertTrue(data['sent_date'] is None)