Skip to content

Commit 965ba0b

Browse files
yolanferyYolanFery
andauthored
fix: Remove slash from api_url (#219)
* fix: Remove slash from api_url * fix: Remove slash from api_url * fix: Remove slash from api_url --------- Co-authored-by: YolanFery <yolanfery@MacBook-Pro-de-Yolan.local>
1 parent b6787eb commit 965ba0b

3 files changed

Lines changed: 85 additions & 34 deletions

File tree

.github/pull_request_template.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
A short, meaningful PR description. Explain the goal of the PR and the ideas behind it.
2+
3+
## Changes
4+
5+
Please list / describe the changes in the codebase for the reviewer(s).
6+
7+
- Change 1
8+
- Change 2
9+
10+
## How/what to test
11+
12+
A few words about what needs to be tested, along with specific testing instructions if needed.
13+
14+
## Screenshots / screencast
15+
16+
If relevant, please add some screenshots or a short video.

openhexa/cli/settings.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ def api_url(self):
5353
"""Return the API URL from the settings file or environment variables."""
5454
url_from_env = os.getenv("HEXA_API_URL") or os.getenv("HEXA_SERVER_URL")
5555
if url_from_env is None:
56-
return self._file_config["openhexa"]["url"]
56+
url_from_env = self._file_config["openhexa"]["url"]
57+
return url_from_env.rstrip("/")
5758

5859
@property
5960
def public_api_url(self):
6061
"""Return the public API URL from the settings file or environment variables."""
61-
url_from_env = os.getenv("HEXA_API_URL") or os.getenv("HEXA_SERVER_URL")
62-
if url_from_env is not None:
63-
return url_from_env
64-
return self._file_config["openhexa"]["url"].replace("api", "app")
62+
return self.api_url.replace("api", "app")
6563

6664
@property
6765
def current_workspace(self):

tests/test_cli.py

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
"""CLI test module."""
22

33
import base64
4+
import os
45
from io import BytesIO
56
from pathlib import Path
67
from tempfile import mkdtemp
78
from unittest import TestCase
8-
from unittest.mock import patch
9+
from unittest.mock import MagicMock, patch
910
from zipfile import ZipFile
1011

1112
import pytest
1213
from click.testing import CliRunner
1314

14-
from openhexa.cli.cli import pipelines_download, pipelines_run, workspaces_add
15+
from openhexa.cli.cli import pipelines_download, pipelines_push, pipelines_run, workspaces_add
16+
from openhexa.sdk.pipelines import Pipeline
17+
18+
python_file_name = "pipeline.py"
19+
python_code = "print('pipeline.py file')"
20+
version = "v1.0"
21+
pipeline_name = "MyPipeline"
22+
23+
24+
def create_zip_with_pipeline():
25+
"""Create a zip file containing the pipeline.py file."""
26+
zip_buffer = BytesIO()
27+
fake_zipfile = ZipFile(zip_buffer, "w")
28+
fake_zipfile.writestr(python_file_name, python_code)
29+
fake_zipfile.close()
30+
return zip_buffer
31+
32+
33+
def setup_graphql_response(zip_buffer=create_zip_with_pipeline()):
34+
"""Set up the mock GraphQL response pipelines."""
35+
return {
36+
"pipelineByCode": {"currentVersion": {"zipfile": base64.b64encode(zip_buffer.getvalue()).decode()}},
37+
"pipelines": {"items": []}, # (empty workspace initially)
38+
}
1539

1640

1741
@pytest.mark.usefixtures("settings")
@@ -58,52 +82,65 @@ def test_download_pipeline_no_pipeline(self, mock_graphql):
5882
def test_download_pipeline_overwrite(self, mock_graphql):
5983
"""Test the download pipeline command with an existing director with and without content."""
6084
with self.runner.isolated_filesystem() as tmp:
61-
# Create a zipfile with a pipeline.py file in a buffer
62-
zip_buffer = BytesIO()
63-
fake_zipfile = ZipFile(zip_buffer, "w")
64-
fake_zipfile.writestr("pipeline.py", "print('pipeline.py file')")
65-
fake_zipfile.close()
66-
67-
# Known Pipeline & non empty directory
68-
with open(tmp + "/pipeline.py", "w") as f:
85+
# Known Pipeline & non-empty directory
86+
with open(Path(tmp) / python_file_name, "w") as f:
6987
f.write("<content>")
70-
mock_graphql.return_value = {
71-
"pipelineByCode": {"currentVersion": {"zipfile": base64.b64encode(zip_buffer.getvalue()).decode()}}
72-
}
88+
89+
mock_graphql.return_value = setup_graphql_response()
90+
7391
result = self.runner.invoke(pipelines_download, ["test_pipeline", tmp], input="N\n")
7492
self.assertIn("Overwrite the files?", result.output)
7593
self.assertIn(f"{tmp} is not empty", result.output)
76-
self.assertEqual(open(tmp + "/pipeline.py").read(), "<content>")
94+
self.assertEqual(open(Path(tmp) / python_file_name).read(), "<content>")
7795

7896
# Overwrite the files in the directory
79-
mock_graphql.return_value = {
80-
"pipelineByCode": {"currentVersion": {"zipfile": base64.b64encode(zip_buffer.getvalue()).decode()}}
81-
}
97+
mock_graphql.return_value = setup_graphql_response()
98+
8299
result = self.runner.invoke(pipelines_download, ["test_pipeline", tmp], input="y\n")
83100
self.assertIn("Overwrite the files?", result.output)
84-
self.assertEqual(open(tmp + "/pipeline.py").read(), "print('pipeline.py file')")
101+
self.assertEqual(open(Path(tmp) / python_file_name).read(), python_code)
85102

86103
@patch("openhexa.cli.api.graphql")
87104
def test_download_pipeline(self, mock_graphql):
88105
"""Test the download pipeline command."""
89106
with self.runner.isolated_filesystem() as tmp:
90107
# Create a zipfile with a pipeline.py file in a buffer
91-
zip_buffer = BytesIO()
92-
fake_zipfile = ZipFile(zip_buffer, "w")
93-
fake_zipfile.writestr("pipeline.py", "print('pipeline.py file')")
94-
fake_zipfile.close()
95-
96-
mock_graphql.return_value = {
97-
"pipelineByCode": {"currentVersion": {"zipfile": base64.b64encode(zip_buffer.getvalue()).decode()}}
98-
}
108+
mock_graphql.return_value = setup_graphql_response()
109+
99110
result = self.runner.invoke(pipelines_download, ["test_pipeline", tmp])
100111
self.assertEqual(result.exit_code, 0)
101-
self.assertTrue((Path(tmp) / "pipeline.py").exists())
102-
self.assertEqual(open(tmp + "/pipeline.py").read(), "print('pipeline.py file')")
112+
path = Path(tmp) / python_file_name
113+
self.assertTrue(path.exists())
114+
self.assertEqual(open(path).read(), python_code)
115+
116+
@patch("openhexa.cli.api.graphql")
117+
@patch("openhexa.cli.cli.get_pipeline")
118+
@patch("openhexa.cli.cli.upload_pipeline")
119+
@patch.dict(os.environ, {"HEXA_API_URL": "https://www.bluesquarehub.com/", "HEXA_WORKSPACE": "workspace"})
120+
def test_push_pipeline(self, mock_upload_pipeline, mock_get_pipeline, mock_graphql):
121+
"""Test pushing a pipeline."""
122+
with self.runner.isolated_filesystem() as tmp:
123+
with open(Path(tmp) / python_file_name, "w") as f:
124+
f.write(python_code)
125+
mock_graphql.return_value = setup_graphql_response()
126+
mock_pipeline = MagicMock(spec=Pipeline)
127+
mock_pipeline.code = pipeline_name
128+
mock_get_pipeline.return_value = mock_pipeline
129+
130+
result = self.runner.invoke(pipelines_push, [tmp, "--name", version])
131+
self.assertEqual(result.exit_code, 0)
132+
self.assertIn(
133+
(
134+
f"✅ New version '{version}' created! "
135+
f"You can view the pipeline in OpenHEXA on https://www.bluesquarehub.com/workspaces/workspace/pipelines/{pipeline_name}"
136+
),
137+
result.output,
138+
)
139+
self.assertTrue(mock_upload_pipeline.called)
103140

104141
@patch("openhexa.cli.api.graphql")
105142
def test_workspaces_add_not_found(self, mock_graphql):
106-
"""Test the add workspace command when the workspae doesn't exist on the current server."""
143+
"""Test the add workspace command when the workspace doesn't exist on the current server."""
107144
with self.runner.isolated_filesystem():
108145
mock_graphql.return_value = {"workspace": None}
109146
result = self.runner.invoke(workspaces_add, ["test_workspace"], input="random_token \n")

0 commit comments

Comments
 (0)