Background Image
Table of Contents Table of Contents
Previous Page  302 / 478 Next Page
Information
Show Menu
Previous Page 302 / 478 Next Page
Page Background

Everything’s still OK:

11 assertions of 11 passed, 0 failed.

If those chained

.done

and

.fail

calls are bugging you—they bug me a little—you can

rewrite that as, eg:

var

deferred

=

$

.

post

(

urls

.

login

,

{

assertion

:

assertion

,

csrfmiddlewaretoken

:

token

}

);

deferred

.

done

(

function

() {

window

.

location

.

reload

(); })

deferred

.

fail

(

function

() {

navigator

.

id

.

logout

(); });

But async code is always a bit mind-bending. I find it just about readable as it is: “do a

post to urls.login with the assertion and csrf token, when it’s done, do a window reload,

or if it fails, do a navigator.id.logout”. You can read up on JavaScript deferreds, aka

“promises”,

here .

We’re approaching the moment of truth: will our FTs get any further? First, we adjust

our

initialize

call:

lists/templates/base.html.

<script>

/*global $, Superlists, navigator */

$

(

document

).

ready

(

function

() {

var

user

=

"{{ user.email }}"

||

null

;

var

token

=

"{{ csrf_token }}"

;

var

urls

=

{

login

:

"TODO"

,

logout

:

"TODO"

,

};

Superlists

.

Accounts

.

initialize

(

navigator

,

user

,

token

,

urls

);

});

</script>

And we run the FT…

$

python3 manage.py test functional_tests.test_login

Creating test database for alias 'default'...

Not Found: /favicon.ico

Not Found: /TODO

E

======================================================================

ERROR: test_login_with_persona (functional_tests.test_login.LoginTest)

---------------------------------------------------------------------

Traceback (most recent call last):

File "/workspace/superlists/functional_tests/test_login.py", line 47, in

test_login_with_persona

self.wait_for_element_with_id('id_logout')

File "/workspace/superlists/functional_tests/test_login.py", line 23, in

wait_for_element_with_id

lambda b: b.find_element_by_id(element_id)

[...]

274

|

Chapter 15: User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript