Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:53:14+00:00 2026-05-23T16:53:14+00:00

This is my view that I want to be tested. def logIn(request): This method

  • 0

This is my view that I want to be tested.

def logIn(request):
    """
    This method will log in user using  username or email
    """
    if request.method == 'POST':
        form = LogInForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['name'],password=form.cleaned_data['password'])
            if user:
                login(request,user)
                return redirect('uindex')
            else:
                error = "Nie prawidlowy login lub haslo.Upewnij sie ze wpisales prawidlowe dane"
    else:
        form = LogInForm(auto_id=False)
    return render_to_response('login.html',locals(),context_instance=RequestContext(request))

And here’s the test

class LoginTest(unittest.TestCase):
    def setUp(self):
        self.client  = Client()
    def test_response_for_get(self):
        response =  self.client.get(reverse('logIn'))
        self.assertEqual(response.status_code, 200)
    def test_login_with_username(self):
        """
        Test if user can login wit username and password
        """
        user_name = 'test'
        user_email = 'test@test.com'
        user_password = 'zaq12wsx'
        u =  User.objects.create_user(user_name,user_email,user_password)
        response = self.client.post(reverse('logIn'),data={'name':user_name,'password':user_password},follow=True)
        self.assertEquals(response.request.user.username,user_name)
        u.delete()

And when i run this test i got failure on test_login_with_username:

AttributeError: 'dict' object has no attribute 'user'

When i use in views request.user.username in works fine no error this just fails in tests. Thanks in advance for any help
edit:Ok I replace the broken part with

self.assertEquals(302, response.status_code)

But now this test breaks and another one too.

AssertionError: 302 != 200

Here is my code for the view that now fail. I want email and username to be unique.

def register(request):
    """
    Function to register new user.
    This function will have to care for email uniqueness,and login
    """
    if request.method == 'POST':
        error=[]
        form = RegisterForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            if  form.cleaned_data['password'] ==  form.cleaned_data['password_confirmation']:
                password = form.cleaned_data['password']
                if len(User.objects.filter(username=username)) == 0 and len(User.objects.filter(email=email)) == 0:
                    #email and username are bouth unique
                    u = User()
                    u.username = username
                    u.set_password(password)
                    u.email = email
                    u.is_active = False
                    u.is_superuser = False
                    u.is_active = True
                    u.save()
                    return render_to_response('success_register.html',locals(),context_instance=RequestContext(request))
                else:
                    if len(User.objects.filter(username=username)) > 0:
                        error.append("Podany login jest juz zajety")
                    if len(User.objects.filter(email=email)) > 0:
                        error.append("Podany email jest juz zajety")
            else:
                error.append("Hasla nie pasuja do siebie")
        #return render_to_response('register.html',locals(),context_instance=RequestContext(request))
    else:
        form = RegisterForm(auto_id=False)
    return render_to_response('register.html',locals(),context_instance=RequestContext(request))

And here is the test that priviously work but now it is broken

def test_user_register_with_unique_data_and_permission(self):
        """
        Will try to register user which provided for sure unique credentials
        And also make sure that profile will be automatically created for him, and also that he he have valid privileges
        """
        user_name = 'test'
        user_email = 'test@test.com'
        password = 'zaq12wsx'
        response = self.client.post(reverse('register'),{'username': user_name,'email':user_email,
        'password':password,'password_confirmation':password},follow=True)
        #check if code is 200
        self.assertEqual(response.status_code, 200)
        u = User.objects.get(username=user_name,email = user_email)
        self.assertTrue(u,"User after creation coudn't be fetched")
        self.assertFalse(u.is_staff,msg="User after registration belong to staff")
        self.assertFalse(u.is_superuser,msg="User after registration is superuser")
        p = UserProfile.objects.get(user__username__iexact = user_name)
        self.assertTrue(p,"After user creation coudn't fetch user profile")
        self.assertEqual(len(response.context['error']),0,msg = 'We shoudnt get error during valid registration')
        u.delete()
        p.delete()

End here is the error:

AssertionError: We shoudnt get error during valid registration

If i disable login test everything is ok. How this test can break another one? And why login test is not passing. I try it on website and it works fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:53:15+00:00Added an answer on May 23, 2026 at 4:53 pm

    The documentation for the response object returned by the test client says this about the request attribute:

    request

    The request data that stimulated the response.

    That suggests to me one of two things. Either it’s just the data of the request, or it’s request object as it was before you handled the request. In either case, you would not expect it to contain the logged in user.

    Another way to write your test that the login completed successfully would be to add follow=False to the client.post call and check the response code:

    self.assertEquals(302, response.status_code)
    

    This checks that the redirect has occurred.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute this script (view source) that uses Google Translate AJAX API
I have a view user control that can post form. This control can be
I got a view that inherits : System.Web.Mvc.ViewPage<IEnumerable<MyProjects.Models.MyAccountWrapper>> In this view I list data
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I have a View that renders something like this: Item 1 and Item 2
https://search.twitter.com/search.json?q=doug How do I read this like VIEW SOURCE, so that I know what
The offending command that msi executes is: .\devenv.com /command View.Toolbox /setup This fails with
I have this layout that works correctly, a relative layout with a text view
I have a Pages table that stores all my view urls and this table
I created a new View (LogView) in Infrastructure.Module project. This view will be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.