def test_delete(client): responses.add(responses.GET, urlfor('jobs', 'delete'), json={'status': 'success'}, status=200) client.jobs_delete(123) assert 'job_id=123' in responses.calls[0].request.url
def test_raw_result_interface(client): responses.add(responses.GET, urlfor('jobs', 'results'), status=200, json={'status': 'success'}) client.raw_results(123) for arg in ('job_id=123', 'page=1', 'items_per_page=10'): assert arg in responses.calls[0].request.url
def test_download_upstream_error(client, tempfile): responses.add(responses.POST, urlfor('jobs', 'download'), status=200, json={'status': 'general_failure', 'message': 'Something went wrong'}) with pytest.raises(GeneralException): client.jobs_download(123, tempfile)
def test_start(client): responses.add(responses.POST, urlfor('jobs', 'start'), json={'status': 'success'}, status=200) client.jobs_start(123) called_with = json.loads(responses.calls[0].request.body.decode('UTF-8')) expected_args = dict(job_id=123, run_sample=0) for k, v in expected_args.items(): assert called_with[k] == v
def test_account_info(): client = neverbounce_sdk.client() client.api_key = 'static key' # this is the exepcted response responses.add(responses.GET, urlfor('account', 'info'), status=200, json={'status': 'success'}) info = client.account_info() assert info == {'status': 'success'} assert len(responses.calls) == 1 assert (responses.calls[0].request.url == 'https://api.neverbounce.com/v4/account/info?key=static+key')
def test_non_json_response(): responses.add( responses.GET, urlfor('account', 'info'), status=200, # empty dict; client should complain that there's no 'status' # key body='Not Json') with pytest.raises(GeneralException) as exc: neverbounce_sdk.client().account_info() assert ('The response from NeverBounce was unable ' + 'to be parsed as json. Try the request ' + 'again, if this error persists' + ' let us know at [email protected].' + '\n\n(Internal error)') in exc.value.message
def test_single_check(): # this is the exepcted response responses.add(responses.GET, urlfor('single', 'check'), status=200, json={'status': 'success'}) with neverbounce_sdk.client(api_key='static key') as client: info = client.single_check('*****@*****.**', credits_info=True) assert info == {'status': 'success'} assert len(responses.calls) == 1 url = responses.calls[0].request.url for urlchunk in ('https://api.neverbounce.com/v4/single/check', 'email=test%40example.com', 'address_info=0', 'credits_info=1', 'timeout=30', 'key=static+key'): assert urlchunk in url
def test_weird_response_no_status_raises(): responses.add( responses.GET, urlfor('account', 'info'), status=200, # empty dict; client should complain that there's no 'status' # key json={'message': 'Something went wrong'}) with pytest.raises(GeneralException) as exc: neverbounce_sdk.client().account_info() assert ('The response from server is incomplete. ' + 'Either a status code was not included ' + 'or the an error was returned without an ' + 'error message. Try the request again, ' + 'if this error persists let us know at ' + '[email protected].' + '\n\n(Internal error [status 200: ' + '{"message": "Something went wrong"}])') in exc.value.message
def test_account_info(err): # this is the exepcted response: we've picked a random simple API target # just so we can mock out some exceptions and see how the client handles # them responses.add(responses.GET, urlfor('account', 'info'), status=200, json={ 'status': err, 'message': 'the-message' }) with pytest.raises(_status_to_exception[err]) as exc: neverbounce_sdk.client().account_info() if err == 'auth_failure': assert 'We were unable to authenticate your request'\ in exc.value.message else: assert 'We were unable to complete your request.' in exc.value.message assert err in exc.value.message
def test_create(client): responses.add(responses.POST, urlfor('jobs', 'create'), json={'status': 'success'}, status=200) raw_args = dict(input=['*****@*****.**'], input_location='supplied', auto_parse=0, auto_start=0, run_sample=0) client.jobs_create(['*****@*****.**']) called_with = json.loads(responses.calls[0].request.body.decode('UTF-8')) assert 'filename' not in called_with for k, v in raw_args.items(): assert called_with[k] == v new_raw_args = raw_args.copy() new_raw_args['filename'] = 'testfile.csv' client.jobs_create(['*****@*****.**'], filename='testfile.csv') called_with = json.loads(responses.calls[1].request.body.decode('UTF-8')) for k, v in raw_args.items(): assert called_with[k] == v
def test_raw_search_interface(client): responses.add(responses.GET, urlfor('jobs', 'search'), status=200, json={'status': 'success'}) # defaults client.raw_search() request_url = responses.calls[0].request.url for arg in ('job_id', 'filename') + tuple(_job_status): assert arg not in request_url for arg in ('page=1', 'items_per_page=10'): assert arg in request_url client.raw_search(job_id=123, filename='test.csv', job_status='complete') request_url = responses.calls[1].request.url for arg in ('page=1', 'items_per_page=10', 'job_id=123', 'filename=test.csv', 'job_status=complete'): assert arg in request_url with pytest.raises(ValueError): client.raw_search(job_status='some unknown value OH NO')
def test_download_defaults(client, tempfile): responses.add(responses.POST, urlfor('jobs', 'download'), body=r'data\ndata', status=200, content_type='application/octet-stream') client.jobs_download(123, tempfile, line_feed_type='LINEFEED_0A') assert tempfile.read() == r'data\ndata' called_with = json.loads(responses.calls[0].request.body.decode('UTF-8')) default_args = { 'line_feed_type': 'LINEFEED_0A', 'binary_operators_type': 'BIN_1_0', 'valids': 1, 'invalids': 1, 'catchalls': 1, 'unknowns': 1, 'job_id': 123 } for k, v in default_args.items(): assert called_with[k] == v
def test_poe_confirm(): # this is the exepcted response responses.add(responses.GET, urlfor('poe', 'confirm'), status=200, json={'status': 'success'}) with neverbounce_sdk.client(api_key='static key') as client: info = client.poe_confirm( email='*****@*****.**', transaction_id='NBPOE-TXN-5942940c09669', confirmation_token='e3173fdbbdce6bad26522dae792911f2', result='valid') assert info == {'status': 'success'} assert len(responses.calls) == 1 url = responses.calls[0].request.url for urlchunk in ('https://api.neverbounce.com/v4/poe/confirm', 'email=support%40neverbounce.com', 'transaction_id=NBPOE-TXN-5942940c09669', 'confirmation_token=e3173fdbbdce6bad26522dae792911f2', 'result=valid'): assert urlchunk in url