Index: appengine/chrome_infra_mon_proxy/test/vm_module_test.py |
diff --git a/appengine/chrome_infra_mon_proxy/test/vm_module_test.py b/appengine/chrome_infra_mon_proxy/test/vm_module_test.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ae02dffb27b93e92b131a2df420461058075a774 |
--- /dev/null |
+++ b/appengine/chrome_infra_mon_proxy/test/vm_module_test.py |
@@ -0,0 +1,90 @@ |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import json |
+import logging |
+import os |
+import webtest |
+ |
+import oauth2client.client |
+import googleapiclient.http |
+from google.appengine.api import app_identity |
+from testing_utils import testing |
+ |
+import common |
+import vm_module |
+ |
+ |
+class VMHandlerTest(testing.AppengineTestCase): |
+ @property |
+ def app_module(self): |
+ return vm_module.app |
+ |
+ def test_get(self): |
+ response = self.test_app.get('/vm') |
+ logging.info('response = %s', response) |
+ self.assertEquals(200, response.status_int) |
+ |
+ def test_post(self): |
+ # Authenticated production server. |
+ self.mock(os, 'environ', {'SERVER_SOFTWARE': 'GAE production server'}) |
+ headers = {'X-Appengine-Inbound-Appid': 'my-app-id'} |
+ |
+ # Authentication fails. |
+ self.mock(app_identity, 'get_application_id', lambda: 'bad-app-id') |
+ with self.assertRaises(webtest.AppError) as cm: |
+ self.test_app.post('/vm', '', headers=headers) |
+ logging.info('exception = %s', cm.exception) |
+ self.assertIn('403', str(cm.exception)) |
+ |
+ # Authentication succeeds. |
+ self.mock(app_identity, 'get_application_id', lambda: 'my-app-id') |
+ |
+ # No data is configured. |
+ self.mock(common, 'get_data', lambda: None) |
+ with self.assertRaises(webtest.AppError) as cm: |
+ self.test_app.post('/vm', '', headers=headers) |
+ logging.info('exception = %s', cm.exception) |
+ self.assertIn('500', str(cm.exception)) |
+ |
+ # Not all required data is present. |
+ self.mock(common, 'get_data', lambda: {'url': 'foo://', 'bad': 'data'}) |
+ with self.assertRaises(webtest.AppError) as cm: |
+ self.test_app.post('/vm', '', headers=headers) |
+ logging.info('exception = %s', cm.exception) |
+ self.assertIn('500', str(cm.exception)) |
+ |
+ # Data is correct. |
+ creds = { |
+ 'client_email': 'we@you.me', |
+ 'client_id': 'agent007', |
+ 'private_key': 'deadbeafyoudneverguess', |
+ 'private_key_id': '!@#$%', |
+ } |
+ class CredentialsMock(object): |
+ def __init__(self, **kwargs): |
+ pass |
+ |
+ def authorize(self, x): |
+ return x |
+ |
+ self.mock(oauth2client.client, 'SignedJwtAssertionCredentials', |
+ CredentialsMock) |
+ self.mock(common, 'get_data', lambda: { |
+ 'url': 'foo://', 'scopes': ['this', 'that'], |
+ 'credentials': creds}) |
+ def execute_mock(self): |
+ self.postproc('response', 'content') |
+ self.mock(googleapiclient.http.HttpRequest, 'execute', execute_mock) |
+ |
+ # Production server. |
+ response = self.test_app.post('/vm', '', headers=headers) |
+ logging.info('response = %s', response) |
+ self.assertEquals(200, response.status_int) |
+ |
+ # Dev appserver (for branch coverage). |
+ self.mock(os, 'environ', {'SERVER_SOFTWARE': 'Development server'}) |
+ response = self.test_app.post('/vm', '', headers=headers) |
+ logging.info('response = %s', response) |
+ self.assertEquals(200, response.status_int) |