-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Optional | ||
|
||
from descope._auth_base import AuthBase | ||
from descope.management.common import MgmtV1 | ||
|
||
|
||
class Project(AuthBase): | ||
def update_name( | ||
self, | ||
name: str, | ||
): | ||
""" | ||
Update the current project name. | ||
Args: | ||
name (str): The new name for the project. | ||
Raise: | ||
AuthException: raised if operation fails | ||
""" | ||
self._auth.do_post( | ||
MgmtV1.project_update_name, | ||
{ | ||
"name": name, | ||
}, | ||
pswd=self._auth.management_key, | ||
) | ||
|
||
def clone( | ||
self, | ||
name: str, | ||
tag: Optional[str] = None, | ||
): | ||
""" | ||
Clone the current project, including its settings and configurations. | ||
Users, tenants and access keys are not cloned. | ||
Args: | ||
name (str): The new name for the project. | ||
tag(str): Optional tag for the project. Currently, only the "production" tag is supported. | ||
Return value (dict): | ||
Return dict Containing the new project details (name, id, tag, and settings). | ||
Raise: | ||
AuthException: raised if clone operation fails | ||
""" | ||
response = self._auth.do_post( | ||
MgmtV1.project_clone, | ||
{ | ||
"name": name, | ||
"tag": tag, | ||
}, | ||
pswd=self._auth.management_key, | ||
) | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import json | ||
from unittest import mock | ||
from unittest.mock import patch | ||
|
||
from descope import AuthException, DescopeClient | ||
from descope.common import DEFAULT_TIMEOUT_SECONDS | ||
from descope.management.common import MgmtV1 | ||
|
||
from .. import common | ||
|
||
|
||
class TestProject(common.DescopeTest): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
self.dummy_project_id = "dummy" | ||
self.dummy_management_key = "key" | ||
self.public_key_dict = { | ||
"alg": "ES384", | ||
"crv": "P-384", | ||
"kid": "P2CtzUhdqpIF2ys9gg7ms06UvtC4", | ||
"kty": "EC", | ||
"use": "sig", | ||
"x": "pX1l7nT2turcK5_Cdzos8SKIhpLh1Wy9jmKAVyMFiOCURoj-WQX1J0OUQqMsQO0s", | ||
"y": "B0_nWAv2pmG_PzoH3-bSYZZzLNKUA0RoE2SH7DaS0KV4rtfWZhYd0MEr0xfdGKx0", | ||
} | ||
|
||
def test_update_name(self): | ||
client = DescopeClient( | ||
self.dummy_project_id, | ||
self.public_key_dict, | ||
False, | ||
self.dummy_management_key, | ||
) | ||
|
||
# Test failed flows | ||
with patch("requests.post") as mock_post: | ||
mock_post.return_value.ok = False | ||
self.assertRaises( | ||
AuthException, | ||
client.mgmt.project.update_name, | ||
"name", | ||
) | ||
|
||
# Test success flow | ||
with patch("requests.post") as mock_post: | ||
mock_post.return_value.ok = True | ||
self.assertIsNone(client.mgmt.project.update_name("new-name")) | ||
mock_post.assert_called_with( | ||
f"{common.DEFAULT_BASE_URL}{MgmtV1.project_update_name}", | ||
headers={ | ||
**common.default_headers, | ||
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", | ||
}, | ||
params=None, | ||
json={ | ||
"name": "new-name", | ||
}, | ||
allow_redirects=False, | ||
verify=True, | ||
timeout=DEFAULT_TIMEOUT_SECONDS, | ||
) | ||
|
||
def test_clone(self): | ||
client = DescopeClient( | ||
self.dummy_project_id, | ||
self.public_key_dict, | ||
False, | ||
self.dummy_management_key, | ||
) | ||
|
||
# Test failed flows | ||
with patch("requests.post") as mock_post: | ||
mock_post.return_value.ok = False | ||
self.assertRaises( | ||
AuthException, | ||
client.mgmt.project.clone, | ||
"new-name", | ||
"production", | ||
) | ||
|
||
# Test success flow | ||
with patch("requests.post") as mock_post: | ||
network_resp = mock.Mock() | ||
network_resp.ok = True | ||
network_resp.json.return_value = json.loads( | ||
""" | ||
{ | ||
"id":"dummy" | ||
} | ||
""" | ||
) | ||
mock_post.return_value = network_resp | ||
resp = client.mgmt.project.clone( | ||
"new-name", | ||
"production", | ||
) | ||
self.assertEqual(resp["id"], "dummy") | ||
mock_post.assert_called_with( | ||
f"{common.DEFAULT_BASE_URL}{MgmtV1.project_clone}", | ||
headers={ | ||
**common.default_headers, | ||
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", | ||
}, | ||
params=None, | ||
json={ | ||
"name": "new-name", | ||
"tag": "production", | ||
}, | ||
allow_redirects=False, | ||
verify=True, | ||
timeout=DEFAULT_TIMEOUT_SECONDS, | ||
) |