def old_auth_test(self, request):
        # Create new session
        auth_request = self.factory.post('/sessions/')
        AuthRouteTestingWithKwargs.add_messages_middleware(auth_request)
        auth_response = sessions.sessions_index(auth_request,
                                                email="*****@*****.**",
                                                password="******",
                                                path=self.route)
        # Make sure initial redirect was called (but not followed)
        self.assertEqual(auth_response.status_code, 302)

        # Generate a new token for the user, the old token has expired and is not valid
        self.mixin_model.generate_token()
        self.mixin_model.save()

        # Add the old auth token cookie to the request
        request.COOKIES['auth_token'] = auth_response.cookies[
            'auth_token'].value
        response = self.view(request, **self.kwargs)

        # We should be redirected
        self.assertEqual(response.status_code, 302)

        # Make a new request to the location of the redirect
        redirect_response = self.client.get(response['LOCATION'], follow=True)
        # Check to make sure the response is correct
        self.assertContains(redirect_response, self.not_logged_in_message)
 def test_bad_email(self):
     request = self.factory.post('/sessions/')
     setattr(request, 'session', 'session')
     messages = FallbackStorage(request)
     setattr(request, '_messages', messages)
     response = sessions.sessions_index(request, email="*****@*****.**",
                                        password="******", path="/dashboard")
     self.assertEqual(response.status_code, 302)
     self.assertEqual(response['message'], "Email incorrect!")
    def bad_email_test(self, request):
        # Create new session
        auth_request = self.factory.post('/sessions/')
        AuthRouteTestingWithKwargs.add_messages_middleware(auth_request)
        auth_response = sessions.sessions_index(auth_request,
                                                email="*****@*****.**",
                                                password="******",
                                                path=self.route)

        # Make sure new session not created
        self.assertEqual(auth_response.status_code, 302)
        self.assertEqual(auth_response["message"], "Email incorrect!")

        # Error should be raised as no cookie should be set (post to create session failed)
        with self.assertRaises(KeyError) as error:
            request.COOKIES['auth_token'] = auth_response.cookies[
                'auth_token'].value

        self.assertTrue('auth_token' in error.exception)
    def good_auth_test(self, request):
        # Create new session
        auth_request = self.factory.post('/sessions/')
        AuthRouteTestingWithKwargs.add_messages_middleware(auth_request)

        auth_response = sessions.sessions_index(auth_request,
                                                email="*****@*****.**",
                                                password="******",
                                                path=self.route)

        # Make sure redirect was called (but not followed)
        self.assertEqual(auth_response.status_code, 302)
        # Add auth token cookie to request
        request.COOKIES['auth_token'] = auth_response.cookies[
            'auth_token'].value
        response = self.view(request, **self.kwargs)
        # Make sure request contains expected content
        self.assertContains(response,
                            self.expected_response_content,
                            status_code=200)
    def bad_password_test(self, request):
        """
        Test to make sure a bad password does not create a new session
        :param request: is the request to be completed
        :return: None
        """
        # Create new session
        auth_request = self.factory.post('/sessions/')
        AuthRouteTestingWithKwargs.add_messages_middleware(auth_request)
        auth_response = sessions.sessions_index(auth_request,
                                                email="*****@*****.**",
                                                password="******",
                                                path="/dashboard")

        # Make sure new session not created
        self.assertEqual(auth_response.status_code, 302)
        self.assertEqual(auth_response['message'], "Password incorrect!")

        # Error should be raised as no cookie should be set (post to create session failed)
        with self.assertRaises(KeyError) as error:
            request.COOKIES['auth_token'] = auth_response.cookies[
                'auth_token'].value

        self.assertTrue('auth_token' in error.exception)