Skip to content

Commit

Permalink
Handle WorkspaceAPIKey and user identified workspace mismatch (#448)
Browse files Browse the repository at this point in the history
* workspace ref

* oops

* redo

* workflow

* forget it

* forget it

* bump
  • Loading branch information
ieaves authored Jun 13, 2023
1 parent 200e23f commit 5c0d73e
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 25 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/server-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ env:
py_ver: "3.11"
poetry_ver: "1.3.1"
SECRET_KEY: "fixed-test-key-that-doesnt-matter"
DB_USER: "grai"
DB_PASSWORD: "grai"
DB_NAME: "grai"
DEBUG: "False"
DB_HOST: "127.0.0.1"
DB_PORT: "5432"

jobs:
lint-server:
Expand Down Expand Up @@ -48,9 +54,9 @@ jobs:
postgres:
image: postgres:latest
env:
POSTGRES_USER: grai
POSTGRES_PASSWORD: grai
POSTGRES_DB: grai
POSTGRES_USER: ${{ env.DB_USER }}
POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }}
POSTGRES_DB: ${{ env.DB_NAME }}
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
Expand All @@ -72,17 +78,13 @@ jobs:
pip install pytest pytest_asyncio pytest-django pytest-mock coverage
pip install .
- name: Tests
- name: Run Migrations
run: python manage.py migrate --noinput

- name: Run Coverage Tests
env:
DEBUG: False
DB_HOST: "127.0.0.1"
DB_PORT: "5432"
DB_USER: grai
DB_NAME: grai
DB_PASSWORD: grai
GITHUB_PRIVATE_KEY: installations/tests/sample.private-key.pem
run: |
python manage.py migrate --noinput
coverage run -m pytest
coverage xml
Expand Down
2 changes: 1 addition & 1 deletion grai-client/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "grai-client"
version = "0.2.16"
version = "0.2.17"
description = ""
authors = ["Ian Eaves <[email protected]>"]
license = "Elastic-2.0"
Expand Down
2 changes: 1 addition & 1 deletion grai-client/src/grai_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from grai_client import authentication, endpoints, schemas, update, utilities

__version__ = "0.2.16"
__version__ = "0.2.17"
1 change: 1 addition & 0 deletions grai-client/src/grai_client/endpoints/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ def client_get_url(client: BaseClient, url: str, options: ClientOptions = Client
url = add_query_params(url, options.query_args)

response = client.session.get(url, headers=options.headers, **options.request_args)

response_status_check(response)
return response

Expand Down
13 changes: 8 additions & 5 deletions grai-client/src/grai_client/endpoints/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ def workspace(self, workspace: Optional[Union[str, UUID]]):
self.default_payload.pop("workspace", None)
return

if is_valid_uuid(workspace):
pass
elif isinstance(workspace, str):
if is_valid_uuid(workspace) or isinstance(workspace, str):
result = self.get("workspace", workspace)

if result is None:
raise Exception(f"No workspace found matching `{workspace}`")
elif "/" in workspace: # workspace ref
if workspace != result.ref:
raise Exception(f"No workspace matching `ref={workspace}`")
elif workspace != result.name: # workspace name
raise Exception(f"No workspace matching `name={workspace}`")
else:
workspace = result.id

workspace = result.id
else:
raise TypeError("Workspace must be either a string, uuid, or None.")

Expand Down
1 change: 1 addition & 0 deletions grai-client/src/grai_client/endpoints/v1/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def get_workspace_by_name_v1(
Raises:
"""

if is_valid_uuid(name):
url = f"{client.get_url(grai_type)}{name}/"
elif len(name.split("/")) == 2:
Expand Down
1 change: 1 addition & 0 deletions grai-client/src/grai_client/schemas/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class Workspace(BaseModel):
""" """

name: str
ref: str
id: UUID
2 changes: 1 addition & 1 deletion grai-server/app/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "the_guide"
version = "0.1.28"
version = "0.1.29"
description = ""
authors = ["Ian Eaves <[email protected]>", "Edward Louth <[email protected]>"]
license = "Elastic-2.0"
Expand Down
1 change: 1 addition & 0 deletions grai-server/app/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Workspace(TenantModel):
def __str__(self):
return f"{self.organisation.name}/{self.name}"

@property
def ref(self):
return f"{self.organisation.name}/{self.name}"

Expand Down
7 changes: 3 additions & 4 deletions grai-server/app/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@


class WorkspaceSerializer(serializers.ModelSerializer):
ref = serializers.ReadOnlyField()

class Meta:
model = Workspace
fields = (
"id",
"name",
)
fields = ("id", "name", "ref")
read_only_fields = ("ref", "created_at", "updated_at")


Expand Down
3 changes: 1 addition & 2 deletions grai-server/app/workspaces/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ def test_organisation_created():
def test_workspace_created():
organisation = Organisation.objects.create(name="O1")
workspace = Workspace.objects.create(name="abc", organisation=organisation)

assert workspace.id == uuid.UUID(str(workspace.id))
assert workspace.name == "abc"
assert str(workspace) == "O1/abc"
assert workspace.ref() == "O1/abc"
assert workspace.ref == "O1/abc"
assert isinstance(workspace.created_at, datetime.datetime)
assert isinstance(workspace.updated_at, datetime.datetime)

Expand Down
7 changes: 7 additions & 0 deletions grai-server/app/workspaces/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def test_get_workspaces(self, auto_login_user):
workspaces = list(response.json())
assert len(workspaces) == 1

def test_get_workspace_has_ref(self, auto_login_user):
client, user, workspace = auto_login_user()
url = reverse("workspaces:workspaces-list")
response = client.get(url)
workspace = response.json()[0]
assert "ref" in workspace, "workspace should have ref"

def test_get_workspaces_by_name(self, auto_login_user, create_organisation, create_workspace2):
client, user, workspace = auto_login_user()
url = reverse(f"workspaces:workspaces-list")
Expand Down

0 comments on commit 5c0d73e

Please sign in to comment.