| # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Integration tests for demo APIs.""" |
| |
| from __future__ import absolute_import |
| from __future__ import division |
| from __future__ import print_function |
| |
| import argparse |
| import requests |
| |
| from test_analyzer import configs |
| from test_analyzer import kms |
| |
| |
| _HOST_FORMAT = "%s.appspot.com" |
| _REQUEST_FORMAT = 'https://%s/%s' |
| |
| |
| def _PrintHttpResponse(request_url, response): |
| """Print responses from http request.""" |
| print('url: %s' % request_url) |
| print(response.text + '\n') |
| |
| |
| def _GetAPIKey(is_stage): |
| """Fetch decrypted API key.""" |
| ciphertext_file_name = configs.GetCipherAPIKeyFile(is_stage=is_stage) |
| return kms.DecryptFromFile(kms.DEFAULT_CRYPTO_KEY_FOR_API, |
| ciphertext_file_name) |
| |
| |
| def _GetHost(is_stage): |
| if is_stage: |
| return _HOST_FORMAT % configs.PROJECT_ID_STAGING |
| else: |
| return _HOST_FORMAT % configs.PROJECT_ID_PROD |
| |
| |
| def MakeRequestToHello(is_stage): |
| api_method = 'hello' |
| request_url = _REQUEST_FORMAT % (_GetHost(is_stage), api_method) |
| response = requests.get(request_url) |
| _PrintHttpResponse(request_url, response) |
| |
| |
| def MakeRequestToInternalHello(is_stage): |
| api_method = 'internal_hello' |
| request_url = _REQUEST_FORMAT % (_GetHost(is_stage), api_method) |
| response = requests.get(request_url, params={'key': _GetAPIKey(is_stage)}) |
| _PrintHttpResponse(request_url, response) |
| |
| |
| def _MakeParser(): |
| """Return parser for testing APIs.""" |
| parser = argparse.ArgumentParser( |
| description=__doc__, |
| formatter_class=argparse.RawDescriptionHelpFormatter) |
| parser.add_argument( |
| '--prod', help='Test prod APIs. Default is to test staging APIs.', |
| action='store_true') |
| return parser |
| |
| |
| if __name__ == '__main__': |
| demo_parser = _MakeParser() |
| args = demo_parser.parse_args() |
| MakeRequestToHello(not args.prod) |
| MakeRequestToInternalHello(not args.prod) |