Django testing in creating a user with email address which is from session input, assertRedirects is not working

Gene9y

The user creation is using an email address as USERNAME_FIELD and it is extracted from session and save in the form save(). It seems it is not going further down to the redirection. How can I test the redirection in this case?

tests.py:

class RegistraionViewTest(TestCase):

    valid_data = {
        'email': '[email protected]',
        'password1': 'test1234', 
    }

    kwargs = {
        'email': '[email protected]'
    }

    def test_registration(self):
        response = self.client.post(reverse('registration'), data=self.valid_data, follow=True)
        self.assertTrue(response.context['form'].is_valid())

        # mocking the session input
        response.context['form'].save(email=self.kwargs['email'])
        self.assertTrue(account.check_password(self.valid_data['password1']))

        # working so far, but it seems there is no redirect url in response
        self.assertRedirects(response, reverse('next_url'))

In views.py:

   if request.method == 'POST':
    form = RegistraionForm(request.POST)

    if form.is_valid():   
        email = request.session.get('email') 
        try: 
            account = form.save(email=email)
            return HttpResponseRedirect('next_url'))

In forms.py:

def save(self, **kwargs):
    user = super(RegistrationForm, self).save(commit=False)
    user.email = kwargs.pop('email')
    user.save()
    return user

It seems there is no url in the response in tests.py. What went wrong here?

Nikolaj Baer

Your response may be a 500, not a 302, which would mean there is no Location header.

The call for request.session.get('email') will likely throw a KeyError, as your test does not appear to set the session['email'] field, and there is no default.

Note that when using a session in a test case, you need to assign it to a variable in the beginning, as in the example below (from Django Testing Tool docs):

def test_registration(self):
    session = self.client.session
    session['email'] = self.kwargs['email']
    session.save()
    # and now make your call to self.client.post
    response = self.client.post(...)
    self.assertEqual(response.status_code,302)
    # .. and the rest

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Find user from email address

分類Dev

User email from GitHub in django social login

分類Dev

How to validate email address from a mat input?(Angular Material)

分類Dev

Wordpress Login not working with email address

分類Dev

Load data from CSV file for load testing and keep session for each user

分類Dev

Creating temp GIT repo for testing is not working

分類Dev

Testing user input against dictionary file(JSON)

分類Dev

how to fetch email address from oracle database

分類Dev

Unable to retrieve email address from Facebook API

分類Dev

"AND $_SESSION['user_id'] = posted_by" not working

分類Dev

Creating an alert box with Hello + user input

分類Dev

Squares from user input

分類Dev

How to get user email address using FBSDK in swift iOS for iPhone?

分類Dev

How to send notification automatically to logged in user secondary email address in php?

分類Dev

Email django from using EmailMultiAlternatives

分類Dev

Using Django to send Email to a User at a scheduled time

分類Dev

Django User Model Inheritance not working

分類Dev

Django Rest Framework angularjs session auth user

分類Dev

How to get User session in Login handling in Django?

分類Dev

prevent django session from expiring

分類Dev

Function for testing system stability, which receives predicted time series as input

分類Dev

Get email from username of user in Phabricator

分類Dev

How to get current user address from firebase

分類Dev

How to save user input data from multiple forms (in a single template/web page) to the sqlite database in django?

分類Dev

How to get Email address from outlook contacts for the names listed in a column?

分類Dev

how to pass email address from checkout form to charge page with Stripe

分類Dev

Automated email from dummy address through iOS app

分類Dev

Tidymodels: Creating an rsplit object from training and testing data

分類Dev

Which paypal SDK/API to use to send credit card payments to email address?

Related 関連記事

  1. 1

    Find user from email address

  2. 2

    User email from GitHub in django social login

  3. 3

    How to validate email address from a mat input?(Angular Material)

  4. 4

    Wordpress Login not working with email address

  5. 5

    Load data from CSV file for load testing and keep session for each user

  6. 6

    Creating temp GIT repo for testing is not working

  7. 7

    Testing user input against dictionary file(JSON)

  8. 8

    how to fetch email address from oracle database

  9. 9

    Unable to retrieve email address from Facebook API

  10. 10

    "AND $_SESSION['user_id'] = posted_by" not working

  11. 11

    Creating an alert box with Hello + user input

  12. 12

    Squares from user input

  13. 13

    How to get user email address using FBSDK in swift iOS for iPhone?

  14. 14

    How to send notification automatically to logged in user secondary email address in php?

  15. 15

    Email django from using EmailMultiAlternatives

  16. 16

    Using Django to send Email to a User at a scheduled time

  17. 17

    Django User Model Inheritance not working

  18. 18

    Django Rest Framework angularjs session auth user

  19. 19

    How to get User session in Login handling in Django?

  20. 20

    prevent django session from expiring

  21. 21

    Function for testing system stability, which receives predicted time series as input

  22. 22

    Get email from username of user in Phabricator

  23. 23

    How to get current user address from firebase

  24. 24

    How to save user input data from multiple forms (in a single template/web page) to the sqlite database in django?

  25. 25

    How to get Email address from outlook contacts for the names listed in a column?

  26. 26

    how to pass email address from checkout form to charge page with Stripe

  27. 27

    Automated email from dummy address through iOS app

  28. 28

    Tidymodels: Creating an rsplit object from training and testing data

  29. 29

    Which paypal SDK/API to use to send credit card payments to email address?

ホットタグ

アーカイブ