def user_index(app): """Initialize the `User` doc type.""" test_index = Index(uuid4().hex) test_index.create() app.cluster.health(wait_for_status='yellow') # monkey patch `auth_index` original_auth_index = auth_models.auth_index auth_models.auth_index = test_index User.init(index=test_index._name) yield test_index auth_models.auth_index = original_auth_index # Remove all `User`s. # # [Don't use delete-by-query to clean out all or most documents in an # index. Rather create a new index...] # (https://www.elastic.co/guide/en/elasticsearch/plugins/2.2/plugins-delete-by-query.html) # # [It is no longer possible to delete the mapping for a type. Instead you # should delete the index and recreate it with the new mappings.] # (https://www.elastic.co/guide/en/elasticsearch/reference/2.2/indices-delete-mapping.html) test_index.delete()
def auth_index(): index = Index('auth') index.create() Customer.init(index=index._name) User.init(index=index._name) yield index index.delete()
def by_email(email): """Retrieve User by email.""" click.secho('Getting user by email: %s' % email, bold=True) try: user = User.get_by_email(email) click.secho('* Found: %s' % user, fg='green') except NotFoundError: click.secho('* Does not exist!', fg='red')
def jwt(email, renewable): """Generate JWT for User.""" try: user = User.get_by_email(email) except NotFoundError: click.secho('* Does not exist!', fg='red') else: click.echo(create_jwt(user, renewable=renewable))
def create_user(email, customer_names): """Create an User.""" click.secho('Creating user: %s' % email, bold=True) for customer_name in customer_names: try: Customer.get_by_name(customer_name) except NotFoundError: click.secho('* customer does not exist: %s' % customer_name, fg='red') raise click.Abort() try: user = User.get_by_email(email) click.secho('* user exists already: %s' % user, fg='red') except NotFoundError: user = User(email=email, customers=customer_names) user.save() click.secho('* user created: %s' % user, fg='green')
def user(auth_index, customer, another_customer): _user = User( email='*****@*****.**', customers=[customer.name, another_customer.name] ) _user.save(index=auth_index._name, refresh=True) yield _user _user.delete(index=auth_index._name, ignore=409)
def list_users(): """List all users.""" click.secho('Listing users...', bold=True) for user in User.search(): click.secho('%s' % user, fg='yellow')
def get_by_email(cls, email): return User(username=uuid4().hex, email=authenticated_email, customers=[uuid4().hex])
def user(user_index): """Create a random `User`.""" user = User(username=uuid4().hex, email='*****@*****.**' % uuid4().hex) user.save(index=user_index._name, refresh=True) return user
def test_cannot_resolve_ambiguous_email(self, user_index, user): User(email=user.email).save(index=user_index._name, refresh=True) with pytest.raises(ConflictError): User.get_by_email(user.email)
def test_cannot_resolve_nonexistent_email(self, user_index): with pytest.raises(NotFoundError): User.get_by_email('*****@*****.**')
def test_can_resolve_unique_emails(self, user_index, user, another_user): match = User.get_by_email(user.email) assert match == user