Esempio n. 1
0
    def test_load_itime(self, test_client, session):
        ws = WorkspaceFactory.create(name="abc")
        command = CommandFactory.create(workspace=ws)
        session.add(ws)
        session.add(command)
        session.commit()

        # Timestamp of 14/12/2018
        itime = 1544745600.0
        data = {
            'command': command.command,
            'tool': command.tool,
            'itime': itime
        }

        res = test_client.put(
            '/v2/ws/{ws_name}/activities/{id}/'.format(ws_name=ws.name,
                                                       id=command.id),
            data=data,
        )

        # Changing res.json['itime'] to timestamp format of itime
        res_itime = res.json['itime'] / 1000
        assert res.status_code == 200
        assert res_itime == itime
    def test_activity_feed(self, test_client, session):
        ws = WorkspaceFactory.create(name="abc")
        command = CommandFactory.create(workspace=ws, tool="nessus")
        session.add(ws)
        session.add(command)
        session.commit()

        res = test_client.get(f'/v2/ws/{ws.name}/activities/')

        assert res.status_code == 200
        activities = res.json['activities'][0]
        assert activities['hosts_count'] == 1
        assert activities['vulnerabilities_count'] == 1
        assert activities['tool'] == 'nessus'
Esempio n. 3
0
    def test_save_with_command(self, many_test_data, monkeypatch, session):
        obj_class = many_test_data['class']
        if obj_class == Command:
            return
        workspace = WorkspaceFactory.create(name='test')
        command = CommandFactory.create(workspace=workspace)
        session.commit()
        mapper_manager = MapperManager()
        mapper_manager.createMappers(workspace.name)
        test_data = many_test_data
        raw_data = test_data['data']
        if test_data['parent']:
            parent = test_data['parent']['parent_factory'].create()
            session.commit()

            test_data['data']['parent'] = parent.id
            test_data['data']['parent_type'] = test_data['parent'][
                'parent_type']
            if obj_class not in [Note]:
                test_data['expected_payload']['parent'] = parent.id
            if obj_class in [Vuln, Credential]:
                test_data['expected_payload']['parent_type'] = test_data[
                    'parent']['parent_type']

        def mock_server_post(test_data,
                             post_url,
                             update=False,
                             expected_response=201,
                             **params):
            assert post_url == '{0}/ws/test/{1}/?command_id={2}'.format(
                _create_server_api_url(), test_data['api_end_point'],
                params['command_id'])
            assert expected_response == 201
            assert update == False
            metadata = params.pop('metadata')
            assert metadata['owner'] == test_data['expected_payload']['owner']
            params.pop('command_id')
            test_data['expected_payload'].pop('command_id')
            assert params == test_data['expected_payload']
            return {'id': 1, 'ok': True, 'rev': ''}

        monkeypatch.setattr(faraday.client.persistence.server.server, '_post',
                            partial(mock_server_post, test_data))
        obj = obj_class(raw_data, workspace.name)
        mapper_manager.save(obj, command.id)
Esempio n. 4
0
    def test_update_with_command(self, many_test_data, monkeypatch, session):
        obj_class = many_test_data['class']
        if obj_class in [Command]:
            return
        workspace = WorkspaceFactory.create(name='test')
        command = CommandFactory.create(workspace=workspace)
        session.add(command)
        session.commit()
        mapper_manager = MapperManager()
        mapper_manager.createMappers(workspace.name)

        test_data = many_test_data
        raw_data = test_data['data']
        if test_data['parent']:
            parent = test_data['parent']['parent_factory'].create()
            session.add(parent)
            session.commit()
            test_data['data']['parent'] = parent.id
            test_data['data']['parent_type'] = test_data['parent'][
                'parent_type']
            test_data['expected_payload']['parent'] = parent.id
            if obj_class in [Vuln, Credential]:
                test_data['expected_payload']['parent_type'] = test_data[
                    'parent']['parent_type']
        relational_model = test_data['factory'].create()
        session.add(relational_model)
        session.commit()

        def mock_server_put(put_url,
                            update=False,
                            expected_response=201,
                            **params):
            assert put_url == '{0}/ws/test/{1}/{2}/?command_id={3}'.format(
                _create_server_api_url(), test_data['api_end_point'],
                test_data['id'], params['command_id'])
            assert expected_response == 200
            assert update == False
            return {'id': 1, 'ok': True, 'rev': ''}

        raw_data['id'] = relational_model.id
        test_data['id'] = relational_model.id
        monkeypatch.setattr(faraday.client.persistence.server.server, '_put',
                            mock_server_put)
        obj = obj_class(raw_data, workspace.name)
        mapper_manager.update(obj, command.id)
Esempio n. 5
0
    def test_load_itime(self, test_client, session):
        ws = WorkspaceFactory.create(name="abc")
        command = CommandFactory.create(workspace=ws)
        session.add(ws)
        session.add(command)
        session.commit()

        new_start_date = command.end_date - datetime.timedelta(days=1)
        data = {
            'command': command.command,
            'tool': command.tool,
            'itime': new_start_date.timestamp()

        }

        res = test_client.put(f'/v3/ws/{ws.name}/activities/{command.id}',
                data=data,
            )
        assert res.status_code == 200

        # Changing res.json['itime'] to timestamp format of itime
        res_itime = res.json['itime'] / 1000.0
        assert res.status_code == 200
        assert datetime.datetime.fromtimestamp(res_itime) == new_start_date