Beispiel #1
0
    def test_user_form(self):
        """
        Tests whether UserForm is in the correct place, and whether the correct fields have been specified for it.
        """
        self.assertTrue(
            'UserForm' in dir(forms),
            f"{FAILURE_HEADER}We couldn't find the UserForm class in Rango's forms.py module. Did you create it in the right place?{FAILURE_FOOTER}"
        )

        user_form = forms.UserForm()
        self.assertEqual(
            type(user_form.__dict__['instance']), User,
            f"{FAILURE_HEADER}Your UserForm does not match up to the User model. Check your Meta definition of UserForm and try again.{FAILURE_FOOTER}"
        )

        fields = user_form.fields

        expected_fields = {
            'username': django_fields.CharField,
            'email': django_fields.EmailField,
            'password': django_fields.CharField,
        }

        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 UserForm 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 UserForm was not of the correct type. Expected {expected_field}; got {type(fields[expected_field_name])}.{FAILURE_FOOTER}"
            )
Beispiel #2
0
    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}")
Beispiel #3
0
    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}"
        )