Esempio n. 1
0
    def test_handles_inconsistent_aliases(self, app, customer_index, customer, random_index, another_index):
        random_read_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.read.name
        )
        random_write_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.write.name
        )

        # Setup
        customer.permissions = {
            random_index._name: [AliasType.read.name, AliasType.write.name],
        }
        customer.save(index=customer_index._name)

        app.es.indices.delete_alias(index=random_index._name, name=random_write_alias)

        # Check
        aliases = app.es.indices.get_alias()
        assert len(customer.permissions[random_index._name]) == 2
        assert len(aliases[random_index._name]['aliases']) == 1
        assert random_read_alias in aliases[random_index._name]['aliases']

        # Test
        customer.permissions[random_index._name] = [AliasType.read.name]
        customer.save(index=customer_index._name)

        # Check
        aliases = app.es.indices.get_alias()
        assert len(customer.permissions[random_index._name]) == 1
        assert len(aliases[random_index._name]['aliases']) == 1
        assert random_read_alias in aliases[random_index._name]['aliases']
Esempio n. 2
0
    def test_can_remove_aliases(self, app, customer_index, customer, random_index):
        customer.permissions = {
            random_index._name: [AliasType.read.name, AliasType.write.name]
        }
        customer.save(index=customer_index._name)

        read_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.read.name
        )
        write_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.write.name
        )

        aliases = app.es.indices.get_alias()[random_index._name]['aliases']
        assert len(aliases) == 2
        assert read_alias in aliases
        assert write_alias in aliases

        customer.permissions[random_index._name] = [AliasType.read.name]
        customer.save(index=customer_index._name)

        aliases = app.es.indices.get_alias()[random_index._name]['aliases']
        assert len(aliases) == 1
        assert read_alias in aliases

        customer.permissions = {}
        customer.save(index=customer_index._name)

        aliases = app.es.indices.get_alias()[random_index._name]['aliases']
        assert not aliases
Esempio n. 3
0
    def test_can_merge_aliases(self, app, customer_index, customer, random_index, another_index):
        customer.permissions = {
            random_index._name: [AliasType.read.name, AliasType.write.name],
            another_index._name: [AliasType.write.name]
        }
        customer.save(index=customer_index._name)

        random_read_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.read.name
        )
        random_write_alias = get_alias(
            random_index._name,
            customer.name,
            AliasType.write.name
        )
        another_read_alias = get_alias(
            another_index._name,
            customer.name,
            AliasType.read.name
        )
        another_write_alias = get_alias(
            another_index._name,
            customer.name,
            AliasType.write.name
        )

        aliases = app.es.indices.get_alias()
        random_aliases = aliases[random_index._name]['aliases']
        other_aliases = aliases[another_index._name]['aliases']

        assert len(random_aliases) == 2
        assert random_read_alias in random_aliases
        assert random_write_alias in random_aliases

        assert len(other_aliases) == 1
        assert another_write_alias in other_aliases

        customer.permissions[random_index._name] = [AliasType.read.name]
        customer.permissions[another_index._name] = [
            AliasType.read.name,
            AliasType.write.name
        ]
        customer.save(index=customer_index._name)

        aliases = app.es.indices.get_alias()
        random_aliases = aliases[random_index._name]['aliases']
        other_aliases = aliases[another_index._name]['aliases']

        assert len(random_aliases) == 1
        assert random_read_alias in random_aliases

        assert len(other_aliases) == 2
        assert another_read_alias in other_aliases
        assert another_write_alias in other_aliases
Esempio n. 4
0
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')
Esempio n. 5
0
    def test_charges_successful_requests(self, book, requests, customer, app):
        assert 'cycles' not in customer

        index = 'library'
        alias = get_alias(index, customer.name, AliasType.read.name)

        response = requests.get(
            url_for('document_retrieve',
                    index=index,
                    doc_type='book',
                    id=book['_id']))

        assert response.status_code == httplib.OK

        customer.refresh()
        assert customer.cycles[alias] == app.config['CYCLES_CRUD']
Esempio n. 6
0
    def test_charges_on_successful_updates(self, book, requests, customer,
                                           app):
        assert 'cycles' not in customer

        index = 'library'
        alias = get_alias(index, customer.name, AliasType.write.name)

        response = requests.put(url_for('document_update',
                                        index=index,
                                        doc_type='book',
                                        id=book['_id']),
                                json={'title': uuid4().hex})

        assert response.status_code == httplib.OK

        customer.refresh()
        assert customer.cycles[alias] == app.config['CYCLES_CRUD']
Esempio n. 7
0
    def test_charges_on_success(self, requests, customer):
        assert 'cycles' not in customer

        index = 'library'
        alias = get_alias(index, customer.name, AliasType.read.name)

        response = requests.get(
            url_for(
                'document_list',
                index=index,
                doc_type='book',
            ), )

        assert response.status_code == httplib.OK

        customer.refresh()

        # I failed to wrap anything on the `current_app` proxy in a `Mock`,
        # so this is the most we can assume
        assert customer.cycles[alias] > 0
Esempio n. 8
0
    def test_charges_on_invalid_id(self, requests, customer, app):
        assert 'cycles' not in customer

        index = 'library'
        alias = get_alias(index, customer.name, AliasType.write.name)

        invalid_id = 'does not exist'

        response = requests.delete(
            url_for(
                'document_delete',
                index=index,
                doc_type='book',
                id=invalid_id,
            ), )

        assert response.status_code == httplib.NOT_FOUND

        customer.refresh()
        assert customer.cycles[alias] == app.config['CYCLES_CRUD']