Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 12 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: CI
name: Pins & tests

# This repo has no PR gate. The pins check is the whole job -- it is cheap, needs
# no secrets, and stops the Dockerfile and the manifest drifting apart again.
# Adding it does NOT make this repo tier 2: nothing here auto-merges.
# This repo has no PR gate beyond what runs here: the pins check (cheap, no
# secrets, stops the Dockerfile and the manifest drifting apart) and the
# pytest suite. Neither auto-merges anything.

on:
pull_request:
Expand All @@ -11,8 +11,15 @@ permissions:
contents: read

jobs:
ci:
pins-and-tests:
name: Pins & tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: WeMoveEU/ci-workflows/.github/actions/python-pins@v14
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: pip install uv
- run: uv sync --group dev
- run: uv run pytest
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ role = "library"
[dependency-groups]
dev = [
"pytest>=7.0.1",
"responses>=0.25",
]
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from actionkit import ActionKit
from actionkit.connection import Connection

HOSTNAME = "example.com"
USERNAME = "user"
PASSWORD = "password"


@pytest.fixture
def base_url():
return f"https://{HOSTNAME}"


@pytest.fixture
def connection():
return Connection(HOSTNAME, USERNAME, PASSWORD)


@pytest.fixture
def ak():
return ActionKit(HOSTNAME, USERNAME, PASSWORD)
81 changes: 81 additions & 0 deletions tests/test_actionkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pytest

from actionkit import ActionKit, connect


# --- connect() ---------------------------------------------------------


def test_connect_requires_credentials(monkeypatch):
for var in ["ACTIONKIT_USERNAME", "ACTIONKIT_PASSWORD", "ACTIONKIT_HOSTNAME"]:
monkeypatch.delenv(var, raising=False)
with pytest.raises(Exception, match="couldn't find login information"):
connect()


def test_connect_uses_env_vars_when_kwargs_omitted(monkeypatch):
monkeypatch.setenv("ACTIONKIT_USERNAME", "envuser")
monkeypatch.setenv("ACTIONKIT_PASSWORD", "envpass")
monkeypatch.setenv("ACTIONKIT_HOSTNAME", "env.example.com")
connection = connect()
assert connection.hostname == "env.example.com"
assert connection.request_kwargs["auth"].username == "envuser"
assert connection.request_kwargs["auth"].password == "envpass"


def test_connect_kwargs_take_precedence_over_env_vars(monkeypatch):
monkeypatch.setenv("ACTIONKIT_USERNAME", "envuser")
monkeypatch.setenv("ACTIONKIT_PASSWORD", "envpass")
monkeypatch.setenv("ACTIONKIT_HOSTNAME", "env.example.com")
connection = connect(hostname="explicit.example.com", username="u", password="p")
assert connection.hostname == "explicit.example.com"
assert connection.request_kwargs["auth"].username == "u"


# --- ActionKit wiring -------------------------------------------------


RESOURCE_ATTRS = [
"Orders",
"OrderRecurring",
"DonationAction",
"Groups",
"Languages",
"Lists",
"Uploads",
"Users",
"UserFields",
"Campaigns",
"MultilingualCampaigns",
"Petitions",
"DonationPages",
"RecurringPaymentPush",
"ProfileCancelPush",
"ProfileUpdatePush",
"SQL",
"Transactions",
"SignupPages",
"SignupActions",
"GenericActions",
"GenericPages",
]


@pytest.mark.parametrize("attr", RESOURCE_ATTRS)
def test_actionkit_wires_every_resource_attribute(ak, attr):
resource = getattr(ak, attr)
assert resource.connection is ak.connection


def test_actionkit_static_helpers_delegate_to_connection():
class _FakeResponse:
headers = {"Location": "https://example.com/rest/v1/thing/1/"}

assert (
ActionKit.get_resource_uri(_FakeResponse())
== "https://example.com/rest/v1/thing/1/"
)
assert ActionKit.get_resource_uri_id("https://example.com/rest/v1/thing/1/") == "1"
assert (
ActionKit.get_resource_uri_id_from_response(_FakeResponse()) == "1"
)
72 changes: 72 additions & 0 deletions tests/test_campaigns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json as json_module

import pytest
import responses

from urls import rest


@pytest.fixture
def campaigns(ak):
return ak.Campaigns


@responses.activate
def test_list_returns_id_to_name_mapping(campaigns):
responses.add(
responses.GET,
rest("allowedpagefield/campaign"),
json={"choices": [["1", "First"], ["2", "Second"]]},
status=200,
)
assert campaigns.list() == {1: "First", 2: "Second"}


@responses.activate
def test_create_full_sequence(campaigns):
responses.add(
responses.POST,
rest("signuppage"),
status=201,
headers={"Location": rest("signuppage/5/")},
)
responses.add(
responses.GET,
rest("allowedpagefield/campaign"),
json={"field_choices": "1=Existing"},
status=200,
)
responses.add(responses.PATCH, rest("signuppage/5"), status=200)
responses.add(responses.PATCH, rest("allowedpagefield/campaign"), status=200)
responses.add(responses.PATCH, rest("allowedmailingfield/campaign"), status=200)

result = campaigns.create(
"My Campaign",
"petition",
lead_campaigner="alice",
topic="climate",
fields={"extra": "1"},
)

assert result == rest("signuppage/5/")

create_body = json_module.loads(responses.calls[0].request.body)
assert create_body == {
"name": "My Campaign",
"title": "My Campaign",
"fields": {
"extra": "1",
"campaign_type": "petition",
"lead_campaigner": "alice",
"topic": "climate",
},
}

self_ref_body = json_module.loads(responses.calls[1].request.body)
assert self_ref_body == {"fields": {"campaign": "5"}}

pagefield_body = json_module.loads(responses.calls[3].request.body)
assert pagefield_body == {"field_choices": "1=Existing\n5=My Campaign"}

mailingfield_body = json_module.loads(responses.calls[4].request.body)
assert mailingfield_body == {"field_choices": "1=Existing\n5=My Campaign"}
Loading
Loading