def test_deletes_previous_temp_logo_after_uploading_logo(
        platform_admin_client, mocker, endpoint, has_data, fake_uuid):
    if has_data:
        mock_get_email_branding(mocker, fake_uuid)

    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    temp_old_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename='old_test.png')

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename='test.png')

    mocked_upload_email_logo = mocker.patch(
        'app.main.views.email_branding.upload_email_logo',
        return_value=temp_filename)

    mocked_delete_email_temp_file = mocker.patch(
        'app.main.views.email_branding.delete_email_temp_file')

    platform_admin_client.post(
        url_for('main.create_email_branding',
                logo=temp_old_filename,
                branding_id=fake_uuid),
        data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
        content_type='multipart/form-data')

    assert mocked_upload_email_logo.called
    assert mocked_delete_email_temp_file.called
    assert mocked_delete_email_temp_file.call_args == call(temp_old_filename)
def test_logo_persisted_when_organisation_saved(platform_admin_client,
                                                mock_create_email_branding,
                                                mocker, fake_uuid):
    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename='test.png')

    mocked_upload_email_logo = mocker.patch(
        'app.main.views.email_branding.upload_email_logo')
    mocked_persist_logo = mocker.patch(
        'app.main.views.email_branding.persist_logo')
    mocked_delete_email_temp_files_by = mocker.patch(
        'app.main.views.email_branding.delete_email_temp_files_created_by')

    resp = platform_admin_client.post(url_for('.create_email_branding',
                                              logo=temp_filename),
                                      content_type='multipart/form-data')
    assert resp.status_code == 302

    assert not mocked_upload_email_logo.called
    assert mocked_persist_logo.called
    assert mocked_delete_email_temp_files_by.called
    assert mocked_delete_email_temp_files_by.call_args == call(user_id)
    assert mock_create_email_branding.called
def test_temp_logo_is_shown_after_uploading_logo(
    platform_admin_client,
    mocker,
    fake_uuid,
):
    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename='test.png')

    mocker.patch('app.main.views.email_branding.upload_email_logo',
                 return_value=temp_filename)
    mocker.patch('app.main.views.email_branding.delete_email_temp_file')

    response = platform_admin_client.post(
        url_for('main.create_email_branding'),
        data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
        content_type='multipart/form-data',
        follow_redirects=True)

    assert response.status_code == 200

    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    assert page.select_one('#logo-img > img').attrs['src'].endswith(
        temp_filename)
Exemple #4
0
def test_logo_does_not_get_persisted_if_updating_email_branding_client_throws_an_error(
        logged_in_platform_admin_client, mock_create_email_branding, mocker,
        fake_uuid):
    with logged_in_platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename='test.png')

    mocked_persist_logo = mocker.patch(
        'app.main.views.email_branding.persist_logo')
    mocked_delete_email_temp_files_by = mocker.patch(
        'app.main.views.email_branding.delete_email_temp_files_created_by')
    mocker.patch(
        'app.main.views.email_branding.email_branding_client.create_email_branding',
        side_effect=HTTPError())

    logged_in_platform_admin_client.post(url_for('.create_email_branding',
                                                 logo=temp_filename),
                                         content_type='multipart/form-data')

    assert not mocked_persist_logo.called
    assert not mocked_delete_email_temp_files_by.called
Exemple #5
0
def update_email_branding(branding_id, logo=None):
    email_branding = email_branding_client.get_email_branding(
        branding_id)["email_branding"]

    form = ServiceUpdateEmailBranding(
        name=email_branding["name"],
        text=email_branding["text"],
        colour=email_branding["colour"],
        brand_type=email_branding["brand_type"],
    )

    logo = logo if logo else email_branding.get(
        "logo") if email_branding else None

    if form.validate_on_submit():
        if form.file.data:
            upload_filename = upload_email_logo(
                form.file.data.filename,
                form.file.data,
                current_app.config["AWS_REGION"],
                user_id=session["user_id"],
            )

            if logo and logo.startswith(
                    TEMP_TAG.format(user_id=session["user_id"])):
                delete_email_temp_file(logo)

            return redirect(
                url_for(
                    ".update_email_branding",
                    branding_id=branding_id,
                    logo=upload_filename,
                ))

        updated_logo_name = permanent_email_logo_name(
            logo, session["user_id"]) if logo else None

        email_branding_client.update_email_branding(
            branding_id=branding_id,
            logo=updated_logo_name,
            name=form.name.data,
            text=form.text.data,
            colour=form.colour.data,
            brand_type=form.brand_type.data,
        )

        if logo:
            persist_logo(logo, updated_logo_name)

        delete_email_temp_files_created_by(session["user_id"])

        return redirect(url_for(".email_branding", branding_id=branding_id))

    return render_template(
        "views/email-branding/manage-branding.html",
        form=form,
        email_branding=email_branding,
        cdn_url=get_logo_cdn_domain(),
        logo=logo,
    )
Exemple #6
0
def test_temp_logo_is_shown_after_uploading_logo(
    platform_admin_client,
    mocker,
    fake_uuid,
):
    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename="test.png")

    mocker.patch("app.main.views.email_branding.upload_email_logo",
                 return_value=temp_filename)
    mocker.patch("app.main.views.email_branding.delete_email_temp_file")

    response = platform_admin_client.post(
        url_for("main.create_email_branding"),
        data={"file": (BytesIO("".encode("utf-8")), "test.png")},
        content_type="multipart/form-data",
        follow_redirects=True,
    )

    assert response.status_code == 200

    page = BeautifulSoup(response.data.decode("utf-8"), "html.parser")

    assert page.select_one("#logo-img > img").attrs["src"].endswith(
        temp_filename)
Exemple #7
0
def test_update_existing_branding(
    platform_admin_client,
    mocker,
    fake_uuid,
    mock_get_email_branding,
    mock_update_email_branding,
):
    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    data = {
        "logo": "test.png",
        "colour": "#0000ff",
        "text": "new text",
        "name": "new name",
        "brand_type": "both_english",
    }

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename=data["logo"],
    )

    mocker.patch("app.main.views.email_branding.persist_logo")
    mocker.patch(
        "app.main.views.email_branding.delete_email_temp_files_created_by")

    platform_admin_client.post(
        url_for(".update_email_branding",
                logo=temp_filename,
                branding_id=fake_uuid),
        content_type="multipart/form-data",
        data={
            "colour": data["colour"],
            "name": data["name"],
            "text": data["text"],
            "cdn_url": get_logo_cdn_domain(),
            "brand_type": data["brand_type"],
        },
    )

    updated_logo_name = "{}-{}".format(fake_uuid, data["logo"])

    assert mock_update_email_branding.called
    assert mock_update_email_branding.call_args == call(
        branding_id=fake_uuid,
        logo=updated_logo_name,
        name=data["name"],
        text=data["text"],
        colour=data["colour"],
        brand_type=data["brand_type"],
    )
Exemple #8
0
def test_update_existing_branding(logged_in_platform_admin_client, mocker,
                                  fake_uuid, mock_get_email_branding,
                                  mock_update_email_branding):
    with logged_in_platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    data = {
        'logo': 'test.png',
        'colour': '#0000ff',
        'text': 'new text',
        'name': 'new name',
        'brand_type': 'both'
    }

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename=data['logo'])

    mocker.patch('app.main.views.email_branding.persist_logo')
    mocker.patch(
        'app.main.views.email_branding.delete_email_temp_files_created_by')

    logged_in_platform_admin_client.post(url_for('.update_email_branding',
                                                 logo=temp_filename,
                                                 branding_id=fake_uuid),
                                         content_type='multipart/form-data',
                                         data={
                                             'colour': data['colour'],
                                             'name': data['name'],
                                             'text': data['text'],
                                             'cdn_url':
                                             'https://static-logos.cdn.com',
                                             'brand_type': data['brand_type']
                                         })

    updated_logo_name = '{}-{}'.format(fake_uuid, data['logo'])

    assert mock_update_email_branding.called
    assert mock_update_email_branding.call_args == call(
        branding_id=fake_uuid,
        logo=updated_logo_name,
        name=data['name'],
        text=data['text'],
        colour=data['colour'],
        brand_type=data['brand_type'])
def test_create_new_email_branding_when_branding_saved(
        platform_admin_client, mocker, mock_create_email_branding, fake_uuid):
    with platform_admin_client.session_transaction() as session:
        user_id = session["user_id"]

    data = {
        'logo': 'test.png',
        'colour': '#ff0000',
        'text': 'new text',
        'name': 'new name',
        'brand_type': 'org_banner'
    }

    temp_filename = EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=user_id),
        unique_id=fake_uuid,
        filename=data['logo'])

    mocker.patch('app.main.views.email_branding.persist_logo')
    mocker.patch(
        'app.main.views.email_branding.delete_email_temp_files_created_by')

    platform_admin_client.post(url_for('.create_email_branding',
                                       logo=temp_filename),
                               content_type='multipart/form-data',
                               data={
                                   'colour': data['colour'],
                                   'name': data['name'],
                                   'text': data['text'],
                                   'cdn_url': get_logo_cdn_domain(),
                                   'brand_type': data['brand_type']
                               })

    updated_logo_name = '{}-{}'.format(fake_uuid, data['logo'])

    assert mock_create_email_branding.called
    assert mock_create_email_branding.call_args == call(
        logo=updated_logo_name,
        name=data['name'],
        text=data['text'],
        colour=data['colour'],
        brand_type=data['brand_type'])
Exemple #10
0
def create_email_branding(logo=None):
    form = ServiceUpdateEmailBranding(brand_type='org')

    if form.validate_on_submit():
        if form.file.data:
            upload_filename = upload_email_logo(
                form.file.data.filename,
                form.file.data,
                current_app.config['AWS_REGION'],
                user_id=session["user_id"]
            )

            if logo and logo.startswith(TEMP_TAG.format(user_id=session['user_id'])):
                delete_email_temp_file(logo)

            return redirect(url_for('.create_email_branding', logo=upload_filename))

        updated_logo_name = permanent_email_logo_name(logo, session["user_id"]) if logo else None

        email_branding_client.create_email_branding(
            logo=updated_logo_name,
            name=form.name.data,
            text=form.text.data,
            colour=form.colour.data,
            brand_type=form.brand_type.data,
        )

        if logo:
            persist_logo(logo, updated_logo_name)

        delete_email_temp_files_created_by(session["user_id"])

        return redirect(url_for('.email_branding'))

    return render_template(
        'views/email-branding/manage-branding.html',
        form=form,
        cdn_url=get_logo_cdn_domain(),
        logo=logo
    )
def upload_filename(fake_uuid):
    return EMAIL_LOGO_LOCATION_STRUCTURE.format(
        temp=TEMP_TAG.format(user_id=fake_uuid),
        unique_id=upload_id,
        filename=filename)