def test_user_profile_form(self): """ Tests whether UserProfileForm is in the correct place, and whether the correct fields have been specified for it. """ self.assertTrue( 'UserProfileForm' in dir(forms), f"{FAILURE_HEADER}We couldn't find the UserProfileForm class in Rango's forms.py module. Did you create it in the right place?{FAILURE_FOOTER}" ) user_profile_form = forms.UserProfileForm() self.assertEqual( type(user_profile_form.__dict__['instance']), rango.models.UserProfile, f"{FAILURE_HEADER}Your UserProfileForm does not match up to the UserProfile model. Check your Meta definition of UserProfileForm and try again.{FAILURE_FOOTER}" ) fields = user_profile_form.fields expected_fields = { 'website': django_fields.URLField, 'picture': django_fields.ImageField, } for expected_field_name in expected_fields: expected_field = expected_fields[expected_field_name] self.assertTrue( expected_field_name in fields.keys(), f"{FAILURE_HEADER}The field {expected_field_name} was not found in the UserProfile form. Check you have complied with the specification, and try again.{FAILURE_FOOTER}" ) self.assertEqual( expected_field, type(fields[expected_field_name]), f"{FAILURE_HEADER}The field {expected_field_name} in UserProfileForm was not of the correct type. Expected {expected_field}; got {type(fields[expected_field_name])}.{FAILURE_FOOTER}" )
def test_good_form_creation(self): """ Tests the functionality of the forms. Creates a UserProfileForm and UserForm, and attempts to save them. Upon completion, we should be able to login with the details supplied. """ user_data = {'username': '******', 'password': '******', 'email': '*****@*****.**'} user_form = forms.UserForm(data=user_data) user_profile_data = {'website': 'http://www.bing.com', 'picture': tempfile.NamedTemporaryFile(suffix=".jpg").name} user_profile_form = forms.UserProfileForm(data=user_profile_data) self.assertTrue(user_form.is_valid(), f"{FAILURE_HEADER}The UserForm was not valid after entering the required data. Check your implementation of UserForm, and try again.{FAILURE_FOOTER}") self.assertTrue(user_profile_form.is_valid(), f"{FAILURE_HEADER}The UserProfileForm was not valid after entering the required data. Check your implementation of UserProfileForm, and try again.{FAILURE_FOOTER}") user_object = user_form.save() user_object.set_password(user_data['password']) user_object.save() user_profile_object = user_profile_form.save(commit=False) user_profile_object.user = user_object user_profile_object.save() self.assertEqual(len(User.objects.all()), 1, f"{FAILURE_HEADER}We were expecting to see a User object created, but it didn't appear. Check your UserForm implementation, and try again.{FAILURE_FOOTER}") self.assertEqual(len(rango.models.UserProfile.objects.all()), 1, f"{FAILURE_HEADER}We were expecting to see a UserProfile object created, but it didn't appear. Check your UserProfileForm implementation, and try again.{FAILURE_FOOTER}") self.assertTrue(self.client.login(username='******', password='******'), f"{FAILURE_HEADER}We couldn't log our sample user in during the tests. Please check your implementation of UserForm and UserProfileForm.{FAILURE_FOOTER}")
def test_working_created_form(self): """ Tests form functionality by creating a UserProfileForm and UserForm. If saved correctly, login should be possible with supplied info. """ user_data = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } user_form = forms.UserForm(data=user_data) user_profile_data = { 'website': 'http://www.bing.com', 'picture': tempfile.NamedTemporaryFile(suffix=".jpg").name } user_profile_form = forms.UserProfileForm(data=user_profile_data) self.assertTrue( user_form.is_valid(), f"{FAILURE_HEADER}Invalid UserForm even after entering the required data.{FAILURE_FOOTER}" ) self.assertTrue( user_profile_form.is_valid(), f"{FAILURE_HEADER}Invlaid UserProfileForm even after entering the required data.{FAILURE_FOOTER}" ) user_object = user_form.save() user_object.set_password(user_data['password']) user_object.save() user_profile_object = user_profile_form.save(commit=False) user_profile_object.user = user_object user_profile_object.save() self.assertEqual( len(User.objects.all()), 1, f"{FAILURE_HEADER}A User object should have been, but wasn't, created.{FAILURE_FOOTER}" ) self.assertEqual( len(rango.models.UserProfile.objects.all()), 1, f"{FAILURE_HEADER}A UserProfile object should have been, but wasn't, created.{FAILURE_FOOTER}" ) self.assertTrue( self.client.login(username='******', password='******'), f"{FAILURE_HEADER}Sample user login was unsuccessful during testing.{FAILURE_FOOTER}" )