-
Notifications
You must be signed in to change notification settings - Fork 683
/
journalist_navigation_steps.py
859 lines (695 loc) · 33.3 KB
/
journalist_navigation_steps.py
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
import pytest
import re
import tempfile
import gzip
import time
import os
import random
import requests
from os.path import dirname
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
# A generator to get unlimited user names for our tests.
# The pages-layout tests require many users during
# the test run, that is why have the following
# implementation.
def get_journalist_usernames():
yield "dellsberg"
yield "jpb"
yield "bassel"
while True:
num = random.randint(1000, 1000000)
yield "journalist" + str(num)
journalist_usernames = get_journalist_usernames()
class JournalistNavigationStepsMixin():
def _get_submission_content(self, file_url, raw_content):
if not file_url.endswith(".gz.gpg"):
return str(raw_content)
with tempfile.TemporaryFile() as fp:
fp.write(raw_content.data)
fp.seek(0)
gzf = gzip.GzipFile(mode='rb', fileobj=fp)
content = gzf.read()
return content
def return_downloaded_content(self, url, cookies):
"""
This downloads and returns the content to the caller
:param url: URL to download
:param cookies: the cookies to access
:return: Content of the URL
"""
proxies = None
if ".onion" in url:
proxies = {
'http': 'socks5h://127.0.0.1:9150',
'https': 'socks5h://127.0.0.1:9150'
}
r = requests.get(url, cookies=cookies, proxies=proxies, stream=True)
if r.status_code != 200:
raise Exception("Failed to download the data.")
data = b""
for chunk in r.iter_content(1024):
data += chunk
return data
def _input_text_in_login_form(self, username, password, token):
self.driver.get(self.journalist_location + "/login")
username_field = self.driver.find_element_by_css_selector(
'input[name="username"]')
username_field.send_keys(username)
password_field = self.driver.find_element_by_css_selector(
'input[name="password"]')
password_field.send_keys(password)
token_field = self.driver.find_element_by_css_selector(
'input[name="token"]')
token_field.send_keys(token)
def _try_login_user(self, username, password, token):
self._input_text_in_login_form(username, password, token)
submit_button = self.driver.find_element_by_css_selector(
'button[type="submit"]')
submit_button.send_keys(u'\ue007')
def _login_user(self, username, password, otp, maxtries=3):
token = str(otp.now())
for i in range(maxtries):
self._try_login_user(username, password, token)
# Successful login should redirect to the index
time.sleep(self.sleep_time+10)
if self.driver.current_url != self.journalist_location + '/':
new_token = str(otp.now())
while token == new_token:
time.sleep(1)
new_token = str(otp.now())
print("Token: {} New token: {}".format(token,
new_token))
token = new_token
else:
return
# If we reach here, assert the error
assert self.driver.current_url == self.journalist_location + '/',\
self.driver.current_url + " " + self.journalist_location
def _journalist_logs_in(self):
# Create a test user for logging in
self.user = self.admin_user['name']
self.user_pw = self.admin_user['password']
self._login_user(self.user, self.user_pw, self.admin_user['totp'])
headline = self.driver.find_element_by_css_selector('span.headline')
if not hasattr(self, 'accept_languages'):
assert 'Sources' in headline.text
def _journalist_visits_col(self):
self.driver.find_element_by_css_selector(
'#un-starred-source-link-1').click()
time.sleep(self.sleep_time)
def _journalist_selects_first_doc(self):
self.driver.find_elements_by_name('doc_names_selected')[0].click()
time.sleep(self.sleep_time)
def _journalist_clicks_on_modal(self, click_id):
self.driver.find_element_by_id(click_id).click()
time.sleep(5)
def _journalist_clicks_delete_collections_cancel_on_modal(self):
self._journalist_clicks_on_modal('cancel-collections-deletions')
def _journalist_clicks_delete_selected_cancel_on_modal(self):
self._journalist_clicks_on_modal('cancel-selected-deletions')
def _journalist_clicks_delete_collection_cancel_on_modal(self):
self._journalist_clicks_on_modal('cancel-collection-deletions')
def _journalist_clicks_delete_collections_on_modal(self):
self._journalist_clicks_on_modal('delete-collections')
def _journalist_clicks_delete_selected_on_modal(self):
self._journalist_clicks_on_modal('delete-selected')
def _journalist_clicks_delete_collection_on_modal(self):
self._journalist_clicks_on_modal('delete-collection-button')
def _journalist_clicks_delete_link(self, click_id, displayed_id):
self.driver.find_element_by_id(click_id).click()
time.sleep(self.sleep_time)
def _journalist_clicks_delete_selected_link(self):
self._journalist_clicks_delete_link(
'delete-selected-link',
'delete-selected-confirmation-modal')
def _journalist_clicks_delete_collections_link(self):
self._journalist_clicks_delete_link(
'delete-collections-link',
'delete-confirmation-modal')
def _journalist_clicks_delete_collection_link(self):
self._journalist_clicks_delete_link(
'delete-collection-link',
'delete-collection-confirmation-modal')
def _journalist_uses_delete_selected_button_confirmation(self):
selected_count = len(self.driver.find_elements_by_name(
'doc_names_selected'))
assert selected_count > 0
self._journalist_selects_first_doc()
self._journalist_clicks_delete_selected_link()
self._journalist_clicks_delete_selected_cancel_on_modal()
time.sleep(5)
assert selected_count == len(self.driver.find_elements_by_name(
'doc_names_selected'))
self._journalist_clicks_delete_selected_link()
self._journalist_clicks_delete_selected_on_modal()
time.sleep(5)
assert selected_count > len(self.driver.find_elements_by_name(
'doc_names_selected'))
def _journalist_uses_delete_collection_button_confirmation(self):
self._journalist_clicks_delete_collection_link()
self._journalist_clicks_delete_collection_cancel_on_modal()
time.sleep(self.sleep_time)
self._journalist_clicks_delete_collection_link()
self._journalist_clicks_delete_collection_on_modal()
# Now we should be redirected to the index.
if not hasattr(self, 'accept-languages'):
assert "Sources" in self.driver.find_element_by_tag_name('h1').text
def _journalist_uses_delete_collections_button_confirmation(self):
sources = self.driver.find_elements_by_class_name("code-name")
assert len(sources) > 0
self.driver.find_element_by_id('select_all').click()
self._journalist_clicks_delete_collections_link()
self._journalist_clicks_delete_collections_cancel_on_modal()
self.driver.find_element_by_id('select_all').click()
sources = self.driver.find_elements_by_class_name("code-name")
assert len(sources) > 0
self._journalist_clicks_delete_collections_link()
self._journalist_clicks_delete_collections_on_modal()
# We should be redirected to the index without those boxes selected.
sources = self.driver.find_elements_by_class_name("code-name")
assert len(sources) == 0
def _admin_logs_in(self):
self.admin = self.admin_user['name']
self.admin_pw = self.admin_user['password']
self._login_user(self.admin, self.admin_pw, self.admin_user['totp'])
if not hasattr(self, 'accept_languages'):
# Admin user should log in to the same interface as a
# normal user, since there may be users who wish to be
# both journalists and admins.
headline = self.driver.find_element_by_css_selector(
'span.headline')
assert 'Sources' in headline.text
# Admin user should have a link that take them to the admin page
links = self.driver.find_elements_by_tag_name('a')
assert 'Admin' in [el.text for el in links]
def _admin_visits_admin_interface(self):
admin_interface_link = self.driver.find_element_by_id(
'link-admin-index')
admin_interface_link.click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
h1s = self.driver.find_elements_by_tag_name('h1')
assert "Admin Interface" in [el.text for el in h1s]
def _admin_visits_system_config_page(self):
system_config_link = self.driver.find_element_by_id(
'update-instance-config'
)
system_config_link.click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
h1 = self.driver.find_element_by_tag_name('h1')
assert "Instance Configuration" in h1.text
def _admin_updates_logo_image(self):
dir_name = dirname(dirname(dirname(os.path.abspath(__file__))))
image_path = os.path.abspath(os.path.join(dir_name,
'static/i/logo.png'))
logo_upload_input = self.driver.find_element_by_id('logo-upload')
logo_upload_input.send_keys(image_path)
submit_button = self.driver.find_element_by_id('submit-logo-update')
submit_button.click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
flashed_msgs = self.driver.find_element_by_css_selector('.flash')
assert 'Image updated.' in flashed_msgs.text
def _add_user(self, username, is_admin=False, hotp=None):
username_field = self.driver.find_element_by_css_selector(
'input[name="username"]')
username_field.send_keys(username)
if hotp:
hotp_checkbox = self.driver.find_element_by_css_selector(
'input[name="is_hotp"]')
print(str(hotp_checkbox.__dict__))
hotp_checkbox.click()
hotp_secret = self.driver.find_element_by_css_selector(
'input[name="otp_secret"]')
hotp_secret.send_keys(hotp)
if is_admin:
is_admin_checkbox = self.driver.find_element_by_css_selector(
'input[name="is_admin"]'
)
is_admin_checkbox.click()
submit_button = self.driver.find_element_by_css_selector(
'button[type=submit]')
submit_button.click()
def _admin_adds_a_user(self, is_admin=False, new_username=''):
add_user_btn = self.driver.find_element_by_css_selector(
'button#add-user')
add_user_btn.click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
# The add user page has a form with an "ADD USER" button
btns = self.driver.find_elements_by_tag_name('button')
assert 'ADD USER' in [el.text for el in btns]
password = self.driver.find_element_by_css_selector('#password') \
.text.strip()
if not new_username:
new_username = journalist_usernames.next()
self.new_user = dict(
username=new_username,
password=password,
)
self._add_user(self.new_user['username'], is_admin=is_admin)
if not hasattr(self, 'accept_languages'):
# Clicking submit on the add user form should redirect to
# the FreeOTP page
h1s = self.driver.find_elements_by_tag_name('h1')
assert "Enable FreeOTP" in [el.text for el in h1s]
shared_secret = self.driver.find_element_by_css_selector('#shared-secret').text.strip().replace(' ', '') # noqa: E501
self.create_new_totp(shared_secret)
# Verify the two-factor authentication
token_field = self.driver.find_element_by_css_selector(
'input[name="token"]')
token_field.send_keys(str(self.new_totp.now()))
submit_button = self.driver.find_element_by_css_selector(
'button[type=submit]')
submit_button.click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
# Successfully verifying the code should redirect to the admin
# interface, and flash a message indicating success
flashed_msgs = self.driver.find_elements_by_css_selector('.flash')
assert (("Token in two-factor authentication "
"accepted for user {}.").format(
self.new_user['username']) in
[el.text for el in flashed_msgs])
def _admin_deletes_user(self):
self.wait_for(lambda: self.driver.find_element_by_css_selector(
'.delete-user'), timeout=60)
# Get the delete user buttons
delete_buttons = self.driver.find_elements_by_css_selector(
'.delete-user')
# Try to delete a user
delete_buttons[0].click()
time.sleep(5)
# Wait for JavaScript alert to pop up and accept it.
self._alert_wait()
self._alert_accept()
time.sleep(5)
if not hasattr(self, 'accept_languages'):
flashed_msg = self.driver.find_element_by_css_selector('.flash')
assert "Deleted user" in flashed_msg.text
def _admin_can_send_test_alert(self):
alert_button = self.driver.find_element_by_id('test-ossec-alert')
alert_button.click()
if not hasattr(self, 'accept_languages'):
flashed_msg = self.driver.find_element_by_css_selector('.flash')
assert ("Test alert sent. Please check your email."
in flashed_msg.text)
def _logout(self):
# Click the logout link
logout_link = self.driver.find_element_by_id('link-logout')
logout_link.send_keys(" ")
logout_link.click()
time.sleep(60) # Because this takes time
# Logging out should redirect back to the login page
def login_page():
assert ("Login to access the journalist interface" in
self.driver.page_source)
self.wait_for(login_page)
def _check_login_with_otp(self, otp):
self._logout()
self._login_user(self.new_user['username'],
self.new_user['password'], otp)
if not hasattr(self, 'accept_languages'):
# Test that the new user was logged in successfully
assert 'Sources' in self.driver.page_source
def _new_user_can_log_in(self):
# Log the admin user out
self._logout()
time.sleep(31)
# Log the new user in
self._login_user(self.new_user['username'],
self.new_user['password'],
self.new_totp)
if not hasattr(self, 'accept_languages'):
# Test that the new user was logged in successfully
assert 'Sources' in self.driver.page_source
# The new user was not an admin, so they should not have the admin
# interface link available
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_id('link-admin-index')
def _new_admin_user_can_log_in(self):
# Test login with mocked token
self._check_login_with_otp('mocked')
# Newly added user who is an admin can visit admin interface
self._admin_visits_admin_interface()
def _edit_account(self):
edit_account_link = self.driver.find_element_by_id(
'link-edit-account')
edit_account_link.click()
time.sleep(self.sleep_time)
# The header says "Edit your account"
h1s = self.driver.find_elements_by_tag_name('h1')[0]
assert 'Edit your account' == h1s.text
# There's no link back to the admin interface.
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_partial_link_text(
'Back to admin interface')
# There's no field to change your username.
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_css_selector('#username')
# There's no checkbox to change the admin status of your
# account.
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_css_selector('#is-admin')
# 2FA reset buttons at the bottom point to the user URLs for reset.
totp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-totp')[0]
assert ('/account/reset-2fa-totp' in
totp_reset_button.get_attribute('action'))
hotp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-hotp')[0]
assert ('/account/reset-2fa-hotp' in
hotp_reset_button.get_attribute('action'))
def _edit_user(self, username, is_admin=False):
# XXXX
new_user_edit_links = filter(
lambda el: el.get_attribute('data-username') == username,
self.driver.find_elements_by_tag_name('a'))
assert 1 == len(new_user_edit_links)
new_user_edit_links[0].click()
time.sleep(self.sleep_time)
# The header says "Edit user "username"".
h1s = self.driver.find_elements_by_tag_name('h1')[0]
assert 'Edit user "{}"'.format(username) == h1s.text
# There's a convenient link back to the admin interface.
admin_interface_link = self.driver.find_element_by_partial_link_text(
'Back to admin interface')
assert re.search('/admin$', admin_interface_link.get_attribute('href'))
# There's a field to change the user's username and it's already filled
# out with the user's username.
username_field = self.driver.find_element_by_css_selector('#username')
assert username_field.get_attribute('placeholder') == username
# There's a checkbox to change the admin status of the user and
# it's already checked appropriately to reflect the current status of
# our user.
username_field = self.driver.find_element_by_css_selector('#is-admin')
assert bool(username_field.get_attribute('checked')) == is_admin
# 2FA reset buttons at the bottom point to the admin URLs for
# resettting 2FA and include the correct user id in the hidden uid.
totp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-totp')[0]
assert '/admin/reset-2fa-totp' in totp_reset_button.get_attribute(
'action')
totp_reset_uid = totp_reset_button.find_element_by_name('uid')
assert totp_reset_uid.is_displayed() is False
hotp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-hotp')[0]
assert '/admin/reset-2fa-hotp' in hotp_reset_button.get_attribute(
'action')
hotp_reset_uid = hotp_reset_button.find_element_by_name('uid')
assert hotp_reset_uid.is_displayed() is False
def _admin_can_edit_new_user(self):
# Log the new user out
self._logout()
time.sleep(self.sleep_time)
self._login_user(self.admin, self.admin_pw, self.admin_user['totp'])
# Go to the admin interface
admin_interface_link = self.driver.find_element_by_id(
'link-admin-index')
admin_interface_link.click()
time.sleep(self.sleep_time)
# Click the "edit user" link for the new user
# self._edit_user(self.new_user['username'])
new_user_edit_links = filter(
lambda el: (el.get_attribute('data-username') ==
self.new_user['username']),
self.driver.find_elements_by_tag_name('a'))
assert len(new_user_edit_links) == 1
new_user_edit_links[0].click()
time.sleep(self.sleep_time)
def can_edit_user():
assert ('"{}"'.format(self.new_user['username']) in
self.driver.page_source)
self.wait_for(can_edit_user)
new_username = self.new_user['username'] + "2"
username_field = self.driver.find_element_by_css_selector(
'input[name="username"]')
username_field.send_keys(new_username)
update_user_btn = self.driver.find_element_by_css_selector(
'button[type=submit]')
update_user_btn.click()
time.sleep(self.sleep_time)
def can_edit_user2():
assert ('"{}"'.format(new_username) in self.driver.page_source)
self.wait_for(can_edit_user2)
# Update self.new_user with the new username for the future tests
self.new_user['username'] = new_username
# Log the new user in with their new username
self._logout()
time.sleep(31)
self._login_user(self.new_user['username'],
self.new_user['password'],
self.new_totp)
if not hasattr(self, 'accept_languages'):
def found_sources():
assert 'Sources' in self.driver.page_source
self.wait_for(found_sources)
# Log the admin user back in
self._logout()
time.sleep(31)
self._login_user(self.admin, self.admin_pw, self.admin_user['totp'])
# Go to the admin interface
admin_interface_link = self.driver.find_element_by_id(
'link-admin-index')
admin_interface_link.click()
time.sleep(self.sleep_time)
# Edit the new user's password
new_user_edit_links = filter(
lambda el: (el.get_attribute('data-username') ==
self.new_user['username']),
self.driver.find_elements_by_tag_name('a'))
assert len(new_user_edit_links) == 1
new_user_edit_links[0].click()
time.sleep(self.sleep_time)
new_password = self.driver.find_element_by_css_selector('#password') \
.text.strip()
self.new_user['password'] = new_password
reset_pw_btn = self.driver.find_element_by_css_selector(
'#reset-password')
reset_pw_btn.click()
time.sleep(self.sleep_time)
def update_password_success():
assert 'Password updated.' in self.driver.page_source
# Wait until page refreshes to avoid causing a broken pipe error (#623)
self.wait_for(update_password_success)
# Log the new user in with their new password
self._logout()
self._login_user(self.new_user['username'],
self.new_user['password'],
self.new_totp)
self.wait_for(found_sources)
def _journalist_checks_messages(self):
self.driver.get(self.journalist_location)
# There should be 1 collection in the list of collections
code_names = self.driver.find_elements_by_class_name('code-name')
assert 0 != len(code_names), code_names
assert 1 <= len(code_names), code_names
if not hasattr(self, 'accept_languages'):
# There should be a "1 unread" span in the sole collection entry
unread_span = self.driver.find_element_by_css_selector(
'span.unread')
assert "1 unread" in unread_span.text
def _journalist_stars_and_unstars_single_message(self):
# Message begins unstarred
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_id('starred-source-link-1')
# Journalist stars the message
self.driver.find_element_by_class_name('button-star').click()
time.sleep(self.sleep_time)
starred = self.driver.find_elements_by_id('starred-source-link-1')
assert 1 == len(starred)
# Journalist unstars the message
self.driver.find_element_by_class_name('button-star').click()
time.sleep(self.sleep_time)
with pytest.raises(NoSuchElementException):
self.driver.find_element_by_id('starred-source-link-1')
def _journalist_selects_all_sources_then_selects_none(self):
self.driver.find_element_by_id('select_all').click()
checkboxes = self.driver.find_elements_by_id('checkbox')
for checkbox in checkboxes:
assert checkbox.is_selected()
self.driver.find_element_by_id('select_none').click()
checkboxes = self.driver.find_elements_by_id('checkbox')
for checkbox in checkboxes:
assert checkbox.is_selected() is False
def _journalist_selects_the_first_source(self):
self.driver.find_element_by_css_selector(
'#un-starred-source-link-1').click()
time.sleep(self.sleep_time)
def _journalist_selects_documents_to_download(self):
self.driver.find_element_by_id('select_all').click()
def _journalist_downloads_message(self):
self._journalist_selects_the_first_source()
submissions = self.driver.find_elements_by_css_selector(
'#submissions a')
assert 1 == len(submissions)
file_url = submissions[0].get_attribute('href')
# Downloading files with Selenium is tricky because it cannot automate
# the browser's file download dialog. We can directly request the file
# using requests, but we need to pass the cookies for logged in user
# for Flask to allow this.
def cookie_string_from_selenium_cookies(cookies):
result = {}
for cookie in cookies:
result[cookie['name']] = cookie['value']
return result
cks = cookie_string_from_selenium_cookies(self.driver.get_cookies())
raw_content = self.return_downloaded_content(file_url, cks)
decrypted_submission = self.gpg.decrypt(raw_content)
submission = self._get_submission_content(file_url,
decrypted_submission)
assert self.secret_message == submission
def _journalist_composes_reply(self):
reply_text = ('Thanks for the documents. Can you submit more '
'information about the main program?')
self.wait_for(lambda: self.driver.find_element_by_id(
'reply-text-field'
), timeout=60)
self.driver.find_element_by_id('reply-text-field').send_keys(
reply_text
)
def _journalist_sends_reply_to_source(self):
self._journalist_composes_reply()
self.driver.find_element_by_id('reply-button').click()
time.sleep(self.sleep_time)
if not hasattr(self, 'accept_languages'):
assert ("Thanks. Your reply has been stored." in
self.driver.page_source)
def _visit_edit_account(self):
edit_account_link = self.driver.find_element_by_id(
'link-edit-account')
edit_account_link.click()
def _visit_edit_secret(self, type):
reset_form = self.driver.find_elements_by_css_selector(
'#reset-two-factor-' + type)[0]
assert ('/account/reset-2fa-' + type in
reset_form.get_attribute('action'))
reset_button = self.driver.find_elements_by_css_selector(
'#button-reset-two-factor-' + type)[0]
reset_button.click()
def _visit_edit_hotp_secret(self):
self._visit_edit_secret('hotp')
def _set_hotp_secret(self):
hotp_secret_field = self.driver.find_elements_by_css_selector(
'input[name="otp_secret"]')[0]
hotp_secret_field.send_keys('123456')
submit_button = self.driver.find_element_by_css_selector(
'button[type=submit]')
submit_button.click()
time.sleep(self.sleep_time)
def _visit_edit_totp_secret(self):
self._visit_edit_secret('totp')
def _admin_visits_add_user(self):
add_user_btn = self.driver.find_element_by_css_selector(
'button#add-user')
add_user_btn.click()
def _admin_visits_edit_user(self):
new_user_edit_links = filter(
lambda el: (el.get_attribute('data-username') ==
self.new_user['username']),
self.driver.find_elements_by_tag_name('a'))
assert len(new_user_edit_links) == 1
new_user_edit_links[0].click()
def can_edit_user():
assert ('"{}"'.format(self.new_user['username']) in
self.driver.page_source)
self.wait_for(can_edit_user)
def _admin_visits_reset_2fa_hotp(self):
hotp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-hotp')[0]
assert ('/admin/reset-2fa-hotp' in
hotp_reset_button.get_attribute('action'))
hotp_reset_button.click()
def _admin_accepts_2fa_js_alert(self):
self._alert_wait()
self._alert_accept()
def _admin_visits_reset_2fa_totp(self):
totp_reset_button = self.driver.find_elements_by_css_selector(
'#reset-two-factor-totp')[0]
assert ('/admin/reset-2fa-totp' in
totp_reset_button.get_attribute('action'))
totp_reset_button.click()
def _admin_creates_a_user(self, hotp):
add_user_btn = self.driver.find_element_by_css_selector(
'button#add-user')
add_user_btn.click()
self.new_user = dict(
username='dellsberg',
password='pentagonpapers')
self._add_user(self.new_user['username'],
is_admin=False,
hotp=hotp)
def _journalist_delete_all(self):
for checkbox in self.driver.find_elements_by_name(
'doc_names_selected'):
checkbox.click()
self.driver.find_element_by_id('delete-selected-link').click()
def _journalist_confirm_delete_selected(self):
self.wait_for(
lambda: self.driver.find_element_by_id('delete-selected'))
confirm_btn = self.driver.find_element_by_id('delete-selected')
confirm_btn.click()
def _source_delete_key(self):
filesystem_id = self.source_app.crypto_util.hash_codename(
self.source_name)
self.source_app.crypto_util.delete_reply_keypair(filesystem_id)
def _journalist_continues_after_flagging(self):
self.driver.find_element_by_id('continue-to-list').click()
def _journalist_delete_none(self):
self.driver.find_element_by_id('delete-selected-link').click()
def _journalist_delete_all_confirmation(self):
self.driver.find_element_by_id('select_all').click()
self.driver.find_element_by_id('delete-selected-link').click()
def _journalist_delete_one(self):
self.driver.find_elements_by_name('doc_names_selected')[0].click()
self.driver.find_element_by_id('delete-selected-link').click()
def _journalist_flags_source(self):
self.driver.find_element_by_id('flag-button').click()
def _journalist_visits_admin(self):
self.driver.get(self.journalist_location + "/admin")
def _journalist_fail_login(self):
self._try_login_user("root", 'worse', 'mocked')
def _journalist_fail_login_many(self):
self.user = ""
for _ in range(5 + 1):
self._try_login_user(self.user, 'worse', 'mocked')
def _admin_enters_journalist_account_details_hotp(self, username,
hotp_secret):
username_field = self.driver.find_element_by_css_selector(
'input[name="username"]')
username_field.send_keys(username)
hotp_secret_field = self.driver.find_element_by_css_selector(
'input[name="otp_secret"]')
hotp_secret_field.send_keys(hotp_secret)
hotp_checkbox = self.driver.find_element_by_css_selector(
'input[name="is_hotp"]')
hotp_checkbox.click()
def _journalist_uses_js_filter_by_sources(self):
self.wait_for(lambda: self.driver.find_element_by_id("filter"))
filter_box = self.driver.find_element_by_id("filter")
filter_box.send_keys("thiswordisnotinthewordlist")
sources = self.driver.find_elements_by_class_name("code-name")
assert len(sources) > 0
for source in sources:
assert source.is_displayed() is False
filter_box.clear()
filter_box.send_keys(Keys.RETURN)
for source in sources:
assert source.is_displayed() is True
def _journalist_uses_js_buttons_to_download_unread(self):
self.driver.find_element_by_id('select_all').click()
checkboxes = self.driver.find_elements_by_name('doc_names_selected')
assert len(checkboxes) > 0
for checkbox in checkboxes:
assert checkbox.is_selected()
self.driver.find_element_by_id('select_none').click()
checkboxes = self.driver.find_elements_by_name('doc_names_selected')
for checkbox in checkboxes:
assert checkbox.is_selected() is False
self.driver.find_element_by_id('select_unread').click()
checkboxes = self.driver.find_elements_by_name('doc_names_selected')
for checkbox in checkboxes:
classes = checkbox.get_attribute('class')
assert 'unread-cb' in classes