def test_add_geofence_success(self): policy = AuthPolicy() latitude = MagicMock(spec=int) longitude = MagicMock(spec=int) radius = MagicMock(spec=int) policy.add_geofence(latitude, longitude, radius) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) factor = retrieved['factors'][1] if retrieved['factors'][1]['factor'] == 'geofence' else retrieved['factors'][0] self.assertEqual(factor['factor'], 'geofence') self.assertEqual(len(factor['attributes']['locations']), 1) location = factor['attributes']['locations'][0] self.assertEqual(location['latitude'], float(latitude)) self.assertEqual(location['longitude'], float(longitude)) self.assertEqual(location['radius'], float(radius)) # Add a second geofence latitude2 = MagicMock(spec=int) longitude2 = MagicMock(spec=int) radius2 = MagicMock(spec=int) policy.add_geofence(latitude2, longitude2, radius2) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) factor = retrieved['factors'][1] if retrieved['factors'][1]['factor'] == 'geofence' else retrieved['factors'][0] self.assertEqual(factor['factor'], 'geofence') self.assertEqual(len(factor['attributes']['locations']), 2) location = factor['attributes']['locations'][1] self.assertEqual(location['latitude'], float(latitude2)) self.assertEqual(location['longitude'], float(longitude2)) self.assertEqual(location['radius'], float(radius2))
def test_add_geofence_success(self): policy = AuthPolicy() latitude = MagicMock(spec=int) longitude = MagicMock(spec=int) radius = MagicMock(spec=int) policy.add_geofence(latitude, longitude, radius) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) factor = retrieved['factors'][1] if retrieved['factors'][1][ 'factor'] == 'geofence' else retrieved['factors'][0] self.assertEqual(factor['factor'], 'geofence') self.assertEqual(len(factor['attributes']['locations']), 1) location = factor['attributes']['locations'][0] self.assertEqual(location['latitude'], float(latitude)) self.assertEqual(location['longitude'], float(longitude)) self.assertEqual(location['radius'], float(radius)) # Add a second geofence latitude2 = MagicMock(spec=int) longitude2 = MagicMock(spec=int) radius2 = MagicMock(spec=int) policy.add_geofence(latitude2, longitude2, radius2) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) factor = retrieved['factors'][1] if retrieved['factors'][1][ 'factor'] == 'geofence' else retrieved['factors'][0] self.assertEqual(factor['factor'], 'geofence') self.assertEqual(len(factor['attributes']['locations']), 2) location = factor['attributes']['locations'][1] self.assertEqual(location['latitude'], float(latitude2)) self.assertEqual(location['longitude'], float(longitude2)) self.assertEqual(location['radius'], float(radius2))
def test_jailbreak_protection_default(self): policy = AuthPolicy() self.assertEqual(len(policy.get_policy()['factors']), 1) self.assertEqual(policy.get_policy()['factors'][0]['factor'], 'device integrity') self.assertEqual( policy.get_policy()['factors'][0]['attributes']['factor enabled'], 0)
def test_setting_specific_requirement(self): knowledge = 0 inherence = 1 possession = True policy = AuthPolicy(knowledge=knowledge, inherence=inherence, possession=possession) self.assertEqual(policy.get_policy()['minimum_requirements'][0]['knowledge'], int(knowledge)) self.assertEqual(policy.get_policy()['minimum_requirements'][0]['inherence'], int(inherence)) self.assertEqual(policy.get_policy()['minimum_requirements'][0]['possession'], int(possession))
def test_jailbreak_protection_true(self): policy = AuthPolicy(jailbreak_protection=True) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 1) factor = retrieved['factors'][0] self.assertEqual(factor['factor'], 'device integrity') self.assertEqual(factor['attributes']['factor enabled'], 1)
def test_remove_geofence(self, name): policy = AuthPolicy() retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 1) self.assertEqual(policy.geofences, []) policy.add_geofence(MagicMock(spec=int), MagicMock(spec=int), MagicMock(spec=int), name) self.assertEqual(len(policy.geofences), 1) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) self.assertEqual(len(retrieved['factors'][1]['attributes']['locations']), 1) policy.remove_geofence(name) self.assertEqual(policy.geofences, []) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors'][1]['attributes']['locations']), 0)
def test_setting_specific_requirement(self): knowledge = 0 inherence = 1 possession = True policy = AuthPolicy(knowledge=knowledge, inherence=inherence, possession=possession) self.assertEqual( policy.get_policy()['minimum_requirements'][0]['knowledge'], int(knowledge)) self.assertEqual( policy.get_policy()['minimum_requirements'][0]['inherence'], int(inherence)) self.assertEqual( policy.get_policy()['minimum_requirements'][0]['possession'], int(possession))
def test_require_jailbreak_protection_new(self, status): policy = AuthPolicy() policy._policy['factors'] = [] policy.require_jailbreak_protection(status) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 1) self.assertEqual(retrieved['factors'][0]['attributes']['factor enabled'], 1 if status else 0)
def test_require_jailbreak_protection_existing(self, status): policy = AuthPolicy() policy.require_jailbreak_protection(status) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 1) self.assertEqual( retrieved['factors'][0]['attributes']['factor enabled'], 1 if status else 0)
def test_remove_geofence(self, name): policy = AuthPolicy() retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 1) self.assertEqual(policy.geofences, []) policy.add_geofence(MagicMock(spec=int), MagicMock(spec=int), MagicMock(spec=int), name) self.assertEqual(len(policy.geofences), 1) retrieved = policy.get_policy() self.assertEqual(len(retrieved['factors']), 2) self.assertEqual( len(retrieved['factors'][1]['attributes']['locations']), 1) policy.remove_geofence(name) self.assertEqual(policy.geofences, []) retrieved = policy.get_policy() self.assertEqual( len(retrieved['factors'][1]['attributes']['locations']), 0)
def test_eq_match(self): policy = AuthPolicy() policy.add_geofence(1, 2, 3, '123') policy2 = AuthPolicy() policy2.set_policy(policy.get_policy()) self.assertEqual(policy, policy2)
def test_invalid_policy(self): policy = AuthPolicy() policy._policy['factors'].append(uuid4()) with self.assertRaises(InvalidParameters): policy.get_policy()
def test_jailbreak_protection_default(self): policy = AuthPolicy() self.assertEqual(len(policy.get_policy()['factors']), 1) self.assertEqual(policy.get_policy()['factors'][0]['factor'], 'device integrity') self.assertEqual(policy.get_policy()['factors'][0]['attributes']['factor enabled'], 0)
def test_eq_match(self): policy = AuthPolicy() policy.add_geofence(1, 2, 3, '123') policy2 = AuthPolicy() policy2.set_policy(policy.get_policy()) self.assertTrue(policy == policy2)
def test_eq_mismatch_non_object_matching_policy(self): policy = AuthPolicy() self.assertNotEqual(policy, policy.get_policy())
def test_setting_any_requirement(self, value): policy = AuthPolicy(any=value) self.assertEqual(policy.get_policy()['minimum_requirements'][0]['any'], value)
def test_empty_policy_creation(self): policy = AuthPolicy() retrieved = policy.get_policy() self.assertIn('minimum_requirements', retrieved) self.assertEqual(retrieved['minimum_requirements'], [])