Exemplo n.º 1
0
async def test_switch_update(client, login, switch, db_conn_acquirer):
    with freeze_time('2020-08-15'):
        await client.post('/zbs/switches/1',
                          data={
                              'is_active': False,
                              'groups': 'group1, group2'
                          })

    async with db_conn_acquirer() as conn:
        switches_query_result = await conn.execute(
            switches.select().where(switches.c.id == 1))
        updated_switch = await switches_query_result.first()
        switch_history_query_result = await conn.execute(
            switch_history.select().where(
                switch_history.c.switch_id == updated_switch.id), )
        new_switch_history = await switch_history_query_result.first()
        admin_query_result = await conn.execute(
            users.select().where(users.c.login == 'admin'))
        admin = await admin_query_result.first()

    assert updated_switch.is_active is False
    assert updated_switch.created_at == datetime.datetime(
        2020, 4, 15, tzinfo=datetime.timezone.utc)
    assert updated_switch.updated_at == datetime.datetime(
        2020, 8, 15, tzinfo=datetime.timezone.utc)
    assert new_switch_history is not None
    assert new_switch_history.new_value == 'False'
    assert new_switch_history.user_id == admin.id
    assert new_switch_history.changed_at == datetime.datetime(
        2020,
        8,
        15,
        tzinfo=datetime.timezone.utc,
    )
Exemplo n.º 2
0
async def test_switch_add_post_method(
    client,
    db_conn_acquirer,
    login,
    switch_data_factory_with_ttl,
):
    switch_data, asserted_ttl = switch_data_factory_with_ttl

    response = await client.post('/zbs/switches/add', data=switch_data)
    content = await response.content.read()
    switch_data['is_hidden'] = False

    assert response.status == HTTPOk.status_code
    assert 'Switches list' in content.decode('utf-8')
    async with db_conn_acquirer() as conn:
        result = await conn.execute(
            switches.select().where(switches.c.name == switch_data['name']))
        created_switch = await result.first()
    for field_name, field_value in switch_data.items():
        if field_name == 'groups':
            field_value = ['check_adding', 'group2']
        assert getattr(created_switch, field_name) == field_value
    assert created_switch.created_at == datetime.datetime(
        2020, 4, 15, tzinfo=datetime.timezone.utc)
    assert created_switch.ttl == asserted_ttl
Exemplo n.º 3
0
    async def resurrect_deleted_flag(
            self, form_data: MultiDictProxy) -> Optional[Dict[str, Any]]:
        async with self.request.app['db'].acquire() as conn:
            result = await conn.execute(
                switches.select().where(switches.c.name == form_data['name']))
            already_created_switch = await result.first()

            if already_created_switch:
                try:
                    form_data = self._validate_form_data(
                        form_data)  # type: ignore
                except ValidationError as error:
                    return await self.get_context_data(
                        errors=error, user_input=dict(form_data))
                form_data['is_hidden'] = False  # type: ignore
                update_query = self.model.update().where(
                    self.model.c.id == str(already_created_switch.id)).values(
                        form_data)
                await conn.execute(update_query)

                new_value = str(form_data.get('is_active'))
                await save_switch_history(self.request, already_created_switch,
                                          new_value)

                location = self.request.app.router['switches_list'].url_for()
                raise web.HTTPFound(location=location)
Exemplo n.º 4
0
async def test_switch_update(setup_tables_and_data, client, login, switch):
    await client.post('/zbs/switches/1', data={'is_active': False})

    async with client.server.app['db'].acquire() as conn:
        result = await conn.execute(switches.select().where(switches.c.id == 1)
                                    )
        updated_switch = await result.first()

    assert updated_switch.is_active is False
Exemplo n.º 5
0
async def test_switches_copy_new_switch(client, login):
    response = await client.post('/zbs/switches/copy')
    async with client.server.app['db'].acquire() as conn:
        result = await conn.execute(
            switches.select().where(switches.c.name == 'extremely_new_switch'))
        new_switch = await result.first()

    assert response.status == 200
    assert new_switch is not None
    assert new_switch.name == 'extremely_new_switch'
Exemplo n.º 6
0
async def test_switch_update(setup_tables_and_data, client, login, switch):
    with freeze_time('2020-08-15'):
        await client.post('/zbs/switches/1', data={'is_active': False})

    async with client.server.app['db'].acquire() as conn:
        result = await conn.execute(switches.select().where(switches.c.id == 1)
                                    )
        updated_switch = await result.first()

    assert updated_switch.is_active is False
    assert updated_switch.created_at == datetime.datetime(
        2020, 4, 15, tzinfo=datetime.timezone.utc)
    assert updated_switch.updated_at == datetime.datetime(
        2020, 8, 15, tzinfo=datetime.timezone.utc)
Exemplo n.º 7
0
async def test_switch_strip_spaces(
    client,
    db_conn_acquirer,
    login,
    switch_data_factory,
    switch_name,
):
    switch_data = switch_data_factory
    switch_data['name'] = switch_name

    await client.post('/zbs/switches/add', data=switch_data)
    switch_data['is_hidden'] = False
    async with db_conn_acquirer() as conn:
        result = await conn.execute(switches.select().order_by(desc('id')))
        created_switch = await result.first()

    assert created_switch.name == 'switch'
Exemplo n.º 8
0
async def test_switches_copy_existing_switch_foo(
    client,
    db_conn_acquirer,
    http_get_arguments,
    old_switch_is_active_expected,
    expected_updated_at,
):
    response = await client.post(f'/zbs/switches/copy{http_get_arguments}')
    async with db_conn_acquirer() as conn:
        result = await conn.execute(switches.count())
        switches_count = await result.first()
        switches_count = switches_count[0]
        result = await conn.execute(
            switches.select().where(switches.c.name == 'switch7'))
        old_switch = await result.first()

    assert response.status == 200
    assert switches_count == 7
    assert old_switch.is_active == old_switch_is_active_expected
    assert old_switch.updated_at == expected_updated_at
Exemplo n.º 9
0
async def test_switch_add(setup_tables_and_data, client, login, switch):
    response = await client.get('/zbs/switches/add')
    content = await response.content.read()

    assert 'Flag adding' in content.decode('utf-8')

    switch_data = {
        'name':
        'switch_to_check_add',
        'is_active':
        True,
        'groups':
        'check_adding, group2,    ,,',
        'version':
        1,
        'comment':
        'This is the story of a big bad wolf an little girl whose name was Little Red Riding Hood',
    }
    response = await client.post('/zbs/switches/add', data=switch_data)
    content = await response.content.read()

    assert 'Switches list' in content.decode('utf-8')

    switch_data['is_hidden'] = False

    async with client.server.app['db'].acquire() as conn:
        result = await conn.execute(
            switches.select().where(switches.c.name == switch_data['name']))
        created_switch = await result.first()

    for field_name, field_value in switch_data.items():
        if field_name == 'groups':
            field_value = list(
                filter(None,
                       [item.strip() for item in field_value.split(',')]), )
        assert getattr(created_switch, field_name) == field_value

    assert created_switch.created_at == datetime.datetime(
        2020, 4, 15, tzinfo=datetime.timezone.utc)
Exemplo n.º 10
0
 def get_queryset(self) -> Select:
     return switches.select(whereclause=(switches.c.is_hidden == false()))
Exemplo n.º 11
0
 async def get_queryset(self) -> Select:
     qs = switches.select().with_only_columns([switches.c.name
                                               ]).order_by(switches.c.name)
     return await self.filter_queryset(qs)
Exemplo n.º 12
0
 def get_queryset(self) -> Select:
     return switches.select()
Exemplo n.º 13
0
async def switch(client):
    async with client.server.app['db'].acquire() as conn:
        result = await conn.execute(switches.select().where(switches.c.id == 1)
                                    )
        return await result.first()
Exemplo n.º 14
0
 async def get_switches(self) -> RowProxy:
     async with self.request.app['db'].acquire() as conn:
         queryset = switches.select(whereclause=(switches.c.is_hidden == false()))
         result = await conn.execute(queryset)
         return await result.fetchall()