Example #1
0
def invite_user():
    form = EmailAddressForm()

    if form.validate_on_submit():
        send_user_account_email(
            'supplier',
            form.email_address.data,
            current_app.config['NOTIFY_TEMPLATES']['invite_contributor'],
            extra_token_data={
                'supplier_id': current_user.supplier_id,
                'supplier_name': current_user.supplier_name
            },
            personalisation={
                'user': current_user.name,
                'supplier': current_user.supplier_name
            })

        data_api_client.create_audit_event(
            audit_type=AuditTypes.invite_user,
            user=current_user.email_address,
            object_type='suppliers',
            object_id=current_user.supplier_id,
            data={'invitedEmail': form.email_address.data},
        )

        flash(USER_INVITED_FLASH_MESSAGE, "success")
        return redirect(url_for('.list_users'))

    errors = get_errors_from_wtform(form)

    return render_template("auth/submit_email_address.html",
                           form=form,
                           errors=errors), 200 if not errors else 400
    def test_correctly_calls_notify_client_for_supplier(self, email_app):
        with email_app.test_request_context():
            self.generate_token.return_value = 'mocked-token'

            send_user_account_email(
                'supplier',
                '*****@*****.**',
                current_app.config['NOTIFY_TEMPLATES']['create_user_account'],
                extra_token_data={
                    'supplier_id': 12345,
                    'supplier_name': 'Digital Marketplace'
                },
                personalisation={
                    'user': '******',
                    'supplier': 'Digital Marketplace'
                })

            self.notify_client.send_email.assert_called_once_with(
                '*****@*****.**',
                template_name_or_id=current_app.config['NOTIFY_TEMPLATES']
                ['create_user_account'],
                personalisation={
                    'url': 'http://localhost/user/create/mocked-token',
                    'user': '******',
                    'supplier': 'Digital Marketplace'
                },
                reference=
                'create-user-account-KmmJkEa5sLyv7vuxG3xja3S3fnnM6Rgq5EZY0S_kCjE='
            )
            assert session['email_sent_to'] == '*****@*****.**'
def submit_create_buyer_account():
    current_app.logger.info("buyercreate: post create-buyer-account")
    form = EmailAddressForm()
    if form.validate_on_submit():
        email_address = form.email_address.data
        if not data_api_client.is_email_address_with_valid_buyer_domain(
                email_address):
            return render_template("create_buyer/create_buyer_user_error.html",
                                   support_email_address=current_app.
                                   config['SUPPORT_EMAIL_ADDRESS'],
                                   error='invalid_buyer_domain'), 400
        else:
            send_user_account_email(
                'buyer', email_address,
                current_app.config['NOTIFY_TEMPLATES']['create_user_account'])

            data_api_client.create_audit_event(
                audit_type=AuditTypes.invite_user,
                data={'invitedEmail': email_address})

            return redirect(url_for('.create_your_account_complete'), 302)
    else:
        return render_template(
            "create_buyer/create_buyer_account.html",
            form=form,
            errors=get_errors_from_wtform(form),
        ), 400
    def test_abort_with_503_if_send_email_fails_with_EmailError(
            self, abort, email_app):
        with email_app.test_request_context():
            self.notify_client.send_email.side_effect = EmailError('OMG!')

            send_user_account_email(
                'buyer', '*****@*****.**',
                current_app.config['NOTIFY_TEMPLATES']['create_user_account'])

            abort.assert_called_once_with(
                503, response="Failed to send user creation email.")
def submit_company_summary():

    required_fields = [
        "email_address",
        "phone_number",
        "contact_name",
        "duns_number",
        "company_name",
        "account_email_address"
    ]

    missing_fields = [field for field in required_fields if field not in session]

    if not missing_fields:
        supplier = {
            "name": session["company_name"],
            "dunsNumber": str(session["duns_number"]),
            "contactInformation": [{
                "email": session["email_address"],
                "phoneNumber": session["phone_number"],
                "contactName": session["contact_name"]
            }]
        }

        account_email_address = session.get("account_email_address", None)

        supplier = data_api_client.create_supplier(supplier)
        session.clear()
        session['email_company_name'] = supplier['suppliers']['name']
        session['email_supplier_id'] = supplier['suppliers']['id']

        send_user_account_email(
            'supplier',
            account_email_address,
            current_app.config['NOTIFY_TEMPLATES']['create_user_account'],
            extra_token_data={
                "supplier_id": session['email_supplier_id'],
                "supplier_name": session['email_company_name']
            }
        )

        data_api_client.create_audit_event(
            audit_type=AuditTypes.invite_user,
            object_type='suppliers',
            object_id=session['email_supplier_id'],
            data={'invitedEmail': account_email_address})

        return redirect(url_for('.create_your_account_complete'), 302)
    else:
        return render_template(
            "suppliers/create_company_summary.html",
            missing_fields=missing_fields
        ), 400
def invite_user(supplier_id):
    invite_form = EmailAddressForm()

    try:
        suppliers = data_api_client.get_supplier(supplier_id)
        users = data_api_client.find_users_iter(supplier_id)
    except HTTPError as e:
        current_app.logger.error(str(e), supplier_id)
        if e.status_code != 404:
            raise
        else:
            abort(404, "Supplier not found")

    if invite_form.validate_on_submit():
        send_user_account_email(
            'supplier',
            invite_form.email_address.data,
            current_app.config['NOTIFY_TEMPLATES']['invite_contributor'],
            extra_token_data={
                'supplier_id': supplier_id,
                'supplier_name': suppliers['suppliers']['name']
            },
            personalisation={
                'user': '******',
                'supplier': suppliers['suppliers']['name']
            }
        )

        data_api_client.create_audit_event(
            audit_type=AuditTypes.invite_user,
            user=current_user.email_address,
            object_type='suppliers',
            object_id=supplier_id,
            data={'invitedEmail': invite_form.email_address.data})

        flash(SUPPLIER_USER_MESSAGES['user_invited'])
        return redirect(url_for('.find_supplier_users', supplier_id=supplier_id))
    else:
        return render_template(
            "view_supplier_users.html",
            invite_form=invite_form,
            move_user_form=MoveUserForm(),
            users=users,
            supplier=suppliers["suppliers"]
        ), 400
    def test_correctly_calls_notify_client_for_buyer(self, email_app):
        with email_app.test_request_context():
            self.generate_token.return_value = 'mocked-token'

            send_user_account_email(
                'buyer', '*****@*****.**',
                current_app.config['NOTIFY_TEMPLATES']['create_user_account'])

            self.notify_client.send_email.assert_called_once_with(
                '*****@*****.**',
                template_name_or_id='this-would-be-the-id-of-the-template',
                personalisation={
                    'url': 'http://localhost/user/create/mocked-token'
                },
                reference=
                'create-user-account-KmmJkEa5sLyv7vuxG3xja3S3fnnM6Rgq5EZY0S_kCjE='
            )
            assert session['email_sent_to'] == '*****@*****.**'
    def test_abort_with_503_if_send_email_fails_with_EmailError(self, DMNotifyClient, abort, current_app, email_app):
        with email_app.test_request_context():
            notify_client_mock = mock.Mock()
            notify_client_mock.send_email.side_effect = EmailError('OMG!')
            DMNotifyClient.return_value = notify_client_mock

            send_user_account_email(
                'buyer',
                '*****@*****.**',
                current_app.config['NOTIFY_TEMPLATES']['create_user_account']
             )

            current_app.logger.error.assert_called_once_with(
                "{code}: Create user email for email_hash {email_hash} failed to send. Error: {error}",
                extra={
                    'error': 'OMG!',
                    'email_hash': 'KmmJkEa5sLyv7vuxG3xja3S3fnnM6Rgq5EZY0S_kCjE=',
                    'code': 'buyercreate.fail'
                }
            )
            abort.assert_called_once_with(503, response="Failed to send user creation email.")