Skip to content

Commit af49b54

Browse files
authored
Add create_organization function to Portal module (#38)
* Adds the ability to create an organization via the SDK for use with the Admin Portal.
1 parent 6d64c01 commit af49b54

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

tests/test_portal.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ class TestPortal(object):
1212
def setup(self, set_api_key):
1313
self.portal = Portal()
1414

15+
@pytest.fixture
16+
def mock_organization(self):
17+
return {
18+
"name": "Test Organization",
19+
"object": "organization",
20+
"id": "org_01EHT88Z8J8795GZNQ4ZP1J81T",
21+
"domains": [
22+
{
23+
"domain": "example.com",
24+
"object": "organization_domain",
25+
"id": "org_domain_01EHT88Z8WZEFWYPM6EC9BX2R8",
26+
}
27+
],
28+
}
29+
1530
@pytest.fixture
1631
def mock_organizations(self):
1732
return {
@@ -45,6 +60,19 @@ def mock_organizations(self):
4560
"listMetadata": {"before": "before-id", "after": None},
4661
}
4762

63+
def test_create_organization(self, mock_organization, mock_request_method):
64+
organization = {"domains": ["example.com"], "name": "Test Organization"}
65+
mock_response = Response()
66+
mock_response.status_code = 201
67+
mock_response.response_dict = mock_organization
68+
mock_request_method("post", mock_response, 201)
69+
70+
result = self.portal.create_organization(organization)
71+
subject = result.response_dict
72+
73+
assert subject["id"] == "org_01EHT88Z8J8795GZNQ4ZP1J81T"
74+
assert subject["name"] == "Test Organization"
75+
4876
def test_list_organizations(self, mock_organizations, mock_request_method):
4977
mock_response = Response()
5078
mock_response.status_code = 200

workos/portal.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import workos
2-
from workos.utils.request import RequestHelper, REQUEST_METHOD_GET
2+
from workos.utils.request import RequestHelper, REQUEST_METHOD_GET, REQUEST_METHOD_POST
33
from workos.utils.validation import PORTAL_MODULE, validate_settings
44

5+
ORGANIZATIONS_PATH = "organizations"
56
RESPONSE_LIMIT = 10
67

78

@@ -16,6 +17,24 @@ def request_helper(self):
1617
self._request_helper = RequestHelper()
1718
return self._request_helper
1819

20+
def create_organization(self, organization):
21+
"""Create an organization
22+
23+
Args:
24+
organization (dict) - An organization object
25+
organization[domains] (list) - List of domains that belong to the organization
26+
organization[name] (str) - A unique, descriptive name for the organization
27+
28+
Returns:
29+
dict: Created Organization response from WorkOS.
30+
"""
31+
return self.request_helper.request(
32+
ORGANIZATIONS_PATH,
33+
method=REQUEST_METHOD_POST,
34+
params=organization,
35+
token=workos.api_key,
36+
)
37+
1938
def list_organizations(
2039
self, domains=None, limit=RESPONSE_LIMIT, before=None, after=None
2140
):
@@ -37,7 +56,7 @@ def list_organizations(
3756
"after": after,
3857
}
3958
return self.request_helper.request(
40-
"organizations",
59+
ORGANIZATIONS_PATH,
4160
method=REQUEST_METHOD_GET,
4261
params=params,
4362
token=workos.api_key,

0 commit comments

Comments
 (0)