Skip to content

Commit fd24c7d

Browse files
andrii-balitskyirazor-xseambot
authored
Test new features (#68)
* Test seam client with api_key * Update test/seam/test_api_key.py Co-authored-by: Evan Sosenko <evan@getseam.com> * Update test_api_key.py * Add PAT test * Update test fn name * Replace relative imports with absolute ones in tests * Add test_env.py * ci: Format code * Add server fixture to get test endpoint and seed * Use endpoint and seed from new server test fixture * ci: Format code * Fix access token key used to access the seeded value --------- Co-authored-by: Evan Sosenko <evan@getseam.com> Co-authored-by: Seam Bot <devops@getseam.com>
1 parent 972ca35 commit fd24c7d

8 files changed

Lines changed: 225 additions & 31 deletions

File tree

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "fake-seam-connect --seed"
1010
},
1111
"devDependencies": {
12-
"@seamapi/fake-seam-connect": "1.65.5",
12+
"@seamapi/fake-seam-connect": "1.66.0",
1313
"@seamapi/nextlove-sdk-generator": "1.13.1",
1414
"@seamapi/types": "1.178.0",
1515
"del": "^7.1.0",

test/conftest.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
import socket
2+
from urllib.parse import urljoin
23
import pytest
34
import subprocess
45
import time
56
import os
6-
from seam import Seam
77
from contextlib import contextmanager
8+
import niquests as requests
9+
from seam import Seam
10+
11+
12+
@pytest.fixture(scope="function")
13+
def server():
14+
port = get_port()
15+
os.environ["PORT"] = str(port)
16+
17+
with subprocess_popen(["npm", "run", "start"]):
18+
# Allow some time for the server to start
19+
time.sleep(0.5)
20+
21+
endpoint = f"http://localhost:{port}"
22+
seed = get_seed(endpoint)
23+
24+
yield endpoint, seed
25+
26+
27+
@pytest.fixture(scope="function")
28+
def seam(server):
29+
endpoint, seed = server
30+
seam = Seam(endpoint=endpoint, api_key=seed["seam_apikey1_token"])
31+
32+
yield seam
833

934

1035
def get_port():
@@ -27,21 +52,6 @@ def subprocess_popen(*args):
2752
process.kill()
2853

2954

30-
@pytest.fixture(scope="function")
31-
def fake_seam_connect_server():
32-
port = get_port()
33-
os.environ["PORT"] = str(port)
34-
35-
with subprocess_popen(["npm", "run", "start"]):
36-
# Allow some time for the server to start
37-
time.sleep(0.5)
38-
39-
endpoint = f"http://localhost:{port}"
40-
41-
yield endpoint
42-
43-
44-
@pytest.fixture(scope="function")
45-
def seam(fake_seam_connect_server):
46-
seam = Seam(endpoint=fake_seam_connect_server, api_key="seam_apikey1_token")
47-
yield seam
55+
def get_seed(endpoint):
56+
seed_url = urljoin(endpoint, "/_fake/default_seed")
57+
return requests.get(seed_url).json()

test/seam/test_api_key.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
from seam import Seam
3+
from seam.auth import SeamHttpInvalidTokenError
4+
5+
6+
def test_seam_client_from_api_key_returns_instance_authorized_with_api_key(
7+
server,
8+
):
9+
endpoint, seed = server
10+
seam = Seam.from_api_key(seed["seam_apikey1_token"], endpoint=endpoint)
11+
devices = seam.devices.list()
12+
13+
assert len(devices) > 0
14+
15+
16+
def test_seam_client_constructor_returns_instance_authorized_with_api_key(
17+
server,
18+
):
19+
endpoint, seed = server
20+
seam = Seam(api_key=seed["seam_apikey1_token"], endpoint=endpoint)
21+
devices = seam.devices.list()
22+
23+
assert len(devices) > 0
24+
25+
26+
def test_seam_client_constructor_interprets_single_string_argument_as_api_key(server):
27+
_, seed = server
28+
seam = Seam(seed["seam_apikey1_token"])
29+
30+
assert seam is not None
31+
32+
with pytest.raises(SeamHttpInvalidTokenError, match=r"api_key"):
33+
Seam(api_key="some-invalid-key-format")
34+
35+
36+
def test_seam_client_checks_api_key_format():
37+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Unknown"):
38+
Seam.from_api_key("some-invalid-key-format")
39+
40+
with pytest.raises(SeamHttpInvalidTokenError, match=r"JWT"):
41+
Seam.from_api_key("ey")
42+
43+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Client Session Token"):
44+
Seam.from_api_key("seam_cst_token")
45+
46+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Access Token"):
47+
Seam.from_api_key("seam_at")

test/seam/test_env.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import os
2+
import pytest
3+
from seam import Seam
4+
from seam.options import SeamHttpInvalidOptionsError
5+
6+
7+
# Cleanup environment variables before and after each test
8+
def cleanup_env():
9+
os.environ.pop("SEAM_API_KEY", None)
10+
os.environ.pop("SEAM_ENDPOINT", None)
11+
os.environ.pop("SEAM_API_URL", None)
12+
13+
14+
@pytest.fixture(autouse=True)
15+
def run_around_tests():
16+
cleanup_env()
17+
yield
18+
cleanup_env()
19+
20+
21+
def test_seam_client_constructor_uses_seam_api_key_env_variable(server):
22+
endpoint, seed = server
23+
os.environ["SEAM_API_KEY"] = seed["seam_apikey1_token"]
24+
seam = Seam(endpoint=endpoint)
25+
26+
devices = seam.devices.list()
27+
assert len(devices) > 0
28+
29+
30+
def test_seam_client_api_key_option_overrides_env_variables(server):
31+
endpoint, seed = server
32+
os.environ["SEAM_API_KEY"] = "some-invalid-api-key-1"
33+
seam = Seam(api_key=seed["seam_apikey1_token"], endpoint=endpoint)
34+
35+
devices = seam.devices.list()
36+
assert len(devices) > 0
37+
38+
39+
def test_seam_client_api_key_option_as_first_argument_overrides_env_variables():
40+
os.environ["SEAM_API_KEY"] = "some-invalid-api-key-2"
41+
seam = Seam("seam_apikey_token")
42+
assert seam is not None
43+
44+
45+
def test_seam_client_constructor_requires_seam_api_key_when_passed_no_argument():
46+
with pytest.raises(SeamHttpInvalidOptionsError, match=r"api_key"):
47+
Seam()
48+
49+
50+
def test_seam_client_seam_endpoint_env_variable_is_used_first(server):
51+
endpoint, seed = server
52+
os.environ["SEAM_API_URL"] = "https://example.com"
53+
os.environ["SEAM_ENDPOINT"] = endpoint
54+
seam = Seam(api_key=seed["seam_apikey1_token"])
55+
56+
devices = seam.devices.list()
57+
assert len(devices) > 0
58+
59+
60+
def test_seam_client_seam_api_url_env_variable_is_used_as_fallback(server):
61+
endpoint, seed = server
62+
os.environ["SEAM_API_URL"] = endpoint
63+
seam = Seam(api_key=seed["seam_apikey1_token"])
64+
65+
devices = seam.devices.list()
66+
assert len(devices) > 0
67+
68+
69+
def test_seam_client_endpoint_option_overrides_env_variables(server):
70+
endpoint, seed = server
71+
os.environ["SEAM_API_URL"] = "https://example.com"
72+
os.environ["SEAM_ENDPOINT"] = "https://example.com"
73+
seam = Seam(api_key=seed["seam_apikey1_token"], endpoint=endpoint)
74+
75+
devices = seam.devices.list()
76+
assert len(devices) > 0
77+
78+
79+
def test_seam_client_seam_endpoint_env_variable_is_used_with_from_api_key(
80+
server,
81+
):
82+
endpoint, seed = server
83+
os.environ["SEAM_API_URL"] = "https://example.com"
84+
os.environ["SEAM_ENDPOINT"] = endpoint
85+
seam = Seam.from_api_key(seed["seam_apikey1_token"])
86+
87+
devices = seam.devices.list()
88+
assert len(devices) > 0
89+
90+
91+
@pytest.mark.xfail(reason="Fake does not support personal access token.")
92+
def test_seam_client_seam_api_key_env_variable_is_ignored_with_personal_access_token(
93+
server,
94+
):
95+
endpoint, seed = server
96+
os.environ["SEAM_API_KEY"] = seed["seam_apikey1_token"]
97+
98+
seam = Seam.from_personal_access_token(
99+
seed["seam_at1_token"],
100+
seed["seed_workspace_1"],
101+
endpoint=endpoint,
102+
)
103+
104+
devices = seam.devices.list()
105+
assert len(devices) > 0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from seam import Seam
3+
from seam.auth import SeamHttpInvalidTokenError
4+
from seam.seam_multi_workspace import SeamMultiWorkspace
5+
6+
7+
def test_seam_client_checks_personal_access_token_format():
8+
workspace_id = "e4203e37-e569-4a5a-bfb7-e3e8de66161d"
9+
10+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Unknown"):
11+
Seam.from_personal_access_token("some-invalid-key-format", workspace_id)
12+
13+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Unknown"):
14+
Seam.from_personal_access_token("seam_apikey_token", workspace_id)
15+
16+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Client Session Token"):
17+
Seam.from_personal_access_token("seam_cst", workspace_id)
18+
19+
with pytest.raises(SeamHttpInvalidTokenError, match=r"JWT"):
20+
Seam.from_personal_access_token("ey", workspace_id)
21+
22+
23+
def test_seam_multi_workspace_client_checks_personal_access_token_format():
24+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Unknown"):
25+
SeamMultiWorkspace.from_personal_access_token("some-invalid-key-format")
26+
27+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Unknown"):
28+
SeamMultiWorkspace.from_personal_access_token("seam_apikey_token")
29+
30+
with pytest.raises(SeamHttpInvalidTokenError, match=r"Client Session Token"):
31+
SeamMultiWorkspace.from_personal_access_token("seam_cst")
32+
33+
with pytest.raises(SeamHttpInvalidTokenError, match=r"JWT"):
34+
SeamMultiWorkspace.from_personal_access_token("ey")

test/workspaces/test_workspaces_create.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import random
2-
import string
3-
from seam import Seam, SeamMultiWorkspace
1+
from seam import SeamMultiWorkspace
42

53

6-
def test_workspaces_create(seam: Seam):
7-
r = "".join(random.choices(string.ascii_uppercase + string.digits, k=10))
4+
def test_workspaces_create(server):
5+
endpoint, seed = server
86
seam = SeamMultiWorkspace(
9-
endpoint=f"https://{r}.fakeseamconnect.seam.vc",
10-
personal_access_token="seam_at1_shorttoken_longtoken",
7+
endpoint=endpoint,
8+
personal_access_token=seed["seam_at1_token"],
119
)
1210

1311
workspace = seam.workspaces.create(

0 commit comments

Comments
 (0)