def test_create_method(self): new_user = UserSerializer().create(self.serializer_user_attributes) logged_in = self.client.login( username=self.serializer_user_attributes['username'], password=self.serializer_user_attributes['password']) self.assertEqual(new_user.username, self.serializer_user_attributes["username"]) self.assertEqual(new_user.email, self.serializer_user_attributes["email"]) self.assertEqual(new_user.has_usable_password(), True) self.assertEqual(logged_in, True)
def setUp(self): self.user_attributes = { 'username': "******", 'email': "*****@*****.**", 'password': "******" } self.serializer_user_attributes = { 'username': "******", 'email': "*****@*****.**", 'password': "******" } self.user = User.objects.create_user(**self.user_attributes) self.serializer = UserSerializer(instance=self.user)
def test_update_method(self): new_attributes = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } new_user = UserSerializer().create(self.serializer_user_attributes) updated_user = UserSerializer().update(new_user, new_attributes) logged_in = self.client.login(username=new_attributes['username'], password=new_attributes['password']) self.assertEqual(updated_user.username, new_attributes['username']) self.assertEqual(updated_user.email, new_attributes['email']) self.assertEqual(updated_user.has_usable_password(), True) self.assertEqual(logged_in, True)
def test_validation_with_not_unique_email(self): new_data = self.serializer_user_attributes new_data['email'] = self.user_attributes['email'] new_serializer_lower = UserSerializer(data=new_data) self.assertEqual(new_serializer_lower.is_valid(), False) new_data['email'] = self.user_attributes['email'].capitalize() new_serializer_capitalize = UserSerializer(data=new_data) self.assertEqual(new_serializer_capitalize.is_valid(), False) new_data['email'] = self.user_attributes['email'].upper() new_serializer_upper = UserSerializer(data=new_data) self.assertEqual(new_serializer_upper.is_valid(), False)
class UserSerializerTestCase(TestCase): def setUp(self): self.user_attributes = { 'username': "******", 'email': "*****@*****.**", 'password': "******" } self.serializer_user_attributes = { 'username': "******", 'email': "*****@*****.**", 'password': "******" } self.user = User.objects.create_user(**self.user_attributes) self.serializer = UserSerializer(instance=self.user) def test_contains_expected_fields(self): data = self.serializer.data self.assertEqual(set(data.keys()), set(['id', 'username', 'projects', 'tasks', 'email'])) def test_username_field(self): data = self.serializer.data self.assertEqual(data['username'], self.user_attributes['username']) def test_email_field(self): data = self.serializer.data self.assertEqual(data['email'], self.user_attributes['email']) def test_password_field(self): password = self.serializer.get_fields()['password'] write_only = password.write_only required = password.required allow_blank = password.allow_blank self.assertEqual(write_only, True) self.assertEqual(required, True) self.assertEqual(allow_blank, False) def test_projects_field(self): read_only = self.serializer.get_fields()['projects'].read_only self.assertEqual(read_only, True) def test_tasks_field(self): read_only = self.serializer.get_fields()['tasks'].read_only self.assertEqual(read_only, True) def test_create_method(self): new_user = UserSerializer().create(self.serializer_user_attributes) logged_in = self.client.login( username=self.serializer_user_attributes['username'], password=self.serializer_user_attributes['password']) self.assertEqual(new_user.username, self.serializer_user_attributes["username"]) self.assertEqual(new_user.email, self.serializer_user_attributes["email"]) self.assertEqual(new_user.has_usable_password(), True) self.assertEqual(logged_in, True) def test_update_method(self): new_attributes = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } new_user = UserSerializer().create(self.serializer_user_attributes) updated_user = UserSerializer().update(new_user, new_attributes) logged_in = self.client.login(username=new_attributes['username'], password=new_attributes['password']) self.assertEqual(updated_user.username, new_attributes['username']) self.assertEqual(updated_user.email, new_attributes['email']) self.assertEqual(updated_user.has_usable_password(), True) self.assertEqual(logged_in, True) def test_validation_with_not_unique_username(self): new_data = self.serializer_user_attributes new_data['username'] = self.user_attributes['username'] new_serializer_usename_lower = UserSerializer(data=new_data) self.assertEqual(new_serializer_usename_lower.is_valid(), False) new_data['username'] = self.user_attributes['username'].capitalize() new_serializer_usename_capitalize = UserSerializer(data=new_data) self.assertEqual(new_serializer_usename_capitalize.is_valid(), False) new_data['username'] = self.user_attributes['username'].upper() new_serializer_usename_upper = UserSerializer(data=new_data) self.assertEqual(new_serializer_usename_upper.is_valid(), False) def test_validation_without_username(self): new_data = self.serializer_user_attributes new_data.pop('username') new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_blank_username(self): new_data = self.serializer_user_attributes new_data['username'] = '' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_not_unique_email(self): new_data = self.serializer_user_attributes new_data['email'] = self.user_attributes['email'] new_serializer_lower = UserSerializer(data=new_data) self.assertEqual(new_serializer_lower.is_valid(), False) new_data['email'] = self.user_attributes['email'].capitalize() new_serializer_capitalize = UserSerializer(data=new_data) self.assertEqual(new_serializer_capitalize.is_valid(), False) new_data['email'] = self.user_attributes['email'].upper() new_serializer_upper = UserSerializer(data=new_data) self.assertEqual(new_serializer_upper.is_valid(), False) def test_validation_without_email(self): new_data = self.serializer_user_attributes new_data.pop('email') new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_blank_email(self): new_data = self.serializer_user_attributes new_data['email'] = '' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_invalid_email(self): new_data = self.serializer_user_attributes new_data['email'] = 'test' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_valid_password(self): new_data = self.serializer_user_attributes new_data['password'] = '******' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), True) def test_validation_without_password(self): new_data = self.serializer_user_attributes new_data.pop('password') new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_blank_password(self): new_data = self.serializer_user_attributes new_data['password'] = '' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False) def test_validation_with_invalid_password(self): new_data = self.serializer_user_attributes new_data['password'] = '******' new_serializer_with_short_passwd = UserSerializer(data=new_data) self.assertEqual(new_serializer_with_short_passwd.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_digit = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_digit.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_capital = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_capital.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_lowecase = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_lowecase.is_valid(), False)
def test_validation_with_invalid_password(self): new_data = self.serializer_user_attributes new_data['password'] = '******' new_serializer_with_short_passwd = UserSerializer(data=new_data) self.assertEqual(new_serializer_with_short_passwd.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_digit = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_digit.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_capital = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_capital.is_valid(), False) new_data['password'] = '******' new_serializer_passwd_without_lowecase = UserSerializer(data=new_data) self.assertEqual(new_serializer_passwd_without_lowecase.is_valid(), False)
def test_validation_with_blank_password(self): new_data = self.serializer_user_attributes new_data['password'] = '' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False)
def test_validation_without_password(self): new_data = self.serializer_user_attributes new_data.pop('password') new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False)
def test_validation_with_valid_password(self): new_data = self.serializer_user_attributes new_data['password'] = '******' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), True)
def test_validation_with_invalid_email(self): new_data = self.serializer_user_attributes new_data['email'] = 'test' new_serializer = UserSerializer(data=new_data) self.assertEqual(new_serializer.is_valid(), False)