1

I'm testing my views and would like to simulate logged in user with certain permissions.

I know that in django-webtest I can simulate logged in user like this:

self.app.get(reverse('profile'), user='bob')

Can i also pass in a group for this user? Or a list of permissions? If i will create a user in setUp and assign a group to him, can i simulate him logged in by passing his username like this:

self.app.get(reverse('profile'), user='user_with_certain_permissions')

or do i need to log him in first?

Neara
  • 3,693
  • 7
  • 29
  • 40

1 Answers1

1

Yes, this should work. You may also pass User instance instead of username:

self.app.get(reverse("profile"), self.user_with_certain_permissions)
Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
  • Thank you. What got me confused a bit is that 'bob' was not created in setUp. There is no User with such name, and still it worked. It got me wondering if I can simulate a user with permissions same way. – Neara Nov 12 '12 at 06:38
  • The example in django-webtest docs assumes the user came from fixtures (``fixtures = ['users', 'blog_posts']``). – Mikhail Korobov Nov 12 '12 at 10:51
  • Well, that's the thing. In my case there is no user 'bob', and still it worked. I ran a test to get a page that has login_required decorator, and it worked. I ran same test with no user supplied and indeed i was redirected to login page. I also know i don't have any other user logged in, b.c this is a test in its own, so tear down was executed and new setup is in order. Unless django-webtest loads its own fixtures... – Neara Nov 12 '12 at 14:52
  • 2
    django-webtest uses a subclass of django's RemoteUserBackend auth backend, and RemoteUserBackend creates User instances for unknown users by default. This is counter-intuitive indeed. – Mikhail Korobov Nov 12 '12 at 18:26
  • oh, i see. This certainly explains this behavior i was getting. Thank you :) – Neara Nov 13 '12 at 07:45