def test_send_ticket_user_profile(notify_api: Flask): def match_json(request): expected = { "product_id": 42, "subject": "Support Request", "description": "<br><br>---<br><br> user_profile", "email": "*****@*****.**", "priority": 1, "status": 2, "tags": [], } encoded_auth = base64.b64encode(b"freshdesk-api-key:x").decode("ascii") json_matches = request.json() == expected basic_auth_header = request.headers.get("Authorization") == f"Basic {encoded_auth}" return json_matches and basic_auth_header with requests_mock.mock() as rmock: rmock.request( "POST", "https://freshdesk-test.com/api/v2/tickets", additional_matcher=match_json, status_code=201, ) with notify_api.app_context(): response = Freshdesk( ContactRequest( email_address="*****@*****.**", user_profile="user_profile", ) ).send_ticket() assert response == 201
def test_contact_info_sanitize_input(): mock_dict_sanitize = { "email_address": "*****@*****.**", "name": "<script>alert('hello')</script>", } contact = ContactRequest(**mock_dict_sanitize) assert contact.name == "<script>alert('hello')</script>"
def test_contact_info_additional_fields(): mock_dict_new_field = { "email_address": "*****@*****.**", "invalid_additional_field": "new_field", } with pytest.raises(TypeError): ContactRequest(**mock_dict_new_field)
def test_send_ticket_go_live_request(notify_api: Flask): def match_json(request): expected = { "product_id": 42, "subject": "Support Request", "description": "t6 just requested to go live.<br><br>" "- Department/org: department_org_name<br>" "- Intended recipients: internal, external, public<br>" "- Purpose: main_use_case<br>" "- Notification types: email, sms<br>" "- Expected monthly volume: 100k+<br>" "---<br>" "http://*****:*****@email.com", "priority": 1, "status": 2, "tags": [], } encoded_auth = base64.b64encode(b"freshdesk-api-key:x").decode("ascii") json_matches = request.json() == expected basic_auth_header = request.headers.get("Authorization") == f"Basic {encoded_auth}" return json_matches and basic_auth_header with requests_mock.mock() as rmock: rmock.request( "POST", "https://freshdesk-test.com/api/v2/tickets", additional_matcher=match_json, status_code=201, ) data: Dict[str, Any] = { "email_address": "*****@*****.**", "name": "name", "department_org_name": "department_org_name", "intended_recipients": "internal, external, public", "main_use_case": "main_use_case", "friendly_support_type": "Support Request", "support_type": "go_live_request", "service_name": "t6", "service_id": "8624bd36-b70b-4d4b-a459-13e1f4770b92", "service_url": "http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92", "notification_types": "email, sms", "expected_volume": "100k+", } with notify_api.app_context(): response = Freshdesk(ContactRequest(**data)).send_ticket() assert response == 201
def test_send_ticket_demo(notify_api: Flask): def match_json(request): expected = { "product_id": 42, "subject": "friendly-support-type-test", "description": "- user: name-test [email protected]<br><br>" "- department/org: dept-test<br><br>" "- program/service: service-test<br><br>" "- intended recipients: internal<br><br>" "- main use case: main-use-case-test<br><br>" "- main use case details: main-use-case-details-test", "email": "*****@*****.**", "priority": 1, "status": 2, "tags": [], } encoded_auth = base64.b64encode(b"freshdesk-api-key:x").decode("ascii") json_matches = request.json() == expected basic_auth_header = request.headers.get("Authorization") == f"Basic {encoded_auth}" return json_matches and basic_auth_header with requests_mock.mock() as rmock: rmock.request( "POST", "https://freshdesk-test.com/api/v2/tickets", additional_matcher=match_json, status_code=201, ) contact_request: Dict[str, Any] = { "email_address": "*****@*****.**", "name": "name-test", "department_org_name": "dept-test", "program_service_name": "service-test", "intended_recipients": "internal", "main_use_case": "main-use-case-test", "main_use_case_details": "main-use-case-details-test", "friendly_support_type": "friendly-support-type-test", "language": "en", "support_type": "demo", } with notify_api.app_context(): response = Freshdesk(ContactRequest(**contact_request)).send_ticket() assert response == 201
def test_contact_info(): mock_dict = { "email_address": "*****@*****.**", "tags": ["test1", "test2"], "name": "test_name", "department_org_name": "org", "program_service_name": "service", "intended_recipients": "recipients", "main_use_case": "usecase", "main_use_case_details": "details", "friendly_support_type": "f_support_type", "language": "en", "support_type": "support_type", "message": "message", "user_profile": "user_profile", "service_name": "service_name", "service_id": "service_id", "service_url": "service_url", "notification_types": "notification_types", "expected_volume": "expected_volume", } contact = ContactRequest(**mock_dict) assert mock_dict["email_address"] == contact.email_address assert mock_dict["name"] == contact.name assert mock_dict["department_org_name"] == contact.department_org_name assert mock_dict["program_service_name"] == contact.program_service_name assert mock_dict["intended_recipients"] == contact.intended_recipients assert mock_dict["main_use_case"] == contact.main_use_case assert mock_dict["main_use_case_details"] == contact.main_use_case_details assert mock_dict["friendly_support_type"] == contact.friendly_support_type assert mock_dict["language"] == contact.language assert mock_dict["support_type"] == contact.support_type assert mock_dict["message"] == contact.message assert mock_dict["user_profile"] == contact.user_profile assert mock_dict["service_name"] == contact.service_name assert mock_dict["service_id"] == contact.service_id assert mock_dict["service_url"] == contact.service_url assert mock_dict["notification_types"] == contact.notification_types assert mock_dict["expected_volume"] == contact.expected_volume
def test_contact_info_defaults(): mock_dict = { "email_address": "*****@*****.**", } contact = ContactRequest(**mock_dict) assert mock_dict["email_address"] == contact.email_address assert contact.language == "en" assert contact.friendly_support_type == "Support Request" assert contact.name == "" assert contact.department_org_name == "" assert contact.program_service_name == "" assert contact.intended_recipients == "" assert contact.main_use_case == "" assert contact.main_use_case_details == "" assert contact.support_type == "" assert contact.message == "" assert contact.user_profile == "" assert contact.service_name == "" assert contact.service_id == "" assert contact.service_url == "" assert contact.notification_types == "" assert contact.expected_volume == ""
def test_send_go_live_request_search_failed(notify_api: Flask, sample_service: Service, mocker: MockFixture): deal_id = "1" search_deal_id_mock = mocker.patch("app.user.rest.ZenDeskSell.search_deal_id", return_value=None) send_create_service_mock = mocker.patch("app.user.rest.ZenDeskSell.send_create_service", return_value=deal_id) create_note_mock = mocker.patch("app.user.rest.ZenDeskSell.create_note", return_value="1") data: Dict[str, Any] = { "email_address": "*****@*****.**", "service_name": "service_name", "department_org_name": "department_org_name", "intended_recipients": "intended_recipients", "main_use_case": "main_use_case", "notification_types": "notification_types", "expected_volume": "expected_volume", "service_url": "service_url", "support_type": "go_live_request", } contact = ContactRequest(**data) with notify_api.app_context(): assert ZenDeskSell().send_go_live_request(sample_service, sample_service.users[0], contact) search_deal_id_mock.assert_called_once_with(sample_service) send_create_service_mock.assert_called_once_with(sample_service, sample_service.users[0]) create_note_mock.assert_called_once_with(ZenDeskSell.NoteResourceType.DEAL, deal_id, contact)
def test_create_lead(notify_api: Flask): def match_json(request): expected = { "data": { "last_name": "User", "first_name": "Test", "organization_name": "", "email": "*****@*****.**", "description": "Program: \n: ", "tags": ["", "en"], "status": "New", "source_id": 2085874, "owner_id": ZenDeskSell.OWNER_ID, "custom_fields": { "Product": ["Notify"], "Intended recipients": "No value", }, } } json_matches = request.json() == expected basic_auth_header = request.headers.get("Authorization") == "Bearer zendesksell-api-key" return json_matches and basic_auth_header with requests_mock.mock() as rmock: rmock.request( "POST", url="https://zendesksell-test.com/v2/leads/[email protected]", headers={"Accept": "application/json", "Content-Type": "application/json"}, additional_matcher=match_json, status_code=201, ) with notify_api.app_context(): response = ZenDeskSell().upsert_lead(ContactRequest(email_address="*****@*****.**", name="Test User")) assert response == 201
def test_create_lead_missing_name(notify_api: Flask): # Name field is a requirement for the zendesk sell API interface with notify_api.app_context(): with pytest.raises(AssertionError): ZenDeskSell().upsert_lead(ContactRequest(email_address="*****@*****.**"))
def test_create_note_invalid_response( notify_api: Flask, sample_service: Service, expected_resp_data: Dict[str, Dict[str, Union[int, str]]], ): with requests_mock.mock() as rmock: rmock.request( "POST", url="https://zendesksell-test.com/v2/notes", headers={"Accept": "application/json", "Content-Type": "application/json"}, status_code=200, text=json.dumps(expected_resp_data), ) data: Dict[str, Any] = { "email_address": "*****@*****.**", "service_name": "service_name", "department_org_name": "department_org_name", "intended_recipients": "intended_recipients", "main_use_case": "main_use_case", "notification_types": "notification_types", "expected_volume": "expected_volume", "service_url": "service_url", "support_type": "go_live_request", } with notify_api.app_context(): note_id = ZenDeskSell().create_note(ZenDeskSell.NoteResourceType.DEAL, "1", ContactRequest(**data)) assert not note_id
def test_create_note(notify_api: Flask): resource_id = "1" def match_json(request): expected = { "data": { "resource_type": "deal", "resource_id": resource_id, "content": "\n".join( [ "Live Notes", "service_name just requested to go live.", "", "- Department/org: department_org_name", "- Intended recipients: intended_recipients", "- Purpose: main_use_case", "- Notification types: notification_types", "- Expected monthly volume: expected_volume", "---", "service_url", ] ), } } json_matches = request.json() == expected basic_auth_header = request.headers.get("Authorization") == "Bearer zendesksell-api-key" return json_matches and basic_auth_header with requests_mock.mock() as rmock: expected_note_id = "1" resp_data = {"data": {"id": expected_note_id}} rmock.request( "POST", url="https://zendesksell-test.com/v2/notes", headers={"Accept": "application/json", "Content-Type": "application/json"}, additional_matcher=match_json, status_code=200, text=json.dumps(resp_data), ) data: Dict[str, Any] = { "email_address": "*****@*****.**", "service_name": "service_name", "department_org_name": "department_org_name", "intended_recipients": "intended_recipients", "main_use_case": "main_use_case", "notification_types": "notification_types", "expected_volume": "expected_volume", "service_url": "service_url", "support_type": "go_live_request", } with notify_api.app_context(): note_id = ZenDeskSell().create_note(ZenDeskSell.NoteResourceType.DEAL, resource_id, ContactRequest(**data)) assert expected_note_id == note_id
def test_contact_info_invalid_email(mock_dict: Dict[str, Any]): with pytest.raises((TypeError, AssertionError, InvalidEmailError)): ContactRequest(**mock_dict)
def send_contact_request(self, contact: ContactRequest) -> int: ret = 200 if contact.is_demo_request(): ret = self.upsert_lead(contact) return ret