Beispiel #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)
Beispiel #2
0
def create_user():
    from server.context import running_context
    from server.database import add_user, User, ResourcePermission, Role, initialize_resource_roles_from_database

    running_context.db.create_all()
    if not User.query.all():
        admin_role = running_context.Role(
            name='admin',
            description='administrator',
            resources=server.database.default_resources)
        running_context.db.session.add(admin_role)
        admin_user = add_user(username='******', password='******')
        admin_user.roles.append(admin_role)
        running_context.db.session.commit()
    if Role.query.all() or ResourcePermission.query.all():
        initialize_resource_roles_from_database()

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

    app.logger.handlers = logging.getLogger('server').handlers
Beispiel #3
0
    def setUp(self):
        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])

        device_db.session.add(self.db_app)
        device_db.session.commit()
Beispiel #4
0
    def test_update_device_fields(self):
        device1 = Device('test', [], [], 'type')
        device2 = Device('test2', [], [], 'type')
        app = App(name=self.test_app_name, devices=[device1, device2])
        device_db.session.add(app)
        device_db.session.commit()

        fields_json = [{
            'name': 'test_name',
            'type': 'integer',
            'encrypted': False
        }, {
            'name': 'test2',
            'type': 'string',
            'encrypted': False
        }]
        core.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
        }
        self.post_with_status_check('/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})
Beispiel #5
0
    def test_create_device(self):
        fields_json = [{
            'name': 'test_name',
            'type': 'integer',
            'encrypted': False
        }, {
            'name': 'test2',
            'type': 'string',
            'encrypted': False
        }]
        core.config.config.app_apis = {
            self.test_app_name: {
                'devices': {
                    'test_type': {
                        'fields': fields_json
                    }
                }
            }
        }
        app = App(name=self.test_app_name)
        device_db.session.add(app)
        device_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.put_with_status_check('/api/devices',
                                              headers=self.headers,
                                              data=json.dumps(device_json),
                                              status_code=OBJECT_CREATED,
                                              content_type='application/json')
        device = device_db.session.query(Device).filter(
            Device.name == 'test').first()
        self.assertIsNotNone(device)
        expected = device.as_json()
        expected['app_name'] = 'TestApp'
        self.assertEqual(response, expected)
Beispiel #6
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])
     device_db.session.add(app)
     device_db.session.commit()
     response = self.get_with_status_check('/api/devices',
                                           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)
Beispiel #7
0
    def test_update_device_invalid_fields(self):
        device1 = Device('test', [], [], 'type')
        device2 = Device('test2', [], [], 'type')
        app = App(name=self.test_app_name, devices=[device1, device2])
        device_db.session.add(app)
        device_db.session.commit()

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

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

        data = {
            'id': device1.id,
            'name': 'renamed',
            'app_name': self.test_app_name,
            'type': 'test_type',
            'fields': fields_json
        }
        self.post_with_status_check('/api/devices',
                                    headers=self.headers,
                                    data=json.dumps(data),
                                    status_code=INVALID_INPUT_ERROR,
                                    content_type='application/json')
Beispiel #8
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'})
Beispiel #9
0
 def test_get_device_invalid(self):
     app = App('test', devices=self.all_devices)
     self.assertIsNone(app.get_device('invalid'))
Beispiel #10
0
 def test_get_device(self):
     app = App('test', devices=self.all_devices)
     self.assertEqual(
         app.get_device('test2').as_json(), self.device2.as_json())
Beispiel #11
0
 def test_init_with_devices(self):
     app = App('test', devices=self.all_devices)
     self.assertConstructionIsCorrect(app, 'test',
                                      {'test1', 'test2', 'test3', 'test4'})
Beispiel #12
0
 def test_init_name_only(self):
     app = App('test')
     self.assertConstructionIsCorrect(app, 'test', set())
 def add_test_app(self, devices=None):
     devices = devices if devices is not None else []
     app = App(self.app_name, devices=devices)
     device_db.session.add(app)
     device_db.session.commit()