forked from firebase/firebase-admin-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_project_management.py
More file actions
1250 lines (1045 loc) · 54.7 KB
/
test_project_management.py
File metadata and controls
1250 lines (1045 loc) · 54.7 KB
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
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for firebase_admin.project_management."""
import base64
import json
import pytest
import firebase_admin
from firebase_admin import exceptions
from firebase_admin import project_management
from firebase_admin import _http_client
from tests import testutils
OPERATION_IN_PROGRESS_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': False
})
OPERATION_FAILED_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': True,
'error': 'some error',
})
ANDROID_APP_OPERATION_SUCCESSFUL_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': True,
'response': {
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'displayName': 'My Android App',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
},
})
ANDROID_APP_NO_DISPLAY_NAME_OPERATION_SUCCESSFUL_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': True,
'response': {
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
},
})
ANDROID_APP_METADATA_RESPONSE = json.dumps({
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'displayName': 'My Android App',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
})
ANDROID_APP_NO_DISPLAY_NAME_METADATA_RESPONSE = json.dumps({
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
})
IOS_APP_OPERATION_SUCCESSFUL_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': True,
'response': {
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'displayName': 'My iOS App',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
},
})
IOS_APP_NO_DISPLAY_NAME_OPERATION_SUCCESSFUL_RESPONSE = json.dumps({
'name': 'operations/abcdefg',
'done': True,
'response': {
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
},
})
IOS_APP_METADATA_RESPONSE = json.dumps({
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'displayName': 'My iOS App',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
})
IOS_APP_NO_DISPLAY_NAME_METADATA_RESPONSE = json.dumps({
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
})
LIST_ANDROID_APPS_RESPONSE = json.dumps({'apps': [
{
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'displayName': 'My Android App',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
},
{
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeefcafe',
'appId': '1:12345678:android:deadbeefcafe',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android2',
}]})
LIST_ANDROID_APPS_PAGE_1_RESPONSE = json.dumps({
'apps': [{
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeef',
'appId': '1:12345678:android:deadbeef',
'displayName': 'My Android App',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android',
}],
'nextPageToken': 'nextpagetoken',
})
LIST_ANDROID_APPS_PAGE_2_RESPONSE = json.dumps({
'apps': [{
'name': 'projects/test-project-id/androidApps/1:12345678:android:deadbeefcafe',
'appId': '1:12345678:android:deadbeefcafe',
'projectId': 'test-project-id',
'packageName': 'com.hello.world.android2',
}]})
LIST_IOS_APPS_RESPONSE = json.dumps({'apps': [
{
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'displayName': 'My iOS App',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
},
{
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5cafe',
'appId': '1:12345678:ios:ca5cade5cafe',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios2',
}]})
LIST_IOS_APPS_PAGE_1_RESPONSE = json.dumps({
'apps': [{
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
'appId': '1:12345678:ios:ca5cade5',
'displayName': 'My iOS App',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios',
}],
'nextPageToken': 'nextpagetoken',
})
LIST_IOS_APPS_PAGE_2_RESPONSE = json.dumps({
'apps': [{
'name': 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5cafe',
'appId': '1:12345678:ios:ca5cade5cafe',
'projectId': 'test-project-id',
'bundleId': 'com.hello.world.ios2',
}]})
# In Python 2.7, the base64 module works with strings, while in Python 3, it works with bytes
# objects. This line works in both versions.
TEST_APP_ENCODED_CONFIG = base64.standard_b64encode('hello world'.encode('utf-8')).decode('utf-8')
TEST_APP_CONFIG_RESPONSE = json.dumps({
'configFilename': 'hello',
'configFileContents': TEST_APP_ENCODED_CONFIG,
})
SHA_1_CERTIFICATE = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a',
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1')
SHA_256_CERTIFICATE = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a123456789a123456789a1234',
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name256')
GET_SHA_CERTIFICATES_RESPONSE = json.dumps({'certificates': [
{'name': cert.name, 'shaHash': cert.sha_hash, 'certType': cert.cert_type}
for cert in [SHA_1_CERTIFICATE, SHA_256_CERTIFICATE]
]})
ANDROID_APP_METADATA = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
IOS_APP_METADATA = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
ALREADY_EXISTS_RESPONSE = ('{"error": {"status": "ALREADY_EXISTS", '
'"message": "The resource already exists"}}')
NOT_FOUND_RESPONSE = '{"error": {"message": "Failed to find the resource"}}'
UNAVAILABLE_RESPONSE = '{"error": {"message": "Backend servers are over capacity"}}'
class TestAndroidAppMetadata:
def test_create_android_app_metadata_errors(self):
# package_name must be a non-empty string.
with pytest.raises(ValueError):
project_management.AndroidAppMetadata(
package_name='',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
# name must be a non-empty string.
with pytest.raises(ValueError):
project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
# app_id must be a non-empty string.
with pytest.raises(ValueError):
project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='',
display_name='My Android App',
project_id='test-project-id')
# display_name must be a string or None.
with pytest.raises(ValueError):
project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name=0,
project_id='test-project-id')
# project_id must be a nonempty string.
with pytest.raises(ValueError):
project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
project_id='')
def test_android_app_metadata_eq_and_hash(self):
metadata_1 = ANDROID_APP_METADATA
metadata_2 = project_management.AndroidAppMetadata(
package_name='different',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
metadata_3 = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='different',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
metadata_4 = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='different',
display_name='My Android App',
project_id='test-project-id')
metadata_5 = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name=None,
project_id='test-project-id')
metadata_6 = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='different')
metadata_7 = project_management.AndroidAppMetadata(
package_name='com.hello.world.android',
name='projects/test-project-id/androidApps/1:12345678:android:deadbeef',
app_id='1:12345678:android:deadbeef',
display_name='My Android App',
project_id='test-project-id')
ios_metadata = IOS_APP_METADATA
# Don't trigger __ne__.
assert not metadata_1 == ios_metadata # pylint: disable=unneeded-not
assert metadata_1 != ios_metadata
assert metadata_1 != metadata_2
assert metadata_1 != metadata_3
assert metadata_1 != metadata_4
assert metadata_1 != metadata_5
assert metadata_1 != metadata_6
assert metadata_1 == metadata_7
assert set([metadata_1, metadata_2, metadata_7]) == set([metadata_1, metadata_2])
def test_android_app_metadata_package_name(self):
assert ANDROID_APP_METADATA.package_name == 'com.hello.world.android'
def test_android_app_metadata_name(self):
assert (ANDROID_APP_METADATA._name ==
'projects/test-project-id/androidApps/1:12345678:android:deadbeef')
def test_android_app_metadata_app_id(self):
assert ANDROID_APP_METADATA.app_id == '1:12345678:android:deadbeef'
def test_android_app_metadata_display_name(self):
assert ANDROID_APP_METADATA.display_name == 'My Android App'
def test_android_app_metadata_project_id(self):
assert ANDROID_APP_METADATA.project_id == 'test-project-id'
class TestIOSAppMetadata:
def test_create_ios_app_metadata_errors(self):
# bundle_id must be a non-empty string.
with pytest.raises(ValueError):
project_management.IOSAppMetadata(
bundle_id='',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
# name must be a non-empty string.
with pytest.raises(ValueError):
project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
# app_id must be a non-empty string.
with pytest.raises(ValueError):
project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='',
display_name='My iOS App',
project_id='test-project-id')
# display_name must be a string or None.
with pytest.raises(ValueError):
project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name=0,
project_id='test-project-id')
# project_id must be a nonempty string.
with pytest.raises(ValueError):
project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
project_id='')
def test_ios_app_metadata_eq_and_hash(self):
metadata_1 = IOS_APP_METADATA
metadata_2 = project_management.IOSAppMetadata(
bundle_id='different',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
metadata_3 = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='different',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
metadata_4 = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='different',
display_name='My iOS App',
project_id='test-project-id')
metadata_5 = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='different',
project_id='test-project-id')
metadata_6 = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='different')
metadata_7 = project_management.IOSAppMetadata(
bundle_id='com.hello.world.ios',
name='projects/test-project-id/iosApps/1:12345678:ios:ca5cade5',
app_id='1:12345678:android:deadbeef',
display_name='My iOS App',
project_id='test-project-id')
android_metadata = ANDROID_APP_METADATA
# Don't trigger __ne__.
assert not metadata_1 == android_metadata # pylint: disable=unneeded-not
assert metadata_1 != android_metadata
assert metadata_1 != metadata_2
assert metadata_1 != metadata_3
assert metadata_1 != metadata_4
assert metadata_1 != metadata_5
assert metadata_1 != metadata_6
assert metadata_1 == metadata_7
assert set([metadata_1, metadata_2, metadata_7]) == set([metadata_1, metadata_2])
def test_ios_app_metadata_bundle_id(self):
assert IOS_APP_METADATA.bundle_id == 'com.hello.world.ios'
def test_ios_app_metadata_name(self):
assert IOS_APP_METADATA._name == 'projects/test-project-id/iosApps/1:12345678:ios:ca5cade5'
def test_ios_app_metadata_app_id(self):
assert IOS_APP_METADATA.app_id == '1:12345678:android:deadbeef'
def test_ios_app_metadata_display_name(self):
assert IOS_APP_METADATA.display_name == 'My iOS App'
def test_ios_app_metadata_project_id(self):
assert IOS_APP_METADATA.project_id == 'test-project-id'
class TestSHACertificate:
def test_create_sha_certificate_errors(self):
# sha_hash cannot be None.
with pytest.raises(ValueError):
project_management.SHACertificate(sha_hash=None)
# sha_hash must be a string.
with pytest.raises(ValueError):
project_management.SHACertificate(sha_hash=0x123456789a123456789a123456789a123456789a)
# sha_hash must be a valid SHA-1 or SHA-256 hash.
with pytest.raises(ValueError):
project_management.SHACertificate(sha_hash='123456789a123456789')
with pytest.raises(ValueError):
project_management.SHACertificate(sha_hash='123456789a123456789a123456789a123456oops')
def test_sha_certificate_eq(self):
sha_cert_1 = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a',
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1')
# sha_hash is different from sha_cert_1, but name is the same.
sha_cert_2 = project_management.SHACertificate(
'0000000000000000000000000000000000000000',
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1')
# name is different from sha_cert_1, but sha_hash is the same.
sha_cert_3 = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a', None)
# name is different from sha_cert_1, but sha_hash is the same.
sha_cert_4 = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a', 'projects/-/androidApps/{0}/sha/notname1')
# sha_hash and cert_type are different from sha_cert_1, but name is the same.
sha_cert_5 = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a123456789a123456789a1234',
'projects/-/androidApps/{0}/sha/name1')
# Exactly the same as sha_cert_1.
sha_cert_6 = project_management.SHACertificate(
'123456789a123456789a123456789a123456789a',
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1')
not_a_sha_cert = {
'name': 'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1',
'sha_hash': '123456789a123456789a123456789a123456789a',
'cert_type': 'SHA_1',
}
assert sha_cert_1 != sha_cert_2
assert sha_cert_1 != sha_cert_3
assert sha_cert_1 != sha_cert_4
assert sha_cert_1 != sha_cert_5
assert sha_cert_1 == sha_cert_6
# Don't trigger __ne__.
assert not sha_cert_1 == not_a_sha_cert # pylint: disable=unneeded-not
assert sha_cert_1 != not_a_sha_cert
def test_sha_certificate_name(self):
assert (SHA_1_CERTIFICATE.name ==
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name1')
assert (SHA_256_CERTIFICATE.name ==
'projects/-/androidApps/1:12345678:android:deadbeef/sha/name256')
def test_sha_certificate_sha_hash(self):
assert (SHA_1_CERTIFICATE.sha_hash ==
'123456789a123456789a123456789a123456789a')
assert (SHA_256_CERTIFICATE.sha_hash ==
'123456789a123456789a123456789a123456789a123456789a123456789a1234')
def test_sha_certificate_cert_type(self):
assert SHA_1_CERTIFICATE.cert_type == 'SHA_1'
assert SHA_256_CERTIFICATE.cert_type == 'SHA_256'
class BaseProjectManagementTest:
@classmethod
def setup_class(cls):
project_management._ProjectManagementService.POLL_BASE_WAIT_TIME_SECONDS = 0.01
project_management._ProjectManagementService.MAXIMUM_POLLING_ATTEMPTS = 3
firebase_admin.initialize_app(
testutils.MockCredential(), {'projectId': 'test-project-id'})
@classmethod
def teardown_class(cls):
testutils.cleanup_apps()
project_management._ProjectManagementService.POLL_BASE_WAIT_TIME_SECONDS = 0.5
def _instrument_service(self, statuses, responses, app=None):
if not app:
app = firebase_admin.get_app()
project_management_service = project_management._get_project_management_service(app)
recorder = []
project_management_service._client.session.mount(
'https://firebase.googleapis.com',
testutils.MockMultiRequestAdapter(responses, statuses, recorder))
return recorder
def _assert_request_is_correct(
self, request, expected_method, expected_url, expected_body=None):
assert request.method == expected_method
assert request.url == expected_url
client_version = 'Python/Admin/{0}'.format(firebase_admin.__version__)
assert request.headers['X-Client-Version'] == client_version
if expected_body is None:
assert request.body is None
else:
assert json.loads(request.body.decode()) == expected_body
class TestTimeout(BaseProjectManagementTest):
def test_default_timeout(self):
app = firebase_admin.get_app()
project_management_service = project_management._get_project_management_service(app)
assert project_management_service._client.timeout == _http_client.DEFAULT_TIMEOUT_SECONDS
@pytest.mark.parametrize('timeout', [4, None])
def test_custom_timeout(self, timeout):
options = {
'httpTimeout': timeout,
'projectId': 'test-project-id'
}
app = firebase_admin.initialize_app(
testutils.MockCredential(), options, 'timeout-{0}'.format(timeout))
project_management_service = project_management._get_project_management_service(app)
assert project_management_service._client.timeout == timeout
class TestCreateAndroidApp(BaseProjectManagementTest):
_CREATION_URL = 'https://firebase.googleapis.com/v1beta1/projects/test-project-id/androidApps'
def test_create_android_app_without_display_name(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create Android app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
ANDROID_APP_NO_DISPLAY_NAME_OPERATION_SUCCESSFUL_RESPONSE, # Operation completed.
])
android_app = project_management.create_android_app(
package_name='com.hello.world.android')
assert android_app.app_id == '1:12345678:android:deadbeef'
assert len(recorder) == 3
body = {'packageName': 'com.hello.world.android'}
self._assert_request_is_correct(
recorder[0], 'POST', TestCreateAndroidApp._CREATION_URL, body)
self._assert_request_is_correct(
recorder[1], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
self._assert_request_is_correct(
recorder[2], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
def test_create_android_app(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create Android app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
ANDROID_APP_OPERATION_SUCCESSFUL_RESPONSE, # Creation operation completed.
])
android_app = project_management.create_android_app(
package_name='com.hello.world.android',
display_name='My Android App')
assert android_app.app_id == '1:12345678:android:deadbeef'
assert len(recorder) == 3
body = {
'packageName': 'com.hello.world.android',
'displayName': 'My Android App',
}
self._assert_request_is_correct(
recorder[0], 'POST', TestCreateAndroidApp._CREATION_URL, body)
self._assert_request_is_correct(
recorder[1], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
self._assert_request_is_correct(
recorder[2], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
def test_create_android_app_already_exists(self):
recorder = self._instrument_service(statuses=[409], responses=[ALREADY_EXISTS_RESPONSE])
with pytest.raises(exceptions.AlreadyExistsError) as excinfo:
project_management.create_android_app(
package_name='com.hello.world.android',
display_name='My Android App')
assert 'The resource already exists' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_create_android_app_polling_rpc_error(self):
recorder = self._instrument_service(
statuses=[200, 200, 503], # Error 503 means that backend servers are over capacity.
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create Android app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
UNAVAILABLE_RESPONSE, # Error 503.
])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.create_android_app(
package_name='com.hello.world.android',
display_name='My Android App')
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 3
def test_create_android_app_polling_failure(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create Android app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
OPERATION_FAILED_RESPONSE, # Operation is finished, but terminated with an error.
])
with pytest.raises(exceptions.UnknownError) as excinfo:
project_management.create_android_app(
package_name='com.hello.world.android',
display_name='My Android App')
assert 'Polling finished, but the operation terminated in an error' in str(excinfo.value)
assert excinfo.value.cause is None
assert excinfo.value.http_response is not None
assert len(recorder) == 3
def test_create_android_app_polling_limit_exceeded(self):
project_management._ProjectManagementService.MAXIMUM_POLLING_ATTEMPTS = 2
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create Android app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation Operation is still not done.
OPERATION_IN_PROGRESS_RESPONSE, # Creation Operation is still not done.
])
with pytest.raises(exceptions.DeadlineExceededError) as excinfo:
project_management.create_android_app(
package_name='com.hello.world.android',
display_name='My Android App')
assert 'Polling deadline exceeded' in str(excinfo.value)
assert excinfo.value.cause is None
assert len(recorder) == 3
class TestCreateIOSApp(BaseProjectManagementTest):
_CREATION_URL = 'https://firebase.googleapis.com/v1beta1/projects/test-project-id/iosApps'
def test_create_ios_app_without_display_name(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create iOS app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
IOS_APP_NO_DISPLAY_NAME_OPERATION_SUCCESSFUL_RESPONSE, # Operation completed.
])
ios_app = project_management.create_ios_app(
bundle_id='com.hello.world.ios')
assert ios_app.app_id == '1:12345678:ios:ca5cade5'
assert len(recorder) == 3
body = {'bundleId': 'com.hello.world.ios'}
self._assert_request_is_correct(recorder[0], 'POST', TestCreateIOSApp._CREATION_URL, body)
self._assert_request_is_correct(
recorder[1], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
self._assert_request_is_correct(
recorder[2], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
def test_create_ios_app(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create iOS app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
IOS_APP_OPERATION_SUCCESSFUL_RESPONSE, # Creation operation completed.
])
ios_app = project_management.create_ios_app(
bundle_id='com.hello.world.ios',
display_name='My iOS App')
assert ios_app.app_id == '1:12345678:ios:ca5cade5'
assert len(recorder) == 3
body = {
'bundleId': 'com.hello.world.ios',
'displayName': 'My iOS App',
}
self._assert_request_is_correct(recorder[0], 'POST', TestCreateIOSApp._CREATION_URL, body)
self._assert_request_is_correct(
recorder[1], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
self._assert_request_is_correct(
recorder[2], 'GET', 'https://firebase.googleapis.com/v1/operations/abcdefg')
def test_create_ios_app_already_exists(self):
recorder = self._instrument_service(statuses=[409], responses=[ALREADY_EXISTS_RESPONSE])
with pytest.raises(exceptions.AlreadyExistsError) as excinfo:
project_management.create_ios_app(
bundle_id='com.hello.world.ios',
display_name='My iOS App')
assert 'The resource already exists' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_create_ios_app_polling_rpc_error(self):
recorder = self._instrument_service(
statuses=[200, 200, 503], # Error 503 means that backend servers are over capacity.
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create iOS app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
UNAVAILABLE_RESPONSE, # Error 503.
])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.create_ios_app(
bundle_id='com.hello.world.ios',
display_name='My iOS App')
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 3
def test_create_ios_app_polling_failure(self):
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create iOS app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation operation is still not done.
OPERATION_FAILED_RESPONSE, # Operation is finished, but terminated with an error.
])
with pytest.raises(exceptions.UnknownError) as excinfo:
project_management.create_ios_app(
bundle_id='com.hello.world.ios',
display_name='My iOS App')
assert 'Polling finished, but the operation terminated in an error' in str(excinfo.value)
assert excinfo.value.cause is None
assert excinfo.value.http_response is not None
assert len(recorder) == 3
def test_create_ios_app_polling_limit_exceeded(self):
project_management._ProjectManagementService.MAXIMUM_POLLING_ATTEMPTS = 2
recorder = self._instrument_service(
statuses=[200, 200, 200],
responses=[
OPERATION_IN_PROGRESS_RESPONSE, # Request to create iOS app asynchronously.
OPERATION_IN_PROGRESS_RESPONSE, # Creation Operation is still not done.
OPERATION_IN_PROGRESS_RESPONSE, # Creation Operation is still not done.
])
with pytest.raises(exceptions.DeadlineExceededError) as excinfo:
project_management.create_ios_app(
bundle_id='com.hello.world.ios',
display_name='My iOS App')
assert 'Polling deadline exceeded' in str(excinfo.value)
assert excinfo.value.cause is None
assert len(recorder) == 3
class TestListAndroidApps(BaseProjectManagementTest):
_LISTING_URL = ('https://firebase.googleapis.com/v1beta1/projects/test-project-id/'
'androidApps?pageSize=100')
_LISTING_PAGE_2_URL = ('https://firebase.googleapis.com/v1beta1/projects/test-project-id/'
'androidApps?pageToken=nextpagetoken&pageSize=100')
def test_list_android_apps(self):
recorder = self._instrument_service(statuses=[200], responses=[LIST_ANDROID_APPS_RESPONSE])
android_apps = project_management.list_android_apps()
expected_app_ids = set(['1:12345678:android:deadbeef', '1:12345678:android:deadbeefcafe'])
assert set(app.app_id for app in android_apps) == expected_app_ids
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestListAndroidApps._LISTING_URL)
def test_list_android_apps_rpc_error(self):
recorder = self._instrument_service(statuses=[503], responses=[UNAVAILABLE_RESPONSE])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.list_android_apps()
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_list_android_apps_empty_list(self):
recorder = self._instrument_service(statuses=[200], responses=[json.dumps(dict())])
android_apps = project_management.list_android_apps()
assert android_apps == []
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestListAndroidApps._LISTING_URL)
def test_list_android_apps_multiple_pages(self):
recorder = self._instrument_service(
statuses=[200, 200],
responses=[LIST_ANDROID_APPS_PAGE_1_RESPONSE, LIST_ANDROID_APPS_PAGE_2_RESPONSE])
android_apps = project_management.list_android_apps()
expected_app_ids = set(['1:12345678:android:deadbeef', '1:12345678:android:deadbeefcafe'])
assert set(app.app_id for app in android_apps) == expected_app_ids
assert len(recorder) == 2
self._assert_request_is_correct(recorder[0], 'GET', TestListAndroidApps._LISTING_URL)
self._assert_request_is_correct(recorder[1], 'GET', TestListAndroidApps._LISTING_PAGE_2_URL)
def test_list_android_apps_multiple_pages_rpc_error(self):
recorder = self._instrument_service(
statuses=[200, 503],
responses=[LIST_ANDROID_APPS_PAGE_1_RESPONSE, UNAVAILABLE_RESPONSE])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.list_android_apps()
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 2
class TestListIOSApps(BaseProjectManagementTest):
_LISTING_URL = ('https://firebase.googleapis.com/v1beta1/projects/test-project-id/'
'iosApps?pageSize=100')
_LISTING_PAGE_2_URL = ('https://firebase.googleapis.com/v1beta1/projects/test-project-id/'
'iosApps?pageToken=nextpagetoken&pageSize=100')
def test_list_ios_apps(self):
recorder = self._instrument_service(statuses=[200], responses=[LIST_IOS_APPS_RESPONSE])
ios_apps = project_management.list_ios_apps()
expected_app_ids = set(['1:12345678:ios:ca5cade5', '1:12345678:ios:ca5cade5cafe'])
assert set(app.app_id for app in ios_apps) == expected_app_ids
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestListIOSApps._LISTING_URL)
def test_list_ios_apps_rpc_error(self):
recorder = self._instrument_service(statuses=[503], responses=[UNAVAILABLE_RESPONSE])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.list_ios_apps()
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_list_ios_apps_empty_list(self):
recorder = self._instrument_service(statuses=[200], responses=[json.dumps(dict())])
ios_apps = project_management.list_ios_apps()
assert ios_apps == []
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestListIOSApps._LISTING_URL)
def test_list_ios_apps_multiple_pages(self):
recorder = self._instrument_service(
statuses=[200, 200],
responses=[LIST_IOS_APPS_PAGE_1_RESPONSE, LIST_IOS_APPS_PAGE_2_RESPONSE])
ios_apps = project_management.list_ios_apps()
expected_app_ids = set(['1:12345678:ios:ca5cade5', '1:12345678:ios:ca5cade5cafe'])
assert set(app.app_id for app in ios_apps) == expected_app_ids
assert len(recorder) == 2
self._assert_request_is_correct(recorder[0], 'GET', TestListIOSApps._LISTING_URL)
self._assert_request_is_correct(recorder[1], 'GET', TestListIOSApps._LISTING_PAGE_2_URL)
def test_list_ios_apps_multiple_pages_rpc_error(self):
recorder = self._instrument_service(
statuses=[200, 503],
responses=[LIST_IOS_APPS_PAGE_1_RESPONSE, UNAVAILABLE_RESPONSE])
with pytest.raises(exceptions.UnavailableError) as excinfo:
project_management.list_ios_apps()
assert 'Backend servers are over capacity' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 2
class TestAndroidApp(BaseProjectManagementTest):
_GET_METADATA_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef')
_SET_DISPLAY_NAME_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef?updateMask=displayName')
_GET_CONFIG_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef/config')
_ADD_CERT_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef/sha')
_LIST_CERTS_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef/sha')
_DELETE_SHA_1_CERT_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef/sha/name1')
_DELETE_SHA_256_CERT_URL = ('https://firebase.googleapis.com/v1beta1/projects/-/androidApps/'
'1:12345678:android:deadbeef/sha/name256')
@pytest.fixture
def android_app(self):
return project_management.android_app('1:12345678:android:deadbeef')
def test_get_metadata_no_display_name(self, android_app):
recorder = self._instrument_service(
statuses=[200], responses=[ANDROID_APP_NO_DISPLAY_NAME_METADATA_RESPONSE])
metadata = android_app.get_metadata()
assert metadata._name == 'projects/test-project-id/androidApps/1:12345678:android:deadbeef'
assert metadata.app_id == '1:12345678:android:deadbeef'
assert metadata.display_name is None
assert metadata.project_id == 'test-project-id'
assert metadata.package_name == 'com.hello.world.android'
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestAndroidApp._GET_METADATA_URL)
def test_get_metadata(self, android_app):
recorder = self._instrument_service(
statuses=[200], responses=[ANDROID_APP_METADATA_RESPONSE])
metadata = android_app.get_metadata()
assert metadata._name == 'projects/test-project-id/androidApps/1:12345678:android:deadbeef'
assert metadata.app_id == '1:12345678:android:deadbeef'
assert metadata.display_name == 'My Android App'
assert metadata.project_id == 'test-project-id'
assert metadata.package_name == 'com.hello.world.android'
assert len(recorder) == 1
self._assert_request_is_correct(recorder[0], 'GET', TestAndroidApp._GET_METADATA_URL)
def test_get_metadata_unknown_error(self, android_app):
recorder = self._instrument_service(
statuses=[428], responses=['precondition required error'])
with pytest.raises(exceptions.UnknownError) as excinfo:
android_app.get_metadata()
message = 'Unexpected HTTP response with status: 428; body: precondition required error'
assert str(excinfo.value) == message
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_get_metadata_not_found(self, android_app):
recorder = self._instrument_service(statuses=[404], responses=[NOT_FOUND_RESPONSE])
with pytest.raises(exceptions.NotFoundError) as excinfo:
android_app.get_metadata()
assert 'Failed to find the resource' in str(excinfo.value)
assert excinfo.value.cause is not None
assert excinfo.value.http_response is not None
assert len(recorder) == 1
def test_set_display_name(self, android_app):
recorder = self._instrument_service(statuses=[200], responses=[json.dumps({})])
new_display_name = 'A new display name!'
android_app.set_display_name(new_display_name)
assert len(recorder) == 1
body = {'displayName': new_display_name}
self._assert_request_is_correct(
recorder[0], 'PATCH', TestAndroidApp._SET_DISPLAY_NAME_URL, body)