Skip to content

Commit

Permalink
test qrcode
Browse files Browse the repository at this point in the history
  • Loading branch information
copelco committed Jan 7, 2025
1 parent 5fc5581 commit 506aca1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
cache-dependency-path: "requirements/*/**.txt"
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends postgresql-client
pip install -U -q pip-tools
pip-sync requirements/base/base.txt requirements/dev/dev.txt
- run: pytest
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: "requirements/*/**.txt"
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends postgresql-client
pip install -U -q pip-tools
pip-sync requirements/base/base.txt requirements/dev/dev.txt
- run: pytest
29 changes: 25 additions & 4 deletions apps/odk_publish/etl/odk/qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,45 @@
logger = structlog.getLogger(__name__)


def create_app_user_qrcode(
def build_collect_settings(
app_user: ProjectAppUserAssignment,
base_url: str,
project_id: int,
project_name_prefix: str,
language: str = "en",
) -> io.BytesIO:
"""Generate a QR code as a PNG for the given app user."""
):
"""Build Collect settings for the given app user."""
collect_settings = DEFAULT_COLLECT_SETTINGS.copy()

# Customize settings
url = f"{base_url}key/{app_user.token}/projects/{project_id}"
url = f"{base_url.rstrip("/")}/key/{app_user.token}/projects/{project_id}"
collect_settings["general"]["server_url"] = url
collect_settings["general"]["username"] = app_user.displayName
collect_settings["general"]["app_language"] = language
project_name = f"{project_name_prefix}: {app_user.displayName} ({language})"
collect_settings["project"]["name"] = project_name

return collect_settings


def create_app_user_qrcode(
app_user: ProjectAppUserAssignment,
base_url: str,
project_id: int,
project_name_prefix: str,
language: str = "en",
) -> io.BytesIO:
"""Generate a QR code as a PNG for the given app user."""

# Build app user settings
collect_settings = build_collect_settings(
app_user=app_user,
base_url=base_url,
project_id=project_id,
project_name_prefix=project_name_prefix,
language=language,
)

# Generate QR code with segno
qr_data = base64.b64encode(zlib.compress(json.dumps(collect_settings).encode("utf-8")))
code = segno.make(qr_data, micro=False)
Expand Down
42 changes: 42 additions & 0 deletions tests/odk_publish/etl/odk/test_qrcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from apps.odk_publish.etl.odk.publish import ProjectAppUserAssignment
from apps.odk_publish.etl.odk.qrcode import build_collect_settings, create_app_user_qrcode


class TestCollectSettings:
@pytest.fixture
def app_user(self) -> ProjectAppUserAssignment:
return ProjectAppUserAssignment(
projectId=1,
id=1,
type=None,
displayName="10000",
createdAt="2025-01-07T14:18:37.300Z",
updatedAt=None,
deletedAt=None,
token="token1",
)

def test_build_collect_settings(self, app_user):
collect_settings = build_collect_settings(
app_user=app_user,
base_url="https://central",
project_id=1,
project_name_prefix="Project",
language="en",
)
assert collect_settings["general"]["server_url"] == "https://central/key/token1/projects/1"
assert collect_settings["general"]["username"] == app_user.displayName
assert collect_settings["general"]["app_language"] == "en"
assert collect_settings["project"]["name"] == "Project: 10000 (en)"

def test_create_app_user_qrcode(self, app_user):
qr_code = create_app_user_qrcode(
app_user=app_user,
base_url="https://central",
project_id=1,
project_name_prefix="Project",
language="en",
)
assert qr_code.getvalue()[:4] == b"\x89PNG"

0 comments on commit 506aca1

Please sign in to comment.