def test_anvil_call(self, mock_logger): url = '{}register'.format(TEST_TERRA_API_ROOT_URL) responses.add(responses.GET, url, status=200, body=REGISTER_RESPONSE) r = anvil_call('get', 'register', 'ya.EXAMPLE') self.assertDictEqual(r['userInfo'], { "userEmail": "*****@*****.**", "userSubjectId": "123456" }) mock_logger.info.assert_called_with( 'GET https://terra.api/register 200 127 None') self.assertEqual(len(mock_logger.method_calls), 1) mock_logger.reset_mock() responses.replace( responses.GET, url, status=404, body= '{"causes": [], "message": "google subject Id 123456 not found in sam", "source": "sam", "stackTrace": [], "statusCode": 404, "timestamp": 1605282720182}' ) with self.assertRaises(TerraNotFoundException) as te: _ = anvil_call('get', 'register', 'ya.EXAMPLE') self.assertEqual( str(te.exception), 'None called Terra API: GET /register got status 404 with reason: Not Found' ) self.assertEqual(len(mock_logger.method_calls), 0)
def validate_anvil_registration(backend, response, *args, **kwargs): if backend.name == 'google-oauth2': try: anvil_call('get', 'register', response['access_token']) except TerraNotFoundException as et: logger.warning('User {} is trying to login without registration on AnVIL. {}'.format(response['email'], str(et))) return _redirect_login('anvilLoginFailed', backend)