@@ -41,14 +41,14 @@ PLAYBOOK: ansible-provision.yaml ***********************************************
41
41
42
42
Here's what happens when we run the functional tests:
43
43
44
- [role="small-code"]
44
+ [role="against-server small-code"]
45
45
[subs="specialcharacters,macros"]
46
46
----
47
47
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests*]
48
48
49
49
======================================================================
50
- ERROR: test_logged_in_users_lists_are_saved_as_my_lists
51
- (functional_tests.test_my_lists. MyListsTest)
50
+ ERROR: test_logged_in_users_lists_are_saved_as_my_lists (functional_tests.test_
51
+ my_lists. MyListsTest.test_logged_in_users_lists_are_saved_as_my_lists )
52
52
---------------------------------------------------------------------
53
53
Traceback (most recent call last):
54
54
File "...goat-book/functional_tests/test_my_lists.py", line 34, in
@@ -100,7 +100,7 @@ First, make sure your 'settings.py' still contains the `LOGGING`
100
100
settings which will actually send stuff to the console:
101
101
102
102
[role="sourcecode currentcontents"]
103
- .superlists/settings.py
103
+ .src/ superlists/settings.py
104
104
====
105
105
[source,python]
106
106
----
@@ -173,7 +173,7 @@ elspeth@server:$ *docker restart superlists*
173
173
174
174
Now if we rerun our FTs, we see a change:
175
175
176
- [role="small-code"]
176
+ [role="against-server small-code"]
177
177
[subs="specialcharacters,macros"]
178
178
----
179
179
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests*]
@@ -260,6 +260,7 @@ import time
260
260
print("getting msg", i)
261
261
_, lines, __ = inbox.retr(i)
262
262
lines = [l.decode("utf8") for l in lines]
263
+ print(lines)
263
264
if f"Subject: {subject}" in lines:
264
265
email_id = i
265
266
body = "\n".join(lines)
@@ -384,7 +385,7 @@ And, believe it or not, that'll actually work, and give us an FT
384
385
that can actually check for logins that work, involving real emails!
385
386
386
387
387
- [role="small-code"]
388
+ [role="against-server small-code"]
388
389
[subs="specialcharacters,macros"]
389
390
----
390
391
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests.test_login*]
@@ -461,30 +462,31 @@ $ <strong>mkdir -p src/functional_tests/management/commands</strong>
461
462
$ <strong>touch src/functional_tests/management/__init__.py</strong>
462
463
$ <strong>touch src/functional_tests/management/commands/__init__.py</strong>
463
464
----
465
+ //ch21l012-1
464
466
465
467
The boilerplate in a management command is a class that inherits from
466
468
`django.core.management.BaseCommand`, and that defines a method called
467
469
`handle`:
468
470
469
471
[role="sourcecode"]
470
- .src/functional_tests/management/commands/create_session.py
472
+ .src/functional_tests/management/commands/create_session.py (ch21l012)
471
473
====
472
474
[source,python]
473
475
----
474
476
from django.conf import settings
475
477
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
476
- User = get_user_model()
477
478
from django.contrib.sessions.backends.db import SessionStore
478
479
from django.core.management.base import BaseCommand
479
480
481
+ User = get_user_model()
480
482
481
- class Command(BaseCommand):
482
483
484
+ class Command(BaseCommand):
483
485
def add_arguments(self, parser):
484
- parser.add_argument(' email' )
486
+ parser.add_argument(" email" )
485
487
486
488
def handle(self, *args, **options):
487
- session_key = create_pre_authenticated_session(options[' email' ])
489
+ session_key = create_pre_authenticated_session(options[" email" ])
488
490
self.stdout.write(session_key)
489
491
490
492
@@ -497,35 +499,33 @@ def create_pre_authenticated_session(email):
497
499
return session.session_key
498
500
----
499
501
====
500
- //12
501
502
502
503
We've taken the code for `create_pre_authenticated_session` from
503
504
'test_my_lists.py'. `handle` will pick up an email address from the parser,
504
505
and then return the session key that we'll want to add to our browser cookies,
505
506
and the management command prints it out at the command line. Try it out:
506
507
507
- //IDEA: test commands that have return code
508
- [role="skipme"]
508
+ [role="ignore-errors"]
509
509
[subs="specialcharacters,macros"]
510
510
----
511
511
$ pass:quotes[*python src/manage.py create_session [email protected] *]
512
- Unknown command: 'create_session'
512
+ Unknown command: 'create_session'. Did you mean clearsessions?
513
513
----
514
514
515
515
One more step: we need to add `functional_tests` to our 'settings.py'
516
516
for it to recognise it as a real app that might have management commands as
517
517
well as tests:
518
518
519
519
[role="sourcecode"]
520
- .superlists/settings.py
520
+ .src/ superlists/settings.py (ch21l014)
521
521
====
522
522
[source,python]
523
523
----
524
524
+++ b/superlists/settings.py
525
525
@@ -42,6 +42,7 @@ INSTALLED_APPS = [
526
- ' lists' ,
527
- ' accounts' ,
528
- + ' functional_tests' ,
526
+ " lists" ,
527
+ " accounts" ,
528
+ + " functional_tests" ,
529
529
]
530
530
----
531
531
====
@@ -557,25 +557,29 @@ and make it run the management command on the staging server if we're on that:
557
557
[source,python]
558
558
----
559
559
from django.conf import settings
560
+
560
561
from .base import FunctionalTest
561
- from .server_tools import create_session_on_server
562
562
from .management.commands.create_session import create_pre_authenticated_session
563
+ from .server_tools import create_session_on_server
563
564
564
- class MyListsTest(FunctionalTest):
565
565
566
+ class MyListsTest(FunctionalTest):
566
567
def create_pre_authenticated_session(self, email):
567
568
if self.test_server:
568
569
session_key = create_session_on_server(self.test_server, email)
569
570
else:
570
571
session_key = create_pre_authenticated_session(email)
572
+
571
573
## to set a cookie we need to first visit the domain.
572
574
## 404 pages load the quickest!
573
575
self.browser.get(self.live_server_url + "/404_no_such_url/")
574
- self.browser.add_cookie(dict(
575
- name=settings.SESSION_COOKIE_NAME,
576
- value=session_key,
577
- path='/',
578
- ))
576
+ self.browser.add_cookie(
577
+ dict(
578
+ name=settings.SESSION_COOKIE_NAME,
579
+ value=session_key,
580
+ path="/",
581
+ )
582
+ )
579
583
580
584
[...]
581
585
----
@@ -595,7 +599,6 @@ from .server_tools import reset_database #<1>
595
599
[...]
596
600
597
601
class FunctionalTest(StaticLiveServerTestCase):
598
-
599
602
def setUp(self):
600
603
self.browser = webdriver.Firefox()
601
604
self.test_server = os.environ.get("TEST_SERVER")
@@ -632,15 +635,13 @@ from fabric.context_managers import settings, shell_env
632
635
633
636
634
637
def _get_manage_dot_py(host):
635
- return f' ~/sites/{host}/.venv/bin/python ~/sites/{host}/manage.py'
638
+ return f" ~/sites/{host}/.venv/bin/python ~/sites/{host}/manage.py"
636
639
637
640
638
641
def reset_database(host):
639
642
manage_dot_py = _get_manage_dot_py(host)
640
- with settings(host_string=f'elspeth@{host}'): #<1>
641
- run(f'{manage_dot_py} flush --noinput') #<2>
642
-
643
-
643
+ with settings(host_string=f"elspeth@{host}"): # <1>
644
+ run(f"{manage_dot_py} flush --noinput") # <2>
644
645
----
645
646
====
646
647
@@ -832,7 +833,7 @@ Before we finish, let's update our deployment fabfile so that it can
832
833
automatically add the `EMAIL_PASSWORD` to the _.env_ file on the server:
833
834
834
835
835
- [role="sourcecode"]
836
+ [role="sourcecode dofirst-ch21l022 "]
836
837
.deploy_tools/fabfile.py (ch18l021)
837
838
====
838
839
[source,python]
0 commit comments