Skip to content

Commit 0a831f4

Browse files
authored
Merge pull request #63 from dynata/user-management-sdks
Added user management sdks
2 parents 0ac59b1 + bb1b277 commit 0a831f4

18 files changed

+376
-1
lines changed

dynatademand/api.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,44 @@ def get_project_detailed_report(self, project_id):
291291
)
292292
return self._api_get('/projects/{}/detailedReport'.format(project_id))
293293

294+
def get_user_info(self):
295+
return self._api_get('/user')
296+
297+
def get_company_users(self):
298+
return self._api_get('/users')
299+
300+
def get_company_teams(self):
301+
return self._api_get('/teams')
302+
303+
def get_roles(self, **kwargs):
304+
self.validator.validate_request(
305+
'get_roles',
306+
query_params=kwargs
307+
)
308+
return self._api_get('/roles', kwargs)
309+
310+
def get_project_permissions(self, project_id):
311+
self.validator.validate_request(
312+
'get_project_permissions',
313+
path_data={'extProjectId': '{}'.format(project_id)},
314+
)
315+
return self._api_get('/projects/{}/permissions'.format(project_id))
316+
317+
def upsert_project_permissions(self, project_id, upsert_permissions_data):
318+
self.validator.validate_request(
319+
'upsert_project_permissions',
320+
path_data={'extProjectId': '{}'.format(project_id)},
321+
request_body=upsert_permissions_data,
322+
)
323+
response_data = self._api_post('/projects/{}/permissions'.format(project_id), upsert_permissions_data)
324+
if response_data.get('status').get('message') != 'success':
325+
raise DemandAPIError(
326+
"Could not upsert project permissions. Demand API responded with: {}".format(
327+
response_data
328+
)
329+
)
330+
return response_data
331+
294332
def add_line_item(self, project_id, lineitem_data):
295333
'''
296334
A line item is a project entity that exist for a specific market and
@@ -524,7 +562,7 @@ def delete_template(self, id):
524562
self.validator.validate_request(
525563
'delete_template',
526564
path_data={'id': '{}'.format(id)},
527-
)
565+
)
528566
return self._api_delete('/templates/quotaplan/{}'.format(id))
529567

530568
def get_templates(self, country, lang, **kwargs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"users": {
5+
"type": "array",
6+
"items": {
7+
"type": "object",
8+
"properties": {
9+
"id": {
10+
"type": "integer"
11+
},
12+
"role": {
13+
"type": "string"
14+
}
15+
}
16+
}
17+
},
18+
"teams": {
19+
"type": "array",
20+
"items": {
21+
"type": "object",
22+
"properties": {
23+
"id": {
24+
"type": "integer"
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"extProjectId": {
5+
"type": "string",
6+
"required": true
7+
}
8+
},
9+
"required": [
10+
"extProjectId"
11+
]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"extProjectId": {
5+
"type": "string",
6+
"required": true
7+
}
8+
},
9+
"required": [
10+
"extProjectId"
11+
]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"name": {
5+
"type": "string",
6+
"description": "This is the name of the role. eg: manager"
7+
},
8+
"id": {
9+
"type": "array",
10+
"items": {
11+
"type": "string"
12+
}
13+
}
14+
},
15+
"required": []
16+
}

dynatademand/validator.py

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
'buy_project': ['path', 'body', ],
1818
'get_project_detailed_report': ['path', ],
1919
'reconcile_project': ['path', ],
20+
'get_project_permissions': ['path', ],
21+
'upsert_project_permissions': ['path', 'body', ],
22+
23+
# Roles
24+
'get_roles': ['query', ],
2025

2126
# Invoices
2227
'get_invoice': ['path', ],

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pytest = "4.6.6"
1818
jsonschema = "3.2.0"
1919
pytest-runner = "5.2"
2020
flake8 = "^3.7.9"
21+
pyrsistent = "0.14.11"
2122

2223
[build-system]
2324
requires = ["poetry>=0.12"]

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
responses>=0.10.0
2+
jsonschema>=2.5.0
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"email": "[email protected]",
4+
"id": 71,
5+
"name": "Bears Beets",
6+
"userName": "battlestargalactica"
7+
},
8+
{
9+
"email": "[email protected]",
10+
"id": 102,
11+
"name": "Michael Scott",
12+
"userName": "papercompany"
13+
}
14+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"currentUser": {
3+
"roles": [
4+
"PROJECT_MANAGER"
5+
]
6+
},
7+
"extProjectId": "1910a9fe-59de-4796-aac6-8b7356a7d340",
8+
"teams": [
9+
{
10+
"id": 139,
11+
"name": "All Users"
12+
}
13+
],
14+
"users": [
15+
{
16+
"id": 71,
17+
"role": "PROJECT_MANAGER",
18+
"username": "samplifyweb"
19+
}
20+
]
21+
}

tests/test_files/get_roles.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"allowedActions": [
4+
{
5+
"action": "GET PROJECT",
6+
"description": "Ability to view all the project details",
7+
"id": "GET_PROJECT"
8+
},
9+
{
10+
"action": "LIST ALL PROJECTS",
11+
"description": "Ability to view a list of projects on samplify",
12+
"id": "LIST_ALL_PROJECTS"
13+
}
14+
],
15+
"assignableRoles": [
16+
"SURVEY_AUTHOR"
17+
],
18+
"description": "",
19+
"id": "SURVEY_AUTHOR",
20+
"name": "Survey Author"
21+
}
22+
]

tests/test_files/get_teams.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"createdAt": "2020/05/06 15:39:03",
4+
"default": true,
5+
"description": "",
6+
"id": 139,
7+
"name": "That's what she said",
8+
"status": "ACTIVE",
9+
"updatedAt": "2020/05/06 15:39:03"
10+
}
11+
]

tests/test_files/get_user_info.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"companies": [
3+
{
4+
"default": true,
5+
"defaultRole": "PROJECT_MANAGER",
6+
"id": 51,
7+
"name": "Samplify Web",
8+
"teams": [
9+
{
10+
"default": true,
11+
"id": 139,
12+
"name": "All Users",
13+
"role": "PROJECT_MANAGER",
14+
"status": "ACTIVE"
15+
}
16+
]
17+
}
18+
],
19+
"email": "[email protected]",
20+
"fullName": "Monica Faloola Gellar",
21+
"id": 71,
22+
"userName": "monicagellarrr"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"currentUser": {
3+
"roles": [
4+
"PROJECT_MANAGER"
5+
]
6+
},
7+
"extProjectId": "1910a9fe-59de-4796-aac6-8b7356a7d340",
8+
"teams": [
9+
{
10+
"id": 139,
11+
"name": "All Users"
12+
}
13+
],
14+
"users": [
15+
{
16+
"id": 71,
17+
"role": "PROJECT_MANAGER",
18+
"username": "samplifyweb"
19+
}
20+
]
21+
}

tests/test_permissions.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 TestProjectPermissionsEndpoints(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(
24+
responses.GET,
25+
'{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
26+
json=permissions_json,
27+
status=200
28+
)
29+
self.api.get_project_permissions(1)
30+
self.assertEqual(len(responses.calls), 1)
31+
self.assertEqual(responses.calls[0].response.json(), permissions_json)
32+
33+
@responses.activate
34+
def test_upsert_project_permissions(self):
35+
# Tests updating a project.
36+
with open('./tests/test_files/upsert_project_permissions.json', 'r') as upsert_project_file:
37+
upsert_project_data = json.load(upsert_project_file)
38+
39+
# Success response
40+
responses.add(
41+
responses.POST,
42+
'{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
43+
json={'status': {'message': 'success'}},
44+
status=200)
45+
# Error message included
46+
responses.add(
47+
responses.POST,
48+
'{}/sample/v1/projects/1/permissions'.format(BASE_HOST),
49+
json={'status': {'message': 'error'}},
50+
status=200)
51+
52+
# Test successful response.
53+
self.api.upsert_project_permissions(1, upsert_project_data)
54+
self.assertEqual(len(responses.calls), 1)
55+
56+
# Test response with error included.
57+
with self.assertRaises(DemandAPIError):
58+
self.api.upsert_project_permissions(1, upsert_project_data)
59+
self.assertEqual(len(responses.calls), 2)

tests/test_roles.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
10+
BASE_HOST = "http://test-url.example"
11+
12+
13+
class TestRolesEndpoints(unittest.TestCase):
14+
def setUp(self):
15+
self.api = DemandAPIClient(client_id='test', username='testuser', password='testpass', base_host=BASE_HOST)
16+
self.api._access_token = 'Bearer testtoken'
17+
18+
@responses.activate
19+
def test_get_roles(self):
20+
# Tests getting all roles.
21+
with open('./tests/test_files/get_roles.json', 'r') as roles:
22+
roles_json = json.load(roles)
23+
# Success response
24+
responses.add(responses.GET, '{}/sample/v1/roles'.format(BASE_HOST), json=roles_json, status=200)
25+
self.api.get_roles()
26+
self.assertEqual(len(responses.calls), 1)

tests/test_teams.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
10+
BASE_HOST = "http://test-url.example"
11+
12+
13+
class TestTeamsEndpoints(unittest.TestCase):
14+
def setUp(self):
15+
self.api = DemandAPIClient(client_id='test', username='testuser', password='testpass', base_host=BASE_HOST)
16+
self.api._access_token = 'Bearer testtoken'
17+
18+
@responses.activate
19+
def test_get_teams(self):
20+
# Tests getting all teams.
21+
with open('./tests/test_files/get_teams.json', 'r') as teams:
22+
teams_json = json.load(teams)
23+
# Success response
24+
responses.add(responses.GET, '{}/sample/v1/teams'.format(BASE_HOST), json=teams_json, status=200)
25+
self.api.get_company_teams()
26+
self.assertEqual(len(responses.calls), 1)

0 commit comments

Comments
 (0)