def test_store_user_othererror(mocker): """Tests the store user function Database commit raises a differerent error.""" mocker.patch('app.dbops.AppUser') mocked_db = mocker.patch('app.dbops.db') mocked_db.session.commit.side_effect = Exception() with pytest.raises(Exception): store_user('foo', '*****@*****.**', 'bar', 'baz') mocked_db.session.rollback.assert_called()
def test_store_user_integrityerror(mocker): """Tests the store_user function. Database commit raises an IntegrityError.""" mocker.patch('app.dbops.AppUser') mocked_db = mocker.patch('app.dbops.db') mocked_db.session.commit.side_effect = IntegrityError('foo', 'bar', 'baz') mocked_update_user = mocker.patch('app.dbops.update_user') store_user('foo', '*****@*****.**', 'bar', 'baz') mocked_db.session.rollback.assert_called() mocked_update_user.assert_called_with( {'name': 'foo', 'email': '*****@*****.**', 'email_hash': 'bar'}, 'baz')
def test_store_user(mocker): """Tests the store_user function.""" mocked_user_obj = mocker.patch('app.dbops.AppUser') mocked_user = mocked_user_obj.return_value mocked_db = mocker.patch('app.dbops.db') user = store_user('foo', '*****@*****.**', 'bar', 'baz') mocked_user_obj.assert_called_with( name='foo', email='*****@*****.**', email_hash='bar', approved=False, orgs=['baz']) mocked_db.session.add.assert_called_with(mocked_user) mocked_db.session.commit.assert_called() assert user == mocked_user
def validate_org_info(): """Validates organization info submitted via POST. Calls the WTF-Form validation function. If the form is valid, stores the organization in the database, then stores a new user/updates an existing user such that they are affiliated with the organization. """ org_form = OrgForm() if org_form.validate_on_submit(): # Create a list of strings from the affiliation checkboxes # and the "Other" affiliation input field affiliations = [*[elt.label.text for elt in org_form if isinstance(elt, BooleanField) and elt.data and elt.label.text != 'Other'], *([org_form.other_affiliation_name.data] if org_form.other_affiliation_name.data else [])] org_details = { 'name': session['org'], 'financial_classification': org_form.financial_classification.data, 'coverage_scope': org_form.coverage_scope.data, 'coverage_focus': org_form.coverage_focus.data, 'platform': org_form.platform.data, 'employee_range': org_form.employee_range.data, 'budget': org_form.budget.data, 'affiliations': json.dumps(affiliations)} org = store_org(org_details) user = store_user(session['user_name'], session['email'], session['email_hash'], org) if user.approved: send_activated_email.delay(user.email, user.email_hash) return jsonify({'user': '******' if user and user.approved else 'other'}) return jsonify(org_form.errors), 422
def validate_basic_info(): """Validates basic info submitted via POST. Calls the WTF-Forms validation function. If form is valid, calculates the md5-hash of the user's email and parses the form data (e.g. titlecase organization names). Checks whether the user and/or organization are already present in the database. If the organization exists, create or update the user such that they are affiliated with the organization. If the user already exists and is approved for access, re-send them an email containing their access link. If the organization does not exist, store the user's data in the session so that it can be used later (see validate_org_info()) Returns: A json containing either the form's errors (if form does not validate) or information about what happened (i.e. was the org new or existing, was the user new or existing, etc.). """ user_form = UserForm() if user_form.validate_on_submit(): user_org = titlecase(user_form.news_org.data) user_name = user_form.name.data.title() email_hash = (hashlib.md5( user_form.email.data.encode()).hexdigest()) user_email = user_form.email.data # See if the user already exists existing_user = AppUser.query.filter_by(email=user_email).first() # See if the organization already exists existing_org = Organization.query.filter_by(name=user_org).first() # If the user selected an organization we're already tracking # Add or update that user with a link to the organization if existing_org: if existing_user: existing_user.name = user_name existing_user.orgs.append(existing_org) db.session.commit() # If the user exists, and was already approved for access # Re-send them the email containing their access link if existing_user.approved: send_activated_email.delay(user_email, email_hash) else: store_user(user_name, user_email, email_hash, existing_org) return jsonify({'org': 'existing', 'user': ('approved' if existing_user and existing_user.approved else 'other')}) # If we're not already tracking the organization, add the user's data # to the session to store later once they've told us about the org session['user_name'] = user_name session['email'] = user_form.email.data session['email_hash'] = email_hash session['org'] = user_org return jsonify({'org': 'new'}) return jsonify(user_form.errors), 422