def test_get_external_clients(self): store = self.store admin_user_id = self.admin_user_id store.add_external_client(admin_user_id, ExternalClient(name="client1")) store.add_external_client(admin_user_id, ExternalClient(name="client2")) external_clients = store.get_external_clients() self.assertItemsEqual( [external_client.name for external_client in external_clients], ["client1", "client2"]) self.assertNotEqual(external_clients[0].access_key_hash, external_clients[1].access_key_hash)
def test_auth_external_client(self): store = self.store admin_user_id = self.admin_user_id external_client_id1, access_key1 = store.add_external_client( admin_user_id, ExternalClient(name="client1")) external_client_id2, access_key2 = store.add_external_client( admin_user_id, ExternalClient(name="client2")) external_client_id = store.auth_external_client(access_key1.key) self.assertEqual(external_client_id, external_client_id1) external_client_id = store.auth_external_client(access_key2.key) self.assertEqual(external_client_id, external_client_id2) self.assertIsNone(store.auth_external_client("test"))
def test_update_external_client(self): store = self.store admin_user_id = self.admin_user_id external_client_id, _ = store.add_external_client( admin_user_id, ExternalClient(name="foo")) now = datetime.now() expiration_date = add_seconds(now, 60) store.update_external_client( admin_user_id, external_client_id, { "name": "bar", "email": "*****@*****.**", "expiration_date": expiration_date }) external_client = store.get_external_client(external_client_id) self.assertEqual(external_client.name, "bar") self.assertEqual(external_client.email, "*****@*****.**") self.assertEqual(external_client.expiration_date, expiration_date) self.assertTrue(external_client.access_key_valid) store.update_external_client(admin_user_id, external_client_id, {"expiration_date": now}) external_client = store.get_external_client(external_client_id) self.assertFalse(external_client.access_key_valid)
def test_add_external_client(self): store = self.store external_client_id, access_key = store.add_external_client( self.admin_user_id, ExternalClient(name="client", email="*****@*****.**")) self.assertEqual(external_client_id, 1) self.assertTrue(access_key.key) self.assertEqual(access_key.key_hash, store.get_access_key_hash(access_key.key)) external_client = store.get_external_client(external_client_id) self.assertEqual(external_client.name, "client") self.assertEqual(external_client.email, "*****@*****.**") self.assertEqual(external_client.access_key_hash, access_key.key_hash) self.assertIsNotNone(external_client.create_date) self.assertEqual(external_client.last_modified, external_client.create_date) self.assertTrue(external_client.access_key_valid)
def test_reset_external_client_access_key(self): store = self.store admin_user_id = self.admin_user_id external_client_id, access_key = store.add_external_client( admin_user_id, ExternalClient(name="client", expiration_date=add_seconds(datetime.now(), 60))) new_access_key = store.reset_external_client_access_key( admin_user_id, external_client_id) self.assertTrue(new_access_key.key) self.assertNotEqual(new_access_key.key, access_key.key) self.assertEqual(new_access_key.key_hash, store.get_access_key_hash(new_access_key.key)) external_client = store.get_external_client(external_client_id) self.assertEqual(external_client.access_key_hash, new_access_key.key_hash) self.assertIsNone(external_client.expiration_date) self.assertTrue(external_client.access_key_valid)
def test_assign_external_client_to_region(self): store = self.store admin_user_id = self.admin_user_id external_client_id, _ = store.add_external_client( admin_user_id, ExternalClient(name="client")) region_id1 = self.add_region("region1") region_id2 = self.add_region("region2") def get_region_ids(): external_client = store.get_external_client(external_client_id) return [region.region_id for region in external_client.regions] store.assign_external_client_to_region(admin_user_id, external_client_id, region_id1) self.assertItemsEqual(get_region_ids(), [region_id1]) # Assigning again should fail. with self.assertRaises(IntegrityError): store.assign_external_client_to_region(admin_user_id, external_client_id, region_id1) # Assign to another region. store.assign_external_client_to_region(admin_user_id, external_client_id, region_id2) self.assertItemsEqual(get_region_ids(), [region_id1, region_id2]) store.remove_external_client_from_region(admin_user_id, external_client_id, region_id1) self.assertItemsEqual(get_region_ids(), [region_id2]) # Removing again is a no-op. store.remove_external_client_from_region(admin_user_id, external_client_id, region_id1)
def test_get_bed_counts_for_external_client(self): store = self.store admin_user_id = self.admin_user_id external_client_id, _ = store.add_external_client( admin_user_id, ExternalClient(name="client")) region_id1 = self.add_region("region1") region_id2 = self.add_region("region2") now = datetime.now() self.add_icu_with_values(region_id1, "icu1", now, [1, 2]) self.add_icu_with_values(region_id1, "icu2", now, [4, 3]) self.add_icu_with_values(region_id2, "icu3", now, [5]) def get_values(latest=True, max_date=None): return key_by_name_and_create_date( store.get_bed_counts_for_external_client(external_client_id, latest=latest, max_date=max_date)) store.assign_external_client_to_region(admin_user_id, external_client_id, region_id1) now_plus_1 = add_seconds(now, 1) self.assertDictEqual(get_values(latest=True), { ("icu1", now_plus_1): 2, ("icu2", now_plus_1): 3 }) self.assertDictEqual( get_values(latest=False), { ("icu1", now): 1, ("icu1", now_plus_1): 2, ("icu2", now): 4, ("icu2", now_plus_1): 3 }) self.assertDictEqual(get_values(max_date=now_plus_1), { ("icu1", now): 1, ("icu2", now): 4, }) self.assertDictEqual(get_values(latest=False, max_date=now_plus_1), { ("icu1", now): 1, ("icu2", now): 4, }) store.assign_external_client_to_region(admin_user_id, external_client_id, region_id2) self.assertDictEqual(get_values(latest=True), { ("icu1", now_plus_1): 2, ("icu2", now_plus_1): 3, ("icu3", now): 5 }) self.assertDictEqual( get_values(latest=False), { ("icu1", now): 1, ("icu1", now_plus_1): 2, ("icu2", now): 4, ("icu2", now_plus_1): 3, ("icu3", now): 5 })