• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Tests for the DjangoORM storage class."""
16
17# Mock a Django environment
18import datetime
19
20from django.db import models
21import mock
22import unittest2
23
24from oauth2client import GOOGLE_TOKEN_URI
25from oauth2client.client import OAuth2Credentials
26from oauth2client.contrib.django_util.models import CredentialsField
27from oauth2client.contrib.django_util.storage import (
28    DjangoORMStorage as Storage)
29
30
31class TestStorage(unittest2.TestCase):
32    def setUp(self):
33        access_token = 'foo'
34        client_id = 'some_client_id'
35        client_secret = 'cOuDdkfjxxnv+'
36        refresh_token = '1/0/a.df219fjls0'
37        token_expiry = datetime.datetime.utcnow()
38        user_agent = 'refresh_checker/1.0'
39
40        self.credentials = OAuth2Credentials(
41            access_token, client_id, client_secret,
42            refresh_token, token_expiry, GOOGLE_TOKEN_URI,
43            user_agent)
44
45        self.key_name = 'id'
46        self.key_value = '1'
47        self.property_name = 'credentials'
48
49    def test_constructor(self):
50        storage = Storage(FakeCredentialsModel, self.key_name,
51                          self.key_value, self.property_name)
52
53        self.assertEqual(storage.model_class, FakeCredentialsModel)
54        self.assertEqual(storage.key_name, self.key_name)
55        self.assertEqual(storage.key_value, self.key_value)
56        self.assertEqual(storage.property_name, self.property_name)
57
58    @mock.patch('django.db.models')
59    def test_locked_get(self, djangoModel):
60        fake_model_with_credentials = FakeCredentialsModelMock()
61        entities = [
62            fake_model_with_credentials
63        ]
64        filter_mock = mock.Mock(return_value=entities)
65        object_mock = mock.Mock()
66        object_mock.filter = filter_mock
67        FakeCredentialsModelMock.objects = object_mock
68
69        storage = Storage(FakeCredentialsModelMock, self.key_name,
70                          self.key_value, self.property_name)
71        credential = storage.locked_get()
72        self.assertEqual(
73            credential, fake_model_with_credentials.credentials)
74
75    @mock.patch('django.db.models')
76    def test_locked_get_no_entities(self, djangoModel):
77        entities = []
78        filter_mock = mock.Mock(return_value=entities)
79        object_mock = mock.Mock()
80        object_mock.filter = filter_mock
81        FakeCredentialsModelMock.objects = object_mock
82
83        storage = Storage(FakeCredentialsModelMock, self.key_name,
84                          self.key_value, self.property_name)
85        credential = storage.locked_get()
86        self.assertIsNone(credential)
87
88    @mock.patch('django.db.models')
89    def test_locked_get_no_set_store(self, djangoModel):
90        fake_model_with_credentials = FakeCredentialsModelMockNoSet()
91        entities = [
92            fake_model_with_credentials
93        ]
94        filter_mock = mock.Mock(return_value=entities)
95        object_mock = mock.Mock()
96        object_mock.filter = filter_mock
97        FakeCredentialsModelMockNoSet.objects = object_mock
98
99        storage = Storage(FakeCredentialsModelMockNoSet, self.key_name,
100                          self.key_value, self.property_name)
101        credential = storage.locked_get()
102        self.assertEqual(
103            credential, fake_model_with_credentials.credentials)
104
105    @mock.patch('django.db.models')
106    def test_locked_put(self, djangoModel):
107        entity_mock = mock.Mock(credentials=None)
108        objects = mock.Mock(
109            get_or_create=mock.Mock(return_value=(entity_mock, None)))
110        FakeCredentialsModelMock.objects = objects
111        storage = Storage(FakeCredentialsModelMock, self.key_name,
112                          self.key_value, self.property_name)
113        storage.locked_put(self.credentials)
114
115    @mock.patch('django.db.models')
116    def test_locked_delete(self, djangoModel):
117        class FakeEntities(object):
118            def __init__(self):
119                self.deleted = False
120
121            def delete(self):
122                self.deleted = True
123
124        fake_entities = FakeEntities()
125        entities = fake_entities
126
127        filter_mock = mock.Mock(return_value=entities)
128        object_mock = mock.Mock()
129        object_mock.filter = filter_mock
130        FakeCredentialsModelMock.objects = object_mock
131        storage = Storage(FakeCredentialsModelMock, self.key_name,
132                          self.key_value, self.property_name)
133        storage.locked_delete()
134        self.assertTrue(fake_entities.deleted)
135
136
137class CredentialWithSetStore(CredentialsField):
138    def __init__(self):
139        self.model = CredentialWithSetStore
140
141    def set_store(self, storage):
142        pass
143
144
145class FakeCredentialsModel(models.Model):
146    credentials = CredentialsField()
147
148
149class FakeCredentialsModelMock(object):
150    def __init__(self, *args, **kwargs):
151        self.model = FakeCredentialsModelMock
152        self.saved = False
153        self.deleted = False
154
155    credentials = CredentialWithSetStore()
156
157
158class FakeCredentialsModelMockNoSet(object):
159    def __init__(self, set_store=False, *args, **kwargs):
160        self.model = FakeCredentialsModelMock
161        self.saved = False
162        self.deleted = False
163
164    credentials = CredentialsField()
165