def test_request(self): """ request() calls requests.request() with the expected parms if everything is normal, and returns the expected result. """ response_data = {u'status': u'ok', u'foo': u'bar'} action, method, token = "ACTION", "METHOD", "TOKEN" url = basket_url(action, token) with patch('basket.base.requests.request', autospec=True) \ as request_call: request_call.return_value = Mock(status_code=200, content=json.dumps(response_data), content_type='application/json') result = request(method, action, data="DATA", token=token, params="PARAMS") request_call.assert_called_with(method, url, data="DATA", params="PARAMS", headers=None, timeout=ANY) self.assertEqual(response_data, result)
def test_request_newsletters_non_string(self): """ If request is passed newsletters as a non-string, newsletters is converted to a comma-separated string """ response_data = {u'status': u'ok', u'foo': u'bar'} input_data = {'newsletters': ['one', 'two'], 'thing': 1} expected_input_data = input_data.copy() newsletters = ','.join(input_data['newsletters']) expected_input_data['newsletters'] = newsletters action, method, token = "ACTION", "METHOD", "TOKEN" url = basket_url(action, token) with patch('basket.base.requests.request', autospec=True) \ as request_call: request_call.return_value = Mock(status_code=200, content=json.dumps(response_data), content_type='application/json') result = request(method, action, data=input_data, token=token, params="PARAMS") request_call.assert_called_with(method, url, data=expected_input_data, params="PARAMS", headers=None, timeout=ANY) self.assertEqual(response_data, result)
def test_request_newsletters_string(self): """ If request is passed newsletters as a string, newsletters is passed along unaltered. """ input_data = {'newsletters': 'one,two'} action, method, token = "ACTION", "METHOD", "TOKEN" url = basket_url(action, token) content = {'one': 100, 'two': 200} with patch('basket.base.requests.request', autospec=True) \ as request_call: request_call.return_value = Mock(status_code=200, content=json.dumps(content), content_type='application/json') result = request(method, action, data=input_data, token=token, params="PARAMS") request_call.assert_called_with(method, url, data=input_data, params="PARAMS", headers=None, timeout=ANY) self.assertEqual(content, result)
def test_basket_url_with_token(self): """Form basket URL properly when token used""" with patch('basket.base.BASKET_URL', new="BASKET_URL"): result = basket_url("METHOD", "TOKEN") self.assertEqual("BASKET_URL/news/METHOD/TOKEN/", result)