|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""App Engine standard application that runs basic system tests for |
| 16 | +google.auth.app_engine. |
| 17 | +
|
| 18 | +This application has to run tests manually instead of using py.test because |
| 19 | +py.test currently doesn't work on App Engine standard. |
| 20 | +""" |
| 21 | + |
| 22 | +import contextlib |
| 23 | +import json |
| 24 | +import sys |
| 25 | +from StringIO import StringIO |
| 26 | +import traceback |
| 27 | + |
| 28 | +from google.appengine.api import app_identity |
| 29 | +import google.auth |
| 30 | +from google.auth import _helpers |
| 31 | +from google.auth import app_engine |
| 32 | +import google.auth.transport.urllib3 |
| 33 | +import urllib3.contrib.appengine |
| 34 | +import webapp2 |
| 35 | + |
| 36 | +TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo' |
| 37 | +EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' |
| 38 | +HTTP = urllib3.contrib.appengine.AppEngineManager() |
| 39 | +HTTP_REQUEST = google.auth.transport.urllib3.Request(HTTP) |
| 40 | + |
| 41 | + |
| 42 | +def test_credentials(): |
| 43 | + credentials = app_engine.Credentials() |
| 44 | + scoped_credentials = credentials.with_scopes([EMAIL_SCOPE]) |
| 45 | + |
| 46 | + scoped_credentials.refresh(None) |
| 47 | + |
| 48 | + assert scoped_credentials.valid |
| 49 | + assert scoped_credentials.token is not None |
| 50 | + |
| 51 | + # Get token info and verify scope |
| 52 | + url = _helpers.update_query(TOKEN_INFO_URL, { |
| 53 | + 'access_token': scoped_credentials.token, |
| 54 | + }) |
| 55 | + response = HTTP_REQUEST(url=url, method='GET') |
| 56 | + token_info = json.loads(response.data.decode('utf-8')) |
| 57 | + |
| 58 | + assert token_info['scope'] == EMAIL_SCOPE |
| 59 | + |
| 60 | + |
| 61 | +def test_default(): |
| 62 | + credentials, project_id = google.auth.default() |
| 63 | + |
| 64 | + assert isinstance(credentials, app_engine.Credentials) |
| 65 | + assert project_id == app_identity.get_application_id() |
| 66 | + |
| 67 | + |
| 68 | +@contextlib.contextmanager |
| 69 | +def capture(): |
| 70 | + """Context manager that captures stderr and stdout.""" |
| 71 | + oldout, olderr = sys.stdout, sys.stderr |
| 72 | + try: |
| 73 | + out = StringIO() |
| 74 | + sys.stdout, sys.stderr = out, out |
| 75 | + yield out |
| 76 | + finally: |
| 77 | + sys.stdout, sys.stderr = oldout, olderr |
| 78 | + |
| 79 | + |
| 80 | +def run_test_func(func): |
| 81 | + with capture() as capsys: |
| 82 | + try: |
| 83 | + func() |
| 84 | + return True, '' |
| 85 | + except Exception as exc: |
| 86 | + output = ( |
| 87 | + 'Test {} failed: {}\n\n' |
| 88 | + 'Stacktrace:\n{}\n\n' |
| 89 | + 'Captured output:\n{}').format( |
| 90 | + func.func_name, exc, traceback.format_exc(), |
| 91 | + capsys.getvalue()) |
| 92 | + return False, output |
| 93 | + |
| 94 | + |
| 95 | +def run_tests(): |
| 96 | + """Runs all tests. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Tuple[bool, str]: A tuple containing True if all tests pass, False |
| 100 | + otherwise, and any captured output from the tests. |
| 101 | + """ |
| 102 | + status = True |
| 103 | + output = '' |
| 104 | + |
| 105 | + tests = (test_credentials, test_default) |
| 106 | + |
| 107 | + for test in tests: |
| 108 | + test_status, test_output = run_test_func(test) |
| 109 | + status = status and test_status |
| 110 | + output += test_output |
| 111 | + |
| 112 | + return status, output |
| 113 | + |
| 114 | + |
| 115 | +class MainHandler(webapp2.RequestHandler): |
| 116 | + def get(self): |
| 117 | + self.response.headers['content-type'] = 'text/plain' |
| 118 | + |
| 119 | + status, output = run_tests() |
| 120 | + |
| 121 | + if not status: |
| 122 | + self.response.status = 500 |
| 123 | + |
| 124 | + self.response.write(output) |
| 125 | + |
| 126 | + |
| 127 | +app = webapp2.WSGIApplication([ |
| 128 | + ('/', MainHandler), |
| 129 | +], debug=True) |
0 commit comments