micgeronimo
micgeronimo

Reputation: 2149

Can not log in with unit test in django-allauth

I've tried to test my views with unittest like this

class TestClassroomView(TestCase):

def test_classroom_view(self):
    auth_headers = {'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode('[email protected]:test')}
    response = self.client.get('/classroom/3/', follow=True, **auth_headers)
    self.assertContains(response, 'Course progress')
    self.assertEqual(response.status_code, 200)

And this:

class TestClassroomView(TestCase):
 def test_classroom(self):
    self.client.login(username='[email protected]', password='test')
    response = self.client.get('/classroom/3/', follow=True)
    self.assertEqual(response.status_code, 200)
    self.assertTemplateUsed(response, 'web/dashboard.html')

response.redirect_chain always shows this

[('http://testserver/?next=/classroom/3/', 302)]

If I leave only status_code checks then tests passing, but actually they test nothing. I'm using django-allauth authentication system Please help me figure out what am I doing wrong.

Also how can I load fixtures on test start?

Upvotes: 1

Views: 1099

Answers (1)

micgeronimo
micgeronimo

Reputation: 2149

That is what helped me with authentication

from django.contrib.auth import get_user_model 

class TestParent(TestCase):

  def setUp(self):
    username = 'testuser'
    password = 'testpass'
    User = get_user_model()
    user = User.objects.create_user(username, password=password)
    logged_in = self.client.login(username=username, password=password)
    self.assertTrue(logged_in)

Upvotes: 2

Related Questions