def test_invalid_form(self):
     data = {'key1': 'value1'}
     required_fields = ['key1', 'key2']
     min_fields = [('key1', 10)]
     errors = validate_form_data(data, required_fields)
     min_errors = validate_form_data(data, min_fields)
     assert 'key2' in errors
     assert errors['key2'] == {"required": True}
     assert 'key1' in min_errors
     assert min_errors['key1'] == {"min": True}
def submit_single_signup():
    user = from_response(request)
    if user['user_type'] != 'buyer':
        return abort(400)

    fields = ['name', 'email_address', 'employment_status', 'user_type']
    errors = validate_form_data(user, fields)
    if not is_government_email(user['email_address']):
        errors['email_address'] = {"government_email": True}
    if errors:
        return single_signup(user, errors)

    if user['employment_status'] == 'employee':
        token = generate_buyer_creation_token(user['name'],
                                              user['email_address'])
        send_buyer_account_activation_email(
            name=user["name"],
            email_address=user["email_address"],
            token=token)
        return render_template('auth/buyer-signup-email-sent.html',
                               email_address=user["email_address"])

    assert user['employment_status'] == 'contractor'

    form = auth_forms.BuyerInviteRequestForm(request.form)
    return render_template_with_csrf('auth/buyer-invite-request.html',
                                     form=form)
def create_application(token):
    token_data = decode_user_token(token.encode())
    form_data = from_response(request)

    fields = [('password', 10)]
    errors = validate_form_data(form_data, fields)
    if errors:
        return render_create_application(token, form_data, errors)

    id = token_data.get('id')
    create_app = not id

    if create_app:
        application = data_api_client.create_application({
            'status':
            'saved',
            'framework':
            'digital-marketplace'
        })
        id = application['application']['id']

    user = data_api_client.create_user({
        'name':
        token_data['name'],
        'password':
        form_data['password'],
        'emailAddress':
        token_data['email_address'],
        'role':
        'applicant',
        'application_id':
        id
    })

    user = User.from_json(user)
    login_user(user)

    if create_app:
        notification_message = 'Application Id:{}\nBy: {} ({})'.format(
            id, token_data['name'], token_data['email_address'])

        notify_team('A new seller has started an application',
                    notification_message)

    return redirect(
        url_for('main.render_application',
                id=id,
                step=('start' if create_app else 'submit')))
def send_seller_signup_email():
    user = from_response(request)
    if user.get('user_type') != 'seller':
        return abort(400)

    fields = ['name', 'email_address']
    errors = validate_form_data(user, fields)
    if errors:
        return start_seller_signup(user, errors)

    duplicate = data_api_client.req.users().checkduplicates().post(data={"email_address": user['email_address']})
    if duplicate.get('duplicate'):
        return render_template('auth/seller-signup-email-sent.html', email_address=user['email_address'])

    token = generate_application_invitation_token(user)
    url = url_for('main.render_create_application', token=token, _external=True)
    email_body = render_template(
        'emails/create_seller_user_email.html',
        url=url,
        user=user,
    )

    try:
        send_email(
            user['email_address'],
            email_body,
            current_app.config['INVITE_EMAIL_SUBJECT'],
            current_app.config['INVITE_EMAIL_FROM'],
            current_app.config['INVITE_EMAIL_NAME']
        )
    except EmailError as e:
        rollbar.report_exc_info()
        current_app.logger.error(
            'Invitation email failed to send. '
            'error {error}',
            extra={'error': six.text_type(e)}
        )
        abort(503, 'Failed to send user invite reset')

    return render_template('auth/seller-signup-email-sent.html', email_address=user['email_address'])
def create_application(token):
    token_data = decode_user_token(token.encode())
    form_data = from_response(request)

    fields = [('password', 10)]
    errors = validate_form_data(form_data, fields)
    if errors:
        return render_create_application(token, form_data, errors)

    id = token_data.get('id')
    create_app = not id

    if create_app:
        application = data_api_client.create_application({'status': 'saved', 'framework': 'digital-marketplace'})
        id = application['application']['id']

    user = data_api_client.create_user({
        'name': token_data['name'],
        'password': form_data['password'],
        'emailAddress': token_data['email_address'],
        'role': 'applicant',
        'application_id': id
    })

    user = User.from_json(user)
    login_user(user)

    if create_app:
        notification_message = 'Application Id:{}\nBy: {} ({})'.format(
            id,
            token_data['name'],
            token_data['email_address']
        )

        notify_team('A new seller has started an application', notification_message)

    return redirect(url_for('main.render_application', id=id, step=('start' if create_app else 'submit')))
 def test_valid_form(self):
     data = {'key1': 'value1', 'key2': 'value2'}
     required_fields = ['key1', 'key2']
     min_fields = ['key1', ('key2', 5)]
     assert not validate_form_data(data, required_fields)
     assert not validate_form_data(data, min_fields)
Ejemplo n.º 7
0
def collaborate_create_project_submit():
    project = from_response(request)

    fields = ['title', 'client', 'stage']

    basename = url_for('.collaborate_create_project')
    errors = validate_form_data(project, fields)
    if errors:
        form = DmForm()
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token,
                'errors': errors
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    try:
        project = data_api_client.req.projects().post(data={'project': project})['project']

        rendered_component = render_component('bundles/Collaborate/ProjectSubmitConfirmationWidget.js', {})

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    except APIError as e:
        form = DmForm()
        flash('', 'error')
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )
def collaborate_create_project_submit():
    project = from_response(request)

    fields = ['title', 'client', 'stage']

    basename = url_for('.collaborate_create_project')
    errors = validate_form_data(project, fields)
    if errors:
        form = DmForm()
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token,
                'errors': errors
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    try:
        project = data_api_client.req.projects().post(data={'project': project})['project']

        rendered_component = render_component('bundles/Collaborate/ProjectSubmitConfirmationWidget.js', {})

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    except APIError as e:
        form = DmForm()
        flash('', 'error')
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )