Ejemplo n.º 1
0
 def test_get_encrypted_field(self):
     fields = [
         EncryptedDeviceField('test_name', 'integer', 123),
         EncryptedDeviceField('test2', 'string', 'something')
     ]
     device = Device('test', [], fields, 'type')
     self.assertEqual(device.get_encrypted_field('test_name'), 123)
Ejemplo n.º 2
0
 def test_update_from_json_with_encrypted_fields(self):
     plaintext_fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     encrypted_fields = [
         EncryptedDeviceField('test3', 'boolean', True),
         EncryptedDeviceField('test4', 'string', 'something else')
     ]
     new_encrypted_fields = [
         EncryptedDeviceField('new_test3', 'boolean', True),
         EncryptedDeviceField('new_test4', 'string', 'something else')
     ]
     encrypted_field_json1 = new_encrypted_fields[0].as_json()
     encrypted_field_json2 = new_encrypted_fields[1].as_json()
     encrypted_field_json1['value'] = True
     encrypted_field_json2['value'] = 'something_else'
     device = Device('test',
                     plaintext_fields,
                     encrypted_fields,
                     'type',
                     description='desc')
     device.update_from_json(
         {'fields': [encrypted_field_json1, encrypted_field_json2]})
     self.assertEqual(device.name, 'test')
     self.assertEqual(device.type, 'type')
     self.assertEqual(device.description, 'desc')
     self.assertSetEqual({field.name
                          for field in device.plaintext_fields}, set())
     self.assertSetEqual({field.name
                          for field in device.encrypted_fields},
                         {'new_test3', 'new_test4'})
Ejemplo n.º 3
0
    def setUp(self):
        self.app_name = 'TestApp'

        self.device1 = Device('test1', [], [], 'type1')
        self.device2 = Device('test2', [], [], 'type1')
        self.device3 = Device('test3', [], [], 'type2')
        self.device4 = Device('test4', [], [], 'type2')
Ejemplo n.º 4
0
 def test_update_from_json_with_plaintext_fields(self):
     plaintext_fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     encrypted_fields = [
         EncryptedDeviceField('test3', 'boolean', True),
         EncryptedDeviceField('test4', 'string', 'something else')
     ]
     new_plaintext_fields = [
         DeviceField('new_test_name', 'integer', 451),
         DeviceField('new_test2', 'string', 'changed')
     ]
     device = Device('test',
                     plaintext_fields,
                     encrypted_fields,
                     'type',
                     description='desc')
     device.update_from_json(
         {'fields': [field.as_json() for field in new_plaintext_fields]})
     self.assertEqual(device.name, 'test')
     self.assertEqual(device.type, 'type')
     self.assertEqual(device.description, 'desc')
     self.assertSetEqual({field.name
                          for field in device.plaintext_fields},
                         {'new_test_name', 'new_test2'})
     self.assertSetEqual({field.name
                          for field in device.encrypted_fields}, set())
Ejemplo n.º 5
0
 def test_get_encrypted_field_dne(self):
     fields = [
         EncryptedDeviceField('test_name', 'integer', 123),
         EncryptedDeviceField('test2', 'string', 'something')
     ]
     device = Device('test', [], fields, 'type')
     with self.assertRaises(UnknownDeviceField):
         device.get_encrypted_field('invalid')
Ejemplo n.º 6
0
 def test_update_from_json_type_only(self):
     plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
     encrypted_fields = [EncryptedDeviceField('test3', 'boolean', True),
                         EncryptedDeviceField('test4', 'string', 'something else')]
     device = Device('test', plaintext_fields, encrypted_fields, 'type', description='desc')
     device.update_from_json({'type': 'new_type'}, True)
     self.assertConstructionIsCorrect(device, 'test', plaintext_fields, encrypted_fields, 'new_type',
                                      description='desc')
Ejemplo n.º 7
0
 def test_update_device_app_dne(self):
     device1 = Device('test', [], [], 'type')
     device2 = Device('test2', [], [], 'type')
     self.app.running_context.execution_db.session.add(device2)
     self.app.running_context.execution_db.session.add(device1)
     self.app.running_context.execution_db.session.commit()
     data = {'id': device1.id, 'name': 'renamed', 'app_name': 'Invalid'}
     self.put_with_status_check('/api/devices', headers=self.headers, data=json.dumps(data),
                                status_code=INVALID_INPUT_ERROR, content_type='application/json')
Ejemplo n.º 8
0
 def test_get_plaintext_fields_plaintext_only(self):
     fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     device = Device('test', fields, [], 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {
         'test_name': 123,
         'test2': 'something'
     })
Ejemplo n.º 9
0
 def test_read_device(self):
     device1 = Device('test', [], [], 'type')
     device2 = Device('test2', [], [], 'type')
     self.app.running_context.execution_db.session.add(device2)
     self.app.running_context.execution_db.session.add(device1)
     self.app.running_context.execution_db.session.commit()
     response = self.get_with_status_check('/api/devices/{}'.format(device1.id), headers=self.headers,
                                           status_code=SUCCESS)
     expected_device1 = device1.as_json()
     expected_device1['app_name'] = ''
     self.assertEqual(response, expected_device1)
Ejemplo n.º 10
0
 def test_delete_device(self):
     device1 = Device('test', [], [], 'type')
     device2 = Device('test2', [], [], 'type')
     self.app.running_context.execution_db.session.add(device2)
     self.app.running_context.execution_db.session.add(device1)
     self.app.running_context.execution_db.session.commit()
     device1_id = device1.id
     self.delete_with_status_check('/api/devices/{}'.format(device1_id), headers=self.headers,
                                   status_code=NO_CONTENT)
     self.assertIsNone(
         self.app.running_context.execution_db.session.query(Device).filter(Device.id == device1_id).first())
Ejemplo n.º 11
0
 def setUp(self):
     self.device1 = Device('test1', [], [], 'type1')
     self.device2 = Device('test2', [], [], 'type1')
     self.device3 = Device('test3', [], [], 'type2')
     self.device4 = Device('test4', [], [], 'type2')
     self.all_devices = [
         self.device1, self.device2, self.device3, self.device4
     ]
     for device in self.all_devices:
         self.execution_db.session.add(device)
     self.execution_db.session.commit()
Ejemplo n.º 12
0
 def test_update_device_device_dne(self):
     device1 = Device('test', [], [], 'type')
     device2 = Device('test2', [], [], 'type')
     executiondb.execution_db.session.add(device2)
     executiondb.execution_db.session.add(device1)
     executiondb.execution_db.session.commit()
     data = {'id': 404, 'name': 'renamed'}
     self.put_with_status_check('/api/devices',
                                headers=self.headers,
                                data=json.dumps(data),
                                status_code=OBJECT_DNE_ERROR,
                                content_type='application/json')
Ejemplo n.º 13
0
 def test_as_json(self):
     plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
     encrypted_fields = [EncryptedDeviceField('test3', 'boolean', True),
                         EncryptedDeviceField('test4', 'string', 'something else')]
     device = Device('test', plaintext_fields, encrypted_fields, 'type', description='desc')
     device_json = device.as_json()
     self.assertEqual(device_json['name'], 'test')
     self.assertEqual(device_json['type'], 'type')
     self.assertEqual(device_json['description'], 'desc')
     plaintext_fields.extend(encrypted_fields)
     for field in plaintext_fields:
         self.assertIn(field.as_json(), device_json['fields'])
Ejemplo n.º 14
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})
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
 def test_update_from_json_with_plaintext_fields(self):
     plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
     encrypted_fields = [EncryptedDeviceField('test3', 'boolean', True),
                         EncryptedDeviceField('test4', 'string', 'something else')]
     new_plaintext_fields = [DeviceField('new_test_name', 'integer', 451),
                             DeviceField('new_test2', 'string', 'changed')]
     device = Device('test', plaintext_fields, encrypted_fields, 'type', description='desc')
     device.update_from_json({'fields': [field.as_json() for field in new_plaintext_fields]}, True)
     self.assertEqual(device.name, 'test')
     self.assertEqual(device.type, 'type')
     self.assertEqual(device.description, 'desc')
     self.assertSetEqual({field.name for field in device.plaintext_fields}, {'new_test_name', 'new_test2'})
     self.assertSetEqual({field.name for field in device.encrypted_fields}, set())
Ejemplo n.º 17
0
    def setUp(self):
        from flask import current_app
        self.app_name = 'TestApp'
        self.app = current_app.test_client(self)
        self.app.testing = True

        self.context = current_app.test_request_context()
        self.context.push()

        self.device1 = Device('test1', [], [], 'type1')
        self.device2 = Device('test2', [], [], 'type1')
        self.device3 = Device('test3', [], [], 'type2')
        self.device4 = Device('test4', [], [], 'type2')
Ejemplo n.º 18
0
    def setUp(self):
        import walkoff.server.flaskserver
        self.app_name = 'TestApp'
        self.app = walkoff.server.flaskserver.app.test_client(self)
        self.app.testing = True

        self.context = walkoff.server.flaskserver.app.test_request_context()
        self.context.push()

        self.device1 = Device('test1', [], [], 'type1')
        self.device2 = Device('test2', [], [], 'type1')
        self.device3 = Device('test3', [], [], 'type2')
        self.device4 = Device('test4', [], [], 'type2')
Ejemplo n.º 19
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')
Ejemplo n.º 20
0
 def test_get_plaintext_fields_with_encrypted(self):
     encrypted_fields = [
         EncryptedDeviceField('test_name', 'integer', 123),
         EncryptedDeviceField('test2', 'string', 'something')
     ]
     plaintext_fields = [
         DeviceField('test3', 'boolean', True),
         DeviceField('test4', 'string', 'something else')
     ]
     device = Device('test', plaintext_fields, encrypted_fields, 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {
         'test3': True,
         'test4': 'something else'
     })
Ejemplo n.º 21
0
 def test_create_device_device_already_exists(self):
     device1 = Device('test', [], [], 'type', description='description')
     self.app.running_context.execution_db.session.add(device1)
     self.app.running_context.execution_db.session.commit()
     device_json = {'app_name': 'test', 'name': 'test', 'type': 'some_type', 'fields': []}
     self.post_with_status_check('/api/devices', headers=self.headers, data=json.dumps(device_json),
                                 status_code=OBJECT_EXISTS_ERROR, content_type='application/json')
Ejemplo n.º 22
0
 def test_init_with_plaintext(self):
     fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     device = Device('test', fields, [], 'type')
     self.assertConstructionIsCorrect(device, 'test', fields, [], 'type')
Ejemplo n.º 23
0
    def test_from_json(self):
        plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
        encrypted_field1 = EncryptedDeviceField('test3', 'boolean', True)
        encrypted_field2 = EncryptedDeviceField('test4', 'string', 'something else')
        encrypted_fields = [encrypted_field1, encrypted_field2]

        encrypted_field_json1 = encrypted_field1.as_json()
        encrypted_field_json2 = encrypted_field2.as_json()
        encrypted_field_json1_with_value = dict(encrypted_field_json1)
        encrypted_field_json2_with_value = dict(encrypted_field_json2)
        encrypted_field_json1_with_value['value'] = True
        encrypted_field_json2_with_value['value'] = 'something_else'

        fields_json = [field.as_json() for field in plaintext_fields]
        fields_json.extend([encrypted_field_json1_with_value, encrypted_field_json2_with_value])

        device = Device.from_json({'name': 'test', 'fields': fields_json, 'type': 'something', 'description': 'desc'})

        expected_plaintext_fields_as_json = [field.as_json() for field in plaintext_fields]
        expected_encrypted_fields_as_json = [field.as_json() for field in encrypted_fields]

        self.assertEqual(device.name, 'test')
        self.assertEqual(device.type, 'something')
        self.assertEqual(device.description, 'desc')
        for field in device.plaintext_fields:
            self.assertIn(field.as_json(), expected_plaintext_fields_as_json)
        for field in device.encrypted_fields:
            self.assertIn(field.as_json(), expected_encrypted_fields_as_json)
Ejemplo n.º 24
0
 def test_init_with_encrypted(self):
     fields = [
         EncryptedDeviceField('test_name', 'integer', 123),
         EncryptedDeviceField('test2', 'string', 'something')
     ]
     device = Device('test', [], fields, 'type')
     self.assertConstructionIsCorrect(device, 'test', [], fields, 'type')
Ejemplo n.º 25
0
 def test_update_from_json_with_encrypted_fields(self):
     plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
     encrypted_fields = [EncryptedDeviceField('test3', 'boolean', True),
                         EncryptedDeviceField('test4', 'string', 'something else')]
     new_encrypted_fields = [EncryptedDeviceField('new_test3', 'boolean', True),
                             EncryptedDeviceField('new_test4', 'string', 'something else')]
     encrypted_field_json1 = new_encrypted_fields[0].as_json()
     encrypted_field_json2 = new_encrypted_fields[1].as_json()
     encrypted_field_json1['value'] = True
     encrypted_field_json2['value'] = 'something_else'
     device = Device('test', plaintext_fields, encrypted_fields, 'type', description='desc')
     device.update_from_json({'fields': [encrypted_field_json1, encrypted_field_json2]}, True)
     self.assertEqual(device.name, 'test')
     self.assertEqual(device.type, 'type')
     self.assertEqual(device.description, 'desc')
     self.assertSetEqual({field.name for field in device.plaintext_fields}, set())
     self.assertSetEqual({field.name for field in device.encrypted_fields}, {'new_test3', 'new_test4'})
Ejemplo n.º 26
0
class TestAppDatabase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        initialize_test_config()
        cls.execution_db = execution_db_help.setup_dbs()

    @classmethod
    def tearDownClass(cls):
        execution_db_help.tear_down_execution_db()

    def setUp(self):
        self.device1 = Device('test1', [], [], 'type1')
        self.device2 = Device('test2', [], [], 'type1')
        self.device3 = Device('test3', [], [], 'type2')
        self.device4 = Device('test4', [], [], 'type2')
        self.all_devices = [self.device1, self.device2, self.device3, self.device4]
        for device in self.all_devices:
            self.execution_db.session.add(device)
        self.execution_db.session.commit()

    def assertConstructionIsCorrect(self, app, name, devices):
        self.assertEqual(app.name, name)
        self.assertSetEqual({device.name for device in app.devices}, devices)

    def test_init_name_only(self):
        app = App('test')
        self.assertConstructionIsCorrect(app, 'test', set())

    def test_init_with_devices(self):
        app = App('test', devices=self.all_devices)
        self.assertConstructionIsCorrect(app, 'test', {'test1', 'test2', 'test3', 'test4'})

    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())

    def test_get_device_invalid(self):
        app = App('test', devices=self.all_devices)
        self.assertIsNone(app.get_device('invalid'))

    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)

    def test_add_device(self):
        app = App('test', devices=[self.device1, self.device2, self.device3])
        app.add_device(self.device4)
        self.assertSetEqual({device.name for device in app.devices}, {'test1', 'test2', 'test3', 'test4'})

    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'})
Ejemplo n.º 27
0
 def setUp(self):
     self.device1 = Device('test1', [], [], 'type1')
     self.device2 = Device('test2', [], [], 'type1')
     self.device3 = Device('test3', [], [], 'type2')
     self.device4 = Device('test4', [], [], 'type2')
     self.all_devices = [self.device1, self.device2, self.device3, self.device4]
     for device in self.all_devices:
         self.execution_db.session.add(device)
     self.execution_db.session.commit()
Ejemplo n.º 28
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(self.test_app_name, [self.device1, self.device2])

        self.execution_db.session.add(self.db_app)
        self.execution_db.session.commit()
Ejemplo n.º 29
0
    def test_get_device_ids(self):
        device_one = Device('test', [
            DeviceField('valid_one', 'integer', 123),
            DeviceField('valid_two', 'string', '456'),
            DeviceField('invalid', 'integer', 789)
        ], [],
                            'type',
                            description='desc')

        device_two = Device('test', [
            DeviceField('valid_one', 'integer', 123),
            DeviceField('valid_two', 'string', '456')
        ], [],
                            'type',
                            description='desc')

        device_three = Device('test', [
            DeviceField('valid_one', 'string', '456'),
            DeviceField('valid_two', 'integer', 123)
        ], [],
                              'type',
                              description='desc')

        device_four = Device('test', [
            DeviceField('valid_one', 'integer', 123),
            DeviceField('invalid', 'integer', 789)
        ], [],
                             'type',
                             description='desc')

        self.execution_db.session.add_all(
            [device_one, device_two, device_three, device_four])
        self.execution_db.session.commit()

        device_ids = get_device_ids_by_fields({
            'valid_one': 123,
            'valid_two': '456'
        })
        self.assertEqual(len(device_ids), 2)
        self.assertIn(device_one.id, device_ids)
        self.assertIn(device_two.id, device_ids)
Ejemplo n.º 30
0
 def test_as_json(self):
     plaintext_fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     encrypted_fields = [
         EncryptedDeviceField('test3', 'boolean', True),
         EncryptedDeviceField('test4', 'string', 'something else')
     ]
     device = Device('test',
                     plaintext_fields,
                     encrypted_fields,
                     'type',
                     description='desc')
     device_json = device.as_json()
     self.assertEqual(device_json['name'], 'test')
     self.assertEqual(device_json['type'], 'type')
     self.assertEqual(device_json['description'], 'desc')
     plaintext_fields.extend(encrypted_fields)
     for field in plaintext_fields:
         self.assertIn(field.as_json(), device_json['fields'])
Ejemplo n.º 31
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])
        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}}}}

        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})
Ejemplo n.º 32
0
 def test_init_with_both_plaintext_and_encrypted(self):
     plaintext_fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     encrypted_fields = [
         EncryptedDeviceField('test3', 'boolean', True),
         EncryptedDeviceField('test4', 'string', 'something else')
     ]
     device = Device('test', plaintext_fields, encrypted_fields, 'type')
     self.assertConstructionIsCorrect(device, 'test', plaintext_fields,
                                      encrypted_fields, 'type')
Ejemplo n.º 33
0
 def test_update_from_json_type_only(self):
     plaintext_fields = [
         DeviceField('test_name', 'integer', 123),
         DeviceField('test2', 'string', 'something')
     ]
     encrypted_fields = [
         EncryptedDeviceField('test3', 'boolean', True),
         EncryptedDeviceField('test4', 'string', 'something else')
     ]
     device = Device('test',
                     plaintext_fields,
                     encrypted_fields,
                     'type',
                     description='desc')
     device.update_from_json({'type': 'new_type'})
     self.assertConstructionIsCorrect(device,
                                      'test',
                                      plaintext_fields,
                                      encrypted_fields,
                                      'new_type',
                                      description='desc')
Ejemplo n.º 34
0
    def test_construct_fields_from_json(self):
        plaintext_fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
        encrypted_field1 = EncryptedDeviceField('test3', 'boolean', True)
        encrypted_field2 = EncryptedDeviceField('test4', 'string', 'something else')

        encrypted_field_json1 = encrypted_field1.as_json()
        encrypted_field_json2 = encrypted_field2.as_json()
        encrypted_field_json1['value'] = True
        encrypted_field_json2['value'] = 'something_else'
        fields_json = [field.as_json() for field in plaintext_fields]
        fields_json.extend([encrypted_field_json1, encrypted_field_json2])

        plaintext_fields, encrypted_fields = Device._construct_fields_from_json(fields_json)
        self.assertSetEqual({field.name for field in plaintext_fields}, {'test_name', 'test2'})
        self.assertSetEqual({field.name for field in encrypted_fields}, {'test3', 'test4'})
Ejemplo n.º 35
0
    def __func():
        if request.files and 'file' in request.files:
            f = request.files['file']
            add_device_json = json.loads(f.read().decode('utf-8'))
        else:
            add_device_json = request.get_json()
        if current_app.running_context.execution_db.session.query(
                Device).filter(Device.name ==
                               add_device_json['name']).first() is not None:
            current_app.logger.error('Could not create device {0}. '
                                     'Device already exists.'.format(
                                         add_device_json['name']))
            return Problem.from_crud_resource(
                OBJECT_EXISTS_ERROR, 'device', 'create',
                'Device with name {} already exists.'.format(
                    add_device_json['name']))

        fields = {
            field['name']: field['value']
            for field in add_device_json['fields']
        }
        app = add_device_json['app_name']
        device_type = add_device_json['type']
        try:
            device_api = get_app_device_api(app, device_type)
            device_fields_api = device_api['fields']
            validate_device_fields(device_fields_api, fields, device_type, app)
        except (UnknownApp, UnknownDevice, InvalidArgument) as e:
            return __crud_device_error_handler('create', e, app, device_type)
        else:
            fields = add_device_json['fields']
            add_configuration_keys_to_device_json(fields, device_fields_api)
            app = current_app.running_context.execution_db.session.query(
                App).filter(App.name == app).first()
            if app is None:
                current_app.logger.error(
                    'SEVERE: App defined in api does not have corresponding entry in database. '
                    'Cannot add device')
                return Problem.from_crud_resource(
                    INVALID_INPUT_ERROR, 'device', 'create',
                    'App {} does not exist.'.format(
                        add_device_json['app_name']))
            device = Device.from_json(add_device_json)
            app.add_device(device)
            current_app.running_context.execution_db.session.add(device)
            current_app.running_context.execution_db.session.commit()
            device_json = get_device_json_with_app_name(device)
            return device_json, OBJECT_CREATED
Ejemplo n.º 36
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)
Ejemplo n.º 37
0
    def test_from_json(self):
        plaintext_fields = [
            DeviceField('test_name', 'integer', 123),
            DeviceField('test2', 'string', 'something')
        ]
        encrypted_field1 = EncryptedDeviceField('test3', 'boolean', True)
        encrypted_field2 = EncryptedDeviceField('test4', 'string',
                                                'something else')
        encrypted_fields = [encrypted_field1, encrypted_field2]

        encrypted_field_json1 = encrypted_field1.as_json()
        encrypted_field_json2 = encrypted_field2.as_json()
        encrypted_field_json1_with_value = dict(encrypted_field_json1)
        encrypted_field_json2_with_value = dict(encrypted_field_json2)
        encrypted_field_json1_with_value['value'] = True
        encrypted_field_json2_with_value['value'] = 'something_else'

        fields_json = [field.as_json() for field in plaintext_fields]
        fields_json.extend([
            encrypted_field_json1_with_value, encrypted_field_json2_with_value
        ])

        device = Device.from_json({
            'name': 'test',
            'fields': fields_json,
            'type': 'something',
            'description': 'desc'
        })

        expected_plaintext_fields_as_json = [
            field.as_json() for field in plaintext_fields
        ]
        expected_encrypted_fields_as_json = [
            field.as_json() for field in encrypted_fields
        ]

        self.assertEqual(device.name, 'test')
        self.assertEqual(device.type, 'something')
        self.assertEqual(device.description, 'desc')
        for field in device.plaintext_fields:
            self.assertIn(field.as_json(), expected_plaintext_fields_as_json)
        for field in device.encrypted_fields:
            self.assertIn(field.as_json(), expected_encrypted_fields_as_json)
Ejemplo n.º 38
0
    def __func():
        if request.files and 'file' in request.files:
            f = request.files['file']
            add_device_json = json.loads(f.read().decode('utf-8'))
        else:
            add_device_json = request.get_json()
        if current_app.running_context.execution_db.session.query(Device).filter(
                Device.name == add_device_json['name']).first() is not None:
            current_app.logger.error('Could not create device {0}. '
                                     'Device already exists.'.format(add_device_json['name']))
            return Problem.from_crud_resource(
                OBJECT_EXISTS_ERROR,
                'device',
                'create',
                'Device with name {} already exists.'.format(add_device_json['name']))

        fields = {field['name']: field['value'] for field in add_device_json['fields']}
        app = add_device_json['app_name']
        device_type = add_device_json['type']
        try:
            device_api = get_app_device_api(app, device_type)
            device_fields_api = device_api['fields']
            validate_device_fields(device_fields_api, fields, device_type, app)
        except (UnknownApp, UnknownDevice, InvalidArgument) as e:
            return __crud_device_error_handler('create', e, app, device_type)
        else:
            fields = add_device_json['fields']
            add_configuration_keys_to_device_json(fields, device_fields_api)
            app = current_app.running_context.execution_db.session.query(App).filter(App.name == app).first()
            if app is None:
                current_app.logger.error('SEVERE: App defined in api does not have corresponding entry in database. '
                                         'Cannot add device')
                return Problem.from_crud_resource(
                    INVALID_INPUT_ERROR,
                    'device',
                    'create',
                    'App {} does not exist.'.format(add_device_json['app_name']))
            device = Device.from_json(add_device_json)
            app.add_device(device)
            current_app.running_context.execution_db.session.add(device)
            current_app.running_context.execution_db.session.commit()
            device_json = get_device_json_with_app_name(device)
            return device_json, OBJECT_CREATED
Ejemplo n.º 39
0
    def test_construct_fields_from_json(self):
        plaintext_fields = [
            DeviceField('test_name', 'integer', 123),
            DeviceField('test2', 'string', 'something')
        ]
        encrypted_field1 = EncryptedDeviceField('test3', 'boolean', True)
        encrypted_field2 = EncryptedDeviceField('test4', 'string',
                                                'something else')

        encrypted_field_json1 = encrypted_field1.as_json()
        encrypted_field_json2 = encrypted_field2.as_json()
        encrypted_field_json1['value'] = True
        encrypted_field_json2['value'] = 'something_else'
        fields_json = [field.as_json() for field in plaintext_fields]
        fields_json.extend([encrypted_field_json1, encrypted_field_json2])

        plaintext_fields, encrypted_fields = Device._construct_fields_from_json(
            fields_json)
        self.assertSetEqual({field.name
                             for field in plaintext_fields},
                            {'test_name', 'test2'})
        self.assertSetEqual({field.name
                             for field in encrypted_fields},
                            {'test3', 'test4'})
Ejemplo n.º 40
0
 def test_get_plaintext_fields_with_encrypted(self):
     encrypted_fields = [EncryptedDeviceField('test_name', 'integer', 123),
                         EncryptedDeviceField('test2', 'string', 'something')]
     plaintext_fields = [DeviceField('test3', 'boolean', True), DeviceField('test4', 'string', 'something else')]
     device = Device('test', plaintext_fields, encrypted_fields, 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {'test3': True, 'test4': 'something else'})
Ejemplo n.º 41
0
class TestAppBase(TestCase):

    @classmethod
    def setUpClass(cls):
        initialize_test_config()
        cls.execution_db = execution_db_help.setup_dbs()
        cls.cache = MockRedisCacheAdapter()

    @classmethod
    def tearDownClass(cls):
        execution_db_help.tear_down_execution_db()

    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(self.test_app_name, [self.device1, self.device2])

        self.execution_db.session.add(self.db_app)
        self.execution_db.session.commit()

    def tearDown(self):
        self.execution_db.session.rollback()
        for device in self.execution_db.session.query(Device).all():
            self.execution_db.session.delete(device)
        for field in self.execution_db.session.query(DeviceField).all():
            self.execution_db.session.delete(field)
        for field in self.execution_db.session.query(EncryptedDeviceField).all():
            self.execution_db.session.delete(field)
        app = self.execution_db.session.query(App).filter(App.name == self.test_app_name).first()
        if app is not None:
            self.execution_db.session.delete(app)
        self.execution_db.session.commit()
        self.cache.clear()

    def test_app_is_tagged(self):
        self.assertTrue(getattr(AppBase, '_is_walkoff_app', False))

    def test_init(self):
        app = AppBase(self.test_app_name, self.device1.id, {})
        self.assertEqual(app.app, self.db_app)
        self.assertEqual(app.device, self.device1)
        self.assertDictEqual(app.device_fields, {})
        self.assertEqual(app.device_type, 'type1')
        self.assertEqual(app.device_id, self.device1.id)

    def test_init_with_fields(self):
        app = AppBase(self.test_app_name, self.device2.id, {})
        self.assertEqual(app.app, self.db_app)
        self.assertEqual(app.device, self.device2)
        self.assertDictEqual(app.device_fields, self.device2.get_plaintext_fields())
        self.assertEqual(app.device_type, 'type2')
        self.assertEqual(app.device_id, self.device2.id)

    def test_init_with_invalid_app(self):
        app = AppBase('Invalid', self.device2.id, {})
        self.assertIsNone(app.app)
        self.assertIsNone(app.device)
        self.assertDictEqual(app.device_fields, {})
        self.assertEqual(app.device_type, None)
        self.assertEqual(app.device_id, self.device2.id)

    def test_init_with_invalid_device(self):
        app = AppBase(self.test_app_name, 'invalid', {})
        self.assertEqual(app.app, self.db_app)
        self.assertIsNone(app.device)
        self.assertDictEqual(app.device_fields, {})
        self.assertEqual(app.device_type, None)
        self.assertEqual(app.device_id, 'invalid')

    def test_get_all_devices(self):
        app = AppBase(self.test_app_name, self.device2.id, {})
        devices = app.get_all_devices()
        self.assertIn(self.device1, devices)
        self.assertIn(self.device2, devices)

    def test_get_all_devices_invalid_app(self):
        app = AppBase('Invalid', self.device2.id, {})
        self.assertListEqual(app.get_all_devices(), [])

    def test_setattr_syncs_to_cache(self):
        workflow_id = uuid4()
        context = {'workflow_execution_id': workflow_id}
        app = AppBase('Something', self.device2.id, context)
        app._cache = self.cache
        app.foo = 42
        app.bar = 23
        self.assertSetEqual(
            set(self.cache.scan()),
            {app._format_cache_key('foo'), app._format_cache_key('bar')}
        )
        for field, expected in (('foo', 42), ('bar', 23)):
            self.assertEqual(dill.loads(self.cache.get(app._format_cache_key(field))), expected)

    def test_getattr_gets_from_cache(self):
        workflow_id = uuid4()
        context = {'workflow_execution_id': workflow_id}
        app = AppBase('Something', self.device2.id, context)
        app._cache = self.cache
        app.foo = 42
        app.bar = 23
        self.cache.set(app._format_cache_key('foo'), dill.dumps('a'))
        self.cache.set(app._format_cache_key('bar'), dill.dumps('b'))
        self.assertEqual(app.foo, 'a')
        self.assertEqual(app.bar, 'b')
        with self.assertRaises(AttributeError):
            y = app.baz

    def test_reset_context(self):
        workflow_id1 = uuid4()
        context1 = {'workflow_execution_id': workflow_id1}
        app1 = AppBase('Something', self.device2.id, context1)
        app1._cache = self.cache
        workflow_id2 = uuid4()
        context2 = {'workflow_execution_id': workflow_id2}
        app2 = AppBase('Something', self.device2.id, context2)
        app2._cache = self.cache
        app1.foo = 42
        app1.bar = 'abc'
        app2.foo = 43
        app2.bar = 'def'
        app1._reset_context(context2)
        self.assertEqual(app1.foo, 43)
        self.assertEqual(app1.bar, 'def')

    def test_from_cache(self):
        class Foo(AppBase):
            def __init__(self, app, device, context):
                super(Foo, self).__init__(app, device, context)
                self.a = 4
                self.b = 'a'

        workflow_id = uuid4()
        context = {'workflow_execution_id': workflow_id}
        app = Foo('Something', self.device2.id, context)
        app.a = 5
        app.b = 'b'

        reconstructed = Foo.from_cache('Something', self.device2.id, context)
        self.assertIsInstance(reconstructed, Foo)
        self.assertEqual(reconstructed.a, 5)
        self.assertEqual(reconstructed.b, 'b')
Ejemplo n.º 42
0
 def test_get_plaintext_fields_no_fields(self):
     device = Device('test', [], [], 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {})
Ejemplo n.º 43
0
 def test_get_plaintext_fields_plaintext_only(self):
     fields = [DeviceField('test_name', 'integer', 123), DeviceField('test2', 'string', 'something')]
     device = Device('test', fields, [], 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {'test_name': 123, 'test2': 'something'})
Ejemplo n.º 44
0
 def test_get_plaintext_fields_no_fields(self):
     device = Device('test', [], [], 'type')
     self.assertDictEqual(device.get_plaintext_fields(), {})
Ejemplo n.º 45
0
 def test_get_encrypted_field(self):
     fields = [EncryptedDeviceField('test_name', 'integer', 123),
               EncryptedDeviceField('test2', 'string', 'something')]
     device = Device('test', [], fields, 'type')
     self.assertEqual(device.get_encrypted_field('test_name'), 123)
Ejemplo n.º 46
0
 def test_get_encrypted_field_dne(self):
     fields = [EncryptedDeviceField('test_name', 'integer', 123),
               EncryptedDeviceField('test2', 'string', 'something')]
     device = Device('test', [], fields, 'type')
     with self.assertRaises(UnknownDeviceField):
         device.get_encrypted_field('invalid')
Ejemplo n.º 47
0
 def test_init_with_description(self):
     device = Device('test', [], [], 'type', description='desc')
     self.assertConstructionIsCorrect(device,
                                      'test', [], [],
                                      'type',
                                      description='desc')