blob: 6fb0b9e16c9f95a301cdb0a437960aa144b9012d [file] [log] [blame]
Joe Gregorioc128bbd2012-08-02 13:18:38 -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.keyring_storage tests.
17
18Unit tests for oauth2client.keyring_storage.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23import datetime
24import keyring
25import unittest
26import mox
27
28from oauth2client.client import OAuth2Credentials
29from oauth2client.keyring_storage import Storage
30
31
32class OAuth2ClientKeyringTests(unittest.TestCase):
33
34 def test_non_existent_credentials_storage(self):
35 m = mox.Mox()
36 m.StubOutWithMock(keyring, 'get_password')
37 m.StubOutWithMock(keyring, 'set_password')
38 keyring.get_password('my_unit_test', 'me').AndReturn(None)
39 m.ReplayAll()
40
41 s = Storage('my_unit_test', 'me')
42 credentials = s.get()
43 self.assertEquals(None, credentials)
44
45 m.UnsetStubs()
46 m.VerifyAll()
47
48 def test_malformed_credentials_in_storage(self):
49 m = mox.Mox()
50 m.StubOutWithMock(keyring, 'get_password')
51 m.StubOutWithMock(keyring, 'set_password')
52 keyring.get_password('my_unit_test', 'me').AndReturn('{')
53 m.ReplayAll()
54
55 s = Storage('my_unit_test', 'me')
56 credentials = s.get()
57 self.assertEquals(None, credentials)
58
59 m.UnsetStubs()
60 m.VerifyAll()
61
62 def test_json_credentials_storage(self):
63 access_token = 'foo'
64 client_id = 'some_client_id'
65 client_secret = 'cOuDdkfjxxnv+'
66 refresh_token = '1/0/a.df219fjls0'
67 token_expiry = datetime.datetime.utcnow()
68 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
69 user_agent = 'refresh_checker/1.0'
70
71 credentials = OAuth2Credentials(
72 access_token, client_id, client_secret,
73 refresh_token, token_expiry, token_uri,
74 user_agent)
75
76 m = mox.Mox()
77 m.StubOutWithMock(keyring, 'get_password')
78 m.StubOutWithMock(keyring, 'set_password')
79 keyring.get_password('my_unit_test', 'me').AndReturn(None)
80 keyring.set_password('my_unit_test', 'me', credentials.to_json())
81 keyring.get_password('my_unit_test', 'me').AndReturn(credentials.to_json())
82 m.ReplayAll()
83
84 s = Storage('my_unit_test', 'me')
85 self.assertEquals(None, s.get())
86
87 s.put(credentials)
88
89 restored = s.get()
90 self.assertEqual('foo', restored.access_token)
91 self.assertEqual('some_client_id', restored.client_id)
92
93 m.UnsetStubs()
94 m.VerifyAll()