|
| 1 | +# Copyright 2015 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 | +import os |
| 16 | +import re |
| 17 | + |
| 18 | +from apiclient.http import HttpMock |
| 19 | + |
| 20 | +from bigquery.samples.appengine_auth import main |
| 21 | + |
| 22 | +import mock |
| 23 | + |
| 24 | +import tests |
| 25 | + |
| 26 | +import webapp2 |
| 27 | + |
| 28 | + |
| 29 | +RESOURCE_PATH = os.path.join( |
| 30 | + os.path.abspath(os.path.dirname(__file__)), 'resources') |
| 31 | + |
| 32 | + |
| 33 | +class TestAuthSample(tests.DatastoreTestbedCase, tests.CloudBaseTest): |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + tests.DatastoreTestbedCase.setUp(self) |
| 37 | + tests.CloudBaseTest.setUp(self) |
| 38 | + |
| 39 | + self.testbed.init_user_stub() |
| 40 | + |
| 41 | + def loginUser( self, email='[email protected]', id='123', is_admin=False): |
| 42 | + self.testbed.setup_env( |
| 43 | + user_email=email, |
| 44 | + user_id=id, |
| 45 | + user_is_admin='1' if is_admin else '0', |
| 46 | + overwrite=True) |
| 47 | + |
| 48 | + def test_anonymous_get(self): |
| 49 | + request = webapp2.Request.blank('/') |
| 50 | + response = request.get_response(main.app) |
| 51 | + |
| 52 | + # Should redirect to login |
| 53 | + self.assertEqual(response.status_int, 302) |
| 54 | + self.assertRegexpMatches(response.headers['Location'], |
| 55 | + r'.*accounts.*Login.*') |
| 56 | + |
| 57 | + def test_loggedin_get(self): |
| 58 | + self.loginUser() |
| 59 | + |
| 60 | + request = webapp2.Request.blank('/') |
| 61 | + response = request.get_response(main.app) |
| 62 | + |
| 63 | + # Should redirect to login |
| 64 | + self.assertEqual(response.status_int, 302) |
| 65 | + self.assertRegexpMatches(response.headers['Location'], r'.*oauth2.*') |
| 66 | + |
| 67 | + @mock.patch.object(main.decorator, 'has_credentials', return_value=True) |
| 68 | + def test_oauthed_get(self, *args): |
| 69 | + self.loginUser() |
| 70 | + |
| 71 | + request = webapp2.Request.blank('/') |
| 72 | + |
| 73 | + mock_http = HttpMock( |
| 74 | + os.path.join(RESOURCE_PATH, 'datasets-list.json'), |
| 75 | + {'status': '200'}) |
| 76 | + with mock.patch.object(main.decorator, 'http', return_value=mock_http): |
| 77 | + original_projectid = main.PROJECTID |
| 78 | + try: |
| 79 | + main.PROJECTID = self.constants['projectId'] |
| 80 | + response = request.get_response(main.app) |
| 81 | + finally: |
| 82 | + main.PROJECTID = original_projectid |
| 83 | + |
| 84 | + # Should make the api call |
| 85 | + self.assertEqual(response.status_int, 200) |
| 86 | + self.assertRegexpMatches( |
| 87 | + response.body, |
| 88 | + re.compile(r'.*datasets.*datasetReference.*etag.*', re.DOTALL)) |
0 commit comments