-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
executable file
·737 lines (561 loc) · 26.3 KB
/
tests.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
#!env/bin/python
"""Tucker Sync test module.
Main test suite for the algorithm, server and client.
WARNING:
DATA LOSS!
Do not run tests against a production database with live data.
All database tables will be dropped and then created after tests.
Leaving the database ready for production with fresh tables.
Remote server:
Functional and integration tests are designed to execute against a running
local or remote server. Since there is no RPC (remote procedure call) to
drop-create all tables and an open connection to a remote database is not
expected this module cannot clean the database of a remote server.
- Use the --remote-server test option.
- On the remote server clean the database with `app_setup.py --only-tables`
Unit tests require direct access to the local modules and database
precluding their use in testing remote installations.
Usage:
tests.py [-h] [--remote-server] [--baseurl BASEURL] [-k K]
Optional arguments:
-h, --help show this help message and exit
--remote-server use when running against a remote server
--baseurl BASEURL specify the server base url
-k K only run tests which match the given substring expression
Usage examples:
./tests.py
./tests.py --help
./tests.py --baseurl "http://0.0.0.0:8080/"
./tests.py -k "TestIntegration or TestMultiple"
./tests.py --baseurl "http://0.0.0.0:8080/" -k "TestServer and not sync"
License:
The MIT License (MIT), see LICENSE.txt for more details.
Copyright:
Copyright (c) 2014 Steven Tucker and Gavin Kromhout.
"""
import argparse
import pytest
import requests
import uuid
from flexmock import flexmock
from requests.exceptions import ConnectionError
from werkzeug.exceptions import MethodNotAllowed, NotImplemented, BadRequest
import client
import server
import app_model
from common import APIRequestType, HTTP, JSON, APIRequest, APIErrorResponse, \
JSONKey, APIErrorCode, SyncDownRequestBody, AccountOpenRequestBody, \
SyncUpRequestBody, SyncCount
from app_config import APP_KEYS
fixture = pytest.fixture
parametrize = pytest.mark.parametrize
use_fixtures = pytest.mark.usefixtures
yield_fixture = pytest.yield_fixture
class TestCommon(object):
"""Common unit tests."""
def test_api_error_response(self):
assert '{"error":0}' == APIErrorResponse.SUCCESS
assert '{"error":1}' == APIErrorResponse.INTERNAL_SERVER_ERROR
assert '{"error":2}' == APIErrorResponse.MALFORMED_REQUEST
@use_fixtures('session_fin_drop_create_tables')
class TestServerUnit(object):
"""Server unit tests.
Running server not required for unit tests.
However connection to a running database is required."""
@yield_fixture
def holder(self):
holder = server.Holder()
holder.response = server.Response()
holder.cursor, holder.cnx, errno = server.open_db()
assert not errno
holder.object_class = app_model.Product
yield holder
# finalization
server.close_db(holder.cursor, holder.cnx)
@use_fixtures('before_test_drop_create_tables')
def test_warn_expired_sessions_committed(self, holder, caplog):
"""Test logged warning when expired sessions are committed."""
from test_sync_count import insert_expired_and_current_sessions
insert_expired_and_current_sessions()
server.mark_expired_sessions_committed(holder)
logged_msg = SyncCount.WARN_EXPIRED_SESSIONS_COMMITTED % 4
assert logged_msg in caplog.text()
count = 0
for record in caplog.records():
if record.getMessage() == logged_msg:
assert 'WARNING' == record.levelname
count += 1
assert 1 == count
@use_fixtures("session_fin_drop_create_tables")
class TestServer(object):
"""Server functional tests.
base_url is a test fixture defined in conftest.py
"""
@fixture(scope='class')
def account_open_request_body(self):
rb = AccountOpenRequestBody()
rb.clientUUID = uuid.uuid4()
return rb
@fixture(scope='class')
def sync_down_request_body(self, account_open_request_body):
rb = SyncDownRequestBody()
rb.objectClass = 'Product'
rb.clientUUID = account_open_request_body.clientUUID
rb.lastSync = 0
return rb
@fixture(scope='class')
def sync_up_request_body(self, account_open_request_body):
rb = SyncUpRequestBody()
rb.objectClass = 'Product'
rb.clientUUID = account_open_request_body.clientUUID
rb.objects = []
return rb
@fixture
def req(self, base_url):
req = APIRequest()
req.base_url = base_url
req.type = APIRequestType.TEST
req.key = APP_KEYS[1]
req.email = 'user@example.com'
req.password = 'secret78901234'
return req
METHODS_NOT_ALLOWED = ('', ' ',
'*', '%', '$', '&', '@',
'None', 'none', 'NONE',
'Null', 'null', 'NULL',
'OPTIONS', 'GET', 'HEAD', 'PUT',
'PATCH', 'DELETE', 'TRACE', 'CONNECT')
@parametrize('method', METHODS_NOT_ALLOWED)
def test_method_not_allowed(self, req, method):
"""Test server base url for method not allowed responses."""
def assert_method_not_allowed():
assert MethodNotAllowed.code == response.status_code
if method != 'TRACE':
assert 'POST' == response.headers.get('Allow')
if method != 'HEAD':
assert 'Method Not Allowed' in response.content
try:
response = requests.request(method, req.base_url,
headers=req.base_headers)
except ConnectionError:
# For some of the methods PHP CLI may get no further than this.
pytest.xfail('PHP CLI server incorrectly aborts connection.')
assert False
if method in ('', ' '):
if 'Apache' in response.headers.get('Server', []):
# Apache responds correctly.
assert_method_not_allowed()
else:
# Python server (Werkzeug run_simple)
# only returns HTTP/0.9 - body of a 400 Bad Request.
mna = '405' in response.content
bad = '400' in response.content
assert mna or bad
return
if method in ('None', 'none', 'NONE',
'Null', 'null', 'NULL',
'CONNECT'):
assert response.status_code in (MethodNotAllowed.code,
NotImplemented.code,
BadRequest.code)
if response.status_code is MethodNotAllowed.code:
assert_method_not_allowed()
return
# All remaining methods.
assert_method_not_allowed()
def test_connection(self, req):
"""Test server 'test' function.
Auth should fail due to no account on server."""
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code # connection ok.
assert APIErrorResponse.AUTH_FAIL == response.content
def test_account_open(self, req, account_open_request_body):
"""Test server 'accountOpen' function."""
req.type = APIRequestType.ACCOUNT_OPEN
req.body = JSON.dumps(account_open_request_body.to_primitive())
response = requests.post(req.base_url, req.body,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.SUCCESS == response.content
def test_authentication(self, req):
"""Test server 'test' function.
Auth should pass."""
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.SUCCESS == response.content
def test_account_open_email_not_unique(self, req):
"""Test server 'accountOpen' function.
Existing client email (created above)."""
req.type = APIRequestType.ACCOUNT_OPEN
account_open_request_body = AccountOpenRequestBody()
account_open_request_body.clientUUID = uuid.uuid4() # unique uuid
req.body = JSON.dumps(account_open_request_body.to_primitive())
response = requests.post(req.base_url, req.body,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.EMAIL_NOT_UNIQUE == response.content
def test_account_open_uuid_not_unique(self, req,
account_open_request_body):
"""Test server 'accountOpen' function.
Existing client UUID (created above)."""
req.type = APIRequestType.ACCOUNT_OPEN
req.email = 'user2@example.com'
req.body = JSON.dumps(account_open_request_body.to_primitive())
response = requests.post(req.base_url, req.body,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.CLIENT_UUID_NOT_UNIQUE == response.content
def test_authentication_invalid_password_too_short(self, req):
"""Test server 'test' function.
Short invalid password. Auth simply fails don't leak why."""
req.password = 'short'
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.AUTH_FAIL == response.content
INVALID_KEYS = ('', ' ', 'notPrivate',
'*', '%', '$', '&', '@',
'None', 'none', 'NONE',
'Null', 'null', 'NULL')
@parametrize('key', INVALID_KEYS)
def test_invalid_key(self, req, key):
"""Test server 'test' function with an invalid key."""
req.key = key
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.INVALID_KEY == response.content
def test_sync_down(self, req, sync_down_request_body):
"""Test server 'syncDown' function."""
req.type = APIRequestType.SYNC_DOWN
req.body = JSON.dumps(sync_down_request_body.to_primitive())
response = requests.post(req.base_url, req.body,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
jo = response.json()
assert APIErrorCode.SUCCESS == jo[JSONKey.ERROR]
assert isinstance(jo[JSONKey.OBJECTS], list)
def test_sync_down_without_content_header(self, req):
"""Test server 'syncDown' function."""
req.type = APIRequestType.SYNC_DOWN
response = requests.post(req.base_url,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.MALFORMED_REQUEST == response.content
def test_sync_up(self, req, sync_up_request_body):
"""Test server 'syncUp' function."""
req.type = APIRequestType.SYNC_UP
req.body = JSON.dumps(sync_up_request_body.to_primitive())
response = requests.post(req.base_url, req.body,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
jo = response.json()
assert APIErrorCode.SUCCESS == jo[JSONKey.ERROR]
assert isinstance(jo[JSONKey.OBJECTS], list)
def test_sync_up_without_content_header(self, req):
"""Test server 'syncUp' function."""
req.type = APIRequestType.SYNC_UP
response = requests.post(req.base_url,
params=req.params, headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.MALFORMED_REQUEST == response.content
def test_authentication_email_not_specified(self, req):
"""Test server 'test' function with no email query param."""
req.email = None
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.AUTH_FAIL == response.content
def test_malformed_request_key_not_specified(self, req):
"""Test server 'test' function with no key query param."""
req.key = None
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.MALFORMED_REQUEST == response.content
def test_malformed_request_type_not_specified(self, req):
"""Test server when no request type is specified."""
req.type = None
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.MALFORMED_REQUEST == response.content
UNSUPPORTED_REQ_TYPE = ('', ' ', 'notSupported',
'*', '%', '$', '&', '@',
'None', 'none', 'NONE',
'Null', 'null', 'NULL')
@parametrize('req_type', UNSUPPORTED_REQ_TYPE)
def test_malformed_request_type_not_supported(self, req, req_type):
"""Test server when an unsupported request type is specified."""
req.type = req_type
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.MALFORMED_REQUEST == response.content
def test_account_close(self, req):
"""Test server 'accountClose' function."""
req.type = APIRequestType.ACCOUNT_CLOSE
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.SUCCESS == response.content
def test_authentication_closed_account(self, req):
"""Test server 'test' function. Auth should fail."""
response = requests.post(req.base_url, params=req.params,
headers=req.headers)
assert HTTP.OK == response.status_code
assert APIErrorResponse.AUTH_FAIL == response.content
@use_fixtures("session_fin_drop_create_tables")
class TestClient(object):
"""Client unit tests."""
@fixture(scope="class")
def client_a(self, base_url):
return client.Client(base_url, APP_KEYS[1],
'user@example.com', 'secret78901234')
@fixture(scope="class")
def client_b(self, base_url):
return client.Client(base_url, APP_KEYS[0],
'user@example.com', 'secret78901234')
@fixture(scope="function")
def mock_response(self):
return flexmock(status_code=200, content='{"error":0}')
def test_instantiate_client_a(self, client_a):
assert client_a
def test_instantiate_client_b(self, client_b):
assert client_b
def test_uuid_isinstance_of_uuid(self, client_a):
assert isinstance(client_a.UUID, uuid.UUID)
def test_uuid_is_unique(self, client_a, client_b):
assert client_a.UUID != client_b.UUID
def test_get_json(self, client_a, mock_response):
jo = client_a.get_json_object(mock_response)
assert mock_response.content == JSON.dumps(jo)
def test_get_json_non_ok_status_code(self, client_a, mock_response):
mock_response.status_code = 401
with pytest.raises(Exception):
client_a.get_json_object(mock_response)
BAD_CONTENT = ('', ' ', '*', '[]', '{}',
'None', 'none', 'NONE',
'Null', 'null', 'NULL',
'{"error":}', '{"objects":[]}')
@parametrize('content', BAD_CONTENT)
def test_get_json_bad_content(self, client_a,
mock_response, content):
mock_response.content = content
with pytest.raises(Exception):
client_a.get_json_object(mock_response)
@use_fixtures("session_fin_drop_create_tables")
class TestIntegration(object):
"""Test the API by exercising the client and server."""
@fixture(scope="class")
def client_a(self, base_url):
return client.Client(base_url,
APP_KEYS[1],
str(uuid.uuid4()) + '@example.com',
'secret78901234')
@fixture(scope="class")
def client_b(self, base_url):
return client.Client(base_url,
APP_KEYS[0],
str(uuid.uuid4()) + '@example.com',
'secret78901234')
def test_connection_a(self, client_a):
"""Test client_a's connection to server."""
result = client_a.check_connection()
assert True == result
def test_connection_b(self, client_b):
"""Test client_b's connection to server."""
result = client_b.check_connection()
assert True == result
def test_account_open(self, client_a):
"""Test opening an account."""
result = client_a.account_open()
assert True == result
def test_account_authentication(self, client_a):
"""Test authentication of the account created above."""
result = client_a.check_authentication()
assert True == result
def test_account_authentication_wrong_password(self, client_a):
"""Test authentication of the account created above.
Wrong password."""
saved_password = client_a.password
client_a.password = 'secret789012345' # set wrong password
result = client_a.check_authentication()
client_a.password = saved_password
assert False == result
def test_account_open_email_not_unique(self, client_a):
"""Test opening an account with the same email as above."""
result = client_a.account_open()
assert False == result
def test_account_open_invalid_password_too_short(self, client_b):
"""Test opening an account with a password that is too short."""
saved_password = client_b.password
client_b.password = 'secret7890123' # set short password
result = client_b.account_open()
client_b.password = saved_password
assert False == result
def test_account_open_invalid_email_syntax(self, client_b):
"""Test opening an account with an invalid email syntax."""
saved_email = client_b.email
client_b.email = str(uuid.uuid4()) + 'example.com' # missing '@'
result = client_b.account_open()
client_b.email = saved_email
assert False == result
def test_account_modify_password(self, client_a):
"""Test modifying the account password created by client_a."""
new_password = 'secret78901235'
result = client_a.account_modify(client_a.email, new_password)
client_a.password = new_password
assert True == result
def test_account_authentication_changed_password(self, client_a):
"""Test authentication of the account modified above."""
result = client_a.check_authentication()
assert True == result
def test_account_modify_email(self, client_a):
"""Test modifying the account email created by client_a."""
new_email = str(uuid.uuid4()) + '@example.com'
result = client_a.account_modify(new_email, client_a.password)
client_a.email = new_email
assert True == result
def test_account_authentication_changed_email(self, client_a):
"""Test authentication of the account modified above."""
result = client_a.check_authentication()
assert True == result
def test_account_modify_password_and_email(self, client_a):
"""Test modifying the account created by client_a."""
new_password = 'secret78901236'
new_email = str(uuid.uuid4()) + '@example.com'
result = client_a.account_modify(new_email, new_password)
client_a.password = new_password
client_a.email = new_email
assert True == result
def test_account_authentication_changed_password_and_email(self, client_a):
"""Test authentication of the account modified above."""
result = client_a.check_authentication()
assert True == result
def test_account_modify_wrong_password(self, client_a):
"""Test modify of the account created above with wrong password."""
new_password = 'secret78901238'
new_email = str(uuid.uuid4()) + '@example.com'
saved_password = client_a.password
client_a.password = 'secret78901237' # set wrong password
result = client_a.account_modify(new_email, new_password)
client_a.password = saved_password
assert False == result
def test_account_authentication_unchanged_password_and_email(self,
client_a):
"""Test authentication of the unchanged account above."""
result = client_a.check_authentication()
assert True == result
def test_account_modify_email_with_no_account(self, client_b):
"""Test modifying an account.
Email that does not have an account."""
result = client_b.account_modify(client_b.email, client_b.password)
assert False == result
def test_account_close_wrong_password(self, client_a):
"""Test closing of the account created by client_a.
Wrong password."""
saved_password = client_a.password
client_a.password = 'secret78901237' # set wrong password
result = client_a.account_close()
client_a.password = saved_password
assert False == result
def test_account_authentication_unclosed_account(self, client_a):
"""Test authentication of the unclosed account above."""
result = client_a.check_authentication()
assert True == result
def test_account_close(self, client_a):
"""Test closing an account."""
result = client_a.account_close()
assert True == result
def test_account_authentication_closed_account(self, client_a):
"""Test authentication of the account closed above."""
result = client_a.check_authentication()
assert False == result
@use_fixtures("session_fin_drop_create_tables")
class TestMultipleClientIntegration(object):
"""Test the API by exercising multiple clients and server."""
@fixture(scope="class")
def client_a(self, base_url):
return client.Client(base_url, APP_KEYS[1],
'user@example.com', 'secret78901234')
@fixture(scope="class")
def client_b(self, base_url):
return client.Client(base_url, APP_KEYS[0],
'user@example.com', 'secret78901234')
def test_connection_with_sequential_clients(self, client_a, client_b):
for x in xrange(8):
r1 = client_a.check_connection()
r2 = client_b.check_connection()
assert True == r1
assert True == r2
def test_connection_with_parallel_clients(self, client_a, base_url):
"""Parallel clients.
Client A is run in the test process while client C is run in another
process. This allows genuine parallel execution of the client module
code in Python. Connections to the server are effectively a race
condition for each client."""
from multiprocessing import Process, Queue
def run_client_a():
short_uuid = str(client_a.UUID)[:6]
for x in xrange(8):
print 'client a, short UUID:', short_uuid
r1 = client_a.check_connection()
assert True == r1
def run_client_c(q, url):
r = True
client_c = client.Client(url, APP_KEYS[1],
'user@example.com', 'secret78901234')
short_uuid = str(client_c.UUID)[:6]
for x in xrange(8):
print 'client c, short UUID:', short_uuid
if client_c.check_connection() is False:
r = False
q.put(r)
queue = Queue()
Process(target=run_client_c, args=(queue, base_url)).start()
run_client_a()
client_c_result = queue.get()
assert True == client_c_result
def get_cmd_args():
"""Get the command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("--remote-server", action='store_true',
help="use when running against a remote server")
parser.add_argument("--baseurl", help="specify the server base url")
parser.add_argument("-k",
help="only run tests matching the given substring "
"expression")
return parser.parse_args()
def get_pytest_args(file_name, cmd_args):
"""Build and return the pytest arguments."""
# PyTest argument list: verbose, exit on first failure and caplog format.
args = ['-vx', '--log-format=%(levelname)s:%(name)s:%(message)s']
if cmd_args.remote_server:
args.append('--remote-server')
# Optional command line argument specifying the server base url.
if cmd_args.baseurl:
args.append('--baseurl')
args.append(cmd_args.baseurl)
# Specify this file as the only test file.
args.append(file_name)
# Optional command line argument to only run tests matching the given
# substring expression.
if cmd_args.k:
args.append('-k %s' % cmd_args.k)
return args
def main(file_name):
"""Run the test suite."""
cmd_args = get_cmd_args()
args = get_pytest_args(file_name, cmd_args)
# Run PyTest with the supplied args.
# Equivalent to PyTest command line:
# env/bin/py.test -vx --log-format="%(levelname)s:%(name)s:%(message)s"
# --baseurl "http://0.0.0.0:8080/" tests.py -k "TestServer"
pytest.main(args)
# Run main when commands read either from standard input,
# from a script file, or from an interactive prompt.
if __name__ == "__main__":
main(__file__)