macropin / django-registration

Django-registration (redux) provides user registration functionality for Django websites.
http://django-registration-redux.readthedocs.org
Other
974 stars 350 forks source link

Testing activation email fails #299

Closed surfer190 closed 6 years ago

surfer190 commented 6 years ago

I am testing that the activation email sends with:

    def test_email_sent_to_activate_user(self):
        self.assertEqual(len(mail.outbox), 0)
        response = self.app.get(
            reverse('users:update', args=(self.user.id, )),
            user=self.manager
        )
        form = response.form
        form['first_name'] = 'Test'
        form['last_name'] = 'User'
        form['email'] = 'mytest@test.com'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        response = response.follow()

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(
            mail.outbox[0].subject, 
            'Complete Registration for mysite.co.za'
        )

My user register function:

    def register(self, form):
        '''Create the user with no password and inactive
        Issue the signal to send the activation email
        '''
        site = get_current_site(self.request)

        email = form.cleaned_data['email']
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        workday_hours = form.cleaned_data['workday_hours']
        role = form.cleaned_data['role']
        employee_type = form.cleaned_data['employee_type']

        new_user_instance = get_user_model().objects.create_user(
            email=email,
            first_name=first_name,
            last_name=last_name,
            workday_hours=workday_hours,
            role=role,
            employee_type=employee_type,
            password=None,
            is_active=False
        )

        new_user = self.registration_profile.objects.create_inactive_user(
            new_user=new_user_instance,
            site=site,
            send_email=self.SEND_ACTIVATION_EMAIL,
            request=self.request,
        )

        user_registered_signal.send(
            sender=self.__class__,
            user=new_user,
            request=self.request
        )

        # Add a user created success message
        messages.success(
            self.request, 
            'The User has been successfully registered.'
            'and will receive an email to activate and create a password'
        )

        return new_user

The test fails with:

FAIL: test_email_sent_to_activate_user (users.tests.test_views.UserCreateTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/project/users/tests/test_views.py", line 515, in test_email_sent_to_activate_user
    self.assertEqual(len(mail.outbox), 1)
AssertionError: 0 != 1
surfer190 commented 6 years ago

I was stupid, set the wrong url/form.