![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0340.png)
def
handle
(
self
,
,
*
_
,
**
__
):
session_key
=
create_pre_authenticated_session
(
)
self
.
stdout
.
write
(
session_key
)
def
create_pre_authenticated_session
(
):
user
=
User
.
objects
.
create
(
=
)
session
=
SessionStore
()
session
[
SESSION_KEY
]
=
user
.
pk
session
[
BACKEND_SESSION_KEY
]
=
settings
.
AUTHENTICATION_BACKENDS
[
0
]
session
.
save
()
return
session
.
session_key
We’ve taken the code for
create_pre_authenticated_session
code from
test_my_lists.py
.
handle
will pick up an email address as the first command-line argu‐
ment, and then return the session key that we’ll want to add to our browser cookies, and
the management command prints it out at the command line. Try it out:
$
python3 manage.py create_session
a@b.comUnknown command: 'create_session'
One more step: we need to add
functional_tests
to our
settings.py
for it to recognise
it as a real app that might have management commands as well as tests:
superlists/settings.py.
+++
b
/
superlists
/
settings
.
py
@@
-
42
,
6
+
42
,
7
@@
INSTALLED_APPS
=
(
'lists'
,
'south'
,
'accounts'
,
+
'functional_tests'
,
)
Now it works:
$
python3 manage.py create_session
a@b.comqnslckvp2aga7tm6xuivyb0ob1akzzwl
Getting the FT to Run the Management Command on the Server
Next we need to adjust
test_my_lists
so that it runs the local function when we’re on
the local server, and make it run the management command on the staging server if
we’re on that:
functional_tests/test_my_lists.py (ch17l016).
from
django.conf
import
settings
from
.base
import
FunctionalTest
from
.server_tools
import
create_session_on_server
from
.management.commands.create_session
import
create_pre_authenticated_session
class
MyListsTest
(
FunctionalTest
):
312
|
Chapter 17: Test Fixtures, Logging, and Server-Side Debugging