def
create_pre_authenticated_session
(
self
,
):
if
self
.
against_staging
:
session_key
=
create_session_on_server
(
self
.
server_host
,
)
else
:
session_key
=
create_pre_authenticated_session
(
)
## to set a cookie we need to first visit the domain.
## 404 pages load the quickest!
self
.
browser
.
get
(
self
.
server_url
+
"/404_no_such_url/"
)
self
.
browser
.
add_cookie
(
dict
(
name
=
settings
.
SESSION_COOKIE_NAME
,
value
=
session_key
,
path
=
'/'
,
))
[
...
]
Let’s see how we know whether or not we’re working against the staging server.
self.against_staging
gets populated in
base.py
:
functional_tests/base.py (ch17l017).
from
.server_tools
import
reset_database
#
class
FunctionalTest
(
StaticLiveServerCase
):
@classmethod
def
setUpClass
(
cls
):
for
arg
in
sys
.
argv
:
if
'liveserver'
in
arg
:
cls
.
server_host
=
arg
.
split
(
'='
)[
1
]
#
cls
.
server_url
=
'http://'
+
cls
.
server_host
cls
.
against_staging
=
True
#
return
super
()
.
setUpClass
()
cls
.
against_staging
=
False
cls
.
server_url
=
cls
.
live_server_url
@classmethod
def
tearDownClass
(
cls
):
if
not
cls
.
against_staging
:
super
()
.
tearDownClass
()
def
setUp
(
self
):
if
self
.
against_staging
:
reset_database
(
self
.
server_host
)
#
self
.
browser
=
webdriver
.
Firefox
()
self
.
browser
.
implicitly_wait
(
3
)
Instead of just storing
cls.server_url
, we also store the
server_host
and
against_staging
attributes if we detect the
liveserver
command-line
argument.
Managing the Test Database on Staging
|
313