Home | History | Annotate | Download | only in contrib
      1 # Copyright 2016 Google Inc. All rights reserved.
      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 """Unit tests for oauth2client.contrib.dictionary_storage"""
     16 
     17 import unittest2
     18 
     19 import oauth2client
     20 from oauth2client import client
     21 from oauth2client.contrib import dictionary_storage
     22 
     23 
     24 def _generate_credentials(scopes=None):
     25     return client.OAuth2Credentials(
     26         'access_tokenz',
     27         'client_idz',
     28         'client_secretz',
     29         'refresh_tokenz',
     30         '3600',
     31         oauth2client.GOOGLE_TOKEN_URI,
     32         'Test',
     33         id_token={
     34             'sub': '123',
     35             'email': 'user (at] example.com'
     36         },
     37         scopes=scopes)
     38 
     39 
     40 class DictionaryStorageTests(unittest2.TestCase):
     41 
     42     def test_constructor_defaults(self):
     43         dictionary = {}
     44         key = 'test-key'
     45         storage = dictionary_storage.DictionaryStorage(dictionary, key)
     46 
     47         self.assertEqual(dictionary, storage._dictionary)
     48         self.assertEqual(key, storage._key)
     49         self.assertIsNone(storage._lock)
     50 
     51     def test_constructor_explicit(self):
     52         dictionary = {}
     53         key = 'test-key'
     54         storage = dictionary_storage.DictionaryStorage(dictionary, key)
     55 
     56         lock = object()
     57         storage = dictionary_storage.DictionaryStorage(
     58             dictionary, key, lock=lock)
     59         self.assertEqual(storage._lock, lock)
     60 
     61     def test_get(self):
     62         credentials = _generate_credentials()
     63         dictionary = {}
     64         key = 'credentials'
     65         storage = dictionary_storage.DictionaryStorage(dictionary, key)
     66 
     67         self.assertIsNone(storage.get())
     68 
     69         dictionary[key] = credentials.to_json()
     70         returned = storage.get()
     71 
     72         self.assertIsNotNone(returned)
     73         self.assertEqual(returned.access_token, credentials.access_token)
     74         self.assertEqual(returned.id_token, credentials.id_token)
     75         self.assertEqual(returned.refresh_token, credentials.refresh_token)
     76         self.assertEqual(returned.client_id, credentials.client_id)
     77 
     78     def test_put(self):
     79         credentials = _generate_credentials()
     80         dictionary = {}
     81         key = 'credentials'
     82         storage = dictionary_storage.DictionaryStorage(dictionary, key)
     83 
     84         storage.put(credentials)
     85         returned = storage.get()
     86 
     87         self.assertIn(key, dictionary)
     88         self.assertIsNotNone(returned)
     89         self.assertEqual(returned.access_token, credentials.access_token)
     90         self.assertEqual(returned.id_token, credentials.id_token)
     91         self.assertEqual(returned.refresh_token, credentials.refresh_token)
     92         self.assertEqual(returned.client_id, credentials.client_id)
     93 
     94     def test_delete(self):
     95         credentials = _generate_credentials()
     96         dictionary = {}
     97         key = 'credentials'
     98         storage = dictionary_storage.DictionaryStorage(dictionary, key)
     99 
    100         storage.put(credentials)
    101 
    102         self.assertIn(key, dictionary)
    103 
    104         storage.delete()
    105 
    106         self.assertNotIn(key, dictionary)
    107         self.assertIsNone(storage.get())
    108