def test_request(self): """ do some mocking of a requests request """ configDict = {} configDict["Timeout"] = "30" configDict["access_certificate"] = os.path.join( self.fixture_path, "cert.pem") configDict["push_url"] = "https://notification.keyidentity.com/send" push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) gda = ("apn.98c78e19e9842a1cfdeb887bf42142b615865b1ec513" "c31ea1a4f3222660435f") message = "Authentication request for user bla" # run the fake request status, response = push_prov.push_notification( challenge=message, gda=gda, transactionId="012345678901234") assert status assert response == VALID_REQUEST return
def test_request(self): """ do some mocking of a requests request """ configDict = {} configDict['Timeout'] = '30' configDict['access_certificate'] = os.path.join(self.fixture_path, 'cert.pem') configDict['push_url'] = "https://notification.keyidentity.com/send" push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) gda = ("apn.98c78e19e9842a1cfdeb887bf42142b615865b1ec513" "c31ea1a4f3222660435f") message = "Authentication request for user bla" # run the fake request status, response = push_prov.push_notification( challenge=message, gda=gda, transactionId='012345678901234') self.assertEquals(status, True) self.assertEquals(response, VALID_REQUEST) return
def test_request(self): """ do some mocking of a requests request """ configDict = {} configDict['Timeout'] = '30' configDict['access_certificate'] = os.path.join(self.fixture_path, 'cert.pem') configDict['push_url'] = "https://notification.keyidentity.com/send" push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) gda = ("apn.98c78e19e9842a1cfdeb887bf42142b615865b1ec513" "c31ea1a4f3222660435f") message = "Authentication request for user bla" # set the response status TestPushProviderController.R_AUTH_STATUS = 200 # run the fake request status, response = push_prov.push_notification( challenge=message, gda=gda, transactionId='012345678901234') self.assertEquals(status, True) self.assertEquals(response, VALID_REQUEST) return
def test_timeout_negative(self): """ Verify that a negative timeout value throws an ValueError """ push_prov = DefaultPushProvider() with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='-1', push_url='https://x'))
def test_timeout_negative(self): """ Verify that a negative timeout value throws an ValueError """ push_prov = DefaultPushProvider() with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout="-1", push_url="https://x"))
def test_timeout_doesnt_accept_strings(self): """ Verify that the timeout parameter only accepts numbers (int, float) and not strings (str, unicode, …) """ push_prov = DefaultPushProvider() for s in [ "invalid timeout", "invalid,timeout", "1,timeout", "invalid,1", ]: v = str(s) with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout=v, push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig( dict(timeout="invalid,timeout", push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig( dict(timeout="1,timeout", push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig( dict(timeout="invalid,1", push_url="https://x"))
def test_timeout_doesnt_accept_strings(self): """ Verify that the timeout parameter only accepts numbers (int, float) and not strings (str, unicode, …) """ push_prov = DefaultPushProvider() for s in [ 'invalid timeout', 'invalid,timeout', '1,timeout', 'invalid,1' ]: for t in [str, unicode]: v = t(s) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout=v, push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig( dict(timeout='invalid,timeout', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig( dict(timeout='1,timeout', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig( dict(timeout='invalid,1', push_url='https://x'))
def test_reuses_remote_service_list(self, append_mock): """ Construct the DefaultPushProvider multiple times while keeping the config the same. The DefaultPushProvider should re-use the RemoteServiceList as long as the remote services do not change. """ with freeze_time() as frozen_dt: config = dict(push_url=['https://srv1/send', 'https://srv2/send']) push_prov = DefaultPushProvider() # initiall the object should be constructed and populated push_prov.loadConfig(config) self.assertTrue(append_mock.called) append_mock.reset_mock() # re-using the same amount list of urls should not create a new object push_prov.loadConfig(config) self.assertFalse(append_mock.called) append_mock.reset_mock() # after a few minutes the cached entry should be expired and re-created frozen_dt.tick(delta=datetime.timedelta(minutes=10)) push_prov.loadConfig(config) self.assertTrue(append_mock.called) append_mock.reset_mock() # having another provider with a different set of urls should # create a new object config2 = dict(push_url='http://srv3/send') push_prov2 = DefaultPushProvider() push_prov2.loadConfig(config2) self.assertTrue(append_mock.called) append_mock.reset_mock() # BUT the other object should still be cached and not recreated push_prov = DefaultPushProvider() push_prov.loadConfig(config) self.assertFalse(append_mock.called)
def _test_servers(self, servers): configDict = {} configDict['Timeout'] = '30' configDict['access_certificate'] = os.path.join( self.fixture_path, 'cert.pem') configDict['push_url'] = servers push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) push_prov = DefaultPushProvider() push_prov.loadConfig(configDict) gda = ("apn.98c78e19e9842a1cfdeb887bf42142b615865b1ec513" "c31ea1a4f3222660435f") message = "Authentication request for user bla" # run the fake request status, response = push_prov.push_notification( challenge=message, gda=gda, transactionId='012345678901234') self.assertEquals(status, True) self.assertEquals(response, VALID_REQUEST)
def test_timeout_invalid_tuple_size(self): """ Verify that tuples with a size != 2 cause an ValueError """ push_prov = DefaultPushProvider() with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout="1,", push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout="1,2,3", push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout="1,2,3,", push_url="https://x")) with pytest.raises(ValueError): push_prov.loadConfig(dict(timeout="1,2,3,4", push_url="https://x"))
def test_timeout_invalid_tuple_size(self): """ Verify that tuples with a size != 2 cause an ValueError """ push_prov = DefaultPushProvider() with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='1,', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='1,2,3', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='1,2,3,', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='1,2,3,4', push_url='https://x'))
def test_timeout_doesnt_accept_strings(self): """ Verify that the timeout parameter only accepts numbers (int, float) and not strings (str, unicode, …) """ push_prov = DefaultPushProvider() for s in ['invalid timeout', 'invalid,timeout', '1,timeout', 'invalid,1']: for t in [str, unicode]: v = t(s) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout=v, push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='invalid,timeout', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='1,timeout', push_url='https://x')) with self.assertRaises(ValueError): push_prov.loadConfig(dict(timeout='invalid,1', push_url='https://x'))
def test_read_config(self): """ test push provider configuration handling """ configDict = {} push_prov = DefaultPushProvider() # # first test the valid configuration # configDict['Timeout'] = '30' configDict['access_certificate'] = os.path.join(self.fixture_path, 'cert.pem') configDict['push_url'] = [ "https://Notification1.keyidentity.com/send", "https://Notification2.keyidentity.com/send", ] push_prov.loadConfig(configDict) # # verify that we support loading of timeout tuples # configDict['Timeout'] = '3,10' push_prov.loadConfig(configDict) self.assertEqual(push_prov.timeout, (3.0, 10.0)) # # verify server url check # with self.assertRaises(requests.exceptions.InvalidSchema): configDict['push_url'] = "hXXXs://proxy.keyidentity.com:8800/send" push_prov.loadConfig(configDict) # # verify that multiple urls are also being checked # with self.assertRaises(requests.exceptions.InvalidSchema): configDict['push_url'] = [ "https://proxy.keyidentity.com:8800/send", "hXXXs://proxy.keyidentity.com:8800/send" ] push_prov.loadConfig(configDict) # # restore configuration for push_url # configDict['push_url'] = [ "https://Notification1.keyidentity.com/send", "https://Notification2.keyidentity.com/send" ] # # extended option: proxy # configDict['proxy'] = "https://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # # extended option: proxy with wrong url scheme # with self.assertRaises(requests.exceptions.InvalidSchema): configDict['proxy'] = "hXXXs://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # restore valid proxy url configDict['proxy'] = "https://proxy.keyidentity.com:8800/" # # valid extended timeout format # configDict['timeout'] = '3,10' push_prov.loadConfig(configDict) del configDict['timeout'] # # invalid timeout format: "invalid literal for float()" # with self.assertRaises(ValueError): configDict['Timeout'] = '30s' push_prov.loadConfig(configDict) # timeout has a default and is not required del configDict['Timeout'] # # non existing certificate file - should raise exception # 'required authenticating client cert could not be found' # with self.assertRaises(IOError): cert_file_name = os.path.join(self.fixture_path, 'non_exist.pem') configDict['access_certificate'] = cert_file_name push_prov.loadConfig(configDict) # restore access certificate parameter cert_file_name = os.path.join(self.fixture_path, 'cert.pem') configDict['access_certificate'] = cert_file_name # check if missing push_url is as well detected with self.assertRaises(KeyError): del configDict['push_url'] push_prov.loadConfig(configDict) # restore required push_url configDict['push_url'] = "https://Notification.keyidentity.com/send" # # check if server cert is provided, the existance of directory or # file is made # server_cert_file_name = os.path.join(self.fixture_path, 'cert.pem') configDict['server_certificate'] = server_cert_file_name push_prov.loadConfig(configDict) with self.assertRaises(IOError): server_cert_file_name = '/abc/ssl/certs' configDict['server_certificate'] = server_cert_file_name push_prov.loadConfig(configDict) return
def test_read_config(self): """ test push provider configuration handling """ configDict = {} push_prov = DefaultPushProvider() # # first test the valid configuration # configDict["Timeout"] = "30" configDict["access_certificate"] = os.path.join( self.fixture_path, "cert.pem") configDict["push_url"] = [ "https://Notification1.keyidentity.com/send", "https://Notification2.keyidentity.com/send", ] push_prov.loadConfig(configDict) # # verify that we support loading of timeout tuples # configDict["Timeout"] = "3,10" push_prov.loadConfig(configDict) assert push_prov.timeout == (3.0, 10.0) # # verify server url check # with pytest.raises(requests.exceptions.InvalidSchema): configDict["push_url"] = "hXXXs://proxy.keyidentity.com:8800/send" push_prov.loadConfig(configDict) # # verify that multiple urls are also being checked # with pytest.raises(requests.exceptions.InvalidSchema): configDict["push_url"] = [ "https://proxy.keyidentity.com:8800/send", "hXXXs://proxy.keyidentity.com:8800/send", ] push_prov.loadConfig(configDict) # # restore configuration for push_url # configDict["push_url"] = [ "https://Notification1.keyidentity.com/send", "https://Notification2.keyidentity.com/send", ] # # extended option: proxy # configDict["proxy"] = "https://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # # extended option: proxy with wrong url scheme # with pytest.raises(requests.exceptions.InvalidSchema): configDict["proxy"] = "hXXXs://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # restore valid proxy url configDict["proxy"] = "https://proxy.keyidentity.com:8800/" # # valid extended timeout format # configDict["timeout"] = "3,10" push_prov.loadConfig(configDict) del configDict["timeout"] # # invalid timeout format: "invalid literal for float()" # with pytest.raises(ValueError): configDict["Timeout"] = "30s" push_prov.loadConfig(configDict) # timeout has a default and is not required del configDict["Timeout"] # # non existing certificate file - should raise exception # 'required authenticating client cert could not be found' # with pytest.raises(IOError): cert_file_name = os.path.join(self.fixture_path, "non_exist.pem") configDict["access_certificate"] = cert_file_name push_prov.loadConfig(configDict) # restore access certificate parameter cert_file_name = os.path.join(self.fixture_path, "cert.pem") configDict["access_certificate"] = cert_file_name # check if missing push_url is as well detected with pytest.raises(KeyError): del configDict["push_url"] push_prov.loadConfig(configDict) # restore required push_url configDict["push_url"] = "https://Notification.keyidentity.com/send" # # check if server cert is provided, the existance of directory or # file is made # server_cert_file_name = os.path.join(self.fixture_path, "cert.pem") configDict["server_certificate"] = server_cert_file_name push_prov.loadConfig(configDict) with pytest.raises(IOError): server_cert_file_name = "/abc/ssl/certs" configDict["server_certificate"] = server_cert_file_name push_prov.loadConfig(configDict) return
def test_read_config(self): """ test push provider configuration handling """ configDict = {} push_prov = DefaultPushProvider() # # first test the valid configuration # configDict['Timeout'] = '30' configDict['access_certificate'] = os.path.join(self.fixture_path, 'cert.pem') configDict['push_url'] = "https://Notification.keyidentity.com/send" push_prov.loadConfig(configDict) # # verify server url check # with self.assertRaises(requests.exceptions.InvalidSchema): configDict['push_url'] = "hXXXs://proxy.keyidentity.com:8800/send" push_prov.loadConfig(configDict) # # restore configuration for push_url # configDict['push_url'] = "https://Notification.keyidentity.com/send" # # extended option: proxy # configDict['proxy'] = "https://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # # extended option: proxy with wrong url scheme # with self.assertRaises(requests.exceptions.InvalidSchema): configDict['proxy'] = "hXXXs://proxy.keyidentity.com:8800/" push_prov.loadConfig(configDict) # restore valid proxy url configDict['proxy'] = "https://proxy.keyidentity.com:8800/" # # valid extended timeout format # configDict['timeout'] = '3,10' push_prov.loadConfig(configDict) del configDict['timeout'] # # invalid timeout format: "invalid literal for float()" # with self.assertRaises(ValueError): configDict['Timeout'] = '30s' push_prov.loadConfig(configDict) # timeout has a default and is not required del configDict['Timeout'] # # non existing certificate file - should raise exception # 'required authenticating client cert could not be found' # with self.assertRaises(IOError): cert_file_name = os.path.join(self.fixture_path, 'non_exist.pem') configDict['access_certificate'] = cert_file_name push_prov.loadConfig(configDict) # restore access certificate parameter cert_file_name = os.path.join(self.fixture_path, 'cert.pem') configDict['access_certificate'] = cert_file_name # check if missing push_url is as well detected with self.assertRaises(KeyError): del configDict['push_url'] push_prov.loadConfig(configDict) # restore required push_url configDict['push_url'] = "https://Notification.keyidentity.com/send" # # check if server cert is provided, the existance of directory or # file is made # server_cert_file_name = os.path.join(self.fixture_path, 'cert.pem') configDict['server_certificate'] = server_cert_file_name push_prov.loadConfig(configDict) with self.assertRaises(IOError): server_cert_file_name = '/abc/ssl/certs' configDict['server_certificate'] = server_cert_file_name push_prov.loadConfig(configDict) return