Skip to content

Commit

Permalink
Refactor view tests authentication.
Browse files Browse the repository at this point in the history
Working towards pushing more validation into the BLL, it means our test
users to be more accurate and more customisable in tests. The existing
`client_with_permissions` and `client_with_user` fixtures don't work as
well with this model, because as our permissions model has developped,
what does with_permission mean?

This change refactors away those fixtures, and replaces it with
a `airlock_client` fixture that is more like normal django client in
usage.

It overrides the login method, so you can call
`airlock_client.login(...)` or `airlock_client.login_with_user(...)`,
just as you do with a normal client.  Under the hood, this uses the new
factories.create_user, giving us all the same conveniences.

I think this makes test setup still simple, but more explicit.
  • Loading branch information
bloodearnest committed Mar 14, 2024
1 parent 5cd90a4 commit 435a103
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 209 deletions.
8 changes: 2 additions & 6 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ def ensure_workspace(workspace_or_name):
raise Exception(f"Invalid workspace: {workspace_or_name})") # pragma: nocover


def create_workspace(name, user=None):
# create a default user with permission on workspace
if user is None:
user = create_user(workspaces=[name])

def create_workspace(name):
workspace_dir = settings.WORKSPACE_DIR / name
workspace_dir.mkdir(exist_ok=True, parents=True)
return bll.get_workspace(name)
Expand All @@ -52,7 +48,7 @@ def create_release_request(workspace, user=None, **kwargs):

# create a default user with permission on workspace
if user is None:
user = create_user(workspaces=[workspace.name])
user = create_user("author", workspaces=[workspace.name])

release_request = bll._create_release_request(
workspace=workspace.name, author=user.username, **kwargs
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ def test_e2e_reject_request(page, live_server, dev_users):
find_and_click(page.get_by_test_id("nav-requests"))

# View submitted request
find_and_click(page.get_by_role("link", name="test-workspace by testuser"))
find_and_click(page.get_by_role("link", name="test-workspace by author"))

# Reject request
find_and_click(page.locator("#reject-request-button"))
# Page contains rejected message text
expect(page.locator("body")).to_contain_text("Request has been rejected")
# Requests view does not show rejected request
find_and_click(page.get_by_test_id("nav-requests"))
expect(page.locator("body")).not_to_contain_text("test-workspace by testuser")
expect(page.locator("body")).not_to_contain_text("test-workspace by author")
24 changes: 11 additions & 13 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import pytest
from django.test import Client

from tests import factories


@pytest.fixture
def client_with_user(client):
def _client(session_user):
session_user.setdefault("username", "test")
user = factories.create_user(**session_user)
session = client.session
class AirlockClient(Client):
def login(self, username="testuser", workspaces={}, output_checker=False):
user = factories.create_user(username, workspaces, output_checker)
self.login_with_user(user)

def login_with_user(self, user):
session = self.session
session["user"] = user.to_dict()
session.save()
client.user = user
return client

return _client
self.user = user


@pytest.fixture
def client_with_permission(client_with_user):
output_checker = {"output_checker": True}
yield client_with_user(output_checker)
def airlock_client():
return AirlockClient()
Loading

0 comments on commit 435a103

Please sign in to comment.