0

I'm testing a form that I wrote up earlier. For some reason, the test won't pass. It's like the form is ignoring the data I pass it, and I don't see why. The traceback tells me that the user variable in the clean method of the form is None, though a User is definitely passed into the form. The traceback:

... in clean if user.pk is not userwebsite.user.pk: AttributeError: 'NoneType' object has no attribute 'pk'

The Form:

class CreateAuditForm(forms.Form):
    user = forms.ModelChoiceField(queryset=User.objects.all(), widget=HiddenInput)
    website = forms.ModelChoiceField(queryset=UserWebsite.objects.all(), widget=HiddenInput)
    emails = forms.CharField(
        max_length=250,
        required=False
    )

    def clean_user(self):
        user = self.cleaned_data.get('user', None)
        if not user.groups.filter(name__iexact='subscribed').exists() and not user.groups.filter(name__iexact='addon').exists():
            raise forms.ValidationError(_("You must have an active subscription to request \
website audits. Please try again after subscribing to us."))
        return user

    def clean(self):
        data = self.cleaned_data
        user = data.get('user')
        userwebsite = data.get('website', None)

        if userwebsite.user:
            if user.pk is not userwebsite.user.pk:
                raise forms.ValidationError(_("Sorry, try again."))
        elif userwebsite.addon:
            if user.pk is not userwebsite.addon.pk:
                raise forms.ValidationError(_("Sorry, try again."))
        return self.cleaned_data

    def save(self):
        # Action

The Test:

class CreateAuditFormTestCase(TestCase):

    def setUp(self):
        super(CreateAuditFormTestCase, self).setUp()
        self.form = CreateAuditForm
        ...
        self.website = Website.objects.create(
            title="permanence",
            url="https://www.badabuyhere.com",
            display="www.bababuyhere.com")
        self.unsubscriber = User.objects.create(
            username="adiagojesse",
            first_name="adiago",
            last_name="jesse",
            email="bannerfare@coldmount.com",
            password="tigermountainvalley"
        )
        self.unsubscriberwebsite = UserWebsite.objects.create(
            user=self.unsubscriber,
            website=self.website,
            is_competitor=False
        )

    ...
    def test_user_validation(self):
        data = {
            "user":self.unsubscriber.pk,
            "website":self.unsubscriberwebsite.pk,
            "emails":"john@gmail.com, jeff@gmail.com"
        }
        self.assertTrue(self.unsubscriber)
        self.assertTrue(self.unsubscriberwebsite)
        audit = self.form(data)
        self.assertEqual(audit.is_valid(), False)

This is probably a simple issue that I can't pick up on, which is what's frustrating me, lol. Help would be appreciated.

user3084860
  • 421
  • 5
  • 19

1 Answers1

1

My guess is that in CreateAuditForm.clean, useris None because clean_user raised a ValidationError. The ValidationError comes from the fact that the user does not have the groups he needs.

Another issue I see is that to test equality between model instances in Django, you should not use primary keys but test using the instances directly, using == and not is. (See https://stackoverflow.com/a/13650309/1644198 for more information on is and ==)

Example:

if user != userwebsite.user:
    # etc...
Community
  • 1
  • 1
aumo
  • 5,344
  • 22
  • 25
  • Hmm, thats the thing, I want to test that the clean_user successfully raises the ValidationError. Thank you, though, I'm gonna go make some changes right now and see what I can do. – user3084860 Apr 26 '15 at 16:29
  • And you were right, of course. The user did not survive the clean_user step. So I just added a conditional to test if the user and the userwebsite objects survived in the clean() method. Once I did that, the tests passed. – user3084860 Apr 26 '15 at 16:51