In my model backend I usually call an external service which returns me an 
user that is saved and returned. When I don't have credentials I want to 
mock the backend for some test cases. In the setup of each test case I am 
calling the login method where CRED contains the real credentials or mock 
credentials. This can be determined by are_credentials_given.

def are_credentials_given():
    return not os.path.isfile(os.path.join(BASE_DIR, 'settings/credentials'))


def login(self):
    u = User.objects.create(id=CRED['ID'], is_staff=True)
    u.save()
    token, c = Token.objects.get_or_create(user=u)
    if c:
        token.save()
    self.client = APIClient()
    self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
    self.client.login(username=CRED['USERNAME'], password=CRED['PASSWORD'])

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/894d8e22-b448-41fc-bd1b-a82a2a35db8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to