(self, client_func, expected_path, expected_body, secret=None, success=True)
| 43 | SERVER_ADDRESS = urlparse(os.getenv(_KAGGLE_URL_BASE_ENV_VAR_NAME, default="http://127.0.0.1:0")) |
| 44 | |
| 45 | def _test_client(self, client_func, expected_path, expected_body, secret=None, success=True): |
| 46 | _request = {} |
| 47 | |
| 48 | class AccessTokenHandler(UserSecretsHTTPHandler): |
| 49 | |
| 50 | def set_request(self): |
| 51 | _request['path'] = self.path |
| 52 | content_len = int(self.headers.get('Content-Length')) |
| 53 | _request['body'] = json.loads(self.rfile.read(content_len)) |
| 54 | _request['headers'] = self.headers |
| 55 | |
| 56 | def get_response(self): |
| 57 | if success: |
| 58 | return {'result': {'secret': secret, 'secretType': 'refreshToken', 'secretProvider': 'google', 'expiresInSeconds': 3600}, 'wasSuccessful': "true"} |
| 59 | else: |
| 60 | return {'wasSuccessful': "false", 'errors': ['No user secrets exist for kernel']} |
| 61 | |
| 62 | env = EnvironmentVarGuard() |
| 63 | env.set(_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, _TEST_JWT) |
| 64 | with env: |
| 65 | with HTTPServer((self.SERVER_ADDRESS.hostname, self.SERVER_ADDRESS.port), AccessTokenHandler) as httpd: |
| 66 | threading.Thread(target=httpd.serve_forever).start() |
| 67 | |
| 68 | try: |
| 69 | # Make sure this server is pointed to by clients (the port may have been dynamically chosen): |
| 70 | env.set(_KAGGLE_URL_BASE_ENV_VAR_NAME, "http://"+httpd.server_address[0]+":"+str(httpd.server_address[1])) |
| 71 | client_func() |
| 72 | finally: |
| 73 | httpd.shutdown() |
| 74 | |
| 75 | path, headers, body = _request['path'], _request['headers'], _request['body'] |
| 76 | self.assertEqual( |
| 77 | path, |
| 78 | expected_path, |
| 79 | msg="Fake server did not receive the right request from the UserSecrets client.") |
| 80 | self.assertEqual( |
| 81 | body, |
| 82 | expected_body, |
| 83 | msg="Fake server did not receive the right body from the UserSecrets client.") |
| 84 | |
| 85 | def test_no_token_fails(self): |
| 86 | env = EnvironmentVarGuard() |
no outgoing calls
no test coverage detected