Commits

Peter Sagerson  committed b4126e8

Fix Device.__unicode__() for unpopulated instances.

  • Participants
  • Parent commits 8ac0208

Comments (0)

Files changed (2)

File django-otp/django_otp/models.py

 from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
 from django.db import models
 from django.utils import six
 
             return self.__unicode__().encode('utf-8')
 
     def __unicode__(self):
-        return six.u('{0}: {1}'.format(self.user.username, self.name))
+        try:
+            user = self.user
+        except ObjectDoesNotExist:
+            user = None
+
+        return six.u("{0} ({1})".format(self.name, user))
 
     @property
     def persistent_id(self):

File django-otp/django_otp/plugins/otp_static/tests.py

 from .models import StaticDevice
 
 
+class DeviceTest(TestCase):
+    """ A few generic tests to get us started. """
+    def setUp(self):
+        try:
+            self.user = self.create_user('alice', 'password')
+        except Exception:
+            self.skipTest("Unable to create the test user.")
+
+    def test_str(self):
+        device = StaticDevice.objects.create(user=self.user, name="Device")
+
+        str(device)
+
+    def test_str_unpopulated(self):
+        device = StaticDevice()
+
+        str(device)
+
+
 class AuthFormTest(TestCase):
     """
     Test the auth form with static tokens.