blob: 15e45c4d9f203ae3e8fb7aadb46cc88562a4cf1a [file] [log] [blame]
Joe Gregoriofd08e432012-08-09 14:17:41 -04001# Copyright 2012 Google Inc.
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
16"""Tests for oauth2client.gce.
17
18Unit tests for oauth2client.gce.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23import unittest
24import mox
25
26from oauth2client.client import AccessTokenRefreshError
27from oauth2client.client import Credentials
28from oauth2client.gce import AppAssertionCredentials
29
30
31class AssertionCredentialsTests(unittest.TestCase):
32
33 def test_good_refresh(self):
34 m = mox.Mox()
35
36 httplib2_response = m.CreateMock(object)
37 httplib2_response.status = 200
38
39 httplib2_request = m.CreateMock(object)
40 httplib2_request.__call__(
41 ('http://metadata.google.internal/0.1/meta-data/service-accounts/'
42 'default/acquire'
43 '?scope=http%3A%2F%2Fexample.com%2Fa%20http%3A%2F%2Fexample.com%2Fb'
44 )).AndReturn((httplib2_response, '{"accessToken": "this-is-a-token"}'))
45
46 m.ReplayAll()
47
48 c = AppAssertionCredentials(scope=['http://example.com/a',
49 'http://example.com/b'])
50
51 c._refresh(httplib2_request)
52
53 self.assertEquals('this-is-a-token', c.access_token)
54
55 m.UnsetStubs()
56 m.VerifyAll()
57
58
59 def test_fail_refresh(self):
60 m = mox.Mox()
61
62 httplib2_response = m.CreateMock(object)
63 httplib2_response.status = 400
64
65 httplib2_request = m.CreateMock(object)
66 httplib2_request.__call__(
67 ('http://metadata.google.internal/0.1/meta-data/service-accounts/'
68 'default/acquire'
69 '?scope=http%3A%2F%2Fexample.com%2Fa%20http%3A%2F%2Fexample.com%2Fb'
70 )).AndReturn((httplib2_response, '{"accessToken": "this-is-a-token"}'))
71
72 m.ReplayAll()
73
74 c = AppAssertionCredentials(scope=['http://example.com/a',
75 'http://example.com/b'])
76
77 try:
78 c._refresh(httplib2_request)
79 self.fail('Should have raised exception on 400')
80 except AccessTokenRefreshError:
81 pass
82
83 m.UnsetStubs()
84 m.VerifyAll()
85
86 def test_to_from_json(self):
87 c = AppAssertionCredentials(scope=['http://example.com/a',
88 'http://example.com/b'])
89 json = c.to_json()
90 c2 = Credentials.new_from_json(json)
91
92 self.assertEqual(c.access_token, c2.access_token)