| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import logging |
| 7 import os |
| 8 import webtest |
| 9 |
| 10 import oauth2client.client |
| 11 import googleapiclient.http |
| 12 from google.appengine.api import app_identity |
| 13 from testing_utils import testing |
| 14 |
| 15 import common |
| 16 import vm_module |
| 17 |
| 18 |
| 19 class VMHandlerTest(testing.AppengineTestCase): |
| 20 @property |
| 21 def app_module(self): |
| 22 return vm_module.app |
| 23 |
| 24 def test_get(self): |
| 25 response = self.test_app.get('/vm') |
| 26 logging.info('response = %s', response) |
| 27 self.assertEquals(200, response.status_int) |
| 28 |
| 29 def test_post(self): |
| 30 # Authenticated production server. |
| 31 self.mock(os, 'environ', {'SERVER_SOFTWARE': 'GAE production server'}) |
| 32 headers = {'X-Appengine-Inbound-Appid': 'my-app-id'} |
| 33 |
| 34 # Authentication fails. |
| 35 self.mock(app_identity, 'get_application_id', lambda: 'bad-app-id') |
| 36 with self.assertRaises(webtest.AppError) as cm: |
| 37 self.test_app.post('/vm', '', headers=headers) |
| 38 logging.info('exception = %s', cm.exception) |
| 39 self.assertIn('403', str(cm.exception)) |
| 40 |
| 41 # Authentication succeeds. |
| 42 self.mock(app_identity, 'get_application_id', lambda: 'my-app-id') |
| 43 |
| 44 # No data is configured. |
| 45 self.mock(common, 'get_data', lambda: None) |
| 46 with self.assertRaises(webtest.AppError) as cm: |
| 47 self.test_app.post('/vm', '', headers=headers) |
| 48 logging.info('exception = %s', cm.exception) |
| 49 self.assertIn('500', str(cm.exception)) |
| 50 |
| 51 # Not all required data is present. |
| 52 self.mock(common, 'get_data', lambda: {'url': 'foo://', 'bad': 'data'}) |
| 53 with self.assertRaises(webtest.AppError) as cm: |
| 54 self.test_app.post('/vm', '', headers=headers) |
| 55 logging.info('exception = %s', cm.exception) |
| 56 self.assertIn('500', str(cm.exception)) |
| 57 |
| 58 # Data is correct. |
| 59 creds = { |
| 60 'client_email': 'we@you.me', |
| 61 'client_id': 'agent007', |
| 62 'private_key': 'deadbeafyoudneverguess', |
| 63 'private_key_id': '!@#$%', |
| 64 } |
| 65 class CredentialsMock(object): |
| 66 def __init__(self, **kwargs): |
| 67 pass |
| 68 |
| 69 def authorize(self, x): |
| 70 return x |
| 71 |
| 72 self.mock(oauth2client.client, 'SignedJwtAssertionCredentials', |
| 73 CredentialsMock) |
| 74 self.mock(common, 'get_data', lambda: { |
| 75 'url': 'foo://', 'scopes': ['this', 'that'], |
| 76 'credentials': creds}) |
| 77 def execute_mock(self): |
| 78 self.postproc('response', 'content') |
| 79 self.mock(googleapiclient.http.HttpRequest, 'execute', execute_mock) |
| 80 |
| 81 # Production server. |
| 82 response = self.test_app.post('/vm', '', headers=headers) |
| 83 logging.info('response = %s', response) |
| 84 self.assertEquals(200, response.status_int) |
| 85 |
| 86 # Dev appserver (for branch coverage). |
| 87 self.mock(os, 'environ', {'SERVER_SOFTWARE': 'Development server'}) |
| 88 response = self.test_app.post('/vm', '', headers=headers) |
| 89 logging.info('response = %s', response) |
| 90 self.assertEquals(200, response.status_int) |
| OLD | NEW |