def test_create_service(notify_db_session): user = create_user() create_letter_branding() assert Service.query.count() == 0 service = Service( name="service_name", email_from="email_from", message_limit=1000, restricted=False, organisation_type="central", created_by=user, ) dao_create_service(service, user) assert Service.query.count() == 1 service_db = Service.query.one() assert service_db.name == "service_name" assert service_db.id == service.id assert service_db.email_from == "email_from" assert service_db.research_mode is False assert service_db.prefix_sms is True assert service.active is True assert user in service_db.users assert service_db.organisation_type == "central" assert service_db.crown is None assert not service.letter_branding assert not service.organisation_id
def test_create_service_creates_a_history_record_with_current_data( notify_db_session): user = create_user() create_letter_branding() assert Service.query.count() == 0 assert Service.get_history_model().query.count() == 0 service = Service( name="service_name", email_from="email_from", message_limit=1000, restricted=False, created_by=user, ) dao_create_service(service, user) assert Service.query.count() == 1 assert Service.get_history_model().query.count() == 1 service_from_db = Service.query.first() service_history = Service.get_history_model().query.first() assert service_from_db.id == service_history.id assert service_from_db.name == service_history.name assert service_from_db.version == 1 assert service_from_db.version == service_history.version assert user.id == service_history.created_by_id assert service_from_db.created_by.id == service_history.created_by_id
def test_update_organisation_does_not_override_service_branding( sample_service, sample_organisation, ): email_branding = create_email_branding() custom_email_branding = create_email_branding(name='custom') letter_branding = create_letter_branding() custom_letter_branding = create_letter_branding(name='custom', filename='custom') sample_service.email_branding = custom_email_branding sample_service.letter_branding = custom_letter_branding sample_organisation.services.append(sample_service) db.session.commit() dao_update_organisation(sample_organisation.id, email_branding_id=email_branding.id) dao_update_organisation(sample_organisation.id, letter_branding_id=letter_branding.id) assert sample_organisation.email_branding == email_branding assert sample_organisation.letter_branding == letter_branding assert sample_service.email_branding == custom_email_branding assert sample_service.letter_branding == custom_letter_branding
def test_get_letter_branding_by_id(client, notify_db_session): hm_gov = create_letter_branding() create_letter_branding(name="test domain", filename="test-domain") response = client.get("/letter-branding/{}".format(hm_gov.id), headers=[create_authorization_header()]) assert response.status_code == 200 assert json.loads(response.get_data(as_text=True)) == hm_gov.serialize()
def test_dao_get_all_letter_branding(notify_db_session): hm_gov = create_letter_branding() test_branding = create_letter_branding( name='test branding', filename='test-branding', ) results = dao_get_all_letter_branding() assert hm_gov in results assert test_branding in results assert len(results) == 2
def test_update_organisation_does_not_update_the_service_if_certain_attributes_not_provided( sample_service, sample_organisation, ): email_branding = create_email_branding() letter_branding = create_letter_branding() sample_service.organisation_type = 'local' sample_organisation.organisation_type = 'central' sample_organisation.email_branding = email_branding sample_organisation.letter_branding = letter_branding sample_organisation.services.append(sample_service) db.session.commit() assert sample_organisation.name == 'sample organisation' dao_update_organisation(sample_organisation.id, name='updated org name') assert sample_organisation.name == 'updated org name' assert sample_organisation.organisation_type == 'central' assert sample_service.organisation_type == 'local' assert sample_organisation.email_branding == email_branding assert sample_service.email_branding is None assert sample_organisation.letter_branding == letter_branding assert sample_service.letter_branding is None
def test_update_organisation_default_branding( admin_request, notify_db_session, ): org = create_organisation(name="Test Organisation") email_branding = create_email_branding() letter_branding = create_letter_branding() assert org.email_branding is None assert org.letter_branding is None admin_request.post( "organisation.update_organisation", _data={ "email_branding_id": str(email_branding.id), "letter_branding_id": str(letter_branding.id), }, organisation_id=org.id, _expected_status=204, ) assert org.email_branding == email_branding assert org.letter_branding == letter_branding
def test_create_nhs_service_get_default_branding_based_on_email_address( notify_db_session, branding_name_to_create, expected_branding, email_address, organisation_type, ): user = create_user(email=email_address) letter_branding = create_letter_branding(name=branding_name_to_create) email_branding = create_email_branding(name=branding_name_to_create) service = Service( name="service_name", email_from="email_from", message_limit=1000, restricted=False, organisation_type=organisation_type, created_by=user, ) dao_create_service(service, user) service_db = Service.query.one() if expected_branding: assert service_db.letter_branding == letter_branding assert service_db.email_branding == email_branding else: assert service_db.letter_branding is None assert service_db.email_branding is None
def test_get_pdf_for_templated_letter_happy_path(mocker, sample_letter_notification, branding_name, logo_filename): if branding_name: letter_branding = create_letter_branding(name=branding_name, filename=logo_filename) sample_letter_notification.service.letter_branding = letter_branding mock_celery = mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task') mocker.patch('app.celery.letters_pdf_tasks.get_letter_pdf_filename', return_value='LETTER.PDF') get_pdf_for_templated_letter(sample_letter_notification.id) letter_data = { 'letter_contact_block': sample_letter_notification.reply_to_text, 'template': { "subject": sample_letter_notification.template.subject, "content": sample_letter_notification.template.content, "template_type": sample_letter_notification.template.template_type }, 'values': sample_letter_notification.personalisation, 'logo_filename': logo_filename, 'letter_filename': 'LETTER.PDF', "notification_id": str(sample_letter_notification.id), 'key_type': sample_letter_notification.key_type } encrypted_data = encryption.encrypt(letter_data) mock_celery.assert_called_once_with( name=TaskNames.CREATE_PDF_FOR_TEMPLATED_LETTER, args=(encrypted_data,), queue=QueueNames.SANITISE_LETTERS )
def test_update_organisation(notify_db_session): create_organisation() organisation = Organisation.query.one() user = create_user() email_branding = create_email_branding() letter_branding = create_letter_branding() data = { 'name': 'new name', "crown": True, "organisation_type": 'local', "agreement_signed": True, "agreement_signed_at": datetime.datetime.utcnow(), "agreement_signed_by_id": user.id, "agreement_signed_version": 999.99, "letter_branding_id": letter_branding.id, "email_branding_id": email_branding.id, } for attribute, value in data.items(): assert getattr(organisation, attribute) != value assert organisation.updated_at is None dao_update_organisation(organisation.id, **data) organisation = Organisation.query.one() for attribute, value in data.items(): assert getattr(organisation, attribute) == value assert organisation.updated_at
def test_get_all_letter_brands(client, notify_db_session): hm_gov = create_letter_branding() test_branding = create_letter_branding( name="test branding", filename="test-branding", ) response = client.get("/letter-branding", headers=[create_authorization_header()]) assert response.status_code == 200 json_response = json.loads(response.get_data(as_text=True)) assert len(json_response) == 2 for brand in json_response: if brand["id"] == str(hm_gov.id): assert hm_gov.serialize() == brand elif brand["id"] == str(test_branding.id): assert test_branding.serialize() == brand else: assert False
def test_update_letter_branding_returns_400_when_integrity_error_is_thrown( client, notify_db_session): create_letter_branding(name='duplicate', filename='duplicate') brand_to_update = create_letter_branding(name='super brand', filename='super brand') form = { 'name': 'duplicate', 'filename': 'super-brand', } response = client.post('/letter-branding/{}'.format(brand_to_update.id), headers=[('Content-Type', 'application/json'), create_authorization_header()], data=json.dumps(form)) assert response.status_code == 400 json_resp = json.loads(response.get_data(as_text=True)) assert json_resp['message'] == {"name": ["Name already in use"]}
def test_update_letter_branding_returns_400_when_integrity_error_is_thrown( client, notify_db_session): create_letter_branding(name="duplicate", filename="duplicate") brand_to_update = create_letter_branding(name="super brand", filename="super brand") form = { "name": "duplicate", "filename": "super-brand", } response = client.post( "/letter-branding/{}".format(brand_to_update.id), headers=[("Content-Type", "application/json"), create_authorization_header()], data=json.dumps(form), ) assert response.status_code == 400 json_resp = json.loads(response.get_data(as_text=True)) assert json_resp["message"] == {"name": ["Name already in use"]}
def test_create_letters_gets_the_right_logo_when_service_has_letter_branding_logo( notify_api, mocker, sample_letter_notification ): letter_branding = create_letter_branding(name='test brand', filename='test-brand') sample_letter_notification.service.letter_branding = letter_branding mock_get_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', 1)) mocker.patch('app.letters.utils.s3upload') mocker.patch('app.celery.letters_pdf_tasks.update_notification_status_by_id') create_letters_pdf(sample_letter_notification.id) mock_get_letters_pdf.assert_called_once_with( sample_letter_notification.template, contact_block=sample_letter_notification.reply_to_text, filename=sample_letter_notification.service.letter_branding.filename, values=sample_letter_notification.personalisation )
def test_update_organisation_updates_the_service_branding_if_branding_is_provided( sample_service, sample_organisation, ): email_branding = create_email_branding() letter_branding = create_letter_branding() sample_organisation.services.append(sample_service) db.session.commit() dao_update_organisation(sample_organisation.id, email_branding_id=email_branding.id) dao_update_organisation(sample_organisation.id, letter_branding_id=letter_branding.id) assert sample_organisation.email_branding == email_branding assert sample_organisation.letter_branding == letter_branding assert sample_service.email_branding == email_branding assert sample_service.letter_branding == letter_branding
def test_dao_get_letter_branding_by_id(notify_db_session): letter_branding = create_letter_branding() result = dao_get_letter_branding_by_id(letter_branding.id) assert result == letter_branding
def test_dao_update_letter_branding(notify_db_session): create_letter_branding(name='original') letter_branding = LetterBranding.query.first() assert letter_branding.name == 'original' dao_update_letter_branding(letter_branding.id, name='new name') assert LetterBranding.query.first().name == 'new name'