Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | """Discovery document tests |
| 19 | |
| 20 | Unit tests for objects created from discovery documents. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
| 25 | import unittest |
| 26 | import urlparse |
| 27 | try: |
| 28 | from urlparse import parse_qs |
| 29 | except ImportError: |
| 30 | from cgi import parse_qs |
| 31 | |
| 32 | from apiclient.http import HttpMockSequence |
| 33 | from oauth2client.client import AccessTokenCredentials |
| 34 | from oauth2client.client import AccessTokenCredentialsError |
| 35 | from oauth2client.client import AccessTokenRefreshError |
| 36 | from oauth2client.client import FlowExchangeError |
| 37 | from oauth2client.client import OAuth2Credentials |
| 38 | from oauth2client.client import OAuth2WebServerFlow |
| 39 | |
| 40 | |
| 41 | class OAuth2CredentialsTests(unittest.TestCase): |
| 42 | |
| 43 | def setUp(self): |
| 44 | access_token = "foo" |
| 45 | client_id = "some_client_id" |
| 46 | client_secret = "cOuDdkfjxxnv+" |
| 47 | refresh_token = "1/0/a.df219fjls0" |
| 48 | token_expiry = "ignored" |
| 49 | token_uri = "https://www.google.com/accounts/o8/oauth2/token" |
| 50 | user_agent = "refresh_checker/1.0" |
| 51 | self.credentials = OAuth2Credentials( |
| 52 | access_token, client_id, client_secret, |
| 53 | refresh_token, token_expiry, token_uri, |
| 54 | user_agent) |
| 55 | |
| 56 | def test_token_refresh_success(self): |
| 57 | http = HttpMockSequence([ |
| 58 | ({'status': '401'}, ''), |
| 59 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 60 | ({'status': '200'}, 'echo_request_headers'), |
| 61 | ]) |
| 62 | http = self.credentials.authorize(http) |
| 63 | resp, content = http.request("http://example.com") |
| 64 | self.assertEqual(content['authorization'], 'OAuth 1/3w') |
| 65 | |
| 66 | def test_token_refresh_failure(self): |
| 67 | http = HttpMockSequence([ |
| 68 | ({'status': '401'}, ''), |
| 69 | ({'status': '400'}, '{"error":"access_denied"}'), |
| 70 | ]) |
| 71 | http = self.credentials.authorize(http) |
| 72 | try: |
| 73 | http.request("http://example.com") |
| 74 | self.fail("should raise AccessTokenRefreshError exception") |
| 75 | except AccessTokenRefreshError: |
| 76 | pass |
| 77 | |
| 78 | def test_non_401_error_response(self): |
| 79 | http = HttpMockSequence([ |
| 80 | ({'status': '400'}, ''), |
| 81 | ]) |
| 82 | http = self.credentials.authorize(http) |
| 83 | resp, content = http.request("http://example.com") |
| 84 | self.assertEqual(400, resp.status) |
| 85 | |
| 86 | |
| 87 | class AccessTokenCredentialsTests(unittest.TestCase): |
| 88 | |
| 89 | def setUp(self): |
| 90 | access_token = "foo" |
| 91 | user_agent = "refresh_checker/1.0" |
| 92 | self.credentials = AccessTokenCredentials(access_token, user_agent) |
| 93 | |
| 94 | def test_token_refresh_success(self): |
| 95 | http = HttpMockSequence([ |
| 96 | ({'status': '401'}, ''), |
| 97 | ]) |
| 98 | http = self.credentials.authorize(http) |
| 99 | try: |
| 100 | resp, content = http.request("http://example.com") |
| 101 | self.fail("should throw exception if token expires") |
| 102 | except AccessTokenCredentialsError: |
| 103 | pass |
| 104 | except Exception: |
| 105 | self.fail("should only throw AccessTokenCredentialsError") |
| 106 | |
| 107 | def test_non_401_error_response(self): |
| 108 | http = HttpMockSequence([ |
| 109 | ({'status': '400'}, ''), |
| 110 | ]) |
| 111 | http = self.credentials.authorize(http) |
| 112 | resp, content = http.request("http://example.com") |
| 113 | self.assertEqual(400, resp.status) |
| 114 | |
| 115 | |
| 116 | class OAuth2WebServerFlowTest(unittest.TestCase): |
| 117 | |
| 118 | def setUp(self): |
| 119 | self.flow = OAuth2WebServerFlow( |
| 120 | client_id='client_id+1', |
| 121 | client_secret='secret+1', |
| 122 | scope='foo', |
| 123 | user_agent='unittest-sample/1.0', |
| 124 | ) |
| 125 | |
| 126 | def test_construct_authorize_url(self): |
| 127 | authorize_url = self.flow.step1_get_authorize_url('oob') |
| 128 | |
| 129 | parsed = urlparse.urlparse(authorize_url) |
| 130 | q = parse_qs(parsed[4]) |
| 131 | self.assertEqual(q['client_id'][0], 'client_id+1') |
| 132 | self.assertEqual(q['response_type'][0], 'code') |
| 133 | self.assertEqual(q['scope'][0], 'foo') |
| 134 | self.assertEqual(q['redirect_uri'][0], 'oob') |
| 135 | |
| 136 | def test_exchange_failure(self): |
| 137 | http = HttpMockSequence([ |
| 138 | ({'status': '400'}, '{"error":"invalid_request"}') |
| 139 | ]) |
| 140 | |
| 141 | try: |
| 142 | credentials = self.flow.step2_exchange('some random code', http) |
| 143 | self.fail("should raise exception if exchange doesn't get 200") |
| 144 | except FlowExchangeError: |
| 145 | pass |
| 146 | |
| 147 | def test_exchange_success(self): |
| 148 | http = HttpMockSequence([ |
| 149 | ({'status': '200'}, |
| 150 | """{ "access_token":"SlAV32hkKG", |
| 151 | "expires_in":3600, |
| 152 | "refresh_token":"8xLOxBtZp8" }"""), |
| 153 | ]) |
| 154 | |
| 155 | credentials = self.flow.step2_exchange('some random code', http) |
| 156 | self.assertEqual(credentials.access_token, 'SlAV32hkKG') |
| 157 | self.assertNotEqual(credentials.token_expiry, None) |
| 158 | self.assertEqual(credentials.refresh_token, '8xLOxBtZp8') |
| 159 | |
| 160 | |
| 161 | def test_exchange_no_expires_in(self): |
| 162 | http = HttpMockSequence([ |
| 163 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 164 | "refresh_token":"8xLOxBtZp8" }"""), |
| 165 | ]) |
| 166 | |
| 167 | credentials = self.flow.step2_exchange('some random code', http) |
| 168 | self.assertEqual(credentials.token_expiry, None) |
| 169 | |
| 170 | |
| 171 | if __name__ == '__main__': |
| 172 | unittest.main() |