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 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 update_safelist(service_id): # doesn't commit so if there are any errors, we preserve old values in db dao_remove_service_safelist(service_id) try: safelist_objs = get_safelist_objects(service_id, request.get_json()) except ValueError as e: current_app.logger.exception(e) dao_rollback() msg = '{} is not a valid email address or phone number'.format(str(e)) raise InvalidRequest(msg, 400) else: dao_add_and_commit_safelisted_contacts(safelist_objs) return '', 204
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 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