AG

Angelo Gladding
lahacker.net

dlv5vbq7lzlthol5 4b942a3185b37d00

angelo@lahacker.net

South Pasadena, California, United States currently feels like 56°F

Home CodecanopyFiles

tests.py

5 tests ran in 144 seconds. Tests are failing.

Download raw file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import pathlib
import time

import pytest
import web
from web import By, EC

import canopy


keysize = 1024
forest = "cnpy.gdn"
characters = {"Alice": {}, "Bob": {}, "Carol": {}, "Dan": {}}
tests_dir = pathlib.Path(__file__).parent / "tests"
test_data_dir = pathlib.Path(__file__).parent / "test_data"
scenes = []


def setup_module(module):
    for name, details in characters.items():
        hostname = f"test{name.lower()}.{forest}"
        canopy.delete_host(hostname)
        canopy.setup_host(name.capitalize(), f"{name.lower()}pass",
                          keysize, hostname)
        browser = web.browser()
        browser.width = 1100
        browser.name = name
        browser.hostname = hostname
        characters[name]["browser"] = browser
    for png in tests_dir.glob("*.png"):
        png.unlink()


def teardown_module(module):
    for name, details in characters.items():
        details["browser"].quit()

    toc = ""
    story = ""
    for index, (who, caption, slug, description) in enumerate(scenes):
        toc += f"<li><a href=#{slug}>{description}</a></li>"
        story += (f"<section id={slug}>"
                  f"<h2>{caption}</h2>"
                  f"<p><small>{', '.join(who)}</small>{description}</p>"
                  f"<div class=storyboard>")
        for character in sorted(characters.keys()):
            if character in who:
                url = f"results/{index:03}_{character}_{slug}.png"
                story += f"<a href={url}><img src={url}></a>"
            else:
                story += "<div class=filler></div>"
        story += (f"</div>"
                  f"</section>")
    with (tests_dir / "results.html").open("w") as fp:
        fp.write(f"""$var title: A walk through the Canopy

                     <style>
                     /* section {{ margin: 6em 0; }} */
                     .filler {{ height: 10em; width: 1em; }}
                     .storyboard {{ column-count: {len(characters)}; }}
                     .storyboard img {{ border: 0; /*.1em solid #586e75;*/ }}
                     a {{
                         color: #268bd2; }}
                     a:link {{
                         color: #268bd2; }}
                     a:visited {{
                         color: #6c71c4; }}
                     a:active {{
                         color: #dc322f; }}
                     </style>

                     <nav><ol>
                     {toc}
                     </ol></nav>
                     <article>
                     {story}
                     </article>""")


def shot(caption, description, *who):
    slug = caption.replace(' ', '_').lower()
    for character in who:
        path = tests_dir / f"{len(scenes):03}_{character}_{slug}"
        characters[character]["browser"].shot(path)
    scenes.append((who, caption, slug, description))


@pytest.fixture
def alice():
    return characters["Alice"]["browser"]


@pytest.fixture
def bob():
    return characters["Bob"]["browser"]


@pytest.fixture
def carol():
    return characters["Carol"]["browser"]


@pytest.fixture
def dan():
    return characters["Dan"]["browser"]


def test_setup():
    """compensate for setup time showing in first result"""
    assert True


def test_home(alice, bob, carol, dan):
    """assert alice, bob and carol's identity details have been created"""
    for character in (alice, bob, carol, dan):
        # via Python
        canopy.contextualize(character.hostname)
        resource = canopy.content.read.get_resource("identity", "me")
        assert resource["name"] == character.name
        # via Web
        character.go(character.hostname)
        character.wait(EC.url_to_be(f"https://{character.hostname}/"))
        assert character.name in character.title
    # TODO leverage function name, positional arguments and docstring for below
    shot("At Home",
         "Alice, Bob and Carol spawn their trees with respective "
         "names, passphrases and domains.",
         "Alice", "Bob", "Carol", "Dan")


def test_identification(alice, bob, carol, dan):
    """"""
    # TODO submit form over AJAX and use loading invisibility
    passphrase_inputs = []
    for character in (alice, bob, carol, dan):
        character.go(character.hostname, "identification")
        character.wait(EC.url_to_be(f"https://{character.hostname}"
                                    f"/authentication"),
                       EC.presence_of_element_located((By.ID, "authenticate")))
        # assert "" in alice.title
        passphrase_input = character.cssel("input[name=passphrase]")
        passphrase_input.send_keys(f"{character.name.lower()}pass")
        passphrase_inputs.append(passphrase_input)
    shot("Passphrase Entered",
         "Alice signs in with her passphrase.",
         "Alice", "Bob", "Carol", "Dan")
    for passphrase_input in passphrase_inputs:
        passphrase_input.submit()
    for character in (alice, bob, carol, dan):
        character.wait(EC.presence_of_element_located((By.ID, "signout")))
        # assert "" in alice.title
    shot("Signed In",
         "Alice is signed in and sent back to her home page.",
         "Alice", "Bob", "Carol", "Dan")


def test_create_note(alice, bob, carol, dan):
    """"""
    alice.find_element_by_link_text("Notes").click()
    alice.wait(EC.url_to_be(f"https://{alice.hostname}/notes"),
               EC.invisibility_of_element_located((By.ID, "loading")))
    note_input = alice.cssel("textarea[name=-source-content]")
    note_input.send_keys("Hello world!")
    shot("Notes Page",
         "Alice goes to her notes page.",
         "Alice")
    alice.cssel("button[value=publish]").click()
    time.sleep(3)  # trigger "loading" UI on websocket message to wait here
    shot("Note Posted",
         "Alice's note is posted to her notes page.",
         "Alice")
    alice.find_element_by_link_text("a few seconds ago").click()
    time.sleep(3)
    # TODO alice.wait(EC.visibility_of_element_located((By.ID, "loading")),
    # TODO            EC.invisibility_of_element_located((By.ID, "loading")))
    alice_note = alice.current_url
    bob.go(alice_note)
    time.sleep(3)
    shot("Note Page",
         "Alice's note's page.",
         "Alice", "Bob")
    bob.cssel("indie-action[do=reply] a").click()
    time.sleep(1)
    url_input = bob.cssel("form#action-handler input[name=url]")
    url_input.send_keys("https://testbob.cnpy.gdn")
    shot("Reply Web Action",
         "Bob clicks reply icon and is presented with a web action prompt.",
         "Bob")
    url_input.submit()
    time.sleep(3)
    shot("Note Reply UI",
         "Bob's note reply type selection.",
         "Bob")
    bob.cssel("div#body article button[value=note]").click()
    time.sleep(3)
    note_input = bob.cssel("textarea#-source-content")
    note_input.send_keys("Hello Alice!")
    shot("Bob Writes Reply",
         "Bob's reply to a note with a note UI.",
         "Bob")
    bob.cssel("button[value=publish]").click()
    time.sleep(3)
    bob_note = bob.current_url
    alice.go(bob_note)
    shot("Bob Reply Note",
         "Bob's reply note on it's own page.",
         "Alice", "Bob")

    carol.go(bob.current_url)
    time.sleep(3)
    carol.cssel("indie-action[do=reply] a").click()
    time.sleep(1)
    url_input = carol.cssel("form#action-handler input[name=url]")
    url_input.send_keys("https://testcarol.cnpy.gdn")
    url_input.submit()
    time.sleep(3)
    carol.cssel("div#body article button[value=note]").click()
    time.sleep(3)
    note_input = carol.cssel("textarea#-source-content")
    note_input.send_keys("Hello Bob!")
    carol.cssel("button[value=publish]").click()
    time.sleep(15)
    alice.go(alice_note)
    bob.go(bob_note)
    shot("Carol Reply Note",
         "Carol's reply to Bob's reply to Alice's post, on it's own page.",
         "Alice", "Bob", "Carol")


# def test_create_note():
#     """assert a note has been created"""
#     canopy.contextualize(f"testalice.{forest}")
#     body = "Hello world!"
#     resource_id = canopy.content.write.quick_draft("note", {"body": body},
#                                                    publish=True)
#     resource = canopy.content.read.get_resource_by_id(resource_id)
#     assert resource["body"] == body


# def test_upload_photo():
#     """"""
#     with (data_dir / "alice.jpg").open("rb") as fp:
#         data = {"image": fp.read()}
#     resource_id = canopy.content.write.quick_draft("image", data,
#                                                    publish=True)
#     resource = canopy.content.read.get_resource_by_id(resource_id)
#     assert "images" in resource
#     # TODO find and assign face to identity
#     # TODO update photo in profile


# def mock():
#    def note(identity, body, tags, license, datetime):
#        n = dict(body=body, tags=tags.split(","), license=license)
#        published = pendulum.strptime(datetime, "%Y-%m-%d %H:%M:%S")
#        web.tx.db = canopy.get_db("testing.{}.lahacker.net".format(identity))
#        canopy.__web__.feed.create_note(n, published)
#    note("alice", "Happy new year! Wish everyone a healthy and "
#         "fulfilling future", "holiday", "cc-zero", "2017-01-01 00:00:00")
#    note("alice", "Happy vernal equinox! Time to prep your plots!",
#         "seasons", "cc-by", "2017-03-20 10:28:00")
#    note("bob", "Washed the dog!", "seasons", "cc-by", "2017-04-03 02:19:12")


# ---------------------------------------------------------------------


# def test_photo_upload(alice):
#     alice.go(f"alice.{forest}", "images")
#     alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("image index",
#          """Alice goes to her images index and clicks to create
#             a new image post.""",
#          "alice")
#     create_form = alice.cssel("#quick_editor button[value=draft]")
#     create_form.click()
#     time.sleep(1)
#     alice.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     # alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     alice.execute_script("$('#fileinput').style.display = 'block';")
#     photo_input = alice.cssel("#fileinput")
#     photo_input.send_keys(str((pathlib.Path(__file__).parent /
#                                "data/alice.jpg").resolve()))
#     alice.execute_script("$('#fileinput').style.display = 'none';")
#     shot("image editor",
#          """Alice uploads a photo of herself.""",
#          "alice")
#     action = alice.action()
#     action.move_to_element(alice.cssel("div.images > div > div"))
#     # starts at center of image
#     action.move_by_offset(40, -130)
#     action.click()
#     # action.click_and_hold()
#     # action.move_by_offset(200, 200)
#     # action.release()
#     action.perform()
#     shot("select face",
#          """Alice selects her face in the photo.""",
#          "alice")
#     face = alice.cssel("div.images > div:first-child > div + p > span")
#     face.click()
#     shot("select face identity",
#          """Alice selects herself as the face's identity.""",
#          "alice")
#     alice.cssel("button[name=action][value=publish]").click()
#     alice.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     # alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("profile photo uploaded",
#          """Alice has uploaded her profile photo.""",
#          "alice")
#
#
# def test_change_profile_photo(alice):
#     alice.go(f"alice.{forest}", "contacts/me")
#     alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("personal profile data",
#          """Alice goes to her profile and clicks to edit.""",
#          "alice")
#     alice.cssel("#edit").click()
#     alice.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     alice.cssel("input[name=photo]").click()
#     shot("editing personal profile",
#          """Alice updates her profile by selecting her face and
#             clicks to publish.""",
#          "alice")
#     alice.cssel("button[value=publish]").click()
#     alice.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     # alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("photo added to profile",
#          """Alice has added her photo to her profile.""",
#          "alice")
#
#
# def test_bob_home_and_signin(bob):
#     """"""
#     bob.go(f"bob.{forest}")
#     bob.wait(EC.url_to_be(f"https://bob.{forest}/"))
#     bob.cssel("#stylesheet_switcher").click()
#     assert "Bob" in bob.title
#
#     bob.go(f"bob.{forest}", "identification")
#     bob.wait(EC.url_to_be(f"https://bob.{forest}/authentication"),
#              EC.presence_of_element_located((By.ID, "authenticate")))
#     passphrase_input = bob.cssel("input[name=passphrase]")
#     passphrase_input.send_keys("bob")
#     passphrase_input.submit()
#     # TODO send form submissions over AJAX w/ updated interface
#     #      & use loading invisibility
#     time.sleep(1)
#     # bob.wait(EC.invisibility_of_element_located((By.ID, "bottom")))
#     bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("bob at home",
#          """Bob spawns his tree with desired name, passphrase and domain.
#             He toggles the contrast and signs in.""",
#          "bob")
#     # assert "" in bob.title
#
#
# def test_bob_follow_alice(bob):
#     bob.go(f"alice.{forest}")
#     bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("bob at alice tree",
#          """Bob travels to Alice's tree and clicks to follow.""",
#          "bob")
#     # # assert "" in bob.title
#     bob.go(f"bob.{forest}", "follows")
#     bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     time.sleep(1)
#     follow_input = bob.cssel("input[name=follow-of]")
#     follow_input.send_keys("https://alice.example.cnpy.gdn/")
#     follow_input.submit()
#     bob.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     # bob.wait(EC.invisibility_of_element_located((By.ID, "bottom")))
#     # time.sleep(1)
#     # bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("bob follows alice",
#          """Bob is following Alice.""",
#          "bob")
#     # assert "" in bob.title
#     bob.go(f"bob.{forest}", "reader/feeds/alice.example.cnpy.gdn")
#     bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("alice in bobs reader",
#          """Bob pulls up Alice's feed in his reader.""",
#          "bob")
#
#
# def test_alice_quick_note_post(alice, bob):
#     alice.go(f"alice.{forest}", "notes")
#     alice.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     notebody_input = alice.cssel("#quick_editor textarea[name=body]")
#     notebody_input.send_keys("Hello world!")
#     shot("quick note post",
#          """Alice goes to her notes index and clicks to create
#             a new note post.""",
#          "alice")
#     notebody_input.submit()
#     alice.wait(EC.invisibility_of_element_located((By.ID, "loading")))
#     bob.refresh()
#     bob.wait(EC.presence_of_element_located((By.ID, "bottom")))
#     shot("note posted",
#          """Alice has posted a note and Bob sees it in his reader.""",
#          "alice", "bob")
#
#
# # # def test_signout(alice):
# # #     alice.cssel("#signout").submit()
# # #     alice.wait(EC.invisibility_of_element_located((By.ID, "bottom")),
# # #                  EC.presence_of_element_located((By.ID, "bottom")))
# # #     alice.cssel("a[rel=home]").click()
# # #     alice.wait(EC.invisibility_of_element_located((By.ID, "bottom")),
# # #                  EC.presence_of_element_located((By.ID, "bottom")))
# # #     alice.shot("alice_14_signed_out.png",
# # #                  """Alice has signed out and is viewing her public
# # #                     homepage.""")
# #
# #
# # # alice.go("web+action://sign-in?url=https://alice.example.cnpy.gdn/"
# # #      "identification&return_to=https://alice.example.cnpy.gdn")
# # #
# # # signin_button = alice.cssel("indie-action[do=sign-in] a")
# # # signin_button.click()
# # # alice.wait().until(EC.presence_of_element_located((By.LINK_TEXT,
# # #                                                  "sign in here")))
# # #
# # # alice.shot("alice_02_unhandled_sign_in.png")