Esempio n. 1
0
 def test_as_json(self):
     app = App('test', devices=self.all_devices)
     app_json = app.as_json(with_devices=True)
     self.assertEqual(app_json['name'], 'test')
     expected_devices_json = [device.as_json() for device in app.devices]
     for device in app_json['devices']:
         self.assertIn(device, expected_devices_json)
Esempio n. 2
0
 def test_as_json(self):
     app = App('test', devices=self.all_devices)
     app_json = app.as_json(with_devices=True)
     self.assertEqual(app_json['name'], 'test')
     expected_devices_json = [device.as_json() for device in app.devices]
     for device in app_json['devices']:
         self.assertIn(device, expected_devices_json)
Esempio n. 3
0
def create_user():
    from walkoff.serverdb import add_user, User, Role, initialize_default_resources_admin, \
        initialize_default_resources_guest
    db.create_all()

    # Setup admin and guest roles
    initialize_default_resources_admin()
    initialize_default_resources_guest()

    # Setup admin user
    admin_role = Role.query.filter_by(id=1).first()
    admin_user = User.query.filter_by(username="******").first()
    if not admin_user:
        add_user(username='******', password='******', roles=[1])
    elif admin_role not in admin_user.roles:
        admin_user.roles.append(admin_role)

    db.session.commit()

    apps = set(helpers.list_apps(walkoff.config.Config.APPS_PATH)) - set([
        _app.name for _app in
        current_app.running_context.execution_db.session.query(App).all()
    ])
    current_app.logger.debug('Found new apps: {0}'.format(apps))
    for app_name in apps:
        current_app.running_context.execution_db.session.add(
            App(name=app_name, devices=[]))
    db.session.commit()
    current_app.running_context.execution_db.session.commit()
    send_all_cases_to_workers()
    current_app.logger.handlers = logging.getLogger('server').handlers
Esempio n. 4
0
def create_user():
    from walkoff import executiondb
    from walkoff.serverdb import add_user, User, Role, initialize_default_resources_admin, \
        initialize_default_resources_guest
    db.create_all()

    # Setup admin and guest roles
    initialize_default_resources_admin()
    initialize_default_resources_guest()

    # Setup admin user
    admin_role = Role.query.filter_by(id=1).first()
    admin_user = User.query.filter_by(username="******").first()
    if not admin_user:
        add_user(username='******', password='******', roles=[1])
    elif admin_role not in admin_user.roles:
        admin_user.roles.append(admin_role)

    db.session.commit()

    apps = set(helpers.list_apps()) - set([
        _app.name
        for _app in executiondb.execution_db.session.query(App).all()
    ])
    app.logger.debug('Found apps: {0}'.format(apps))
    for app_name in apps:
        executiondb.execution_db.session.add(App(name=app_name, devices=[]))
    db.session.commit()
    executiondb.execution_db.session.commit()
    CaseSubscription.sync_to_subscriptions()

    app.logger.handlers = logging.getLogger('server').handlers
Esempio n. 5
0
    def test_device_pagination(self):
        fields_json = [{'name': 'test_name', 'type': 'integer', 'encrypted': False},
                       {'name': 'test2', 'type': 'string', 'encrypted': False}]
        walkoff.config.app_apis = {self.test_app_name: {'devices': {'test_type': {'fields': fields_json}}}}
        app = App(name=self.test_app_name)
        self.app.running_context.execution_db.session.add(app)
        self.app.running_context.execution_db.session.commit()

        for i in range(40):
            device_json = {'app_name': 'TestApp', 'name': str(i), 'type': 'test_type',
                           'fields': [{'name': 'test_name', 'value': 123}, {'name': 'test2', 'value': 'something'}]}
            self.post_with_status_check('/api/devices', headers=self.headers, data=json.dumps(device_json),
                                        status_code=OBJECT_CREATED, content_type='application/json')

        response = self.get_with_status_check('/api/devices?page=1', headers=self.headers, status_code=SUCCESS)
        self.assertEqual(len(response), 20)
        devices = [str(i) for i in range(20)]
        for device in response:
            self.assertIn(device['name'], devices)
        response = self.get_with_status_check('/api/devices?page=2', headers=self.headers, status_code=SUCCESS)
        self.assertEqual(len(response), 20)
        devices = [str(i) for i in range(20, 40)]
        for device in response:
            self.assertIn(device['name'], devices)
        response = self.get_with_status_check('/api/devices?page=3', headers=self.headers, status_code=SUCCESS)
        self.assertEqual(len(response), 0)
Esempio n. 6
0
    def setUp(self):
        from walkoff import executiondb
        self.test_app_name = 'TestApp'
        self.device1 = Device('test', [], [], 'type1')
        plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
        encrypted_fields = [EncryptedDeviceField('test3', 'boolean', True),
                            EncryptedDeviceField('test4', 'string', 'something else')]
        self.device2 = Device('test2', plaintext_fields, encrypted_fields, 'type2')
        self.db_app = App(name=self.test_app_name, devices=[self.device1, self.device2])

        executiondb.execution_db.session.add(self.db_app)
        executiondb.execution_db.session.commit()
Esempio n. 7
0
    def put_patch_update(self, verb):
        send_func = self.put_with_status_check if verb == 'put' else self.patch_with_status_check
        device1 = Device('test', [], [], 'type')
        device2 = Device('test2', [], [], 'type')
        app = App(name=self.test_app_name, devices=[device1, device2])
        executiondb.execution_db.session.add(app)
        executiondb.execution_db.session.commit()

        fields_json = [{
            'name': 'test_name',
            'type': 'integer',
            'encrypted': False
        }, {
            'name': 'test2',
            'type': 'string',
            'encrypted': False
        }]
        walkoff.config.config.app_apis = {
            self.test_app_name: {
                'devices': {
                    'test_type': {
                        'fields': fields_json
                    }
                }
            }
        }

        fields_json = [{
            'name': 'test_name',
            'value': 123
        }, {
            'name': 'test2',
            'value': 'something'
        }]

        data = {
            'id': device1.id,
            'name': 'renamed',
            'app_name': self.test_app_name,
            'type': 'test_type',
            'fields': fields_json
        }
        send_func('/api/devices',
                  headers=self.headers,
                  data=json.dumps(data),
                  status_code=SUCCESS,
                  content_type='application/json')

        self.assertEqual(device1.name, 'renamed')
        self.assertEqual(
            device1.get_plaintext_fields(),
            {field['name']: field['value']
             for field in fields_json})
Esempio n. 8
0
 def test_read_all_devices(self):
     device1 = Device('test', [], [], 'type')
     device2 = Device('test2', [], [], 'type')
     app = App(name=self.test_app_name, devices=[device1, device2])
     self.app.running_context.execution_db.session.add(app)
     self.app.running_context.execution_db.session.commit()
     response = self.get_with_status_check('/api/devices?page=1', headers=self.headers, status_code=SUCCESS)
     expected_device1 = device1.as_json()
     expected_device1['app_name'] = 'TestApp'
     expected_device2 = device2.as_json()
     expected_device2['app_name'] = 'TestApp'
     self.assertIn(expected_device1, response)
     self.assertIn(expected_device2, response)
Esempio n. 9
0
    def test_update_device_type_dne(self):
        device1 = Device('test', [], [], 'type')
        device2 = Device('test2', [], [], 'type')
        app = App(name=self.test_app_name, devices=[device1, device2])
        self.app.running_context.execution_db.session.add(app)
        self.app.running_context.execution_db.session.commit()

        fields_json = [{'name': 'test_name', 'type': 'integer', 'encrypted': False},
                       {'name': 'test2', 'type': 'string', 'encrypted': False}]
        walkoff.config.app_apis = {self.test_app_name: {'devices': {'test_type': {'fields': fields_json}}}}

        data = {'id': device1.id, 'name': 'renamed', 'app_name': self.test_app_name, 'type': 'Invalid'}
        self.put_with_status_check('/api/devices', headers=self.headers, data=json.dumps(data),
                                   status_code=INVALID_INPUT_ERROR, content_type='application/json')
Esempio n. 10
0
    def test_create_device(self):
        fields_json = [{
            'name': 'test_name',
            'type': 'integer',
            'encrypted': False
        }, {
            'name': 'test2',
            'type': 'string',
            'encrypted': False
        }]
        walkoff.config.config.app_apis = {
            self.test_app_name: {
                'devices': {
                    'test_type': {
                        'fields': fields_json
                    }
                }
            }
        }
        app = App(name=self.test_app_name)
        executiondb.execution_db.session.add(app)
        executiondb.execution_db.session.commit()

        device_json = {
            'app_name':
            'TestApp',
            'name':
            'test',
            'type':
            'test_type',
            'fields': [{
                'name': 'test_name',
                'value': 123
            }, {
                'name': 'test2',
                'value': 'something'
            }]
        }
        response = self.post_with_status_check('/api/devices',
                                               headers=self.headers,
                                               data=json.dumps(device_json),
                                               status_code=OBJECT_CREATED,
                                               content_type='application/json')
        device = executiondb.execution_db.session.query(Device).filter(
            Device.name == 'test').first()
        self.assertIsNotNone(device)
        expected = device.as_json()
        expected['app_name'] = 'TestApp'
        self.assertEqual(response, expected)
Esempio n. 11
0
def create_user():
    from walkoff.serverdb import add_user, User, Role, initialize_default_resources_admin, \
        initialize_default_resources_guest
    from sqlalchemy_utils import database_exists, create_database

    if not database_exists(db.engine.url):
        create_database(db.engine.url)
    db.create_all()

    alembic_cfg = Config(walkoff.config.Config.ALEMBIC_CONFIG,
                         ini_section="walkoff",
                         attributes={'configure_logger': False})

    # This is necessary for a flask database
    connection = db.engine.connect()
    context = MigrationContext.configure(connection)
    script = ScriptDirectory.from_config(alembic_cfg)
    context.stamp(script, "head")

    # Setup admin and guest roles
    initialize_default_resources_admin()
    initialize_default_resources_guest()

    # Setup admin user
    admin_role = Role.query.filter_by(id=1).first()
    admin_user = User.query.filter_by(username="******").first()
    if not admin_user:
        add_user(username='******', password='******', roles=[1])
    elif admin_role not in admin_user.roles:
        admin_user.roles.append(admin_role)

    db.session.commit()

    apps = set(helpers.list_apps(walkoff.config.Config.APPS_PATH)) - set([
        _app.name for _app in
        current_app.running_context.execution_db.session.query(App).all()
    ])
    current_app.logger.debug('Found new apps: {0}'.format(apps))
    for app_name in apps:
        current_app.running_context.execution_db.session.add(
            App(name=app_name, devices=[]))
    db.session.commit()
    current_app.running_context.execution_db.session.commit()
    reschedule_all_workflows()
    current_app.logger.handlers = logging.getLogger('server').handlers
Esempio n. 12
0
 def test_get_device(self):
     app = App('test', devices=self.all_devices)
     self.assertEqual(app.get_device(self.device2.id).as_json(), self.device2.as_json())
Esempio n. 13
0
 def test_add_device_already_exists(self):
     app = App('test', devices=[self.device1, self.device2, self.device3])
     app.add_device(self.device3)
     self.assertSetEqual({device.name
                          for device in app.devices},
                         {'test1', 'test2', 'test3'})
Esempio n. 14
0
 def test_get_device_invalid(self):
     app = App('test', devices=self.all_devices)
     self.assertIsNone(app.get_device('invalid'))
Esempio n. 15
0
 def test_get_device(self):
     app = App('test', devices=self.all_devices)
     self.assertEqual(
         app.get_device(self.device2.id).as_json(), self.device2.as_json())
Esempio n. 16
0
 def test_init_with_devices(self):
     app = App('test', devices=self.all_devices)
     self.assertConstructionIsCorrect(app, 'test',
                                      {'test1', 'test2', 'test3', 'test4'})
Esempio n. 17
0
 def test_init_name_only(self):
     app = App('test')
     self.assertConstructionIsCorrect(app, 'test', set())
Esempio n. 18
0
 def test_get_device_invalid(self):
     app = App('test', devices=self.all_devices)
     self.assertIsNone(app.get_device('invalid'))
Esempio n. 19
0
 def add_test_app(self, devices=None):
     devices = devices if devices is not None else []
     app = App(self.app_name, devices=devices)
     executiondb.execution_db.session.add(app)
     executiondb.execution_db.session.commit()
Esempio n. 20
0
 def test_add_device_already_exists(self):
     app = App('test', devices=[self.device1, self.device2, self.device3])
     app.add_device(self.device3)
     self.assertSetEqual({device.name for device in app.devices}, {'test1', 'test2', 'test3'})