-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·1644 lines (1380 loc) · 57.8 KB
/
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/env python
# coding: utf-8
# Copyright (c) 2012, Polar Mobile.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name Polar Mobile nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL POLAR MOBILE BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Used to define and run unit tests.
from unittest import TestCase, main
# Used to mimic objects in order to test more complex calls.
from mock import patch
# Used to generate fake http requests and test for responses.
from itty import Request, Response
# Used to test error handling code in errors.py.
from simplejson import dumps
from publisher.errors import (bad_syntax, unauthorized, forbidden, not_found,
internal_error)
from publisher.utils import (JsonBadSyntax, JsonUnauthorized, JsonForbidden,
JsonNotFound, JsonAppError)
# Used to test error encoding.
from publisher.utils import encode_error, raise_error, check_base_url
# Used to test auth handling code.
from publisher.auth import (check_authorization_header, decode_body,
check_device, check_auth_params, auth,
check_publisher_auth_params,)
# Used to test the model code.
from publisher.model import model
# Used to reset the models singleton and test timeouts.
from publisher.constants import SESSION_TIMEOUT
# Used to test the timestamps in model.py.
from datetime import datetime, timedelta
# Used to test the validate API entry point.
from publisher.validate import get_session_id, validate
def test_start_response(status, headers):
'''
This function is a testing replacement for the start_response function
provided by the WSGI web framework. It is called by the request object to
start the transfer of data back to the client. For testing purposes, this
function does not do anything.
'''
# In python, pass is a keyword that is synonymous for "don't do
# anything".
pass
def create_request(http_path):
'''
A helper function used to create fake request objects for testing.
'''
# WSGI and itty operate like CGI. They have a dictionary of environment
# variables that specify the parameters of the request. For testing, all
# we care to specify is the PATH_INFO variable, which is the URL that the
# request is processed with.
environ = {}
environ['PATH_INFO'] = http_path
# Create the request object. start_response is a function pointer to an
# internal function that itty uses to send a request. For the purposes of
# testing, we replace it with a function that does nothing.
result = Request(environ, start_response=test_start_response)
# Return the result.
return result
class TestUtils(TestCase):
'''
Test the code in publisher/utils.py.
'''
def test_encode_error(self):
'''
Tests generation of an error report.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
# Call encode_error and get the result.
result = encode_error(url, code, message)
# Check the result.
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(result, content)
def test_raise_error_bad_syntax(self):
'''
Tests generation of a 400 error.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
status = 400
# Call raise_error and get the result.
try:
raise_error(url, code, message, status)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_raise_error_unauthorized(self):
'''
Tests generation of a 401 error.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
status = 401
# Call raise_error and get the result.
try:
raise_error(url, code, message, status)
# Catch the exception and analyze it.
except JsonUnauthorized, exception:
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_raise_error_forbidden(self):
'''
Tests generation of a 403 error.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
status = 403
# Call raise_error and get the result.
try:
raise_error(url, code, message, status)
# Catch the exception and analyze it.
except JsonForbidden, exception:
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_raise_error_not_found(self):
'''
Tests generation of a 404 error.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
status = 404
# Call raise_error and get the result.
try:
raise_error(url, code, message, status)
# Catch the exception and analyze it.
except JsonNotFound, exception:
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_raise_error_internal_error(self):
'''
Tests generation of a 500 error.
'''
# Create the seed data for the test.
url = '/test/'
code = 'TestError'
message = 'This is a test error.'
status = 500
# Call raise_error and get the result.
try:
raise_error(url, code, message, status)
# Catch the exception and analyze it.
except JsonAppError, exception:
content = u'{"error": {"message": "This is a test error.", '\
'"code": "TestError", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_base_url_api(self):
'''
Tests base url checking on the api parameter.
'''
# Create the seed data for the test.
url = '/test/'
api = 'test'
version = 'v1.0.0'
format = 'json'
# Call the check_base_url function and expect an exception.
try:
check_base_url(url, api, version, format)
# Catch the exception and analyze it.
except JsonNotFound, exception:
content = u'{"debug": {"message": "The requested api is not '\
'implemented: test"}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": "InvalidAPI", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_base_url_version(self):
'''
Tests base url checking on the version parameter.
'''
# Create the seed data for the test.
url = '/test/'
api = 'paywallproxy'
version = 'test'
format = 'json'
# Call the check_base_url function and expect an exception.
try:
check_base_url(url, api, version, format)
# Catch the exception and analyze it.
except JsonNotFound, exception:
content = u'{"debug": {"message": "The requested version is not '\
'implemented: test"}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": '\
'"InvalidVersion", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_base_url_format(self):
'''
Tests base url checking on the format parameter.
'''
# Create the seed data for the test.
url = '/test/'
api = 'paywallproxy'
version = 'v1.0.0'
format = 'test'
# Call the check_base_url function and expect an exception.
try:
check_base_url(url, api, version, format)
# Catch the exception and analyze it.
except JsonNotFound, exception:
content = u'{"debug": {"message": "The requested format is not '\
'implemented: test"}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": "InvalidFormat",'\
' "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_base_url(self):
'''
A valid pass for check_base_url.
'''
# Create the seed data for the test.
url = '/test/'
api = 'paywallproxy'
version = 'v1.0.0'
format = 'json'
# This function should raise no exception.
check_base_url(url, api, version, format)
class TestErrors(TestCase):
'''
Test the code in publisher/errors.py.
'''
def test_bad_syntax_pass(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
request = create_request('/test/')
content = dumps('test')
exception = JsonBadSyntax(content)
request._environ = {}
# Issue the request to the method being tested.
result = bad_syntax(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_bad_syntax_unknown_exception(self):
'''
Checks to make sure that the framework fails when an unknown exception
is passed.
'''
# Create the seed data for the test.
request = create_request('/test/')
exception = Exception(u'test')
request._environ = {}
# Issue the request to the method being tested and ensure it raises
# an assertion error.
self.assertRaises(AssertionError, bad_syntax, request, exception)
def test_unauthorized_pass_auth(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
url = '/paywallproxy/v1.0.0/json/auth/product01'
request = create_request(url)
content = dumps('test')
exception = JsonUnauthorized(content)
request._environ = {}
# Issue the request to the method being tested.
result = unauthorized(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_unauthorized_pass_validation(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
url = '/paywallproxy/v1.0.0/json/validate/product01'
request = create_request(url)
content = dumps('test')
exception = JsonUnauthorized(content)
request._environ = {}
# Issue the request to the method being tested.
result = unauthorized(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_unauthorized_unknown_exception(self):
'''
Checks to make sure that the framework fails when an unknown exception
is passed.
'''
# Create the seed data for the test.
request = create_request('/test/')
exception = Exception(u'test')
request._environ = {}
# Issue the request to the method being tested and ensure it raises
# an assertion error.
self.assertRaises(AssertionError, unauthorized, request, exception)
def test_forbidden_pass(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
request = create_request('/test/')
content = dumps('test')
exception = JsonForbidden(content)
request._environ = {}
# Issue the request to the method being tested.
result = forbidden(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_forbidden_unknown_exception(self):
'''
Checks to make sure that the framework fails when an unknown exception
is passed.
'''
# Create the seed data for the test.
request = create_request('/test/')
exception = Exception(u'test')
request._environ = {}
# Issue the request to the method being tested and ensure it raises
# an assertion error.
self.assertRaises(AssertionError, forbidden, request, exception)
def test_not_found_pass(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
request = create_request('/test/')
content = dumps('test')
exception = JsonNotFound(content)
request._environ = {}
# Issue the request to the method being tested.
result = not_found(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_not_found_unknown_exception(self):
'''
Tests handling of a 404 error when an unknown exception is passed.
'''
# Create the seed data for the test.
request = create_request('/test/')
exception = Exception(u'test')
request._environ = {}
# Issue the request to the method being tested.
result = not_found(request, exception)
# Check the result.
content = u'{"debug": {"message": "No handler could be found for the '\
'requested resource."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "NoHandler", "resource": '\
'"/test/"}}'
self.assertEqual(result, content)
def test_internal_error_pass(self):
'''
Checks to make sure that json encoded exceptions are passed through
the exception handling framework properly.
'''
# Create the seed data for the test.
request = create_request('/test/')
content = dumps('test')
exception = JsonAppError(content)
request._environ = {}
# Issue the request to the method being tested.
result = internal_error(request, exception)
# Check the result.
self.assertEqual(result, content)
def test_internal_error_unknown_exception(self):
'''
Tests handling of a 500 error when an unknown exception is passed.
'''
# Create the seed data for the test.
request = create_request('/test/')
exception = Exception(u'test')
request._environ = {}
# Issue the request to the method being tested.
result = internal_error(request, exception)
# Check the result.
content = u'{"debug": {"message": "An internal server error occurred.'\
' Please check logs."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InternalError", "resource": '\
'"/test/"}}'
self.assertEqual(result, content)
class TestAuth(TestCase):
'''
Test the code in publisher/auth.py.
'''
def test_check_authorization_header_exists(self):
'''
Tests to see if the check_authorization_header function checks for
the existence of the "Authorization" header.
'''
# Create seed data for the test.
url = '/test/'
environment = {}
# Call the check_authorization_header function and expect an exception.
try:
check_authorization_header(url, environment)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The authorization token has '\
'not been provided."}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": '\
'"InvalidAuthScheme", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_authorization_header_value(self):
'''
Tests to see if the check_authorization_header function checks for
the right authorization header.
'''
# Create seed data for the test.
url = '/test/'
environment = {}
environment['HTTP_AUTHORIZATION'] = 'invalid'
# Call the check_authorization_header function and expect an exception.
try:
check_authorization_header(url, environment)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The authorization token is '\
'incorrect."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InvalidAuthScheme", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_authorization_header(self):
'''
Tests to see if the check_authorization_header function passes a
proper header.
'''
# Create seed data for the test.
url = '/test/'
environment = {}
environment['HTTP_AUTHORIZATION'] = 'PolarPaywallProxyAuthv1.0.0'
# Call the check_authorization_header function.
check_authorization_header(url, environment)
def test_decode_body_invalid_json(self):
'''
Tests to see if the decode_json function checks for invalid json.
'''
# Create seed data for the test.
url = '/test/'
body = '{"test'
# Call the check_device function and expect an exception.
try:
decode_body(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "Could not decode post body. '\
'json is expected."}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": "InvalidFormat",'\
' "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_decode_body_many_arguments(self):
'''
Tests to see if the decode_json function checks for an invalid number
of json parameters.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['parameter1'] = 'test'
body['parameter2'] = 'test'
body['parameter3'] = 'test'
body['parameter4'] = 'test'
# Call the check_device function and expect an exception.
try:
decode_body(url, dumps(body))
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "Post body has an invalid '\
'number of parameters."}, "error": {"message": "An error '\
'occurred. Please contact support.", "code": "InvalidFormat",'\
' "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_device_exists(self):
'''
Tests to see if the check_device function checks for a missing device.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['test'] = 'test'
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The device has not been '\
'provided."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InvalidDevice", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_device_type(self):
'''
Tests to see if the check_device function checks for an invalid device
type.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = 'test'
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The device is not a map."}, '\
'"error": {"message": "An error occurred. Please contact '\
'support.", "code": "InvalidDevice", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_manufacturer_exists(self):
'''
Tests to see if the check_device function checks for a missing
manufacturer.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The manufacturer has not been '\
'provided."}, "error": {"message": "An error occurred. Please'\
' contact support.", "code": "InvalidDevice", "resource": '\
'"/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_manufacturer_type(self):
'''
Tests to see if the check_device function checks for an invalid
manufacturer type.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = []
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The manufacturer is not a '\
'string."}, "error": {"message": "An error occurred. Please '\
'contact support.", "code": "InvalidDevice", "resource": '\
'"/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_model_exists(self):
'''
Tests to see if the check_device function checks for a missing
model.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = u'test'
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The model has not been '\
'provided."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InvalidDevice", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_model_type(self):
'''
Tests to see if the check_device function checks for an invalid
model type.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = u'test'
body['device']['model'] = []
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The model is not a string."}, '\
'"error": {"message": "An error occurred. Please contact '\
'support.", "code": "InvalidDevice", "resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_os_version_exists(self):
'''
Tests to see if the check_device function checks for a missing
os_version.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = u'test'
body['device']['model'] = u'test'
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The os_version has not been '\
'provided."}, "error": {"message": "An error occurred. Please'\
' contact support.", "code": "InvalidDevice", "resource": '\
'"/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_os_version_type(self):
'''
Tests to see if the check_device function checks for an invalid
os_version type.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = u'test'
body['device']['model'] = u'test'
body['device']['os_version'] = []
# Call the check_device function and expect an exception.
try:
check_device(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The os_version is not a '\
'string."}, "error": {"message": "An error occurred. Please'\
' contact support.", "code": "InvalidDevice", "resource": '\
'"/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_device(self):
'''
Tests to see if the check_device with a positive example to make sure
that None is returned.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['device'] = {}
body['device']['manufacturer'] = u'test'
body['device']['model'] = u'test'
body['device']['os_version'] = u'test'
# Issue the request to the method being tested. The function should
# not do anything.
check_device(url, body)
def test_check_no_auth_params(self):
'''
Tests to see if the check_auth_params function passes for a missing
authParams parameter.
'''
# Create seed data for the test.
url = '/test/'
body = {}
# Issue the request to the method being tested. The function should not
# do anything or raise any exceptions.
check_auth_params(url, body)
def test_check_auth_params_type(self):
'''
Tests to see if the check_auth_params function checks for an invalid
authParams type.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['authParams'] = u'test'
# Call the check_auth_params function and expect an exception.
try:
check_auth_params(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The authParams is not a map.'\
'"}, "error": {"message": "An error occurred. Please contact '\
'support.", "code": "InvalidAuthParams", "resource": '\
'"/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_auth_params_value_types(self):
'''
Tests to see if the check_auth_params function checks for an invalid
authParams value types.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['authParams'] = {}
body['authParams']['test'] = []
# Call the check_auth_params function and expect an exception.
try:
check_auth_params(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "This authParams value is not a'\
' string: test"}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InvalidAuthParams", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_auth_params(self):
'''
Tests to see if the check_auth_params with a positive example.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['authParams'] = {}
body['authParams']['test'] = u'test'
# Issue the request to the method being tested. The function should not
# do anything or raise any exceptions.
check_auth_params(url, body)
def test_check_publisher_auth_params_exists(self):
'''
Tests to see if the check_publisher_auth_params function checks
to ensure that authParams is provided.
'''
# Create seed data for the test.
url = '/test/'
body = {}
# Call the check_publisher_auth_params function and expect an
# exception.
try:
check_publisher_auth_params(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The authParams has not been '\
'provided."}, "error": {"message": "An error occurred. Please'\
' contact support.", "code": "InvalidAuthParams", "resource":'\
' "/test/"}}'
self.assertEqual(unicode(exception), content)
# If no exception was raised, raise an error.
else:
raise AssertionError('No exception raised.')
def test_check_publisher_auth_params_username_exists(self):
'''
Tests to see if the check_publisher_auth_params function checks
to ensure that username is provided.
'''
# Create seed data for the test.
url = '/test/'
body = {}
body['authParams'] = {}
# Call the check_publisher_auth_params function and expect an
# exception.
try:
check_publisher_auth_params(url, body)
# Catch the exception and analyze it.
except JsonBadSyntax, exception:
content = u'{"debug": {"message": "The username has not been '\
'provided."}, "error": {"message": "An error occurred. '\
'Please contact support.", "code": "InvalidAuthParams", '\
'"resource": "/test/"}}'
self.assertEqual(unicode(exception), content)