def create_customer(customer_name): """Create a customer.""" click.secho('Creating customer: %s...' % customer_name, bold=True) try: customer = Customer.get_by_name(customer_name) click.secho('* customer exists already: %s' % customer, fg='red') except NotFoundError: customer = Customer(name=customer_name, permissions={}, cycles={}) customer.save() click.secho('* customer created: %s' % customer, fg='green')
def auth_index(): index = Index('auth') index.create() Customer.init(index=index._name) User.init(index=index._name) yield index index.delete()
def test_can_charge_for_multiple_indexes(self, customer): costs = [('foo', 1), ('bar', 2)] for cost in costs: Customer.charge_cycles(customer.meta.id, cost[0], cost[1]) # refresh customer ref customer = Customer.get(id=customer.meta.id) assert len(customer.cycles.to_dict()) == 2 for cost in costs: assert customer.cycles[cost[0]] == cost[1]
def test_can_init_cycles(self, customer): assert 'cycles' not in customer target = 'some_index' cycles = 5 Customer.charge_cycles(customer.meta.id, target, cycles) # refresh customer ref customer = Customer.get(id=customer.meta.id) assert 'cycles' in customer assert len(customer.cycles.to_dict()) == 1 assert customer.cycles[target] == cycles
def test_can_update_cycles(self, customer): target = 'some_index' initial_cycles = 2 new_cycles = 3 customer.cycles[target] = initial_cycles customer.save() Customer.charge_cycles(customer.meta.id, target, new_cycles) # refresh customer ref customer = Customer.get(id=customer.meta.id) assert len(customer.cycles.to_dict()) == 1 assert customer.cycles[target] == initial_cycles + new_cycles
def list_permissions(customer_name): """Show permissions for a given customer.""" try: customer = Customer.get_by_name(customer_name) except NotFoundError: click.secho('* customer does not exist: %s' % customer_name, fg='red') raise click.Abort() click.secho( 'Listing customer permissions "<index>=<permission> (<alias exists>)":', bold=True) # TODO get from context instead! es = current_app.es # type: elasticsearch.Elasticsearch permissions = customer.permissions.to_dict() if len(permissions) <= 0: click.secho('* No existing permissions', fg='yellow') else: for index in permissions: for permission in permissions[index]: alias_exists = es.indices.exists_alias( index, get_alias(index, customer_name, permission)) click.secho('- %s=%s (%s)' % (index, permission, alias_exists), fg='green' if alias_exists else 'yellow')
def named_customer(name, auth_index): _customer = Customer(name=name, permissions={}) _customer.save(index=auth_index._name, refresh=True) yield _customer # update reference _customer.refresh() _customer.delete(index=auth_index._name)
def list_customers(): """List all customers in attached elasticsearch""" click.secho('List of customers:', bold=True) customer_list = Customer.search(index=auth_index._name).execute() for customer in customer_list: click.secho('* customer name: %s' % customer.name, fg='green')
def customer_index(app): """Initialize the `Customer` 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 Customer.init(index=test_index._name) Customer._doc_type.index = test_index._name yield test_index auth_models.auth_index = original_auth_index test_index.delete()
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 test_can_add_permissions(self, customer_index, customer, random_index, another_index): # Setup permissions = { random_index._name: [ AliasType.read.name ] } customer.permissions = permissions customer.save(index=customer_index._name, refresh=True) customer = Customer.get_by_name(customer.name) assert customer.permissions == permissions # Test permissions = { random_index._name: [ AliasType.write.name, ], another_index._name: [ AliasType.read.name, ], } customer.add_permissions(permissions) customer.save(index=customer_index._name, refresh=True) # Check merged_permissions = Customer.get_by_name(customer.name).permissions.to_dict() assert len(merged_permissions) == 2 assert random_index._name in merged_permissions assert another_index._name in merged_permissions assert len(merged_permissions[random_index._name]) == 2 assert AliasType.read.name in merged_permissions[random_index._name] assert AliasType.write.name in merged_permissions[random_index._name] assert len(merged_permissions[another_index._name]) == 1 assert AliasType.read.name in merged_permissions[random_index._name]
def deny(customer_name, grants): """Deny (remove) customer permissions. Usage: auth deny CUSTOMER_NAME index1=read index1=write index2=read """ try: customer = Customer.get_by_name(customer_name) except NotFoundError: click.secho('* customer does not exist: %s' % customer_name, fg='red') raise click.Abort() else: click.secho('* denying previous permissions from "%s"' % customer_name, bold=True) customer.remove_permissions(grants) customer.save()
def grant(customer_name, grants): """Grant (add) customer permissions. Usage: auth grant CUSTOMER_NAME index1=read index1=write index2=read """ try: customer = Customer.get_by_name(customer_name) except NotFoundError: click.secho('* customer does not exist: %s' % customer_name, fg='red') raise click.Abort() else: click.secho('* granting new permissions to "%s"' % customer_name, bold=True) customer.add_permissions(grants) customer.save()
def test_handles_conflicting_customer_names(self, customer_index, customer): Customer(name=customer.name).save(index=customer_index._name, refresh=True) with pytest.raises(ConflictError): Customer.get_by_name(customer.name)
def test_handles_charging_of_invalid_customers(self): with pytest.raises(NotFoundError) as exception_info: Customer.charge_cycles('DoesNotExist', 'some_index', 5) assert exception_info.value.error == 'Invalid customer'
def customer(customer_index): """Instantiate a random `Customer`.""" customer = Customer(name=uuid4().hex) customer.save(index=customer_index._name, refresh=True) return customer
def test_can_retrieve_instance_by_name(self, customer): match = Customer.get_by_name(customer.name) assert match.to_dict() == customer.to_dict()
def test_handles_non_existent_customers(self): with pytest.raises(NotFoundError): Customer.get_by_name(uuid4().hex)