Skip to content
Navigation Menu
{{ message }}
This repository was archived by the owner on Oct 9, 2018. It is now read-only.
forked from hotsyk/python-odesk
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtests.py
More file actions
1563 lines (1182 loc) · 52.7 KB
/
Copy pathtests.py
File metadata and controls
1563 lines (1182 loc) · 52.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
# -*- coding: utf-8 -*-
# Python bindings to oDesk API
# python-odesk version 0.5
# (C) 2010-2015 oDesk
from decimal import Decimal
from odesk import Client
from odesk import utils
from odesk.exceptions import (HTTP400BadRequestError,
HTTP401UnauthorizedError,
HTTP403ForbiddenError,
HTTP404NotFoundError,
ApiValueError,
IncorrectJsonResponseError)
from odesk.namespaces import Namespace
from odesk.oauth import OAuth
from odesk.routers.team import Team, Team_V2
from odesk.http import ODESK_ERROR_CODE, ODESK_ERROR_MESSAGE
from nose.tools import eq_, ok_
from mock import Mock, patch
import urlparse
import urllib2
import httplib
try:
import json
except ImportError:
import simplejson as json
class MicroMock(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
sample_json_dict = {u'glossary':
{u'GlossDiv':
{u'GlossList':
{u'GlossEntry':
{u'GlossDef':
{u'GlossSeeAlso': [u'GML', u'XML'],
u'para': u'A meta-markup language'},
u'GlossSee': u'markup',
u'Acronym': u'SGML',
u'GlossTerm': u'Standard Generalized Markup Language',
u'Abbrev': u'ISO 8879:1986',
u'SortAs': u'SGML',
u'ID': u'SGML'}},
u'title': u'S'},
u'title': u'example glossary'}}
def patched_urlopen(*args, **kwargs):
return MicroMock(data=json.dumps(sample_json_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen)
def test_client_urlopen():
public_key = 'public'
secret_key = 'secret'
client = Client(public_key, secret_key,
oauth_access_token='some access token',
oauth_access_token_secret='some access token secret')
#test urlopen
data = [{'url': 'http://test.url',
'data': {'foo': 'bar'},
'method': 'GET',
'result_data': None,
'result_url': 'http://test.url?api_sig=ddbf4b10a47ca8300554441dc7c9042b&api_key=public&foo=bar',
'result_method': 'GET'},
{'url': 'http://test.url',
'data': {},
'method': 'POST',
'result_data': 'api_sig=ba343f176db8166c4b7e88911e7e46ec&api_key=public',
'result_url': 'http://test.url',
'result_method': 'POST'},
{'url': 'http://test.url',
'data': {},
'method': 'PUT',
'result_data': 'api_sig=52cbaea073a5d47abdffc7fc8ccd839b&api_key=public&http_method=put',
'result_url': 'http://test.url',
'result_method': 'POST'},
{'url': 'http://test.url',
'data': {},
'method': 'DELETE',
'result_data': 'api_sig=8621f072b1492fbd164d808307ba72b9&api_key=public&http_method=delete',
'result_url': 'http://test.url',
'result_method': 'POST'},
]
result_json = json.dumps(sample_json_dict)
for params in data:
result = client.urlopen(url=params['url'],
data=params['data'],
method=params['method'])
assert result.data == result_json, (result.data, result_json)
def patched_urlopen_error(method, url, code=httplib.BAD_REQUEST,
message=None, data=None, **kwargs):
getheaders = Mock()
getheaders.return_value = {ODESK_ERROR_CODE: code,
ODESK_ERROR_MESSAGE: message}
return MicroMock(data=data, getheaders=getheaders, status=code)
def patched_urlopen_incorrect_json(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.OK, data='Service temporarily unavailable')
def patched_urlopen_400(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.BAD_REQUEST,
message='Limit exceeded', **kwargs)
def patched_urlopen_401(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.UNAUTHORIZED,
message='Not authorized', **kwargs)
def patched_urlopen_403(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.FORBIDDEN,
message='Forbidden', **kwargs)
def patched_urlopen_404(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.NOT_FOUND,
message='Not found', **kwargs)
def patched_urlopen_500(self, method, url, **kwargs):
return patched_urlopen_error(
method, url, code=httplib.INTERNAL_SERVER_ERROR,
message='Internal server error', **kwargs)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_incorrect_json)
def client_read_incorrect_json(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_400)
def client_read_400(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_401)
def client_read_401(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_403)
def client_read_403(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_404)
def client_read_404(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_500)
def client_read_500(client, url):
return client.read(url)
@patch('urllib3.PoolManager.urlopen', patched_urlopen)
def test_client_read():
"""Test client read() method.
Test cases:
method default (get) - other we already tested
format json|yaml ( should produce error)
codes 200|400|401|403|404|500
"""
public_key = 'public'
secret_key = 'secret'
client = Client(public_key, secret_key,
oauth_access_token='some access token',
oauth_access_token_secret='some access token secret')
test_url = 'http://test.url'
# Produce error on format other then json
class NotJsonException(Exception):
pass
try:
client.read(url=test_url, format='yaml')
raise NotJsonException("Client.read() doesn't produce error on "
"yaml format")
except NotJsonException, e:
raise e
except Exception:
pass
# Test get, all ok
result = client.read(url=test_url)
assert result == sample_json_dict, result
# Test get and status is ok, but json is incorrect,
# IncorrectJsonResponseError should be raised
try:
result = client_read_incorrect_json(client=client, url=test_url)
ok_(0, "No exception raised for 200 code and "
"incorrect json response: {0}".format(result))
except IncorrectJsonResponseError:
pass
except Exception, e:
assert 0, "Incorrect exception raised for 200 code " \
"and incorrect json response: " + str(e)
# Test get, 400 error
try:
result = client_read_400(client=client, url=test_url)
except HTTP400BadRequestError, e:
pass
except Exception, e:
assert 0, "Incorrect exception raised for 400 code: " + str(e)
# Test get, 401 error
try:
result = client_read_401(client=client, url=test_url)
except HTTP401UnauthorizedError, e:
pass
except Exception, e:
assert 0, "Incorrect exception raised for 401 code: " + str(e)
# Test get, 403 error
try:
result = client_read_403(client=client, url=test_url)
except HTTP403ForbiddenError, e:
pass
except Exception, e:
assert 0, "Incorrect exception raised for 403 code: " + str(e)
# Test get, 404 error
try:
result = client_read_404(client=client, url=test_url)
except HTTP404NotFoundError, e:
pass
except Exception, e:
assert 0, "Incorrect exception raised for 404 code: " + str(e)
# Test get, 500 error
try:
result = client_read_500(client=client, url=test_url)
except urllib2.HTTPError, e:
if e.code == httplib.INTERNAL_SERVER_ERROR:
pass
else:
assert 0, "Incorrect exception raised for 500 code: " + str(e)
except Exception, e:
assert 0, "Incorrect exception raised for 500 code: " + str(e)
def get_client():
public_key = 'public'
secret_key = 'secret'
oauth_access_token = 'some token'
oauth_access_token_secret = 'some token secret'
return Client(public_key, secret_key,
oauth_access_token,
oauth_access_token_secret)
@patch('urllib3.PoolManager.urlopen', patched_urlopen)
def test_client():
c = get_client()
test_url = "http://test.url"
result = c.get(test_url)
assert result == sample_json_dict, result
result = c.post(test_url)
assert result == sample_json_dict, result
result = c.put(test_url)
assert result == sample_json_dict, result
result = c.delete(test_url)
assert result == sample_json_dict, result
@patch('urllib3.PoolManager.urlopen', patched_urlopen)
def test_namespace():
ns = Namespace(get_client())
test_url = "http://test.url"
#test full_url
full_url = ns.full_url('test')
assert full_url == 'https://www.odesk.com/api/Nonev1/test', full_url
result = ns.get(test_url)
assert result == sample_json_dict, result
result = ns.post(test_url)
assert result == sample_json_dict, result
result = ns.put(test_url)
assert result == sample_json_dict, result
result = ns.delete(test_url)
assert result == sample_json_dict, result
teamrooms_dict = {'teamrooms':
{'teamroom':
{u'team_ref': u'1',
u'name': u'oDesk',
u'recno': u'1',
u'parent_team_ref': u'1',
u'company_name': u'oDesk',
u'company_recno': u'1',
u'teamroom_api': u'/api/team/v1/teamrooms/odesk:some.json',
u'id': u'odesk:some'}},
'teamroom': {'snapshot': 'test snapshot'},
'snapshots': {'user': 'test', 'snapshot': 'test'},
'snapshot': {'status': 'private'}
}
def patched_urlopen_teamrooms(*args, **kwargs):
return MicroMock(data=json.dumps(teamrooms_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_teamrooms)
def test_team():
te = Team(get_client())
te_v2 = Team_V2(get_client())
#test full_url
full_url = te.full_url('test')
assert full_url == 'https://www.odesk.com/api/team/v1/test', full_url
#test get_teamrooms
assert te_v2.get_teamrooms() == \
[teamrooms_dict['teamrooms']['teamroom']], te_v2.get_teamrooms()
#test get_snapshots
assert te_v2.get_snapshots(1) == \
[teamrooms_dict['teamroom']['snapshot']], te_v2.get_snapshots(1)
#test get_snapshot by contract
assert te_v2.get_snapshot_by_contract(1) == teamrooms_dict['snapshot'], \
te_v2.get_snapshot_by_contract(1)
#test update_snapshot by contract
assert te_v2.update_snapshot_by_contract(1, memo='memo') == teamrooms_dict, \
te_v2.update_snapshot_by_contract(1, memo='memo')
#test update_snapshot by contract
assert te_v2.delete_snapshot_by_contract(1) == teamrooms_dict, te_v2.delete_snapshot_by_contract(1)
#test get_snapshot
assert te.get_snapshot(1, 1) == teamrooms_dict['snapshot'], \
te.get_snapshot(1, 1)
#test update_snapshot
assert te.update_snapshot(1, 1, memo='memo') == teamrooms_dict, \
te.update_snapshot(1, 1, memo='memo')
#test update_snapshot
assert te.delete_snapshot(1, 1) == teamrooms_dict, te.delete_snapshot(1, 1)
#test get_workdiaries
eq_(te.get_workdiaries(1, 1, 1), (teamrooms_dict['snapshots']['user'],
[teamrooms_dict['snapshots']['snapshot']]))
#test get_workdiaries_by_contract
eq_(te_v2.get_workdiaries_by_contract(1, 1), (teamrooms_dict['snapshots']['user'],
[teamrooms_dict['snapshots']['snapshot']]))
#test get_snapshot_by_contract
eq_(te_v2.get_snapshot_by_contract(1), {'status':'private'})
teamrooms_dict_none = {'teamrooms': '',
'teamroom': '',
'snapshots': '',
'snapshot': ''
}
def patched_urlopen_teamrooms_none(*args, **kwargs):
return MicroMock(data=json.dumps(teamrooms_dict_none), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_teamrooms_none)
def test_teamrooms_none():
te = Team(get_client())
te_v2 = Team_V2(get_client())
#test full_url
full_url = te.full_url('test')
assert full_url == 'https://www.odesk.com/api/team/v1/test', full_url
#test get_teamrooms
assert te_v2.get_teamrooms() == [], te_v2.get_teamrooms()
#test get_snapshots
assert te_v2.get_snapshots(1) == [], te_v2.get_snapshots(1)
#test get_snapshot
eq_(te.get_snapshot(1, 1), teamrooms_dict_none['snapshot'])
userroles = {u'userrole':
[{u'parent_team__reference': u'1',
u'user__id': u'testuser', u'team__id': u'test:t',
u'reference': u'1', u'team__name': u'te',
u'company__reference': u'1',
u'user__reference': u'1',
u'user__first_name': u'Test',
u'user__last_name': u'Development',
u'parent_team__id': u'testdev',
u'team__reference': u'1', u'role': u'manager',
u'affiliation_status': u'none', u'engagement__reference': u'',
u'parent_team__name': u'TestDev', u'has_team_room_access': u'1',
u'company__name': u'Test Dev',
u'permissions':
{u'permission': [u'manage_employment', u'manage_recruiting']}}]}
engagement = {u'status': u'active',
u'buyer_team__reference': u'1', u'provider__reference': u'2',
u'job__title': u'development', u'roles': {u'role': u'buyer'},
u'reference': u'1', u'engagement_end_date': u'',
u'fixed_price_upfront_payment': u'0',
u'fixed_pay_amount_agreed': u'1.00',
u'provider__id': u'test_provider',
u'buyer_team__id': u'testteam:aa',
u'engagement_job_type': u'fixed-price',
u'job__reference': u'1', u'provider_team__reference': u'',
u'engagement_title': u'Developer',
u'fixed_charge_amount_agreed': u'0.01',
u'created_time': u'0000', u'provider_team__id': u'',
u'offer__reference': u'',
u'engagement_start_date': u'000', u'description': u''}
engagements = {u'lister':
{u'total_items': u'10', u'query': u'',
u'paging': {u'count': u'10', u'offset': u'0'}, u'sort': u''},
u'engagement': [engagement, engagement],
}
offer = {u'provider__reference': u'1',
u'signed_by_buyer_user': u'',
u'reference': u'1', u'job__description': u'python',
u'buyer_company__name': u'Python community',
u'engagement_title': u'developer', u'created_time': u'000',
u'buyer_company__reference': u'2', u'buyer_team__id': u'testteam:aa',
u'interview_status': u'in_process', u'buyer_team__reference': u'1',
u'signed_time_buyer': u'', u'has_buyer_signed': u'',
u'signed_time_provider': u'', u'created_by': u'testuser',
u'job__reference': u'2', u'engagement_start_date': u'00000',
u'fixed_charge_amount_agreed': u'0.01', u'provider_team__id': u'',
u'status': u'', u'signed_by_provider_user': u'',
u'engagement_job_type': u'fixed-price', u'description': u'',
u'provider_team__name': u'', u'fixed_pay_amount_agreed': u'0.01',
u'candidacy_status': u'active', u'has_provider_signed': u'',
u'message_from_provider': u'', u'my_role': u'buyer',
u'key': u'~~0001', u'message_from_buyer': u'',
u'buyer_team__name': u'Python community 2',
u'engagement_end_date': u'', u'fixed_price_upfront_payment': u'0',
u'created_type': u'buyer', u'provider_team__reference': u'',
u'job__title': u'translation', u'expiration_date': u'',
u'engagement__reference': u''}
offers = {u'lister':
{u'total_items': u'10', u'query': u'', u'paging':
{u'count': u'10', u'offset': u'0'}, u'sort': u''},
u'offer': [offer, offer]}
job = {u'subcategory': u'Development', u'reference': u'1',
u'buyer_company__name': u'Python community',
u'job_type': u'fixed-price', u'created_time': u'000',
u'created_by': u'test', u'duration': u'',
u'last_candidacy_access_time': u'',
u'category': u'Web',
u'buyer_team__reference': u'169108', u'title': u'translation',
u'buyer_company__reference': u'1', u'num_active_candidates': u'0',
u'buyer_team__name': u'Python community 2', u'start_date': u'000',
u'status': u'filled', u'num_new_candidates': u'0',
u'description': u'test', u'end_date': u'000',
u'public_url': u'http://www.odesk.com/jobs/~~0001',
u'visibility': u'invite-only', u'buyer_team__id': u'testteam:aa',
u'num_candidates': u'1', u'budget': u'1000', u'cancelled_date': u'',
u'filled_date': u'0000'}
jobs = [job, job]
task = {u'reference': u'test', u'company_reference': u'1',
u'team__reference': u'1', u'user__reference': u'1',
u'code': u'1', u'description': u'test task',
u'url': u'http://url.odesk.com/task', u'level': u'1'}
tasks = [task, task]
auth_user = {u'first_name': u'TestF', u'last_name': u'TestL',
u'uid': u'testuser', u'timezone_offset': u'0',
u'timezone': u'Europe/Athens', u'mail': u'test_user@odesk.com',
u'messenger_id': u'', u'messenger_type': u'yahoo'}
user = {u'status': u'active', u'first_name': u'TestF',
u'last_name': u'TestL', u'reference': u'0001',
u'timezone_offset': u'10800',
u'public_url': u'http://www.odesk.com/users/~~000',
u'is_provider': u'1',
u'timezone': u'GMT+02:00 Athens, Helsinki, Istanbul',
u'id': u'testuser'}
team = {u'status': u'active', u'parent_team__reference': u'0',
u'name': u'Test',
u'reference': u'1',
u'company__reference': u'1',
u'id': u'test',
u'parent_team__id': u'test_parent',
u'company_name': u'Test', u'is_hidden': u'',
u'parent_team__name': u'Test parent'}
company = {u'status': u'active',
u'name': u'Test',
u'reference': u'1',
u'company_id': u'1',
u'owner_user_id': u'1', }
hr_dict = {u'auth_user': auth_user,
u'server_time': u'0000',
u'user': user,
u'team': team,
u'company': company,
u'teams': [team, team],
u'companies': [company, company],
u'users': [user, user],
u'tasks': task,
u'userroles': userroles,
u'engagements': engagements,
u'engagement': engagement,
u'offer': offer,
u'offers': offers,
u'job': job,
u'jobs': jobs}
def patched_urlopen_hr(*args, **kwargs):
return MicroMock(data=json.dumps(hr_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_user():
hr = get_client().hr
#test get_user
assert hr.get_user(1) == hr_dict[u'user'], hr.get_user(1)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_companies():
hr = get_client().hr
#test get_companies
assert hr.get_companies() == hr_dict[u'companies'], hr.get_companies()
#test get_company
assert hr.get_company(1) == hr_dict[u'company'], hr.get_company(1)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_company_teams():
hr = get_client().hr
#test get_company_teams
assert hr.get_company_teams(1) == hr_dict['teams'], hr.get_company_teams(1)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_company_users():
hr = get_client().hr
#test get_company_users
assert hr.get_company_users(1) == hr_dict['users'], hr.get_company_users(1)
assert hr.get_company_users(1, False) == hr_dict['users'], \
hr.get_company_users(1, False)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_teams():
hr = get_client().hr
#test get_teams
assert hr.get_teams() == hr_dict[u'teams'], hr.get_teams()
#test get_team
assert hr.get_team(1) == hr_dict[u'team'], hr.get_team(1)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_team_users():
hr = get_client().hr
#test get_team_users
assert hr.get_team_users(1) == hr_dict[u'users'], hr.get_team_users(1)
assert hr.get_team_users(1, False) == hr_dict[u'users'], \
hr.get_team_users(1, False)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_userroles():
hr = get_client().hr
#test get_user_roles
assert hr.get_user_roles() == hr_dict['userroles'], hr.get_user_role()
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_jobs():
hr = get_client().hr
#test get_jobs
assert hr.get_jobs(1) == hr_dict[u'jobs'], hr.get_jobs()
assert hr.get_job(1) == hr_dict[u'job'], hr.get_job(1)
result = hr.update_job(1, 2, 'title', 'desc', 'public', budget=100,
status='open')
eq_(result, hr_dict)
assert hr.delete_job(1, 41) == hr_dict, hr.delete_job(1, 41)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_offers():
hr = get_client().hr
#test get_offers
assert hr.get_offers(1) == hr_dict[u'offers'], hr.get_offers()
assert hr.get_offer(1) == hr_dict[u'offer'], hr.get_offer(1)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_get_hrv2_engagements():
hr = get_client().hr
eq_(hr.get_engagements(), hr_dict[u'engagements'])
eq_(hr.get_engagements(provider_reference=1), hr_dict[u'engagements'])
eq_(hr.get_engagements(profile_key=1), hr_dict[u'engagements'])
eq_(hr.get_engagement(1), hr_dict[u'engagement'])
adjustments = {u'adjustment': {u'reference': '100'}}
def patched_urlopen_hradjustment(*args, **kwargs):
return MicroMock(data=json.dumps(adjustments), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hradjustment)
def test_hrv2_post_adjustment():
hr = get_client().hr
# Using ``charge_amount``
result = hr.post_team_adjustment(
1, 2, 'a test', charge_amount=100, notes='test note')
assert result == adjustments[u'adjustment'], result
try:
# If ``charge_amount`` is absent,
# error should be raised
hr.post_team_adjustment(1, 2, 'a test', notes='test note', charge_amount=0)
raise Exception('No error ApiValueError was raised when ``charge_amount`` is absent')
except ApiValueError:
pass
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_hr_end_contract():
hr = get_client().hr
result = hr.end_contract(1, 'API_REAS_JOB_COMPLETED_SUCCESSFULLY', 'yes')
assert result == hr_dict, result
# Test options validation
try:
result = hr.end_contract(1, 'reason', 'yes')
assert result == hr_dict, result
except ApiValueError:
pass
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_hr_suspend_contract():
hr = get_client().hr
result = hr.suspend_contract(1, 'message')
assert result == hr_dict, result
@patch('urllib3.PoolManager.urlopen', patched_urlopen_hr)
def test_hr_restart_contract():
hr = get_client().hr
result = hr.restart_contract(1, 'message')
assert result == hr_dict, result
job_data = {
'buyer_team_reference': 111,
'title': 'Test job from API',
'job_type': 'hourly',
'description': 'this is test job, please do not apply to it',
'visibility': 'odesk',
'category': 'Web Development',
'subcategory': 'Other - Web Development',
'budget': 100,
'duration': 10,
'start_date': 'some start date',
'skills': ['Python', 'JS']
}
job_data2 = {
'buyer_team_reference': 111,
'title': 'Test job from API',
'job_type': 'hourly',
'description': 'this is test job, please do not apply to it',
'visibility': 'odesk',
'subcategory2': 'Web & Mobile Development',
'budget': 100,
'duration': 10,
'start_date': 'some start date',
'skills': ['Python', 'JS']
}
def patched_urlopen_job_data_parameters2(self, method, url, **kwargs):
post_dict = urlparse.parse_qs(kwargs.get('body'))
post_dict.pop('oauth_timestamp')
post_dict.pop('oauth_signature')
post_dict.pop('oauth_nonce')
eq_(
dict(post_dict.items()),
{'buyer_team__reference': ['111'],
'subcategory2': ['Web & Mobile Development'],
'title': ['Test job from API'],
'skills': ['Python;JS'], 'job_type': ['hourly'],
'oauth_consumer_key': ['public'],
'oauth_signature_method': ['HMAC-SHA1'], 'budget': ['100'],
'visibility': ['odesk'],
'oauth_version': ['1.0'], 'oauth_token': ['some token'],
'oauth_body_hash': ['2jmj7l5rSw0yVb/vlWAYkK/YBwk='],
'duration': ['10'],
'start_date': ['some start date'],
'description': ['this is test job, please do not apply to it']})
return MicroMock(data='{"some":"data"}', status=200)
def patched_urlopen_job_data_parameters(self, method, url, **kwargs):
post_dict = urlparse.parse_qs(kwargs.get('body'))
post_dict.pop('oauth_timestamp')
post_dict.pop('oauth_signature')
post_dict.pop('oauth_nonce')
eq_(
dict(post_dict.items()),
{'category': ['Web Development'], 'buyer_team__reference': ['111'],
'subcategory': ['Other - Web Development'],
'title': ['Test job from API'],
'skills': ['Python;JS'], 'job_type': ['hourly'],
'oauth_consumer_key': ['public'],
'oauth_signature_method': ['HMAC-SHA1'], 'budget': ['100'],
'visibility': ['odesk'],
'oauth_version': ['1.0'], 'oauth_token': ['some token'],
'oauth_body_hash': ['2jmj7l5rSw0yVb/vlWAYkK/YBwk='],
'duration': ['10'],
'start_date': ['some start date'],
'description': ['this is test job, please do not apply to it']})
return MicroMock(data='{"some":"data"}', status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_job_data_parameters)
def test_job_data_parameters():
hr = get_client().hr
hr.post_job(**job_data)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_job_data_parameters2)
def test_job_data_parameters_subcategory2():
hr = get_client().hr
hr.post_job(**job_data2)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_job_data_parameters)
def test_job_data_no_category():
hr = get_client().hr
try:
hr.post_job('111', 'test', 'hourly', 'descr', 'odesk')
raise Exception('Request should raise ApiValueError exception.')
except ApiValueError:
pass
provider_dict = {'profile':
{u'response_time': u'31.0000000000000000',
u'dev_agency_ref': u'',
u'dev_adj_score_recent': u'0',
u'dev_ui_profile_access': u'Public',
u'dev_portrait': u'',
u'dev_ic': u'Freelance Provider',
u'certification': u'',
u'dev_usr_score': u'0',
u'dev_country': u'Ukraine',
u'dev_recent_rank_percentile': u'0',
u'dev_profile_title': u'Python developer',
u'dev_groups': u'',
u'dev_scores':
{u'dev_score':
[{u'description': u'competency and skills for the job, understanding of specifications/instructions',
u'avg_category_score_recent': u'',
u'avg_category_score': u'',
u'order': u'1', u'label': u'Skills'},
{u'description': u'quality of work deliverables',
u'avg_category_score_recent': u'',
u'avg_category_score': u'', u'order': u'2', u'label': u'Quality'},
]
}},
'providers': {'test': 'test'},
'jobs': {'test': 'test'},
'otherexp': 'experiences',
'skills': 'skills',
'tests': 'tests',
'certificates': 'certificates',
'employments': 'employments',
'educations': 'employments',
'projects': 'projects',
'quick_info': 'quick_info',
'categories': 'category 1',
'regions': 'region 1',
'tests': 'test 1',
}
def patched_urlopen_provider(*args, **kwargs):
return MicroMock(data=json.dumps(provider_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_provider)
def test_provider():
pr = get_client().provider
#test full_url
full_url = pr.full_url('test')
assert full_url == 'https://www.odesk.com/api/profiles/v1/test', full_url
#test get_provider
assert pr.get_provider(1) == provider_dict['profile'], pr.get_provider(1)
#test get_provider_brief
assert pr.get_provider_brief(1) == provider_dict['profile'], \
pr.get_provider_brief(1)
#test search_providers
assert pr.search_providers(data={'a': 1}) == provider_dict['providers'], \
pr.search_providers(data={'a': 1})
#test search_jobs
assert pr.search_jobs(data={'a': 1}) == provider_dict['jobs'], \
pr.get_jobs(data={'a': 1})
result = pr.get_categories_metadata()
assert result == provider_dict['categories']
result = pr.get_skills_metadata()
assert result == provider_dict['skills']
result = pr.get_regions_metadata()
assert result == provider_dict['regions']
result = pr.get_tests_metadata()
assert result == provider_dict['tests']
trays_dict = {'trays': [{u'unread': u'0',
u'type': u'sent',
u'id': u'1',
u'tray_api': u'/api/mc/v1/trays/username/sent.json'},
{u'unread': u'0',
u'type': u'inbox',
u'id': u'2',
u'tray_api': u'/api/mc/v1/trays/username/inbox.json'},
{u'unread': u'0',
u'type': u'notifications',
u'id': u'3',
u'tray_api': u'/api/mc/v1/trays/username/notifications.json'}]}
def patched_urlopen_trays(*args, **kwargs):
return MicroMock(data=json.dumps(trays_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_trays)
def test_get_trays():
mc = get_client().mc
#test full_url
full_url = mc.full_url('test')
assert full_url == 'https://www.odesk.com/api/mc/v1/test', full_url
#test get_trays
assert mc.get_trays(1) == trays_dict['trays'], mc.get_trays(1)
assert mc.get_trays(1, paging_offset=10, paging_count=10) ==\
trays_dict['trays'], mc.get_trays(1, paging_offset=10, paging_count=10)
tray_content_dict = {"current_tray": {"threads": '1'}}
def patched_urlopen_tray_content(*args, **kwargs):
return MicroMock(data=json.dumps(tray_content_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_tray_content)
def test_get_tray_content():
mc = get_client().mc
#test get_tray_content
assert mc.get_tray_content(1, 1) ==\
tray_content_dict['current_tray']['threads'], mc.get_tray_content(1, 1)
assert mc.get_tray_content(1, 1, paging_offset=10, paging_count=10) ==\
tray_content_dict['current_tray']['threads'], \
mc.get_tray_content(1, 1, paging_offset=10, paging_count=10)
thread_content_dict = {"thread": {"test": '1'}}
def patched_urlopen_thread_content(*args, **kwargs):
return MicroMock(data=json.dumps(thread_content_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_thread_content)
def test_get_thread_content():
mc = get_client().mc
#test get_provider
assert mc.get_thread_content(1, 1) ==\
thread_content_dict['thread'], mc.get_thread_content(1, 1)
assert mc.get_thread_content(1, 1, paging_offset=10, paging_count=10) ==\
thread_content_dict['thread'], \
mc.get_thread_content(1, 1, paging_offset=10, paging_count=10)
read_thread_content_dict = {"thread": {"test": '1'}}
def patched_urlopen_read_thread_content(*args, **kwargs):
return MicroMock(data=json.dumps(read_thread_content_dict), status=200)
@patch('urllib3.PoolManager.urlopen', patched_urlopen_read_thread_content)
def test_put_threads_read_unread():
mc = get_client().mc
read = mc.put_threads_read('test', [1, 2, 3])
assert read == read_thread_content_dict, read
unread = mc.put_threads_read('test', [5, 6, 7])
assert unread == read_thread_content_dict, unread
read = mc.put_threads_unread('test', [1, 2, 3])
assert read == read_thread_content_dict, read
@patch('urllib3.PoolManager.urlopen', patched_urlopen_read_thread_content)
def test_put_threads_starred_unstarred():
mc = get_client().mc
starred = mc.put_threads_starred('test', [1, 2, 3])
assert starred == read_thread_content_dict, starred
unstarred = mc.put_threads_unstarred('test', [5, 6, 7])
assert unstarred == read_thread_content_dict, unstarred
@patch('urllib3.PoolManager.urlopen', patched_urlopen_read_thread_content)
def test_put_threads_deleted_undeleted():
mc = get_client().mc
deleted = mc.put_threads_deleted('test', [1, 2, 3])
assert deleted == read_thread_content_dict, deleted
undeleted = mc.put_threads_undeleted('test', [5, 6, 7])
assert undeleted == read_thread_content_dict, undeleted
@patch('urllib3.PoolManager.urlopen', patched_urlopen_read_thread_content)
You can’t perform that action at this time.
