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)
def test_read_device(self): device1 = Device('test', [], [], 'type') device2 = Device('test2', [], [], 'type') device_db.session.add(device2) device_db.session.add(device1) device_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)
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'])
class TestAppDatabase(unittest.TestCase): 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 ] 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('test2').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'})