How to test a new user activation in Django?

680 views Asked by At

I am trying to test django.contrib.auth-based user signup view with django-nose, where an activation link is being sent to a new user:

def signup(request):
    if request.method == 'POST':
        user_form = SignUpForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.is_active = False
            user.save()

            user.profile.user_type = profile_form['user_type'].data
            user.save()

            current_site = get_current_site(request)
            subject = 'activation email'
            message = render_to_string('registration/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
    else:
        user_form = SignUpForm()
        profile_form = ProfileForm()
    return render(request, 'registration/signup.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

Currently I use Django built-in email back-end, so that activation email is being sent to the server terminal.

I want to test the activation view which requires uid and token. Is there any way to access the email sent to the user? Is there any other way to test that?

Regenerating token in the test does not work, because hash value is generated using timestamp.

0

There are 0 answers