1+ # encoding: utf-8
2+ from __future__ import unicode_literals , print_function
3+
4+ import json
5+ import unittest
6+ import responses
7+
8+ from dynatademand .api import DemandAPIClient
9+ from dynatademand .errors import DemandAPIError
10+
11+ BASE_HOST = "http://test-url.example"
12+
13+
14+ class TestUsersEndpoints (unittest .TestCase ):
15+ def setUp (self ):
16+ self .api = DemandAPIClient (client_id = 'test' , username = 'testuser' , password = 'testpass' , base_host = BASE_HOST )
17+ self .api ._access_token = 'Bearer testtoken'
18+
19+ @responses .activate
20+ def test_get_project_permissions (self ):
21+ with open ('./tests/test_files/get_project_permissions.json' , 'r' ) as get_permissions_file :
22+ permissions_json = json .load (get_permissions_file )
23+ responses .add (responses .GET , '{}/sample/v1/projects/1/permissions' .format (BASE_HOST ), json = permissions_json , status = 200 )
24+ self .api .get_project_permissions (1 )
25+ self .assertEqual (len (responses .calls ), 1 )
26+ self .assertEqual (responses .calls [0 ].response .json (), permissions_json )
27+
28+ @responses .activate
29+ def test_upsert_project_permissions (self ):
30+ # Tests updating a project.
31+ with open ('./tests/test_files/upsert_project_permissions.json' , 'r' ) as upsert_project_file :
32+ upsert_project_data = json .load (upsert_project_file )
33+
34+ # Success response
35+ responses .add (
36+ responses .POST ,
37+ '{}/sample/v1/projects/1/permissions' .format (BASE_HOST ),
38+ json = {'status' : {'message' : 'success' }},
39+ status = 200 )
40+ # Error message included
41+ responses .add (
42+ responses .POST ,
43+ '{}/sample/v1/projects/1/permissions' .format (BASE_HOST ),
44+ json = {'status' : {'message' : 'error' }},
45+ status = 200 )
46+
47+ # Test successful response.
48+ self .api .upsert_project_permissions (1 , upsert_project_data )
49+ self .assertEqual (len (responses .calls ), 1 )
50+
51+ # Test response with error included.
52+ with self .assertRaises (DemandAPIError ):
53+ self .api .upsert_project_permissions (1 , upsert_project_data )
54+ self .assertEqual (len (responses .calls ), 2 )
0 commit comments