Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add superuser as part of OpenKlantServiceManager #1419

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/openklant2/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import logging
import os
import subprocess
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Mapping
from urllib.parse import urljoin

import requests
Expand All @@ -23,16 +25,24 @@ class OpenKlantServiceManager:
_api_token: str = "b2eb1da9861da88743d72a3fb4344288fe2cba44"
_docker_compose_project_name: str = "openklant2-api-test"
_docker_compose_path: Path = BASE_DIR / "docker-compose.yaml"
_superuser_password: str = "admin"
_superuser_username: str = "supersecret99"

def _docker_compose(
self,
*args: str,
check: bool = True,
input: str | None = None,
env: Mapping | None = None,
):
_env = None
if env:
_env = os.environ.copy()
_env = _env.update(env)

input_data = {"text": True, "input": input} if input else {}
try:
return subprocess.run(
result = subprocess.run(
args=[
"docker-compose",
"-f",
Expand All @@ -43,6 +53,7 @@ def _docker_compose(
],
check=check,
capture_output=True,
env=_env,
**input_data,
)
except subprocess.CalledProcessError as exc:
Expand All @@ -53,10 +64,13 @@ def _docker_compose(
)
raise

return result

def _manage_py(
self,
*args: str,
input: str | None = None,
env: Mapping | None = None,
):
self._docker_compose(
"run",
Expand All @@ -66,6 +80,7 @@ def _manage_py(
"src/manage.py",
*args,
input=input,
env=env,
)

def _service_teardown(self):
Expand All @@ -81,6 +96,18 @@ def _service_init(self):
def reset_db_state(self):
self._manage_py("flush", "--no-input")
self._load_fixture_from_json_string(self._generate_token_fixture())
self._create_superuser()

def _create_superuser(self):
self._manage_py(
"createsuperuser",
"--username",
self._superuser_username,
"--email",
"[email protected]",
"--no-input",
env={"DJANGO_SUPERUSER_PASSWORD": self._superuser_password},
)

def _load_fixture_from_json_string(self, fixture: str):
self._manage_py(
Expand Down
Loading