I've scoured stackoverflow and tried several different solutions, but my test code always goes to the login redirect.
This is what I have:
class LoggedInSetup(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username="lolwutboi", password="whatisthepassword")
self.client.login(username='lolwutboi', password='whatisthepassword')
number_of_submissions = 13
for sub in range(number_of_submissions):
Submission.objects.create(title='Amazing', description='This is the body of the post',
author=str(self.user), pub_date=timezone.now())
class LoggedInTestCases(LoggedInSetup):
def test_logged_in_post_reachable(self):
self.client.login(username='lolwutboi', password='whatisthepassword')
resp = self.client.get(reverse('submission_post'))
self.assertEqual(resp.status_code, 200)
You can't set the password like that - the password stored in the database is a hashed value and you are setting it to the password string itself. This means the validation will fail.
You would need to set the password like so:
self.user = User.objects.create(username="lolwutboi")
self.user.set_password("whatisthepassword")
self.user.save()
However, since all you really need is to log in the test user, use force_login
in the test:
self.client.force_login(self.user)
Which will log in the user without you having to worry about passwords.