def test_user_updates_email_and_password(self): """ User updating any two fields should not affect the third field. """ email = self.user.email username = self.user.username password = self.user.password userid = self.user.id pk = self.user.pk profile = self.user.profile update = { 'password': '******', 'email': '*****@*****.**' } serializer = UserSerializer(self.user, data=update, partial=True) self.assertTrue(serializer.is_valid()) updated_user = serializer.save() self.assertTrue(updated_user) self.assertEqual(username, updated_user.username) self.assertEqual(userid, updated_user.id) self.assertEqual(pk, updated_user.pk) self.assertEqual(profile, updated_user.profile) self.assertNotEqual(updated_user.password, password) self.assertNotEqual(updated_user.email, email)
def test_user_updates_password(self): """ User updating password should have password hashed before storage, and update should not affect other fields. """ email = self.user.email username = self.user.username password = self.user.password userid = self.user.id pk = self.user.pk profile = self.user.profile update = {'password': '******'} serializer = UserSerializer(self.user, data=update, partial=True) self.assertTrue(serializer.is_valid()) updated_user = serializer.save() self.assertTrue(updated_user) self.assertEqual(updated_user.email, email) self.assertEqual(updated_user.username, username) self.assertEqual(userid, updated_user.id) self.assertEqual(pk, updated_user.pk) self.assertEqual(profile, updated_user.profile) self.assertNotEqual(updated_user.password, update['password']) self.assertNotEqual(password, updated_user.password)
def test_user_updates_email(self): """ User updating email should not affect other fields. """ email = self.user.email username = self.user.username password = self.user.password userid = self.user.id pk = self.user.pk profile = self.user.profile update = {'email': '*****@*****.**'} serializer = UserSerializer(self.user, data=update, partial=True, context={'request': None}) self.assertTrue(serializer.is_valid()) updated_user = serializer.save() self.assertTrue(updated_user) self.assertEqual(updated_user.email, update['email']) self.assertEqual(updated_user.username, username) self.assertEqual(userid, updated_user.id) self.assertEqual(pk, updated_user.pk) self.assertEqual(password, updated_user.password) self.assertEqual(profile, updated_user.profile) self.assertNotEqual(email, updated_user.email)
def test_user_updates_username(self): """ User updating username should not affect other fields. """ user = User.objects.get(username=self.user.username) email = user.email userid = user.id pk = user.pk profile = user.profile username = user.username update = { 'username': '******', } serializer = UserSerializer(user, data=update, partial=True, context={'request': None}) self.assertTrue(serializer.is_valid()) updated_user = serializer.save() self.assertTrue(updated_user) self.assertEqual(updated_user.username, 'NewUserName') self.assertEqual(userid, updated_user.id) self.assertEqual(pk, updated_user.pk) self.assertEqual(email, updated_user.email) self.assertEqual(profile, updated_user.profile) self.assertNotEqual(username, updated_user.username)
def test_serializer_accepts_valid_data(self): """ Serializer should be valid when provided with a username, email and password. """ serializer = UserSerializer(data=self.data) self.assertTrue(serializer.is_valid())
def test_user_and_profile_created_upon_save(self): serializer = UserSerializer(data=self.data) if serializer.is_valid(): serializer.save() user = User.objects.get(username=self.data['username']) profile = UserProfile.objects.get_or_create(user=user)[0] self.assertTrue(user) self.assertTrue(user.profile) self.assertEqual(profile.user, user) self.assertEqual(user.profile, profile)
def test_password_hashed_for_new_user(self): """ Upon creation, new User's passwords should be hashed before storing in the database. """ serializer = UserSerializer(data=self.data) if serializer.is_valid(): user = serializer.save() self.assertNotEqual(user.password, self.data['password']) self.assertNotEqual(len(user.password), len(self.data['password']))
def test_password_not_included_upon_retrieval(self): """ Password should not be included when User is serialized """ serializer = UserSerializer(self.user, context={'request': None}) self.assertNotIn('password', serializer.data) included_fields = ['id', 'username', 'profile', 'email'] for field in included_fields: self.assertIn(field, serializer.data)