forked from superm1/lvfs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvfs_test.py
executable file
·1675 lines (1402 loc) · 66.3 KB
/
lvfs_test.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Richard Hughes <richard@hughsie.com>
# Licensed under the GNU General Public License Version 2
#
# pylint: disable=fixme,too-many-public-methods,line-too-long,too-many-lines
from __future__ import print_function
import os
import unittest
import tempfile
import subprocess
import gzip
import cStringIO
def _gzip_decompress_buffer(buf):
fgz = cStringIO.StringIO()
fgz.write(buf)
fgz.seek(0)
with gzip.GzipFile(fileobj=fgz, mode='rb') as gzip_obj:
try:
buf_d = gzip_obj.read()
except IOError as e:
print(e, buf)
fgz.close()
return buf_d
class LvfsTestCase(unittest.TestCase):
def setUp(self):
# create new database
self.db_fd, self.db_filename = tempfile.mkstemp()
self.db_uri = 'sqlite:///' + self.db_filename
# write out custom settings file
self.cfg_fd, self.cfg_filename = tempfile.mkstemp()
cfgfile = open(self.cfg_filename, 'w')
cfgfile.write('\n'.join([
"SQLALCHEMY_DATABASE_URI = '%s'" % self.db_uri,
"DEBUG = True",
"RESTORE_DIR = '/tmp'",
"DOWNLOAD_DIR = '/tmp'",
"SECRET_PASSWORD_SALT = 'lvfs%%%'",
"SECRET_ADDR_SALT = 'addr%%%'",
"SECRET_VENDOR_SALT = 'vendor%%%'",
"MAIL_SUPPRESS_SEND = True",
]))
cfgfile.close()
# create instance
import app as lvfs
from app import db
from app.dbutils import init_db
self.app = lvfs.app.test_client()
lvfs.app.config.from_pyfile(self.cfg_filename)
with lvfs.app.app_context():
init_db(db)
# ensure the plugins settings are set up
self.login()
self.app.get('/lvfs/settings_create')
self.app.get('/lvfs/agreement/create')
self.app.get('/lvfs/agreement/1/accept')
self.logout()
def tearDown(self):
os.close(self.db_fd)
os.unlink(self.db_filename)
os.close(self.cfg_fd)
os.unlink(self.cfg_filename)
def _login(self, username, password='Pa$$w0rd'):
return self.app.post('/lvfs/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def _logout(self):
return self.app.get('/lvfs/logout', follow_redirects=True)
def login(self, username='sign-test@fwupd.org', password='Pa$$w0rd', accept_agreement=True):
rv = self._login(username, password)
assert b'/lvfs/upload' in rv.data, rv.data
assert b'Incorrect username or password' not in rv.data, rv.data
if accept_agreement and username != 'sign-test@fwupd.org':
rv = self.app.get('/lvfs/agreement/1/accept', follow_redirects=True)
assert b'Recorded acceptance of the agreement' in rv.data, rv.data
def logout(self):
rv = self._logout()
assert b'Logged out' in rv.data, rv.data
assert b'/lvfs/upload' not in rv.data, rv.data
def delete_firmware(self, firmware_id=1):
rv = self.app.get('/lvfs/firmware/%i/delete' % firmware_id,
follow_redirects=True)
assert b'Firmware deleted' in rv.data, rv.data
def _add_user(self, username, group_id, password):
return self.app.post('/lvfs/user/add', data=dict(
username=username,
password_new=password,
group_id=group_id,
display_name=u'Generic Name',
), follow_redirects=True)
def add_user(self, username='testuser@fwupd.org', group_id='testgroup',
password='Pa$$w0rd', is_qa=False, is_analyst=False,
is_vendor_manager=False, is_approved_public=False, is_robot=False):
rv = self._add_user(username, group_id, password)
assert b'Added user' in rv.data, rv.data
user_id_idx = rv.data.find('Added user ')
assert user_id_idx != -1, rv.data
user_id = int(rv.data[user_id_idx+11:user_id_idx+12])
assert user_id != 0, rv.data
if is_qa or is_analyst or is_vendor_manager or is_approved_public or is_robot:
data = {'auth_type': 'local'}
if is_qa:
data['is_qa'] = '1'
if is_analyst:
data['is_analyst'] = '1'
if is_vendor_manager:
data['is_vendor_manager'] = '1'
if is_approved_public:
data['is_approved_public'] = '1'
if is_robot:
data['is_robot'] = '1'
rv = self.app.post('/lvfs/user/%i/modify_by_admin' % user_id,
data=data, follow_redirects=True)
assert b'Updated profile' in rv.data, rv.data
def _upload(self, filename='contrib/hughski-colorhug2-2.0.3.cab', target='private', vendor_id=None):
fd = open(filename, 'rb')
data = {
'target': target,
'file': (fd, filename)
}
if vendor_id:
data['vendor_id'] = vendor_id
return self.app.post('/lvfs/upload', data=data, follow_redirects=True)
def upload(self, target='private', vendor_id=None, filename='contrib/hughski-colorhug2-2.0.3.cab'):
rv = self._upload(filename, target, vendor_id)
assert b'Uploaded file' in rv.data, rv.data
assert b'7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
def test_login_logout(self):
# test logging in and out
rv = self._login('sign-test@fwupd.org', u'Pa$$w0rd')
assert b'/lvfs/upload' in rv.data, rv.data
rv = self._logout()
rv = self._login('sign-test@fwupd.org', u'Pa$$w0rd')
assert b'/lvfs/upload' in rv.data, rv.data
rv = self._logout()
assert b'/lvfs/upload' not in rv.data, rv.data
rv = self._login('sign-test@fwupd.orgx', u'default')
assert b'Incorrect username or password' in rv.data, rv.data
rv = self._login('sign-test@fwupd.org', u'defaultx')
assert b'Incorrect username or password' in rv.data, rv.data
def test_upload_invalid(self):
# upload something that isn't a cabinet archive
self.login()
rv = self._upload('contrib/Dockerfile', 'private')
assert b'Failed to upload file' in rv.data, rv.data
rv = self._upload('contrib/hughski-colorhug2-2.0.3.cab', 'NOTVALID')
assert b'Target not valid' in rv.data, rv.data
def test_firmware_nuke(self):
# upload firmware
self.login()
rv = self._upload('contrib/hughski-colorhug2-2.0.3.cab', 'private')
assert b'7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1')
assert b'>☠ Nuke ☠<' not in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/nuke', follow_redirects=True)
assert b'Cannot nuke file not yet deleted' in rv.data, rv.data
self.delete_firmware()
rv = self.app.get('/lvfs/firmware/1')
assert b'>☠ Nuke ☠<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/nuke', follow_redirects=True)
assert b'No firmware has been uploaded' in rv.data, rv.data
def test_upload_valid(self):
# upload firmware
self.login()
rv = self._upload('contrib/hughski-colorhug2-2.0.3.cab', 'private')
assert b'7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/components')
assert b'com.hughski.ColorHug2.firmware' in rv.data, rv.data
# download
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab')
assert rv.status_code == 200, rv.status_code
assert len(rv.data) == 10974, len(rv.data)
# check analytics works
uris = ['/lvfs/firmware/1/analytics',
'/lvfs/firmware/1/analytics/clients',
'/lvfs/firmware/1/analytics/month',
'/lvfs/firmware/1/analytics/reports',
'/lvfs/firmware/1/analytics/year']
for uri in uris:
rv = self.app.get(uri)
assert b'favicon.ico' in rv.data, rv.data
assert b'LVFS: Error' not in rv.data, rv.data
# check component view shows GUID
rv = self.app.get('/lvfs/component/1')
assert b'2082b5e0-7a64-478a-b1b2-e3404fab6dad' in rv.data, rv.data
# check devices page shows private firmware as admin -- and hidden when anon
rv = self.app.get('/lvfs/device')
assert b'2082b5e0-7a64-478a-b1b2-e3404fab6dad' in rv.data, rv.data
rv = self.app.get('/lvfs/device/2082b5e0-7a64-478a-b1b2-e3404fab6dad')
assert b'MCDC04 errata' in rv.data, rv.data
self.logout()
# check private firmware isn't visible when not logged in
rv = self.app.get('/lvfs/device')
assert b'2082b5e0-7a64-478a-b1b2-e3404fab6dad' not in rv.data, rv.data
rv = self.app.get('/lvfs/device/2082b5e0-7a64-478a-b1b2-e3404fab6dad')
# FIXME is it a bug that we show the device exists even though it's not got any mds?
assert b'MCDC04 errata' not in rv.data, rv.data
rv = self.app.get('/lvfs/devicelist')
assert b'ColorHug' not in rv.data, rv.data
self.login()
# promote the firmware to testing then stable
self.run_cron_firmware()
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'>testing<' in rv.data, rv.data
assert b'>stable<' not in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/stable',
follow_redirects=True)
assert b'>stable<' in rv.data, rv.data
# check it's now in the devicelist as anon
self.logout()
rv = self.app.get('/lvfs/devicelist')
assert b'ColorHug' in rv.data, rv.data
self.login()
# download it
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab')
assert rv.status_code == 200, rv.status_code
# test deleting the firmware
self.delete_firmware()
# download missing file
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab')
assert rv.status_code == 410, rv.status_code
# re-upload the same file
rv = self._upload('contrib/hughski-colorhug2-2.0.3.cab', 'private')
assert b'Failed to upload file: A file with hash' in rv.data, rv.data
# undelete it
rv = self.app.get('/lvfs/firmware/1/undelete', follow_redirects=True)
assert b'Firmware undeleted' in rv.data, rv.data
def test_user_delete_wrong_user(self):
# create user
self.login()
self.add_user('testuser@fwupd.org')
self.add_user('otheruser@fwupd.org')
self.logout()
# upload as testuser
self.login('testuser@fwupd.org')
self.upload()
self.logout()
# try to delete as otheruser
self.login('otheruser@fwupd.org')
rv = self.app.get('/lvfs/firmware/1/delete',
follow_redirects=True)
assert b'Firmware deleted' not in rv.data, rv.data
assert b'Insufficient permissions to delete firmware' in rv.data, rv.data
def test_user_delete_qa_wrong_group(self):
# create user
self.login()
self.add_user('testuser@fwupd.org')
self.add_user('otheruser@fwupd.org', 'different_group', is_qa=True)
self.logout()
# upload as testuser
self.login('testuser@fwupd.org')
self.upload()
self.logout()
# try to delete as otheruser
self.login('otheruser@fwupd.org')
rv = self.app.get('/lvfs/firmware/1/delete',
follow_redirects=True)
assert b'Firmware deleted' not in rv.data, rv.data
assert b'Insufficient permissions to delete firmware' in rv.data, rv.data
def test_firmware_limits(self):
# upload firmware
self.login()
self.upload()
# check no limits set
rv = self.app.get('/lvfs/firmware/1/limits',
follow_redirects=True)
assert rv.status_code == 200, rv.status_code
assert b'ETOOSLOW' not in rv.data, rv.data
# set download limit of 2
rv = self.app.post('/lvfs/firmware/limit/add', data=dict(
firmware_id='1',
value='2',
user_agent_glob='fwupd/*',
response='ETOOSLOW',
), follow_redirects=True)
assert b'ETOOSLOW' in rv.data, rv.data
assert b'Added limit' in rv.data, rv.data
# download twice, both, success
for _ in range(2):
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab',
environ_base={'HTTP_USER_AGENT': 'fwupd/1.1.1'})
assert rv.status_code == 200, rv.status_code
# download, fail
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab',
environ_base={'HTTP_USER_AGENT': 'fwupd/1.1.1'})
assert rv.status_code == 429, rv.status_code
assert rv.data == 'ETOOSLOW', rv.data
# download not matching glob, success
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab',
environ_base={'HTTP_USER_AGENT': 'wget/1.2.3'})
assert rv.status_code == 200, rv.status_code
# delete download limit
rv = self.app.get('/lvfs/firmware/limit/1/delete',
follow_redirects=True)
assert b'Deleted limit' in rv.data, rv.data
# check no limits set
rv = self.app.get('/lvfs/firmware/1/limits',
follow_redirects=True)
assert rv.status_code == 200, rv.status_code
assert b'ETOOSLOW' not in rv.data, rv.data
# download, success
rv = self.app.get('/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab',
environ_base={'HTTP_USER_AGENT': 'fwupd/1.1.1'})
assert rv.status_code == 200, rv.status_code
def _run_cron(self, kind='metadata'):
env = {}
env['LVFS_CUSTOM_SETTINGS'] = self.cfg_filename
ps = subprocess.Popen(['./cron.py', kind], env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, _ = ps.communicate()
return stdout
def run_cron_firmware(self, fn='hughski-colorhug2-2.0.3'):
stdout = self._run_cron('firmware')
assert fn in stdout, stdout
def run_cron_metadata(self, remote_ids=None):
stdout = self._run_cron('metadata')
if remote_ids:
for remote_id in remote_ids:
assert 'Updating: %s' % remote_id in stdout, stdout
def test_cron_metadata(self):
# verify all metadata is in good shape
self.login()
rv = self.app.get('/lvfs/metadata')
assert b'Will be signed in' not in rv.data, rv.data
# upload file, dirtying the admin-embargo remote
self.upload('embargo')
rv = self.app.get('/lvfs/metadata')
assert b'Will be signed in' in rv.data, rv.data
# run the cron job manually
self.run_cron_metadata(['embargo-admin'])
# verify all metadata is in good shape
rv = self.app.get('/lvfs/metadata')
assert b'Will be signed in' not in rv.data, rv.data
def test_cron_firmware(self):
# upload file, which will be unsigned
self.login()
self.upload('embargo')
rv = self.app.get('/lvfs/firmware/1')
assert b'>Signed<' not in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/problems')
assert b'Firmware is unsigned' in rv.data, rv.data
# run the cron job manually
env = {}
env['LVFS_CUSTOM_SETTINGS'] = self.cfg_filename
ps = subprocess.Popen(['./cron.py', 'firmware'], env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, _ = ps.communicate()
assert 'Signing: /tmp/7514fc4b0e1a306337de78c58f10e9e68f791de2-hughski-colorhug2-2.0.3.cab' in stdout, stdout
# verify the firmware is now signed
rv = self.app.get('/lvfs/firmware/1')
assert b'>Signed<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/problems')
assert b'Firmware is unsigned' not in rv.data, rv.data
def test_user_only_view_own_firmware(self):
# create User:alice, User:bob, Analyst:clara, and QA:mario
self.login()
self.add_user('alice@fwupd.org')
self.add_user('bob@fwupd.org')
self.add_user('clara@fwupd.org', is_analyst=True)
self.add_user('mario@fwupd.org', is_qa=True, is_approved_public=True)
self.logout()
# let alice upload a file to embargo
self.login('alice@fwupd.org')
self.upload('embargo')
rv = self.app.get('/lvfs/firmware/1')
assert b'/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware')
assert b'/lvfs/firmware/1' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/analytics/clients')
assert b'Insufficient permissions to view analytics' in rv.data, rv.data
self.logout()
# bob can't see the file, nor can upload a duplicate
self.login('bob@fwupd.org')
rv = self._upload('contrib/hughski-colorhug2-2.0.3.cab', 'embargo')
assert b'Another user has already uploaded this firmware' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1')
assert b'Insufficient permissions to view firmware' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware')
assert b'No firmware has been uploaded' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/analytics/clients')
assert b'Insufficient permissions to view analytics' in rv.data, rv.data
self.logout()
# clara can see all firmwares, but can't promote them
self.login('clara@fwupd.org')
rv = self.app.get('/lvfs/firmware/1')
assert b'/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware')
assert b'/lvfs/firmware/1' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/analytics/clients')
assert b'User Agent' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'Permission denied: No QA access' in rv.data, rv.data
self.logout()
# mario can see things from both users and promote
self.login('mario@fwupd.org')
rv = self.app.get('/lvfs/firmware/1')
assert b'/downloads/7514fc4b0e1a306337de78c58f10e9e68f791de2' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware')
assert b'/lvfs/firmware/1' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/analytics/clients')
assert b'User Agent' in rv.data, rv.data
self.run_cron_firmware()
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'>testing<' in rv.data, rv.data
self.logout()
def test_eventlog(self):
# login, upload then check both events were logged
self.login()
self.add_user('alice@fwupd.org')
self.add_user('bob@fwupd.org', is_qa=True)
self.add_user('mario@oem.com', is_qa=True, group_id='oem')
self.logout()
# alice cannot see her own event
self.login('alice@fwupd.org')
self.upload()
rv = self.app.get('/lvfs/eventlog')
assert b'Unable to show event log for non-QA user' in rv.data, rv.data
assert b'Uploaded file' not in rv.data, rv.data
assert b'Logged in' not in rv.data, rv.data
self.logout()
# sign firmware, to create a admin-only event
self.run_cron_firmware()
# mario can't see anything as he's in a different vendor group
self.login('mario@oem.com')
rv = self.app.get('/lvfs/eventlog')
assert b'Uploaded file' not in rv.data, rv.data
assert b'Logged in' in rv.data, rv.data
assert b'Signed firmware' not in rv.data, rv.data
assert b'mario@oem.com' in rv.data, rv.data
assert b'alice@fwupd.org' not in rv.data, rv.data
self.logout()
# bob is QA and can see just event for his vendor group
self.login('bob@fwupd.org')
rv = self.app.get('/lvfs/eventlog')
assert b'Uploaded file' in rv.data, rv.data
assert b'Logged in' in rv.data, rv.data
assert b'>anonymous<' not in rv.data, rv.data
assert b'Signed firmware' not in rv.data, rv.data
assert b'mario@oem.com' not in rv.data, rv.data
assert b'alice@fwupd.org' in rv.data, rv.data
self.logout()
# root can see everything
self.login()
rv = self.app.get('/lvfs/eventlog')
assert b'Uploaded file' in rv.data, rv.data
assert b'Logged in' in rv.data, rv.data
assert b'Signed firmware' in rv.data, rv.data
assert b'alice@fwupd.org' in rv.data, rv.data
assert b'bob@fwupd.org' in rv.data, rv.data
assert b'mario@oem.com' in rv.data, rv.data
def test_vendorlist(self):
# check users can't modify the list
rv = self.app.get('/lvfs/vendorlist')
assert b'Create a new vendor' not in rv.data, rv.data
# check admin can
self.login()
rv = self.app.get('/lvfs/vendorlist')
assert b'Create a new vendor' in rv.data, rv.data
# create new vendor
rv = self.app.post('/lvfs/vendor/add', data=dict(group_id='testvendor'),
follow_redirects=True)
assert b'Added vendor' in rv.data, rv.data
rv = self.app.get('/lvfs/vendorlist')
assert b'testvendor' in rv.data, rv.data
# create duplicate
rv = self.app.post('/lvfs/vendor/add', data=dict(group_id='testvendor'),
follow_redirects=True)
assert b'Group ID already exists' in rv.data, rv.data
# show the details page
rv = self.app.get('/lvfs/vendor/2/details')
assert b'testvendor' in rv.data, rv.data
# create a restriction
rv = self.app.post('/lvfs/vendor/2/restriction/add', data=dict(value='USB:0x1234'),
follow_redirects=True)
assert b'Added restriction' in rv.data, rv.data
# show the restrictions page
rv = self.app.get('/lvfs/vendor/2/restrictions')
assert b'USB:0x1234' in rv.data, rv.data
# delete a restriction
rv = self.app.get('/lvfs/vendor/2/restriction/1/delete', follow_redirects=True)
assert b'Deleted restriction' in rv.data, rv.data
assert b'USB:0x1234' not in rv.data, rv.data
# change some properties
rv = self.app.post('/lvfs/vendor/2/modify_by_admin', data=dict(
display_name='VendorName',
plugins='dfu 1.2.3',
description='Everything supported',
visible=True,
is_fwupd_supported='1',
is_account_holder='1',
is_uploading='1',
keywords='keyword',
comments='Emailed Dave on 2018-01-14 to follow up.',
), follow_redirects=True)
assert b'Updated vendor' in rv.data, rv.data
rv = self.app.get('/lvfs/vendorlist')
assert b'testvendor' in rv.data, rv.data
assert b'Everything supported' in rv.data, rv.data
assert b'Emailed Dave' not in rv.data, rv.data
# delete
rv = self.app.get('/lvfs/vendor/999/delete', follow_redirects=True)
assert b'No a vendor with that group ID' in rv.data, rv.data
rv = self.app.get('/lvfs/vendor/2/delete', follow_redirects=True)
assert b'Removed vendor' in rv.data, rv.data
rv = self.app.get('/lvfs/vendorlist')
assert b'testvendor' not in rv.data, rv.data
def add_vendor(self, group_id):
rv = self.app.post('/lvfs/vendor/add', data=dict(group_id=group_id),
follow_redirects=True)
assert b'Added vendor' in rv.data, rv.data
def test_vendor_split_with_reparent(self):
# add vendor1 and user, then upload firmware
self.login()
self.add_vendor('acme') # 2
self.add_user('alice@acme.com', 'acme') # 3
self.logout()
self.login('alice@acme.com')
self.upload()
self.logout()
# add vendor2 and move user to that
self.login()
self.add_vendor('odm') # 3
rv = self.app.post('/lvfs/user/3/modify_by_admin', data=dict(
vendor_id='3',
reparent='1',
), follow_redirects=True)
assert b'Updated profile' in rv.data, rv.data
rv = self.app.get('/lvfs/userlist')
assert b'>odm<' in rv.data, rv.data
assert b'>acme<' not in rv.data, rv.data
# ensure firmware is reparented
rv = self.app.get('/lvfs/firmware/1')
assert b'odm</a>' in rv.data, rv.data
self.logout()
# ensure user can still view firmware
self.login('alice@acme.com', accept_agreement=False)
rv = self.app.get('/lvfs/firmware')
assert b'Hughski Limited ColorHug2 Device Update' in rv.data, rv.data
def test_users(self):
# login then add invalid users
self.login()
rv = self._add_user('testuser@fwupd.org', 'testgroup', u'unsuitable')
assert b'requires at least one uppercase character' in rv.data, rv.data
rv = self._add_user('testuser', 'testgroup', u'Pa$$w0rd')
assert b'Invalid email address' in rv.data, rv.data
rv = self._add_user('testuser@fwupd.org', 'XX', u'Pa$$w0rd')
assert b'QA group invalid' in rv.data, rv.data
# add a good user, and check the user and group was created
rv = self._add_user('testuser@fwupd.org', 'testgroup', u'Pa$$w0rd')
assert b'Added user' in rv.data, rv.data
rv = self.app.get('/lvfs/userlist')
assert b'testuser' in rv.data, rv.data
rv = self.app.get('/lvfs/user/3/admin')
assert b'testuser@fwupd.org' in rv.data, rv.data
rv = self.app.get('/lvfs/vendorlist')
assert b'testgroup' in rv.data, rv.data
# modify an existing user as the admin
rv = self.app.post('/lvfs/user/3/modify_by_admin', data=dict(
auth_type='local',
auth_warning='Caveat Emptor',
is_qa='1',
is_analyst='1',
group_id='testgroup',
display_name='Slightly Less Generic Name',
), follow_redirects=True)
assert b'Updated profile' in rv.data, rv.data
rv = self.app.get('/lvfs/user/3/admin')
assert b'Slightly Less Generic Name' in rv.data, rv.data
# ensure the user can log in
self.logout()
rv = self._login('testuser@fwupd.org')
assert b'/lvfs/upload' in rv.data, rv.data
assert b'Caveat Emptor' in rv.data, rv.data
# ensure the user can change thier own password
rv = self.app.post('/lvfs/user/3/modify', data=dict(
password_old='not-even-close',
password_new='Hi$$t0ry',
display_name='Something Funky',
), follow_redirects=True)
assert b'Incorrect existing password' in rv.data, rv.data
rv = self.app.post('/lvfs/user/3/modify', data=dict(
password_old='Pa$$w0rd',
password_new='Hi$$t0ry',
display_name='Something Funky',
), follow_redirects=True)
assert b'Updated profile' in rv.data, rv.data
rv = self.app.get('/lvfs/profile')
assert b'Something Funky' in rv.data, rv.data
# try to self-delete
rv = self.app.get('/lvfs/user/3/delete')
assert b'Unable to remove user as not admin' in rv.data, rv.data
# delete the user as the admin
self.logout()
self.login()
rv = self.app.get('/lvfs/user/3/delete', follow_redirects=True)
assert b'Deleted user' in rv.data, rv.data
rv = self.app.get('/lvfs/userlist')
assert b'testuser@fwupd.org' not in rv.data, rv.data
def test_manager_users(self):
# create a new vendor
self.login()
rv = self.app.post('/lvfs/vendor/add', data=dict(group_id='testvendor'),
follow_redirects=True)
assert b'Added vendor' in rv.data, rv.data
# set the username glob
rv = self.app.post('/lvfs/vendor/2/modify_by_admin', data=dict(
username_glob='*@testvendor.com,*@anothervendor.com',
), follow_redirects=True)
assert b'Updated vendor' in rv.data, rv.data
# create a manager user
self.add_user('alice@testvendor.com', group_id='testvendor', is_vendor_manager=True)
# log in as the manager
self.logout()
self.login('alice@testvendor.com')
# try to add new user to new vendor with non-matching domain (fail)
rv = self.app.post('/lvfs/vendor/2/user/add', data=dict(
username='bob@hotmail.com',
display_name=u'Generic Name',
), follow_redirects=True)
assert b'Email address does not match account policy' in rv.data, rv.data
# add new user with matching domain
rv = self.app.post('/lvfs/vendor/2/user/add', data=dict(
username='clara@testvendor.com',
display_name=u'Generic Name',
), follow_redirects=True)
assert b'Added user' in rv.data, rv.data
# change the new user to allow a local login
rv = self.app.post('/lvfs/user/4/modify_by_admin', data=dict(
auth_type='local',
password='Pa$$w0rd',
is_vendor_manager=True,
), follow_redirects=True)
assert b'Updated profile' in rv.data, rv.data
# log in as the new user
self.logout()
self.login('clara@testvendor.com')
def test_promote_as_user(self):
# create User
self.login()
self.add_user('testuser@fwupd.org')
self.logout()
# login as user, upload file, then promote
self.login('testuser@fwupd.org')
self.upload()
rv = self.app.get('/lvfs/firmware/1/promote/embargo',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>embargo-testgroup<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/embargo',
follow_redirects=True)
assert b'Firmware already in that target' in rv.data, rv.data
assert b'>embargo-testgroup<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'Permission denied' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/stable',
follow_redirects=True)
assert b'Permission denied' in rv.data, rv.data
# demote back to private
rv = self.app.get('/lvfs/firmware/1/promote/private',
follow_redirects=True)
assert b'>private<' in rv.data, rv.data
assert b'Moved firmware' in rv.data, rv.data
def test_promote_as_qa(self):
# login as user, upload file, then promote FIXME: do as QA user, not admin
self.login()
self.upload()
rv = self.app.get('/lvfs/firmware/1/promote/embargo',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>embargo-admin<' in rv.data, rv.data
self.run_cron_firmware()
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>testing<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/stable',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>stable<' in rv.data, rv.data
# demote back to testing then private
rv = self.app.get('/lvfs/firmware/1/promote/testing',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>testing<' in rv.data, rv.data
rv = self.app.get('/lvfs/firmware/1/promote/private',
follow_redirects=True)
assert b'Moved firmware' in rv.data, rv.data
assert b'>private<' in rv.data, rv.data
def _report(self, updatestate=2, distro_id='fedora', checksum='3f1b8ec0fa8ee323d1934a0256037c8100175755'):
return self.app.post('/lvfs/firmware/report', data=
'{'
' "ReportVersion" : 2,'
' "MachineId" : "abc",'
' "Metadata" : {'
' "DistroId" : "%s",'
' "DistroVersion" : "27",'
' "DistroVariant" : "workstation"'
' },'
' "Reports" : ['
' {'
' "Checksum" : "%s",'
' "UpdateState" : %i,'
' "UpdateError" : "UEFI firmware update failed: failed to make /boot/efi/EFI/arch/fw: No such file or directory",'
' "Guid" : "7514fc4b0e1a306337de78c58f10e9e68f791de2",'
' "Plugin" : "colorhug",'
' "VersionOld" : "2.0.0",'
' "VersionNew" : "2.0.3",'
' "Flags" : 34,'
' "Created" : 1518212684,'
' "Modified" : 1518212754,'
' "Metadata" : {'
' "AppstreamGlibVersion" : "0.7.5",'
' "CpuArchitecture" : "x86_64",'
' "FwupdVersion" : "1.0.5",'
' "GUsbVersion" : "0.2.11",'
' "BootTime" : "1518082325",'
' "KernelVersion" : "4.14.16-300.fc27.x86_64"'
' }'
' }'
' ]'
'}' % (distro_id, checksum, updatestate))
def test_reports(self):
# upload a firmware that can receive a report
self.login()
self.upload(target='testing')
# send empty
rv = self.app.post('/lvfs/firmware/report')
assert b'No JSON object could be decoded' in rv.data, rv.data
# self less than what we need
rv = self.app.post('/lvfs/firmware/report', data='{"MachineId" : "abc"}')
assert b'invalid data, expected ReportVersion' in rv.data, rv.data
# send a valid report for firmware that is not known to us
rv = self._report(checksum='c0243a8553f19d3c405004d3642d1485a723c948')
assert b'c0243a8553f19d3c405004d3642d1485a723c948 did not match any known firmware archive' in rv.data, rv.data
# send a valid report for firmware that is known
rv = self._report(updatestate=3)
assert b'"success": true' in rv.data, rv.data
assert b'replaces old report' not in rv.data, rv.data
# send an update
rv = self._report()
assert b'"success": true' in rv.data, rv.data
assert b'replaces old report' in rv.data, rv.data
# get a report that does not exist
rv = self.app.get('/lvfs/report/123456')
assert b'Report does not exist' in rv.data, rv.data
# check the saved report
rv = self.app.get('/lvfs/report/1')
assert b'UpdateState=success' in rv.data, rv.data
# check the report appeared on the telemetry page
rv = self.app.get('/lvfs/telemetry')
assert b'ColorHug2 Device Update' in rv.data, rv.data
assert b'>1<' in rv.data, rv.data
# delete the report
rv = self.app.get('/lvfs/report/1/delete', follow_redirects=True)
assert b'Deleted report' in rv.data, rv.data
# check it is really deleted
rv = self.app.get('/lvfs/report/1')
assert b'Report does not exist' in rv.data, rv.data
def test_settings(self):
# open the main page
self.login()
rv = self.app.get('/lvfs/settings')
assert b'General server settings' in rv.data, rv.data
assert b'Windows Update' in rv.data, rv.data
# dig into the Windows Update page
rv = self.app.get('/lvfs/settings/wu-copy')
assert b'Copy files generated' in rv.data, rv.data
assert b'value="enabled" checked>' in rv.data, rv.data
# change both values to False
rv = self.app.post('/lvfs/settings/modify/wu-copy', data=dict(
wu_copy_inf='disabled',
wu_copy_cat='disabled',
), follow_redirects=True)
assert b'Copy files generated' in rv.data, rv.data
assert b'value="enabled">' in rv.data, rv.data
# and back to True
rv = self.app.post('/lvfs/settings/modify/wu-copy', data=dict(
wu_copy_inf='enabled',
wu_copy_cat='enabled',
), follow_redirects=True)
assert b'value="enabled" checked>' in rv.data, rv.data
def test_updateinfo(self):
# get the default update info from the firmware archive
self.login()
self.upload()
rv = self.app.get('/lvfs/component/1/update')
assert b'Work around the MCDC04 errata' in rv.data, rv.data
assert b'value="low" selected' in rv.data, rv.data
# edit the description and severity
rv = self.app.post('/lvfs/component/1/modify', data=dict(
urgency='critical',
description=u'Not enough cats!',
), follow_redirects=True)
assert b'Component updated' in rv.data, rv.data
# verify the new update info
rv = self.app.get('/lvfs/component/1/update')
assert b'Not enough cats' in rv.data, rv.data
assert b'value="critical" selected' in rv.data, rv.data
def test_requires(self):
# check existing requires were added
self.login()
self.upload()
# check requirements were copied out from the .metainfo.xml file
rv = self.app.get('/lvfs/component/1/requires')
assert b'85d38fda-fc0e-5c6f-808f-076984ae7978' in rv.data, rv.data
assert b'name="version" value="1.0.3' in rv.data, rv.data
assert b'ge" selected' in rv.data, rv.data
assert b'regex" selected' in rv.data, rv.data
assert b'BOT03.0[2-9]_*' in rv.data, rv.data
# remove the CHID requirement
rv = self.app.get('/lvfs/component/1/requirement/delete/3', follow_redirects=True)
assert b'Removed requirement 85d38fda-fc0e-5c6f-808f-076984ae7978' in rv.data, rv.data
# add an invalid CHID
rv = self.app.post('/lvfs/component/1/requirement/add', data=dict(
kind='hardware',
value='NOVALIDGUID',
), follow_redirects=True)
assert b'NOVALIDGUID is not a valid GUID' in rv.data, rv.data
# add a valid CHID
rv = self.app.post('/lvfs/component/1/requirement/add', data=dict(
kind='hardware',
value='85d38fda-fc0e-5c6f-808f-076984ae7978',
), follow_redirects=True)
assert b'85d38fda-fc0e-5c6f-808f-076984ae7978' in rv.data, rv.data
assert b'Added requirement' in rv.data, rv.data
# modify an existing requirement by adding it again
rv = self.app.post('/lvfs/component/1/requirement/add', data=dict(
kind='id',
value='org.freedesktop.fwupd',
compare='ge',
version='1.0.4',
), follow_redirects=True)
assert b'name="version" value="1.0.4' in rv.data, rv.data
assert b'Modified requirement' in rv.data, rv.data
# delete a requirement by adding an 'any' comparison
rv = self.app.post('/lvfs/component/1/requirement/add', data=dict(
kind='id',
value='org.freedesktop.fwupd',
compare='any',