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_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_set_policy_geofence(self): policy = AuthPolicy() self.assertEqual(len(policy.geofences), 0) policy.set_policy({ 'minimum_requirements': [], 'factors': [{ 'quickfail': False, 'priority': 1, 'requirement': 'forced requirement', 'attributes': { 'locations': [{ 'latitude': 1.0, 'radius': 3.0, 'name': '123', 'longitude': 2.0 }] }, 'factor': 'geofence' }] }) self.assertEqual(len(policy.geofences), 1) self.assertEqual(policy.geofences[0].latitude, 1.0) self.assertEqual(policy.geofences[0].longitude, 2.0) self.assertEqual(policy.geofences[0].radius, 3.0) self.assertEqual(policy.geofences[0].name, '123')\
def test_set_minimum_requirements(self, possession, inherence, knowledge, minimum_requirements): policy = AuthPolicy() policy.set_policy( { 'minimum_requirements': [ { 'possession': possession, 'requirement': 'authenticated', 'any': minimum_requirements, 'inherence': inherence, 'knowledge': knowledge } ], 'factors': [] } ) self.assertEqual(policy.minimum_amount, minimum_requirements) if possession: self.assertIn('possession', policy.minimum_requirements) else: self.assertNotIn('possession', policy.minimum_requirements) if inherence: self.assertIn('inherence', policy.minimum_requirements) else: self.assertNotIn('inherence', policy.minimum_requirements) if knowledge: self.assertIn('knowledge', policy.minimum_requirements) else: self.assertNotIn('knowledge', policy.minimum_requirements)
def test_set_minimum_requirements(self, possession, inherence, knowledge, minimum_requirements): policy = AuthPolicy() policy.set_policy({ 'minimum_requirements': [{ 'possession': possession, 'requirement': 'authenticated', 'any': minimum_requirements, 'inherence': inherence, 'knowledge': knowledge }], 'factors': [] }) self.assertEqual(policy.minimum_amount, minimum_requirements) if possession: self.assertIn('possession', policy.minimum_requirements) else: self.assertNotIn('possession', policy.minimum_requirements) if inherence: self.assertIn('inherence', policy.minimum_requirements) else: self.assertNotIn('inherence', policy.minimum_requirements) if knowledge: self.assertIn('knowledge', policy.minimum_requirements) else: self.assertNotIn('knowledge', policy.minimum_requirements)
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_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_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_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_eq_mismatch(self): policy = AuthPolicy() policy.add_geofence(1, 2, 3, '123') policy2 = AuthPolicy() self.assertNotEqual(policy, policy2) policy2.add_geofence(1, 2, 2, '122') self.assertNotEqual(policy, policy2)
def test_authorize_returns_auth_request_id_from_authorization_request_response( self): expected = str(uuid4()) self._response.data = {"auth_request": expected} actual = self._service_client.authorize('user', 'context', AuthPolicy()) self.assertEqual(actual, expected)
def test_set_minimum_requirments_all(self): policy = AuthPolicy() policy.set_policy({ 'minimum_requirements': [{ 'possession': 1, 'requirement': 'authenticated', 'all': 1, 'inherence': 1, 'knowledge': 1 }], 'factors': [] }) self.assertEqual(policy.minimum_amount, 1) self.assertIn('possession', policy.minimum_requirements) self.assertIn('inherence', policy.minimum_requirements) self.assertIn('knowledge', policy.minimum_requirements)
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_set_policy_jailbreak(self, enabled): policy = AuthPolicy() self.assertEqual(len(policy.geofences), 0) policy.set_policy({ 'minimum_requirements': [], 'factors': [{ 'quickfail': False, 'priority': 1, 'requirement': 'forced requirement', 'attributes': { 'factor enabled': enabled }, 'factor': 'device integrity' }] }) self.assertEqual(policy.jailbreak_protection, True if enabled else False)
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_set_policy_jailbreak(self, enabled): policy = AuthPolicy() self.assertEqual(len(policy.geofences), 0) policy.set_policy( { 'minimum_requirements': [], 'factors': [ { 'quickfail': False, 'priority': 1, 'requirement': 'forced requirement', 'attributes': {'factor enabled': enabled}, 'factor': 'device integrity' } ] } ) self.assertEqual(policy.jailbreak_protection, True if enabled else False)
def test_authorize_calls_authorization_request(self): policy = AuthPolicy() auth_response = AuthorizationRequest(str(uuid4()), None) self._service_client.authorization_request = MagicMock( return_value=auth_response) self._service_client.authorize('user', 'context', policy, 'title', 30, 'push_title', 'push_body') self._service_client.authorization_request.assert_called_once_with( 'user', 'context', policy, 'title', 30, 'push_title', 'push_body')
def test_set_minimum_requirments_all(self): policy = AuthPolicy() policy.set_policy( { 'minimum_requirements': [ { 'possession': 1, 'requirement': 'authenticated', 'all': 1, 'inherence': 1, 'knowledge': 1 } ], 'factors': [] } ) self.assertEqual(policy.minimum_amount, 1) self.assertIn('possession', policy.minimum_requirements) self.assertIn('inherence', policy.minimum_requirements) self.assertIn('knowledge', policy.minimum_requirements)
def test_eq_mismatch(self): policy = AuthPolicy() policy.add_geofence(1, 2, 3, '123') policy2 = AuthPolicy() self.assertNotEqual(policy, policy2) policy2.add_geofence(1, 2, 2, '122') self.assertFalse(policy == policy2)
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_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_set_policy_geofence(self): policy = AuthPolicy() self.assertEqual(len(policy.geofences), 0) policy.set_policy( { 'minimum_requirements': [], 'factors': [ { 'quickfail': False, 'priority': 1, 'requirement': 'forced requirement', 'attributes': { 'locations': [ {'latitude': 1.0, 'radius': 3.0, 'name': '123', 'longitude': 2.0}]}, 'factor': 'geofence' } ] } ) self.assertEqual(len(policy.geofences), 1) self.assertEqual(policy.geofences[0].latitude, 1.0) self.assertEqual(policy.geofences[0].longitude, 2.0) self.assertEqual(policy.geofences[0].radius, 3.0) self.assertEqual(policy.geofences[0].name, '123')\
def test_possession_factor_failure(self, value): with self.assertRaises(InvalidParameters): AuthPolicy(possession=value)
def test_set_policy_json(self): policy = AuthPolicy() self.assertEqual(len(policy.geofences), 0) policy.set_policy(dumps({'minimum_requirements': [], 'factors': []}))
def test_add_geofence_invalid_input(self): policy = AuthPolicy() with self.assertRaises(InvalidParameters): policy.add_geofence(ANY, ANY, ANY)
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_inherence_factor_success(self, value): AuthPolicy(inherence=value)
def test_possession_factor_success(self, value): AuthPolicy(possession=value)
def test_set_policy_invalid_json(self): with self.assertRaises(InvalidPolicyFormat): AuthPolicy().set_policy("{{{{Invalid JSON")
def test_eq_mismatch_non_object(self, value): policy = AuthPolicy() self.assertNotEqual(policy, value)
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_remove_invalid_geofence(self): policy = AuthPolicy() policy.add_geofence(1, 1, 2) with self.assertRaises(InvalidGeoFenceName): policy.remove_geofence(MagicMock(spec=str))
def test_setting_any_requirement(self, value): policy = AuthPolicy(any=value) self.assertEqual(policy.get_policy()['minimum_requirements'][0]['any'], value)
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_invalid_policy(self): policy = AuthPolicy() policy._policy['factors'].append(uuid4()) with self.assertRaises(InvalidParameters): policy.get_policy()
def test_mixing_factor_requirements_exception(self): with self.assertRaises(InvalidParameters): AuthPolicy(any=1, knowledge=1, inherence=1, possession=1)
def test_empty_policy_creation(self): policy = AuthPolicy() retrieved = policy.get_policy() self.assertIn('minimum_requirements', retrieved) self.assertEqual(retrieved['minimum_requirements'], [])
def test_eq_mismatch_non_object_matching_policy(self): policy = AuthPolicy() self.assertNotEqual(policy, policy.get_policy())
def test_set_policy_invalid(self, policy): with self.assertRaises(InvalidPolicyFormat): AuthPolicy().set_policy(policy)
def test_remove_partially_invalid_geofence(self): policy = AuthPolicy() policy.add_geofence(1, 1, 2, 'name') policy.geofences.pop() with self.assertRaises(InvalidGeoFenceName): policy.remove_geofence('name')
def test_knowledge_factor_failure(self, value): with self.assertRaises(InvalidParameters): AuthPolicy(knowledge=value)
def test_inherence_factor_failure(self, value): with self.assertRaises(InvalidParameters): AuthPolicy(inherence=value)