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 | |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 25 | import httplib2 |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 26 | import unittest |
| 27 | import urlparse |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 28 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 29 | try: |
| 30 | from urlparse import parse_qs |
| 31 | except ImportError: |
| 32 | from cgi import parse_qs |
| 33 | |
| 34 | from apiclient.http import HttpMockSequence |
| 35 | from oauth2client.client import AccessTokenCredentials |
| 36 | from oauth2client.client import AccessTokenCredentialsError |
| 37 | from oauth2client.client import AccessTokenRefreshError |
| 38 | from oauth2client.client import FlowExchangeError |
| 39 | from oauth2client.client import OAuth2Credentials |
| 40 | from oauth2client.client import OAuth2WebServerFlow |
| 41 | |
| 42 | |
| 43 | class OAuth2CredentialsTests(unittest.TestCase): |
| 44 | |
| 45 | def setUp(self): |
| 46 | access_token = "foo" |
| 47 | client_id = "some_client_id" |
| 48 | client_secret = "cOuDdkfjxxnv+" |
| 49 | refresh_token = "1/0/a.df219fjls0" |
| 50 | token_expiry = "ignored" |
| 51 | token_uri = "https://www.google.com/accounts/o8/oauth2/token" |
| 52 | user_agent = "refresh_checker/1.0" |
| 53 | self.credentials = OAuth2Credentials( |
| 54 | access_token, client_id, client_secret, |
| 55 | refresh_token, token_expiry, token_uri, |
| 56 | user_agent) |
| 57 | |
| 58 | def test_token_refresh_success(self): |
| 59 | http = HttpMockSequence([ |
| 60 | ({'status': '401'}, ''), |
| 61 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 62 | ({'status': '200'}, 'echo_request_headers'), |
| 63 | ]) |
| 64 | http = self.credentials.authorize(http) |
| 65 | resp, content = http.request("http://example.com") |
| 66 | self.assertEqual(content['authorization'], 'OAuth 1/3w') |
| 67 | |
| 68 | def test_token_refresh_failure(self): |
| 69 | http = HttpMockSequence([ |
| 70 | ({'status': '401'}, ''), |
| 71 | ({'status': '400'}, '{"error":"access_denied"}'), |
| 72 | ]) |
| 73 | http = self.credentials.authorize(http) |
| 74 | try: |
| 75 | http.request("http://example.com") |
| 76 | self.fail("should raise AccessTokenRefreshError exception") |
| 77 | except AccessTokenRefreshError: |
| 78 | pass |
| 79 | |
| 80 | def test_non_401_error_response(self): |
| 81 | http = HttpMockSequence([ |
| 82 | ({'status': '400'}, ''), |
| 83 | ]) |
| 84 | http = self.credentials.authorize(http) |
| 85 | resp, content = http.request("http://example.com") |
| 86 | self.assertEqual(400, resp.status) |
| 87 | |
| 88 | |
| 89 | class AccessTokenCredentialsTests(unittest.TestCase): |
| 90 | |
| 91 | def setUp(self): |
| 92 | access_token = "foo" |
| 93 | user_agent = "refresh_checker/1.0" |
| 94 | self.credentials = AccessTokenCredentials(access_token, user_agent) |
| 95 | |
| 96 | def test_token_refresh_success(self): |
| 97 | http = HttpMockSequence([ |
| 98 | ({'status': '401'}, ''), |
| 99 | ]) |
| 100 | http = self.credentials.authorize(http) |
| 101 | try: |
| 102 | resp, content = http.request("http://example.com") |
| 103 | self.fail("should throw exception if token expires") |
| 104 | except AccessTokenCredentialsError: |
| 105 | pass |
| 106 | except Exception: |
| 107 | self.fail("should only throw AccessTokenCredentialsError") |
| 108 | |
| 109 | def test_non_401_error_response(self): |
| 110 | http = HttpMockSequence([ |
| 111 | ({'status': '400'}, ''), |
| 112 | ]) |
| 113 | http = self.credentials.authorize(http) |
| 114 | resp, content = http.request("http://example.com") |
| 115 | self.assertEqual(400, resp.status) |
| 116 | |
| 117 | |
| 118 | class OAuth2WebServerFlowTest(unittest.TestCase): |
| 119 | |
| 120 | def setUp(self): |
| 121 | self.flow = OAuth2WebServerFlow( |
| 122 | client_id='client_id+1', |
| 123 | client_secret='secret+1', |
| 124 | scope='foo', |
| 125 | user_agent='unittest-sample/1.0', |
| 126 | ) |
| 127 | |
| 128 | def test_construct_authorize_url(self): |
| 129 | authorize_url = self.flow.step1_get_authorize_url('oob') |
| 130 | |
| 131 | parsed = urlparse.urlparse(authorize_url) |
| 132 | q = parse_qs(parsed[4]) |
| 133 | self.assertEqual(q['client_id'][0], 'client_id+1') |
| 134 | self.assertEqual(q['response_type'][0], 'code') |
| 135 | self.assertEqual(q['scope'][0], 'foo') |
| 136 | self.assertEqual(q['redirect_uri'][0], 'oob') |
| 137 | |
| 138 | def test_exchange_failure(self): |
| 139 | http = HttpMockSequence([ |
| 140 | ({'status': '400'}, '{"error":"invalid_request"}') |
| 141 | ]) |
| 142 | |
| 143 | try: |
| 144 | credentials = self.flow.step2_exchange('some random code', http) |
| 145 | self.fail("should raise exception if exchange doesn't get 200") |
| 146 | except FlowExchangeError: |
| 147 | pass |
| 148 | |
| 149 | def test_exchange_success(self): |
| 150 | http = HttpMockSequence([ |
| 151 | ({'status': '200'}, |
| 152 | """{ "access_token":"SlAV32hkKG", |
| 153 | "expires_in":3600, |
| 154 | "refresh_token":"8xLOxBtZp8" }"""), |
| 155 | ]) |
| 156 | |
| 157 | credentials = self.flow.step2_exchange('some random code', http) |
| 158 | self.assertEqual(credentials.access_token, 'SlAV32hkKG') |
| 159 | self.assertNotEqual(credentials.token_expiry, None) |
| 160 | self.assertEqual(credentials.refresh_token, '8xLOxBtZp8') |
| 161 | |
| 162 | |
| 163 | def test_exchange_no_expires_in(self): |
| 164 | http = HttpMockSequence([ |
| 165 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 166 | "refresh_token":"8xLOxBtZp8" }"""), |
| 167 | ]) |
| 168 | |
| 169 | credentials = self.flow.step2_exchange('some random code', http) |
| 170 | self.assertEqual(credentials.token_expiry, None) |
| 171 | |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | unittest.main() |