def test_get_safelist_separates_emails_and_phones(client, sample_service): dao_add_and_commit_safelisted_contacts([ ServiceSafelist.from_string(sample_service.id, EMAIL_TYPE, '*****@*****.**'), ServiceSafelist.from_string(sample_service.id, MOBILE_TYPE, '6502532222'), ServiceSafelist.from_string(sample_service.id, MOBILE_TYPE, '+1800-234-1242'), ]) response = client.get('service/{}/safelist'.format(sample_service.id), headers=[create_authorization_header()]) assert response.status_code == 200 json_resp = json.loads(response.get_data(as_text=True)) assert json_resp['email_addresses'] == ['*****@*****.**'] assert sorted(json_resp['phone_numbers']) == sorted(['+1800-234-1242', '6502532222'])
def test_remove_service_safelist_only_removes_for_my_service(notify_db, notify_db_session): service_1 = create_service(notify_db, notify_db_session, service_name="service 1") service_2 = create_service(notify_db, notify_db_session, service_name="service 2") dao_add_and_commit_safelisted_contacts( [ ServiceSafelist.from_string(service_1.id, EMAIL_TYPE, "*****@*****.**"), ServiceSafelist.from_string(service_2.id, EMAIL_TYPE, "*****@*****.**"), ] ) dao_remove_service_safelist(service_1.id) assert service_1.safelist == [] assert len(service_2.safelist) == 1
def sample_service_safelist(notify_db, notify_db_session, service=None, email_address=None, mobile_number=None): if service is None: service = create_service(check_if_service_exists=True) if email_address: safelisted_user = ServiceSafelist.from_string(service.id, EMAIL_TYPE, email_address) elif mobile_number: safelisted_user = ServiceSafelist.from_string(service.id, MOBILE_TYPE, mobile_number) else: safelisted_user = ServiceSafelist.from_string(service.id, EMAIL_TYPE, "*****@*****.**") notify_db.session.add(safelisted_user) notify_db.session.commit() return safelisted_user
def test_add_and_commit_safelisted_contacts_saves_data(sample_service): safelist = ServiceSafelist.from_string(sample_service.id, EMAIL_TYPE, "*****@*****.**") dao_add_and_commit_safelisted_contacts([safelist]) db_contents = ServiceSafelist.query.all() assert len(db_contents) == 1 assert db_contents[0].id == safelist.id
def get_safelist_objects(service_id, request_json): return [ ServiceSafelist.from_string(service_id, type, recipient) for type, recipient in ( get_recipients_from_request(request_json, 'phone_numbers', MOBILE_TYPE) + get_recipients_from_request(request_json, 'email_addresses', EMAIL_TYPE)) ]
def test_send_one_off_notification_raises_if_cant_send_to_recipient( notify_db_session, recipient, ): service = create_service(restricted=True) template = create_template(service=service) dao_add_and_commit_safelisted_contacts([ ServiceSafelist.from_string(service.id, MOBILE_TYPE, "+16502532229"), ]) post_data = { "template_id": str(template.id), "to": recipient, "created_by": str(service.created_by_id), } with pytest.raises(BadRequestError) as e: send_one_off_notification(service.id, post_data) assert "service is in trial mode" in e.value.message
def test_should_not_build_service_safelist_from_invalid_contact( recipient_type, contact): with pytest.raises(ValueError): ServiceSafelist.from_string('service_id', recipient_type, contact)
def test_should_build_service_safelist_from_email_address(email_address): service_safelist = ServiceSafelist.from_string('service_id', EMAIL_TYPE, email_address) assert service_safelist.recipient == email_address
def test_should_build_service_safelist_from_mobile_number(mobile_number): service_safelist = ServiceSafelist.from_string('service_id', MOBILE_TYPE, mobile_number) assert service_safelist.recipient == mobile_number