def test_execute_request(base_client): """ Scenario : Execute the request and handle error scenario. Given: - Request object. When: - Handling error scenarios. Then: - Checks if correct Error message is there or not. """ from GoogleCloudSCC import HttpError, httplib2 from httplib2 import Response, socks mock_request = Mock() mock_request.execute = Mock(side_effect=[ HttpError(Response({"status": 404}), b'{}'), HttpError(Response({"status": 501}), b'{}'), socks.HTTPError("Proxy Error"), httplib2.ServerNotFoundError("Not Found.") ]) errors = [ ERROR_MESSAGES["NOT_FOUND_ERROR"].format("Ok"), ERROR_MESSAGES["UNKNOWN_ERROR"].format(501, "Ok"), ERROR_MESSAGES["PROXY_ERROR"], ERROR_MESSAGES["TIMEOUT_ERROR"].format("Not Found.") ] for error in errors: with pytest.raises(ValueError, match=error): base_client.execute_request(mock_request)
def request( self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None, ): if (method, uri) in self.static_responses: return ( Response({ "status": "200", "content-type": "application/json; charset=UTF-8" }), self.static_responses[(method, uri)], ) fpath = self.get_next_file_path(uri, method, record=False) fopen = open if fpath.endswith(".bz2"): if fpath in self._cache: return self._cache[fpath] fopen = bz2.BZ2File with fopen(fpath, "rb") as fh: data = json.load(fh) response = Response(data["headers"]) serialized = json.dumps(data["body"]).encode("utf8") if fpath.endswith("bz2"): self._cache[fpath] = response, serialized return response, serialized
def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): if (method, uri) in self.static_responses: return (Response({ 'status': '200', 'content-type': 'application/json; charset=UTF-8' }), self.static_responses[(method, uri)]) fpath = self.get_next_file_path(uri, method, record=False) fopen = open if fpath.endswith('.bz2'): if fpath in self._cache: return self._cache[fpath] fopen = bz2.BZ2File with fopen(fpath, 'rb') as fh: data = json.load(fh) response = Response(data['headers']) serialized = json.dumps(data['body']).encode('utf8') if fpath.endswith('bz2'): self._cache[fpath] = response, serialized return response, serialized
def post(self, request): food_group = FoodGroupSerializer(data=request.data) if food_group.is_valid(): food_group.save() return Response({'success': True, 'data': food_group.data}) else: return Response({ 'success': False, 'message': food_group.error_messages })
def _response_generator(bad_status_code, reason): # TODO: Should this be a mock? We shouldn't know any more than # necessary about the interface of Response bad_response = Response({}) bad_response.status = bad_status_code bad_response.reason = reason good_response = Response({}) good_response.status = 200 content = 'some content' return [(bad_response, content), (bad_response, content), (good_response, content)]
def test_valid_oauth_credentials(self, Client): client = Client.return_value client.request.return_value = [ Response({}), "oauth_token=aabbccdd&oauth_token_secret=efgh1234" ] url = reverse("services", args=['readability']) data = { 'username': '******', 'password': '******', } response = self.client.post(url, data, follow=True) self.assertEqual(len(response.redirect_chain), 1) self.assertContains( response, "You have successfully added Readability", ) client.request.assert_called_with( "https://www.readability.com/api/rest/v1/oauth/access_token/", method='POST', body=('x_auth_mode=client_auth&x_auth_password=correct+password&' 'x_auth_username=example'), ) user = User.objects.get(pk=self.user.pk) self.assertEqual(user.read_later, 'readability') self.assertEqual(json.loads(user.read_later_credentials), { "oauth_token": "aabbccdd", "oauth_token_secret": "efgh1234", })
def update(request, id): qs = Executive.objects.get(pk=id) form = ExecutiveForm(request.POST or None, instance=qs) if form.is_valid(): form.save() return Response('home') return render(request, 'reg.html', {'form': form})
def test_backend_error(self): """ Scenario: Evaluate behaviour when Google API returns http/500 (Backend Error) Given a list of terms of type List[str] And start date in ISO format And end date in ISO format When Google API returns HTTP 403 Then GoogleApiClient#fetch_google_scores() logs the HttpError """ http = HttpMock(datafile('trends_discovery.json'), {'status': '200'}) error_bytes = b'{"error" : { ' \ b'"code" : 500, ' \ b'"message" : "Backend Error", ' \ b'"errors" : [{ "reason" : "backendError" }] } }' response = Response({'status': 500}) request_builder = RequestMockBuilder( {'trends.getTimelinesForHealth': (response, error_bytes)}) with patch.object(GoogleApiClient, '__init__', lambda x: None): instance = GoogleApiClient() instance.service = build(serviceName=SERVICE_NAME, version=SERVICE_VERSION, http=http, developerKey='APIKEY', requestBuilder=request_builder, cache_discovery=False, static_discovery=False) instance.block_until = None terms = ['flu'] start = date.today() - timedelta(days=5) end = start + timedelta(days=1) with self.assertLogs(level='WARNING') as logContext: instance.fetch_google_scores(terms, start, end) self.assertListEqual(logContext.output, [ 'WARNING:root:<HttpError 500 when requesting None returned "Backend Error". Details: "Backend Error">' ])
def test_not_found(self): response = Response({"status": 404, "reason": "Not Found"}) error = ResourceNotFound(response) self.assertEqual( str(error), "Failed. Response status = 404. Response message = %s." % (response.reason))
def test_add_to_readability(self, Client): client = Client.return_value r = Response({ 'status': 202, 'reason': 'Accepted', 'location': '/api/rest/v1/bookmarks/119', 'x-article-location': '/api/rest/v1/articles/xj28dwkx', }) value = json.dumps({'article': {'id': 'foo'}}) client.request.return_value = [r, value] self.user.read_later = 'readability' self.user.read_later_credentials = json.dumps({ 'oauth_token': 'token', 'oauth_token_secret': 'token secret', }) self.user.save() fake_update(self.feed.url) entry_pk = Entry.objects.all()[0].pk url = reverse('feeds:item', args=[entry_pk]) response = self.client.get(url) self.assertContains(response, "Add to Readability") data = {'action': 'read_later'} response = self.client.post(url, data) client.request.assert_called_with('/api/rest/v1/bookmarks/119', method='GET') self.assertEqual( Entry.objects.get(pk=entry_pk).read_later_url, 'https://www.readability.com/articles/foo') response = self.client.get(url) self.assertNotContains(response, "Add to Instapaper")
def test_request_raises_error_after_5_retries(self, mock_sleep, mock_request): service_unavailable_response = Response({}) service_unavailable_response.status = 403 content = google_error_response(403, "userRateLimitExceeded") logging.info(content) mock_request.return_value = (service_unavailable_response, content) response = HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal(response, (service_unavailable_response, content)) assert_equal( [call(10), call(20), call(40), call(80), call(160)], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_unauthorized_access(self): response = Response({"status": 401, "reason": "Unauthorized"}) error = UnauthorizedAccess(response) self.assertEqual( str(error), "Failed. Response status = 401. Response message = %s." % (response.reason))
def setUp(self): self.database = mongomock.Connection().db self.parser_client = ParserClient('readability secret parser key') response = Response(dict()) response.content = dict(content='<p>article</p>') self.parser_client.get_article_content = MagicMock( return_value=response)
def test_delete_group(self, logger_mock): with self.subTest("Successful"): self.sync_service.delete_group("new_group") self.settings_api.groups().patch.assert_called_once_with( body={ "archiveOnly": "true", "whoCanPostMessage": "NONE_CAN_POST" }, groupUniqueId=f"new_group@{settings.GSUITE_DOMAIN}", ) self.directory_api.members().list.assert_called() self.directory_api.groups().aliases().list.assert_called() self.settings_api.reset_mock() self.directory_api.reset_mock() with self.subTest("Failure"): self.settings_api.groups().patch().execute.side_effect = HttpError( Response({"status": 500}), bytes()) self.sync_service.delete_group("new_group") self.directory_api.members().list.assert_not_called() self.directory_api.groups().aliases().list.assert_not_called() logger_mock.error.assert_called_once_with( "Could not delete list %s: %s", "new_group", bytes())
def _create_google_account_aux(self, title, groups_to_check, mailto=False): (response, ) = self.chat.respond('I need to create a new google account.') self.assertEqual( "What is the employee's full name (Capitalized First & Last)?", response) self.assertEqual(self.chat.action_state['text']['action'], 'create_google_account') (response, ) = self.chat.respond('Bill') self.assertEqual( "What is the employee's full name (Capitalized First & Last)?", response) (response, ) = self.chat.respond('Bill Tester') self.assertEqual("What is a personal email address for Bill?", response) (response, ) = self.chat.respond( '<mailto:[email protected]>' if mailto else '*****@*****.**') self.assertEqual("What is Bill's job title?", response) if 'dev' in groups_to_check: (response, ) = self.chat.respond(title) self.assertIn( "I see we're adding a developer! What team will they be on?", response) responses = self.chat.respond('frontend') else: responses = self.chat.respond(title) expected_responses = [ partial(self.assertIn, "'bill' is an available Google username."), partial( self.assertIn, 'Aw nuts, that name is taken. Might I suggest a nickname or something like btester' ) ] for index, response in enumerate(responses): expected_responses[index](response) # Make sure that the username is available self.admin_service_mock.users().get( userKey='*****@*****.**').execute = Mock(side_effect=HttpError( Response({'status': 404}), b'User does not exist.')) # Set up proper gmail response self.gmail_service_mock.users().messages().send().execute = Mock( return_value={'id': '123456789'}) self.assertEqual(self.chat.action_state['text']['step'], 'username') responses = self.chat.respond('btester') expected_responses = [ partial(self.assertIn, "'btester' is an available Google username."), partial(self.assertIn, 'good to go'), partial(self.assertIn, 'groups now: *{}*'.format(', '.join(groups_to_check))), partial(self.assertIn, 'Sending them a welcome email'), partial(self.assertIn, 'Google account creation complete!'), self._clear_action_state_assert( True, "At the bidding of my master (text), I have created a new Google Account for Bill Tester." ) ] for index, response in enumerate(responses): expected_responses[index](response)
def test_request_with_only_uri_calls_Http_with_default_params( self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com') mock_request.assert_called_with('http://fake.com', "GET", None, None, 5, None)
def test_update_group(self, logger_mock): with self.subTest("Successful"): self.sync_service.update_group( "new_group", GSuiteSyncService.GroupData( "new_group", "some description", False, ["alias2"], [f"test2@{settings.GSUITE_DOMAIN}"], ), ) self.directory_api.groups().update.assert_called_once_with( body={ "email": f"new_group@{settings.GSUITE_DOMAIN}", "name": "new_group", "description": "some description", }, groupKey=f"new_group@{settings.GSUITE_DOMAIN}", ) self.settings_api.groups().update.assert_called_once_with( groupUniqueId=f"new_group@{settings.GSUITE_DOMAIN}", body=self.sync_service._group_settings(False), ) self.directory_api.members().list.assert_called() self.directory_api.groups().aliases().list.assert_called() self.settings_api.reset_mock() self.directory_api.reset_mock() with self.subTest("Failure"): self.directory_api.groups().update( ).execute.side_effect = HttpError(Response({"status": 500}), bytes()) self.sync_service.update_group( "new_group", GSuiteSyncService.GroupData( "new_group", "some description", False, ["alias2"], [f"test2@{settings.GSUITE_DOMAIN}"], ), ) self.directory_api.members().list.assert_not_called() self.directory_api.groups().aliases().list.assert_not_called() logger_mock.error.assert_called_once_with( "Could not update list %s: %s", "new_group", bytes())
def test_request_with_full_args_calls_Http_with_args(self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_run_should_ignore_template_for_multivalued_fields_if_sheet_not_found( self, mock_read_helper, mock_create_tag_template): error_response = Response({'status': 400, 'reason': 'Not Found'}) mock_read_helper.side_effect = HttpError(resp=error_response, content=b'{}') TemplateMaker().run(spreadsheet_id=None, project_id=None, template_id='test-template-id', display_name='Test Template') mock_read_helper.assert_called_once() mock_create_tag_template.assert_called_once( ) # Only the master Template is created.
def test_staff_gets_alert_if_http_error_on_search_activity_list(self): mock = Mock() mock.configure_mock(**{ "activities.return_value.search.return_value.execute.side_effect": HttpError( content=self.error_content, resp=Response(self.error_resp_dict)) }) with patch("cmsplugin_googleplus.googleplus.build", return_value=mock): google_plus_api = GooglePlusAPI(TEST_DEVELOPER_KEY) search_activity_list, alert = google_plus_api.get_search_activity_list( query='test') self.assertEqual(search_activity_list, []) self.assertEqual(alert, 'Google Plus API error: `<HttpError 400 "Bad Request">`')
def request_return_value(response_code, content): data = { 'status': response_code, 'content-length': '3', 'content-location': 'https://example.com/', 'x-powered-by': 'ASP.NET', 'set-cookie': 'ASP.NET_SessionId=wf1bkl0d5vykfpihwv0hlnhc; path=/; HttpOnly', 'x-aspnet-version': '4.0.30319', 'server': 'Microsoft-IIS/7.0', 'cache-control': 'private', 'date': 'Tue, 01 Oct 2013 06:29:28 GMT', 'content-type': 'text/html; charset=utf-8' } resp = Response(data) return resp, content
def test_declared_favicon(self, get): class Response: status_code = 200 content = 'some content' headers = {'foo': 'bar'} get.return_value = Response() Favicon.objects.update_favicon('http://example.com/') get.assert_called_with( 'http://example.com/favicon.ico', headers={ 'User-Agent': ('FeedHQ/%s +https://github.com/feedhq/feedhq (favicon fetcher)' ' - https://github.com/feedhq/feedhq/wiki/User-' 'Agent' % __version__) })
def test_add_to_instapaper(self, Client, get): # noqa client = Client.return_value r = Response({'status': 200}) client.request.return_value = [ r, json.dumps([{'type': 'bookmark', 'bookmark_id': 12345, 'title': 'Some bookmark', 'url': 'http://example.com/some-bookmark'}]) ] user = UserFactory.create( read_later='instapaper', read_later_credentials=json.dumps({ 'oauth_token': 'token', 'oauth_token_secret': 'token secret', }), ) get.return_value = responses(304) feed = FeedFactory.create(category__user=user, user=user) get.return_value = responses(200, 'sw-all.xml') update_feed(feed.url) get.assert_called_with( feed.url, headers={'User-Agent': USER_AGENT % '1 subscriber', 'Accept': feedparser.ACCEPT_HEADER}, timeout=10) entry_pk = Entry.objects.all()[0].pk url = reverse('feeds:item', args=[entry_pk]) response = self.app.get(url, user=user) self.assertContains(response, "Add to Instapaper") form = response.forms['read-later'] response = form.submit() body = 'url=http%3A%2F%2Fsimonwillison.net%2F2010%2FMar%2F12%2Fre2%2F' client.request.assert_called_with( 'https://www.instapaper.com/api/1/bookmarks/add', body=body, method='POST', ) self.assertEqual(Entry.objects.get(pk=entry_pk).read_later_url, 'https://www.instapaper.com/read/12345') response = self.app.get(url, user=user) self.assertNotContains(response, "Add to Instapaper")
def test_request_does_not_sleep_on_503(self, mock_sleep, mock_request): backendError = Response({}) backendError.status = 503 content = google_error_response(503, "backendError") mock_request.return_value = (backendError, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_does_not_sleep_on_404(self, mock_sleep, mock_request): not_found_response = Response({}) not_found_response.status = 404 content = 'content' mock_request.return_value = (not_found_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_parse_reason_with_json_content(self): assert_equal( parse_reason( Response({}), """{ "error": { "errors": [ { "domain": "global", "reason": "invalidParameter", "message": "Invalid value '-1' for max-results. Value must be within the range: [1, 1000]", "locationType": "parameter", "location": "max-results" } ], "code": 400, "message": "Invalid value '-1' for max-results. Value must be within the range: [1, 1000]" } }"""), 'invalidParameter')
def test_run_should_raise_exception_template_for_multivalued_fields_if_unknown_error( self, mock_read_helper, mock_create_tag_template): error_response = Response({ 'status': 500, 'reason': 'Internal Server Error' }) mock_read_helper.side_effect = HttpError(resp=error_response, content=b'{}') with self.assertRaises(HttpError): TemplateMaker().run(spreadsheet_id=None, project_id=None, template_id='test-template-id', display_name='Test Template') mock_read_helper.assert_called_once() mock_create_tag_template.assert_called_once( ) # Only the master Template is created.
def test_request_treats_403_dailyLimitExceeded_as_bad_error( self, mock_error, mock_sleep, mock_request): bad_response = Response({}) bad_response.status = 403 content = google_error_response(403, 'dailyLimitExceeded') mock_request.return_value = (bad_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble") mock_error.assert_called()
def test_make_request_basic_auth(self, mock_request, mock_response): response = Response({ 'status': '401', 'WWW-Authenticate': 'Basic realm="Twilio API"' }) mock_request.side_effect = [(response, Mock()), (Mock(), Mock())] make_request('GET', 'http://httpbin.org/get', auth=('AC123', 'AuthToken')) auth = "{0}:{1}".format('AC123', 'AuthToken') encoded_auth = auth.encode('utf-8') b64_auth = base64.b64encode(encoded_auth) mock_request.assert_called_with( ANY, '/get', 'GET', None, { 'accept-encoding': 'gzip, deflate', 'authorization': 'Basic {0}'.format(b64_auth.decode('utf-8')), 'user-agent': ANY, })
def test_invalid_oauth_credentials(self, Client): client = Client.return_value client.request.return_value = [ Response({'status': 401}), "xAuth error" ] url = reverse("services", args=['instapaper']) data = { 'username': '******', 'password': '******', } response = self.client.post(url, data) self.assertContains(response, "Unable to verify") client.request.assert_called_with( 'https://www.instapaper.com/api/1/oauth/access_token', body=('x_auth_mode=client_auth&' 'x_auth_password=incorrect+password&' 'x_auth_username=example'), method='POST', )