0

My goal: when a user is saved, set the username to the email address

Test class userAccount/tests.py:

#----------------------------------------------------------------------#
#   Test that the User is saved correctly 
#----------------------------------------------------------------------#
class UserAccountTests(TestCase):

    # assert that a users email address is saved as the username also
    def test_username_is_email(self):
        from userAccount.models import *
        from django.contrib.auth.hashers import make_password
        user_email = u"testuser@baflist.com",
        test_user = User.objects.create(
                first_name="test", 
                last_name="user",
                email="testuser@baflist.com",
                password=make_password("whatever")
                )

        self.assertEqual(test_user.username, user_email)

My solution userAccount/models.py:

from django.contrib.auth.models import User as DjangoUser

class User(DjangoUser):
    def save(self, *args, **kwargs):
        self.username = self.email
        super(User, self).save(*args, **kwargs) 

I'm still new to django (and web development) so I'm not sure if there is a better way to do this..? Perhaps by implementing AbstractBaseUser in some way?

My main issue is that the test fails because:

======================================================================
FAIL: test_username_is_email (userAccount.tests.UserAccountTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/bdhammel/Documents/web_development/baflist/baflist/userAccount/tests.py", line 55, in test_username_is_email
    self.assertEqual(test_user.username, user_email)
AssertionError: 'testuser@baflist.com' != (u'testuser@baflist.com',)

why/how is user_email being converted to a tuple?

Ben
  • 6,986
  • 6
  • 44
  • 71

1 Answers1

3

You have a trailing comma after the email in your test. That converts your variable into a tuple.

Specifically this line:

    user_email = u"testuser@baflist.com",

Remove the trailing comma to fix it like this:

    user_email = u"testuser@baflist.com"
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • ah, yes I do. Is my use of inheriting the django User model O.K.? – Ben Jun 01 '14 at 20:35
  • @Ben: yes, the inheriting is just fine but you should note that you need to tell Django that you are using a custom user model. Just set the `AUTH_USER_MODEL` and you're done: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-AUTH_USER_MODEL – Wolph Jun 01 '14 at 20:42
  • Great, thank you. However, setting `AUTH_USER_MODEL='userAccount.User'` returns a `CommandError:` saying `User` relies on `auth.User` and needs to be set to `settings.AUTH_USER_MODEL`. This seems like it creates a circular logic problem. – Ben Jun 01 '14 at 21:14
  • 1
    @Ben: my bad, your model should inherit `AbstractBaseUser` instead of `User`: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser After that it will work just fine :) – Wolph Jun 01 '14 at 21:33