Home | History | Annotate | Download | only in django_util
      1 # Copyright 2014 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 """Django model tests.
     16 
     17 Unit tests for models and fields defined by the django_util helper.
     18 """
     19 
     20 import base64
     21 import pickle
     22 
     23 from tests.contrib.django_util.models import CredentialsModel
     24 
     25 import unittest2
     26 
     27 from oauth2client._helpers import _from_bytes
     28 from oauth2client.client import Credentials
     29 from oauth2client.contrib.django_util.models import CredentialsField
     30 
     31 
     32 class TestCredentialsField(unittest2.TestCase):
     33 
     34     def setUp(self):
     35         self.fake_model = CredentialsModel()
     36         self.fake_model_field = self.fake_model._meta.get_field('credentials')
     37         self.field = CredentialsField(null=True)
     38         self.credentials = Credentials()
     39         self.pickle_str = _from_bytes(
     40             base64.b64encode(pickle.dumps(self.credentials)))
     41 
     42     def test_field_is_text(self):
     43         self.assertEqual(self.field.get_internal_type(), 'BinaryField')
     44 
     45     def test_field_unpickled(self):
     46         self.assertIsInstance(
     47             self.field.to_python(self.pickle_str), Credentials)
     48 
     49     def test_field_already_unpickled(self):
     50         self.assertIsInstance(
     51             self.field.to_python(self.credentials), Credentials)
     52 
     53     def test_none_field_unpickled(self):
     54         self.assertIsNone(self.field.to_python(None))
     55 
     56     def test_from_db_value(self):
     57         value = self.field.from_db_value(
     58             self.pickle_str, None, None, None)
     59         self.assertIsInstance(value, Credentials)
     60 
     61     def test_field_unpickled_none(self):
     62         self.assertEqual(self.field.to_python(None), None)
     63 
     64     def test_field_pickled(self):
     65         prep_value = self.field.get_db_prep_value(self.credentials,
     66                                                   connection=None)
     67         self.assertEqual(prep_value, self.pickle_str)
     68 
     69     def test_field_value_to_string(self):
     70         self.fake_model.credentials = self.credentials
     71         value_str = self.fake_model_field.value_to_string(self.fake_model)
     72         self.assertEqual(value_str, self.pickle_str)
     73 
     74     def test_field_value_to_string_none(self):
     75         self.fake_model.credentials = None
     76         value_str = self.fake_model_field.value_to_string(self.fake_model)
     77         self.assertIsNone(value_str)
     78 
     79     def test_credentials_without_null(self):
     80         credentials = CredentialsField()
     81         self.assertTrue(credentials.null)
     82 
     83 
     84 class CredentialWithSetStore(CredentialsField):
     85     def __init__(self):
     86         self.model = CredentialWithSetStore
     87 
     88     def set_store(self, storage):
     89         pass  # pragma: NO COVER
     90 
     91 
     92 class FakeCredentialsModelMock(object):
     93 
     94     credentials = CredentialWithSetStore()
     95 
     96 
     97 class FakeCredentialsModelMockNoSet(object):
     98 
     99     credentials = CredentialsField()
    100