def test_create_user_validate_email(app, organization, user): # When no email is provided a ValueError is raised db.session.add(user) db.session.add(organization) db.session.commit() with pytest.raises(ValueError): services.create_user("", A_PASSWORD, A_FIRST, A_LAST) # When an invalid email is provided a ValueError is raised. with pytest.raises(ValueError): services.create_user("bad@@example.com", A_PASSWORD, A_FIRST, A_LAST)
def test_create_user_validate_password(app, organization): db.session.add(organization) db.session.commit() # When a password of length < 10 is provided, a ValueError is raised. with pytest.raises(ValueError): services.create_user(USER1_EMAIL, "", A_FIRST, A_LAST) with pytest.raises(ValueError): services.create_user(USER1_EMAIL, "123456789", A_FIRST, A_LAST) # When a password of length >= 10 is provided, a user is created: assert services.create_user(USER1_EMAIL, "1234567890", A_FIRST, A_LAST) is not None
def create_user(): try: data = request.get_json() new_user = services.create_user(data) except ValueError as error: raise BadRequest(str(error)) from error else: return {'id': new_user.id}, 201
def test_update_user(app, organization): user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) user = services.update_user(user, "*****@*****.**", "Bob", "Thomas") assert user.first_name == "Bob" assert user.last_name == "Thomas" assert user.email == "*****@*****.**"
def test_create_user(app, organization): # Given: good email/passwords are provided, a user is saved user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) # Then: a user is created, with email and password hash. assert user.email == USER1_EMAIL assert user.password_hash != A_PASSWORD assert user.first_name == A_FIRST assert user.last_name == A_LAST assert len(user.password_salt) > 0 # and the hash would return true if verified assert utils.verify_hash(A_PASSWORD, user.password_hash, user.password_salt)
def test_request_password_reset_email_down(app, organization, monkeypatch): # Given: existing user, matching password user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) db.session.add(user) db.session.commit() driver_mock = Mock() driver_mock.send_password_reset_email.return_value = False monkeypatch.setattr(services.mail, "make_driver", lambda: driver_mock) # Then: the user's reset_token is updated, and an email is sent. assert not services.request_password_reset(user) driver_mock.send_password_reset_email.assert_called_once()
def test_update_user_validate_email(app, organization): user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) with pytest.raises(ValueError): # badly formatted email services.update_user(user, "ttt", "Bob", "Thomas") with pytest.raises(TypeError): # no email services.update_user(user, "Bob", "Thomas") with pytest.raises(ValueError): # None type services.update_user(user, None, "Bob", "Thomas")
def test_remove_organization_member_must_belong(organization, user, admin): organization.organization_members.append( OrganizationMember( user=user, organization=organization, organization_role=find_organization_role(ROLE_USER_CODE), )) db.session.commit() user_two = services.create_user("*****@*****.**", "password10chars", "tom", "smith") with pytest.raises(ValueError): services.remove_organization_member(organization, user_two)
def test_update_user_validate_user(app, organization): user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) with pytest.raises(ValueError): # badly formatted user services.update_user(1, "*****@*****.**", "Bob", "Thomas") with pytest.raises(TypeError): # no user services.update_user("*****@*****.**", "Bob", "Thomas") with pytest.raises(ValueError): # None type for first_name services.update_user(None, "*****@*****.**", "Bob", "Thomas")
def test_update_user_validate_last_name(app, organization): user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) with pytest.raises(ValueError): # badly formatted first_name services.update_user(user, "*****@*****.**", "Bob", 1) with pytest.raises(TypeError): # no first_name services.update_user(user, "*****@*****.**", "Bob") with pytest.raises(ValueError): # None type for first_name services.update_user(user, "*****@*****.**", "Bob", None)
def create_user(c, email, password, first_name, last_name, is_system_admin=False): """ Create a user. Returns the UUID of the organization. """ from app import create_app, models, services (app, db, _) = create_app() with app.app_context(): user = services.create_user(email, password, first_name, last_name) user.is_system_admin = is_system_admin models.db.session.commit() print(user.uuid)
def test_request_password_reset(app, organization, monkeypatch): # Given: existing user, matching password user = services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, A_LAST) db.session.add(user) db.session.commit() driver_mock = Mock() monkeypatch.setattr(services.mail, "make_driver", lambda: driver_mock) # Then: the user's reset_token is updated, and an email is sent. original_reset_token = user.reset_token assert services.request_password_reset(user) driver_mock.send_password_reset_email.assert_called_once() assert driver_mock.mock_calls[0][1][0].id == user.id assert user.reset_token != original_reset_token assert user.reset_token_expires_at < datetime.utcnow()
def test_create_user_validate_last_name(app): # When no last name is provided, a ValueError is raised. with pytest.raises(ValueError): services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, None) with pytest.raises(ValueError): services.create_user(USER1_EMAIL, A_PASSWORD, A_FIRST, "")
def test_create_user_validate_first_name(app): # When no first name is provided, a ValueError is raised. with pytest.raises(ValueError): services.create_user(USER1_EMAIL, A_PASSWORD, None, A_LAST) with pytest.raises(ValueError): services.create_user(USER1_EMAIL, A_PASSWORD, "", A_LAST)