def test_missing_date(self): test_copy = test_data.copy() del test_copy['sent_date'] refined_data_copy = refined_data.copy() refined_data_copy['sent_date'] = None refined_message = refine(test_copy) self.assertEqual(refined_message, refined_data_copy)
def test_from_internal(self): test_copy = test_data.copy() test_copy['from_internal'] = True refined_data_copy = refined_data.copy() refined_data_copy['from'] = 'ONS Business Surveys Team' refined_message = refine(test_copy) self.assertEqual(refined_message, refined_data_copy)
def view_conversation(session, thread_id): party_id = session.get('party_id') logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) # TODO, do we really want to do a GET every time, even if we're POSTing? Rops does it this # way so we can get it working, then get it right. conversation = get_conversation(thread_id) logger.info('Successfully retrieved conversation', thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation['messages']) ] except KeyError as e: logger.error('Message is missing important data', thread_id=thread_id, party_id=party_id) raise e if refined_conversation[-1]['unread']: remove_unread_label(refined_conversation[-1]['message_id']) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get('subject') if not conversation['is_closed']: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) send_message( _get_message_json(form, refined_conversation[0], party_id=session['party_id'])) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup('Message sent. <a href={}>View Message</a>'.format( thread_url))) return redirect( url_for('secure_message_bp.view_conversation_list')) return render_template('secure-messages/conversation-view.html', form=form, conversation=refined_conversation, conversation_data=conversation)
def test_refine(self): expected_data = { "subject": "testy2", "survey_id": "02b9c366-7397-42f7-942a-76dc5876d86d", "thread_id": "9e3465c0-9172-4974-a7d1-3a01592d1594", "from": "Peter Griffin", "from_internal": False, "internal_user": "******", "ru_ref": "e359b838-0d89-43e8-b5d0-68079916de80", "sent_date": "02 Apr 2018 10:27", "body": "something else", "message_id": "2ac51b39-a0d7-465d-92a4-263dfe3eb475", "unread": False } refined_message = refine(test_conversation_data) self.assertEqual(refined_message, expected_data)
def view_conversation_list(session): party_id = session.get('party_id') logger.info('Getting conversation list', party_id=party_id) is_closed = request.args.get('is_closed', default='false') params = {'is_closed': is_closed} conversation = get_conversation_list(params=params) try: refined_conversation = [refine(message) for message in conversation] except KeyError as e: logger.error('A key error occurred', party_id=party_id) raise e logger.info('Retrieving and refining conversation successful', party_id=party_id) return render_template('secure-messages/conversation-list.html', messages=refined_conversation, is_closed=strtobool(is_closed))
def test_from_internal(self): expected_data = { "subject": "testy2", "survey_id": "02b9c366-7397-42f7-942a-76dc5876d86d", "thread_id": "9e3465c0-9172-4974-a7d1-3a01592d1594", "from": "ONS Business Surveys Team", "from_internal": True, "internal_user": "******", "ru_ref": "e359b838-0d89-43e8-b5d0-68079916de80", "sent_date": "02 Apr 2018 10:27", "body": "something else", "message_id": "2ac51b39-a0d7-465d-92a4-263dfe3eb475", "unread": False } test_copy = test_conversation_data.copy() test_copy['from_internal'] = True test_copy['internal_user'] = '******' refined_message = refine(test_copy) self.assertEqual(refined_message, expected_data)
def view_conversation_list(session): party_id = session.get_party_id() logger.info("Getting conversation list", party_id=party_id) is_closed = request.args.get("is_closed", default="false") params = {"is_closed": is_closed} conversation = get_conversation_list(params=params) try: refined_conversation = [refine(message) for message in conversation] except KeyError: logger.error("A key error occurred", party_id=party_id) raise logger.info("Retrieving and refining conversation successful", party_id=party_id) unread_message_count = { "unread_message_count": try_message_count_from_session(session) } return render_template( "secure-messages/conversation-list.html", messages=refined_conversation, is_closed=strtobool(is_closed), unread_message_count=unread_message_count, )
def view_conversation(session, thread_id): """Endpoint to view conversations by thread_id""" party_id = session.get_party_id() logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) conversation = get_conversation(thread_id) # secure message will send category in case the conversation is technical or miscellaneous is_survey_category = ( False if "category" in conversation and conversation["category"] in ["TECHNICAL", "MISC"] else True) # sets appropriate message category category = "SURVEY" if is_survey_category else conversation["category"] logger.info("Successfully retrieved conversation", thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation["messages"]) ] except KeyError: logger.error("Message is missing important data", thread_id=thread_id, party_id=party_id) raise if refined_conversation[-1]["unread"]: remove_unread_label(refined_conversation[-1]["message_id"]) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get("subject") if not conversation["is_closed"]: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) msg_to = get_msg_to(refined_conversation) if is_survey_category: send_message( _get_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id)) else: send_message( _get_non_survey_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id, category=category)) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect( url_for("secure_message_bp.view_conversation_list")) unread_message_count = { "unread_message_count": get_message_count_from_api(session) } survey_name = None business_name = None if is_survey_category: try: survey_name = get_survey( app.config["SURVEY_URL"], app.config["BASIC_AUTH"], refined_conversation[-1]["survey_id"]).get("longName") except ApiError as exc: logger.info("Failed to get survey name, setting to None", status_code=exc.status_code) try: business_name = conversation["messages"][-1]["@business_details"][ "name"] except (KeyError, TypeError): logger.info("Failed to get business name, setting to None") return render_template( "secure-messages/conversation-view.html", form=form, conversation=refined_conversation, conversation_data=conversation, unread_message_count=unread_message_count, survey_name=survey_name, business_name=business_name, category=category, )
def test_missing_business_id(self): test_copy = test_conversation_data.copy() del test_copy['@business_details'] with self.assertRaises(KeyError): refine(test_copy)
def test_missing_name(self): test_copy = test_conversation_data.copy() del test_copy['@msg_from'] with self.assertRaises(KeyError): refine(test_copy)
def test_missing_subject(self): test_copy = test_conversation_data.copy() del test_copy['subject'] with self.assertRaises(KeyError): refine(test_copy)
def view_conversation(session, thread_id): """Endpoint to view conversations by thread_id""" party_id = session.get_party_id() logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) conversation = get_conversation(thread_id) logger.info('Successfully retrieved conversation', thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation['messages']) ] except KeyError: logger.error('Message is missing important data', thread_id=thread_id, party_id=party_id) raise if refined_conversation[-1]['unread']: remove_unread_label(refined_conversation[-1]['message_id']) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get('subject') if not conversation['is_closed']: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) msg_to = get_msg_to(refined_conversation) send_message( _get_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id)) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup(f'Message sent. <a href={thread_url}>View Message</a>')) return redirect( url_for('secure_message_bp.view_conversation_list')) unread_message_count = { 'unread_message_count': get_message_count_from_api(session) } survey_name = None try: survey_name = get_survey( app.config['SURVEY_URL'], app.config['BASIC_AUTH'], refined_conversation[-1]['survey_id']).get('longName') except ApiError as exc: logger.info('Failed to get survey name, setting to None', status_code=exc.status_code) business_name = None try: business_name = conversation['messages'][-1]['@business_details'][ 'name'] except KeyError: logger.info('Failed to get business name, setting to None') return render_template('secure-messages/conversation-view.html', form=form, conversation=refined_conversation, conversation_data=conversation, unread_message_count=unread_message_count, survey_name=survey_name, business_name=business_name)
def test_refine(self): refined_message = refine(test_data) self.assertEqual(refined_message, refined_data)
def test_missing_ru_id(self): test_copy = test_data.copy() del test_copy['@ru_id'] with self.assertRaises(KeyError): refine(test_copy)