-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Moved test Client implementation to utils inside user_sessions so i…
…t is available to users installing the egg only * Changed comments format to suit the project's requirements
- Loading branch information
Horacio Duran
committed
Jan 7, 2014
1 parent
6789d7d
commit 10f3f13
Showing
3 changed files
with
58 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from django.conf import settings | ||
from django.contrib.auth import authenticate, login | ||
from django.http import HttpRequest | ||
from django.test import Client as BaseClient | ||
|
||
from user_sessions.backends.db import SessionStore | ||
|
||
|
||
class Client(BaseClient): | ||
""" | ||
Custom implementation of django.test.Client. | ||
It is required to perform tests that require to login in sites using | ||
django-user-sessions since its implementation of SessionStore has to | ||
required parameters which is not in concordance with what is expected | ||
from the original Client | ||
""" | ||
def login(self, **credentials): | ||
""" | ||
Sets the Factory to appear as if it has successfully logged into a site. | ||
Returns True if login is possible; False if the provided credentials | ||
are incorrect, or the user is inactive, or if the sessions framework is | ||
not available. | ||
""" | ||
user = authenticate(**credentials) | ||
if user and user.is_active: | ||
# Create a fake request to store login details. | ||
request = HttpRequest() | ||
if self.session: | ||
request.session = self.session | ||
else: | ||
request.session = SessionStore('Python/2.7', '127.0.0.1') | ||
login(request, user) | ||
|
||
# Save the session values. | ||
request.session.save() | ||
|
||
# Set the cookie to represent the session. | ||
session_cookie = settings.SESSION_COOKIE_NAME | ||
self.cookies[session_cookie] = request.session.session_key | ||
cookie_data = { | ||
'max-age': None, | ||
'path': '/', | ||
'domain': settings.SESSION_COOKIE_DOMAIN, | ||
'secure': settings.SESSION_COOKIE_SECURE or None, | ||
'expires': None, | ||
} | ||
self.cookies[session_cookie].update(cookie_data) | ||
|
||
return True | ||
else: | ||
return False |