blob: e5b99719207e1632411a68be4df8448e40340547 [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
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -080028from oauth2client import GOOGLE_TOKEN_URI
Joe Gregorioc128bbd2012-08-02 13:18:38 -040029from oauth2client.client import OAuth2Credentials
30from oauth2client.keyring_storage import Storage
31
32
33class OAuth2ClientKeyringTests(unittest.TestCase):
34
35 def test_non_existent_credentials_storage(self):
36 m = mox.Mox()
37 m.StubOutWithMock(keyring, 'get_password')
38 m.StubOutWithMock(keyring, 'set_password')
39 keyring.get_password('my_unit_test', 'me').AndReturn(None)
40 m.ReplayAll()
41
42 s = Storage('my_unit_test', 'me')
43 credentials = s.get()
44 self.assertEquals(None, credentials)
45
46 m.UnsetStubs()
47 m.VerifyAll()
48
49 def test_malformed_credentials_in_storage(self):
50 m = mox.Mox()
51 m.StubOutWithMock(keyring, 'get_password')
52 m.StubOutWithMock(keyring, 'set_password')
53 keyring.get_password('my_unit_test', 'me').AndReturn('{')
54 m.ReplayAll()
55
56 s = Storage('my_unit_test', 'me')
57 credentials = s.get()
58 self.assertEquals(None, credentials)
59
60 m.UnsetStubs()
61 m.VerifyAll()
62
63 def test_json_credentials_storage(self):
64 access_token = 'foo'
65 client_id = 'some_client_id'
66 client_secret = 'cOuDdkfjxxnv+'
67 refresh_token = '1/0/a.df219fjls0'
68 token_expiry = datetime.datetime.utcnow()
Joe Gregorioc128bbd2012-08-02 13:18:38 -040069 user_agent = 'refresh_checker/1.0'
70
71 credentials = OAuth2Credentials(
72 access_token, client_id, client_secret,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -080073 refresh_token, token_expiry, GOOGLE_TOKEN_URI,
Joe Gregorioc128bbd2012-08-02 13:18:38 -040074 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()