Skip to content

Commit 0e94250

Browse files
authored
Merge pull request #7 from QualiSystemsLab/token-login-fix
fixed token bug
2 parents b62bfd7 + a8990f2 commit 0e94250

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/cloudshell/sandbox_rest/sandbox_api.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(
4747
self.login()
4848

4949
def login(self, user="", password="", token="", domain="") -> None:
50+
""" Called from init - can also be used to refresh session with credentials """
5051
self.user = user or self.user
5152
self.password = password or self.password
5253
self.token = token or self.token
@@ -55,10 +56,10 @@ def login(self, user="", password="", token="", domain="") -> None:
5556
if not self.domain:
5657
raise ValueError("Domain must be passed to login")
5758

58-
if not self.token and self.user and self.password:
59+
if not self.token:
60+
if not self.user or not self.password:
61+
raise ValueError("Login requires Token or Username / Password")
5962
self.token = self._get_token_with_credentials(self.user, self.password, self.domain)
60-
else:
61-
raise ValueError("Login requires Token or Username / Password")
6263

6364
self._set_auth_header_on_session()
6465

tests/conftest.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ def admin_session() -> SandboxRestApiSession:
1212
admin_api = SandboxRestApiSession(
1313
host=CLOUDSHELL_SERVER, username=CLOUDSHELL_ADMIN_USER, password=CLOUDSHELL_ADMIN_PASSWORD, domain=CLOUDSHELL_DOMAIN
1414
)
15+
print(f"Admin session started. Token: {admin_api.token}")
1516
with admin_api:
1617
yield admin_api
1718
time.sleep(3)
18-
print("admin session token revoked")
19-
print(f"total requests: {admin_api.rest_service.request_counter}")
19+
20+
print("admin session token revoked")
21+
print(f"total requests: {admin_api.rest_service.request_counter}")
2022

2123

2224
@pytest.fixture(scope="session")

tests/test_api_no_sandbox.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
Live Cloudshell server is still a dependency
44
"""
55
import common
6+
import constants
7+
import env_settings
68
import pytest
7-
from constants import DEFAULT_EMPTY_BLUEPRINT
89

910
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
1011

1112

1213
@pytest.fixture(scope="module")
1314
def api_token(admin_session: SandboxRestApiSession):
1415
token = admin_session.get_token_for_target_user("admin")
16+
print(f"Get User token response: '{token}'")
1517
return token
1618

1719

18-
def test_delete_token(admin_session: SandboxRestApiSession, api_token: str):
19-
assert isinstance(api_token, str)
20-
print(f"Token response: '{api_token}'")
21-
admin_session.delete_token(api_token)
20+
def test_login_with_token(api_token):
21+
new_api = SandboxRestApiSession(host=env_settings.CLOUDSHELL_SERVER, token=api_token)
22+
print(f"\nnew api token: {new_api.token}")
23+
sandbox_res = new_api.get_sandboxes()
24+
assert isinstance(sandbox_res, list)
25+
new_api.delete_token(new_api.token)
26+
print("new api token deleted")
2227

2328

2429
def test_get_sandboxes(admin_session: SandboxRestApiSession):
@@ -36,7 +41,7 @@ def test_get_blueprints(admin_session: SandboxRestApiSession):
3641

3742

3843
def test_get_default_blueprint(admin_session: SandboxRestApiSession):
39-
bp_res = admin_session.get_blueprint_details(DEFAULT_EMPTY_BLUEPRINT)
44+
bp_res = admin_session.get_blueprint_details(constants.DEFAULT_EMPTY_BLUEPRINT)
4045
common.random_sleep()
4146
assert isinstance(bp_res, dict)
4247
bp_name = bp_res["name"]

0 commit comments

Comments
 (0)