diff --git a/tests/test_portal.py b/tests/test_portal.py index 0bb50c28..dc509995 100644 --- a/tests/test_portal.py +++ b/tests/test_portal.py @@ -12,6 +12,10 @@ class TestPortal(object): def setup(self, set_api_key): self.portal = Portal() + @pytest.fixture + def mock_portal_link(self): + return {"link": "https://id.workos.com/portal/launch?secret=secret"} + @pytest.fixture def mock_organization(self): return { @@ -73,6 +77,17 @@ def test_create_organization(self, mock_organization, mock_request_method): assert subject["id"] == "org_01EHT88Z8J8795GZNQ4ZP1J81T" assert subject["name"] == "Test Organization" + def test_generate_link(self, mock_portal_link, mock_request_method): + mock_response = Response() + mock_response.status_code = 201 + mock_response.response_dict = mock_portal_link + mock_request_method("post", mock_response, 201) + + result = self.portal.generate_link("sso", "org_01EHQMYV6MBK39QC5PZXHY59C3") + subject = result.response_dict + + assert subject["link"] == "https://id.workos.com/portal/launch?secret=secret" + def test_list_organizations(self, mock_organizations, mock_request_method): mock_response = Response() mock_response.status_code = 200 diff --git a/workos/portal.py b/workos/portal.py index 6e7867d6..a0a8b170 100644 --- a/workos/portal.py +++ b/workos/portal.py @@ -3,6 +3,7 @@ from workos.utils.validation import PORTAL_MODULE, validate_settings ORGANIZATIONS_PATH = "organizations" +PORTAL_GENERATE_PATH = "portal/generate_link" RESPONSE_LIMIT = 10 @@ -35,6 +36,31 @@ def create_organization(self, organization): token=workos.api_key, ) + def generate_link(self, intent, organization, return_url=None): + """Generate a link to grant access to an organization's Admin Portal + + Args: + intent (str): The access scope for the generated Admin Portal link. Valid values are: ["sso"] + organization (string): The ID of the organization the Admin Portal link will be generated for + + Kwargs: + return_url (str): The URL that the end user will be redirected to upon exiting the generated Admin Portal. If none is provided, the default redirect link set in your WorkOS Dashboard will be used. (Optional) + + Returns: + str: URL to redirect a User to to access an Admin Portal session + """ + params = { + "intent": intent, + "organization": organization, + "return_url": return_url, + } + return self.request_helper.request( + PORTAL_GENERATE_PATH, + method=REQUEST_METHOD_POST, + params=params, + token=workos.api_key, + ) + def list_organizations( self, domains=None, limit=RESPONSE_LIMIT, before=None, after=None ):