def test_call_recommendation_server_parameters(requests_get): taar_url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) # No locale or platform call_recommendation_server('123456', {}, taar_url) requests_get.assert_called_with(taar_url + '123456/', timeout=taar_timeout) # locale no platform call_recommendation_server('123456', {'locale': 'en-US'}, taar_url) requests_get.assert_called_with( taar_url + '123456/?locale=en-US', timeout=taar_timeout) # platform no locale call_recommendation_server('123456', {'platform': 'WINNT'}, taar_url) requests_get.assert_called_with( taar_url + '123456/?platform=WINNT', timeout=taar_timeout) # both locale and platform call_recommendation_server( '123456', {'locale': 'en-US', 'platform': 'WINNT'}, taar_url) requests_get.assert_called_with( taar_url + '123456/?locale=en-US&platform=WINNT', timeout=taar_timeout) # and some extra parameters call_recommendation_server( '123456', {'locale': 'en-US', 'platform': 'WINNT', 'study': 'sch', 'branch': 'tree'}, settings.RECOMMENDATION_ENGINE_URL) requests_get.assert_called_with( taar_url + '123456/?branch=tree&locale=en-US&platform=WINNT&study=sch', timeout=taar_timeout)
def test_call_recommendation_server_parameters(requests_get): taar_url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) # No locale or platform call_recommendation_server('123456', {}) requests_get.assert_called_with(taar_url + '123456/', timeout=taar_timeout) # locale no platform call_recommendation_server('123456', {'locale': 'en-US'}) requests_get.assert_called_with(taar_url + '123456/?locale=en-US', timeout=taar_timeout) # platform no locale call_recommendation_server('123456', {'platform': 'WINNT'}) requests_get.assert_called_with(taar_url + '123456/?platform=WINNT', timeout=taar_timeout) # both locale and platform call_recommendation_server('123456', { 'locale': 'en-US', 'platform': 'WINNT' }) requests_get.assert_called_with(taar_url + '123456/?locale=en-US&platform=WINNT', timeout=taar_timeout) # and some extra parameters call_recommendation_server('123456', { 'locale': 'en-US', 'platform': 'WINNT', 'study': 'sch', 'branch': 'tree' }) requests_get.assert_called_with( taar_url + '123456/?branch=tree&locale=en-US&platform=WINNT&study=sch', timeout=taar_timeout)
def test_call_recommendation_server_no_parameters(requests_get, requests_post): url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse(json.dumps({'results': ['@lolwut']})) # No parameters call_recommendation_server(url, '123456', {}) requests_get.assert_called_with(url + '123456/', timeout=taar_timeout) assert not requests_post.called
def test_call_recommendation_server_no_parameters(requests_get, requests_post): url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) # No parameters call_recommendation_server(url, '123456', {}) requests_get.assert_called_with(url + '123456/', timeout=taar_timeout) assert not requests_post.called
def test_call_recommendation_server_post_no_parameters(requests_get, requests_post): url = 'http://example.com/taar_is_awesome/' taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse(json.dumps({'results': ['@lolwut']})) call_recommendation_server(url, '4815162342', None, verb='post') assert not requests_get.called requests_post.assert_called_with( url + '4815162342/', json=None, timeout=taar_timeout )
def test_call_recommendation_server_some_parameters(requests_get, requests_post): url = 'http://example.com/whatever/' taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse(json.dumps({'results': ['@lolwut']})) data = {'some': 'params', 'and': 'more'} call_recommendation_server(url, '123456', data) requests_get.assert_called_with( url + '123456/?and=more&some=params', timeout=taar_timeout ) assert not requests_post.called
def test_call_recommendation_server_post_no_parameters( requests_get, requests_post): url = 'http://example.com/taar_is_awesome/' taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) call_recommendation_server(url, '4815162342', None, verb='post') assert not requests_get.called requests_post.assert_called_with( url + '4815162342/', json=None, timeout=taar_timeout)
def test_call_recommendation_server_some_parameters( requests_get, requests_post): url = 'http://example.com/whatever/' taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) data = {'some': 'params', 'and': 'more'} call_recommendation_server(url, '123456', data) requests_get.assert_called_with( url + '123456/?and=more&some=params', timeout=taar_timeout) assert not requests_post.called
def test_call_recommendation_server_fails_nice(requests_get, statsd_incr): requests_get.side_effect = requests.exceptions.RequestException() # Check the exception in requests.get is handled okay. assert ( call_recommendation_server(settings.RECOMMENDATION_ENGINE_URL, '123456', {}) is None ) statsd_incr.assert_called_with('services.recommendations.fail')
def test_call_recommendation_server_get_parameter_is_an_url( requests_get, requests_post ): url = 'http://example.com/taar_is_awesome/' requests_get.return_value = HttpResponse(json.dumps({'results': []})) assert call_recommendation_server(url, 'http://evil.com', {}, verb='get') is None assert not requests_get.called assert not requests_post.called assert call_recommendation_server(url, 'http://evil.com/', {}, verb='get') is None assert not requests_get.called assert not requests_post.called assert call_recommendation_server(url, 'http://[evil.com/', {}, verb='get') is None assert not requests_get.called assert not requests_post.called assert ( call_recommendation_server(url, 'http://evil.com/foo', {}, verb='get') is None ) assert not requests_get.called assert not requests_post.called assert call_recommendation_server(url, '/foo', {}, verb='get') is None assert not requests_get.called assert not requests_post.called assert call_recommendation_server(url, '//foo', {}, verb='get') is None assert not requests_get.called assert not requests_post.called
def get_addon_recommendations(guid_param, taar_enable): guids = None fail_reason = None if taar_enable: guids = call_recommendation_server( settings.TAAR_LITE_RECOMMENDATION_ENGINE_URL, guid_param, {}) outcome = (TAAR_LITE_OUTCOME_REAL_SUCCESS if guids else TAAR_LITE_OUTCOME_REAL_FAIL) if not guids: fail_reason = (TAAR_LITE_FALLBACK_REASON_EMPTY if guids == [] else TAAR_LITE_FALLBACK_REASON_TIMEOUT) else: outcome = TAAR_LITE_OUTCOME_CURATED if not guids: guids = TAAR_LITE_FALLBACKS return guids, outcome, fail_reason
def get_addon_recommendations(guid_param, taar_enable): guids = None fail_reason = None if taar_enable: guids = call_recommendation_server( guid_param, {}, settings.TAAR_LITE_RECOMMENDATION_ENGINE_URL) outcome = (TAAR_LITE_OUTCOME_REAL_SUCCESS if guids else TAAR_LITE_OUTCOME_REAL_FAIL) if not guids: fail_reason = (TAAR_LITE_FALLBACK_REASON_EMPTY if guids == [] else TAAR_LITE_FALLBACK_REASON_TIMEOUT) else: outcome = TAAR_LITE_OUTCOME_CURATED if not guids: guids = TAAR_LITE_FALLBACKS return guids, outcome, fail_reason
def test_call_recommendation_server_parameters(requests_get): taar_url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) # No locale or platform call_recommendation_server('123456', None, None) requests_get.assert_called_with(taar_url + '123456/', timeout=taar_timeout) # locale no platform call_recommendation_server('123456', 'en-US', None) requests_get.assert_called_with( taar_url + '123456/?locale=en-US', timeout=taar_timeout) # platform no locale call_recommendation_server('123456', None, 'WINNT') requests_get.assert_called_with( taar_url + '123456/?platform=WINNT', timeout=taar_timeout) # both locale and platform call_recommendation_server('123456', 'en-US', 'WINNT') requests_get.assert_called_with( taar_url + '123456/?locale=en-US&platform=WINNT', timeout=taar_timeout)
def test_call_recommendation_server_parameters(requests_get): taar_url = settings.RECOMMENDATION_ENGINE_URL taar_timeout = settings.RECOMMENDATION_ENGINE_TIMEOUT requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) # No locale or platform call_recommendation_server('123456', None, None) requests_get.assert_called_with(taar_url + '123456/', timeout=taar_timeout) # locale no platform call_recommendation_server('123456', 'en-US', None) requests_get.assert_called_with(taar_url + '123456/?locale=en-US', timeout=taar_timeout) # platform no locale call_recommendation_server('123456', None, 'WINNT') requests_get.assert_called_with(taar_url + '123456/?platform=WINNT', timeout=taar_timeout) # both locale and platform call_recommendation_server('123456', 'en-US', 'WINNT') requests_get.assert_called_with(taar_url + '123456/?locale=en-US&platform=WINNT', timeout=taar_timeout)
def test_call_recommendation_server_fails_nice(requests_post): requests_post.side_effect = requests.exceptions.RequestException() # Check the exception in requests.post is handled okay. assert call_recommendation_server('123456') == []
def test_call_recommendation_server_succeeds(requests_get, statsd_incr): requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) assert call_recommendation_server(settings.RECOMMENDATION_ENGINE_URL, '123456', {}) == ['@lolwut'] statsd_incr.assert_called_with('services.recommendations.success')
def test_call_recommendation_server_succeeds(requests_get, statsd_incr): requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) assert call_recommendation_server('123456', None, None) == ['@lolwut'] statsd_incr.assert_called_with('services.recommendations.success')
def test_call_recommendation_server_succeeds(requests_get, statsd_incr): requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) assert call_recommendation_server( settings.RECOMMENDATION_ENGINE_URL, '123456', {}) == ['@lolwut'] statsd_incr.assert_called_with('services.recommendations.success')
def test_call_recommendation_server_fails_nice(requests_get, statsd_incr): requests_get.side_effect = requests.exceptions.RequestException() # Check the exception in requests.get is handled okay. assert call_recommendation_server( settings.RECOMMENDATION_ENGINE_URL, '123456', {}) is None statsd_incr.assert_called_with('services.recommendations.fail')
def test_call_recommendation_server_succeeds(requests_get, statsd_incr): requests_get.return_value = HttpResponse( json.dumps({'results': ['@lolwut']})) assert call_recommendation_server('123456') == ['@lolwut'] assert statsd_incr.called_with('services.recommendations.succeed')
def test_call_recommendation_server_fails_nice(requests_get, statsd_incr): requests_get.side_effect = requests.exceptions.RequestException() # Check the exception in requests.get is handled okay. assert call_recommendation_server('123456', {}) == [] statsd_incr.assert_called_with('services.recommendations.fail')
def test_call_recommendation_server_fails_nice(requests_get, statsd_incr): requests_get.side_effect = requests.exceptions.RequestException() # Check the exception in requests.get is handled okay. assert call_recommendation_server('123456', None, None) == [] statsd_incr.assert_called_with('services.recommendations.fail')