Пример #1
0
def validate_api_key():
    """Validates an API key submitted via POST."""
    api_key_form = ApiKeyForm()
    api_key_form.organization.choices = session['orgs_list']
    if api_key_form.validate_on_submit():
        session['org_id'] = api_key_form.organization.data
        return jsonify(True)
    return jsonify(api_key_form.errors), 422
Пример #2
0
def test_api_key_form_basic_validation(test_app, key, org_choices, org,
                                       expect):
    """Tests the basic validation of the ApiKeyForm."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = key
        api_key_form.organization.choices = org_choices
        api_key_form.organization.data = org
        assert api_key_form.validate() == expect
Пример #3
0
def test_api_key_form_bad_data_center(test_app):
    """Tests that the ApiKeyForm flags a key without a data center."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = 'foo'
        api_key_form.organization.choices = [('0', 'bar')]
        api_key_form.organization.data = '0'
        assert not api_key_form.validate()
        assert ['Key missing data center'
                ] == list(api_key_form.errors.values())[0]
Пример #4
0
def test_api_key_form_bad_request(test_app, mocker):
    """Tests that a bad request to MailChimp is handled correctly."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = 'foo-bar1'
        api_key_form.key.errors = []
        mocked_validate = mocker.patch('app.forms.FlaskForm.validate')
        mocked_validate.return_value = True
        mocked_request = mocker.patch('app.forms.requests')
        mocked_request.get.return_value.status_code = 404
        assert not api_key_form.validate()
        assert ['MailChimp responded with error code 404'
                ] == list(api_key_form.errors.values())[0]
Пример #5
0
def test_api_key_form_connection_error(test_app, mocker):
    """Tests that a ConnectionError from MailChimp is handled correctly."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = 'foo-bar1'
        api_key_form.key.errors = []
        mocked_validate = mocker.patch('app.forms.FlaskForm.validate')
        mocked_validate.return_value = True
        mocked_get_request = mocker.patch('app.forms.requests.get')
        mocked_get_request.side_effect = requests.exceptions.ConnectionError()
        assert not api_key_form.validate()
        assert ['Connection to MailChimp servers refused'
                ] == list(api_key_form.errors.values())[0]
Пример #6
0
def test_api_key_form_wellformed_request(test_app, mocker):
    """Tests that the request to MailChimp is properly formed."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = 'foo-bar1'
        api_key_form.key.errors = []
        mocked_validate = mocker.patch('app.forms.FlaskForm.validate')
        mocked_validate.return_value = True
        mocked_request = mocker.patch('app.forms.requests')
        api_key_form.validate()
        mocked_request.get.assert_called_with(
            'https://bar1.api.mailchimp.com/3.0/lists',
            params=(('fields', 'total_items'), ),
            auth=('shorenstein', 'foo-bar1'))
Пример #7
0
def benchmarks(user):
    """A secret route for activated users.

    Verifies that the md5-hash submitted in the url string
    is in the database and that the corresponding user has been
    approved.
    If not approved, return a 403 error.
    If approved, render the form allowing the user to submit
    their API key.
    """
    result = AppUser.query.filter_by(email_hash=user, approved=True).first()
    if result is None:
        abort(403)
    session['user_id'] = result.id
    session['email'] = result.email

    # Dynamically generate options for the organizations select box
    orgs_list = [(str(org.id), org.name) for org in result.orgs]
    session['orgs_list'] = orgs_list
    num_orgs = len(orgs_list)
    api_key_form = ApiKeyForm()

    # Don't include an empty select field if there is only one option
    api_key_form.organization.choices = (
        [*[('', '')], *orgs_list]
        if num_orgs > 1
        else orgs_list)
    return render_template(
        'enter-api-key.html', api_key_form=api_key_form, num_orgs=num_orgs)
Пример #8
0
def test_api_key_form_validates(test_app, mocker):
    """Tests succesful validation of the ApiKeyForm."""
    with test_app.test_request_context():
        api_key_form = ApiKeyForm()
        api_key_form.key.data = 'foo-bar1'
        api_key_form.store_aggregates.data = True
        api_key_form.monthly_updates.data = True
        mocked_validate = mocker.patch('app.forms.FlaskForm.validate')
        mocked_validate.return_value = True
        mocked_request = mocker.patch('app.forms.requests')
        mocked_request.get.return_value.status_code = 200
        mocked_request.get.return_value.json.return_value.get.return_value = 2
        assert api_key_form.validate()
        assert flask.session['key'] == 'foo-bar1'
        assert flask.session['data_center'] == 'bar1'
        assert flask.session['num_lists'] == 2
        assert flask.session['store_aggregates']
        assert flask.session['monthly_updates']
Пример #9
0
def test_api_key_form(test_app):
    """Tests the API Key Form."""
    with test_app.app_context():
        api_key_form = ApiKeyForm()
        assert isinstance(api_key_form, ApiKeyForm)
Пример #10
0
def index():
    keyForm = ApiKeyForm()
    emailForm = EmailForm()
    return render_template('index.html', keyForm=keyForm, emailForm=emailForm)
Пример #11
0
def validate_key():
    form = ApiKeyForm()
    if form.validate_on_submit():
        return jsonify(True)
    else:
        return jsonify(form.errors), 400