Skip to content
Closed
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
3 changes: 1 addition & 2 deletions e2e_config.test.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"accounts.account.id": "ACC-9042-0088",
"accounts.api_token.id": "TKN-8857-1729",
"accounts.buyer.account.id": "ACC-1086-6867",
"accounts.client_account.id": "ACC-1086-6867",
"accounts.buyer.id": "BUY-1591-2112",
"accounts.licensee.account.id": "ACC-1086-6867",
"accounts.licensee.group.id": "UGR-2757-7226",
"accounts.licensee.id": "LCE-5758-4071-6507",
"accounts.module.id": "MOD-1756",
Expand Down
38 changes: 38 additions & 0 deletions seed/accounts/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging
import os
from functools import partial

from dependency_injector.wiring import Provide, inject

from seed.container import Container
from seed.context import Context
from seed.helper import init_resource

logger = logging.getLogger(__name__)


@inject
async def init_account_id(env_var: str, context: Context = Provide[Container.context]) -> str: # noqa: RUF029
"""Generic initializer for account IDs from environment variables."""
account_id = os.getenv(env_var)
if not account_id:
raise ValueError(f"{env_var} environment variable is required")
return account_id


async def seed_account_ids() -> None:
"""Seed account."""
logger.debug("Seeding vendor account ...")
await init_resource(
"accounts.account.id",
partial(init_account_id, "VENDOR_ACCOUNT_ID", context=Provide[Container.context]),
)
await init_resource(
"accounts.client_account.id",
partial(init_account_id, "CLIENT_ACCOUNT_ID", context=Provide[Container.context]),
)
await init_resource(
"accounts.operations_account.id",
partial(init_account_id, "OPERATIONS_ACCOUNT_ID", context=Provide[Container.context]),
)
logger.debug("Seeding account completed.")
6 changes: 5 additions & 1 deletion seed/accounts/accounts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from seed.accounts.account import seed_account_ids
from seed.accounts.api_tokens import seed_api_token
from seed.accounts.buyer import seed_buyer
from seed.accounts.licensee import seed_licensee
Expand All @@ -10,9 +11,12 @@
logger = logging.getLogger(__name__)


async def seed_accounts() -> None: # noqa: WPS217
async def seed_accounts() -> None: # noqa: WPS217, WPS213
"""Seed accounts data including account."""
logger.debug("Seeding accounts ...")

await seed_account_ids()

await seed_seller()
await seed_buyer()
await seed_module()
Expand Down
48 changes: 9 additions & 39 deletions seed/accounts/api_tokens.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
import logging
import os

from dependency_injector.wiring import Provide, inject

from mpt_api_client import AsyncMPTClient
from mpt_api_client.resources.accounts.api_tokens import ApiToken
from seed.container import Container
from seed.context import Context
from seed.helper import init_resource, require_context_id

logger = logging.getLogger(__name__)


@inject
async def get_api_token(
context: Context = Provide[Container.context],
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
) -> ApiToken | None:
"""Get API token from context or fetch from API."""
api_token_id = context.get_string("accounts.api_token.id")
if not api_token_id:
return None
try:
api_token = context.get_resource("accounts.api_token", api_token_id)
except ValueError:
api_token = None
if not isinstance(api_token, ApiToken):
api_token = await mpt_ops.accounts.api_tokens.get(api_token_id)
context.set_resource("accounts.api_token", api_token)
context["accounts.api_token.id"] = api_token.id
return api_token
return api_token


@inject
def build_api_token_data(
context: Context = Provide[Container.context],
) -> dict[str, object]:
"""Get API token data dictionary for creation."""
account_id = os.getenv("CLIENT_ACCOUNT_ID")
module_id = context.get_string("accounts.module.id")
account_id = require_context_id(context, "accounts.client_account.id", "creating API token")
module_id = require_context_id(context, "accounts.module.id", "creating API token")
return {
"account": {"id": account_id},
"name": "E2E Seeded API Token",
Expand All @@ -49,27 +28,18 @@ def build_api_token_data(


@inject
async def init_api_token(
async def create_api_token(
context: Context = Provide[Container.context],
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
) -> ApiToken:
"""Get or create API token."""
api_token = await get_api_token(context=context, mpt_ops=mpt_ops)
if api_token is None:
logger.debug("Creating API token ...")
api_token_data = build_api_token_data(context=context)
api_token = await mpt_ops.accounts.api_tokens.create(api_token_data)
context.set_resource("accounts.api_token", api_token)
context["accounts.api_token.id"] = api_token.id
logger.info("API token created: %s", api_token.id)
else:
logger.info("API token found: %s", api_token.id)
return api_token
"""Creates an API token."""
logger.debug("Creating API token ...")
api_token_data = build_api_token_data(context=context)
return await mpt_ops.accounts.api_tokens.create(api_token_data)


@inject
async def seed_api_token() -> None:
"""Seed API token."""
logger.debug("Seeding API token ...")
await init_api_token()
await init_resource("accounts.api_token.id", create_api_token)
logger.debug("Seeding API token completed.")
60 changes: 12 additions & 48 deletions seed/accounts/buyer.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
import logging
import os

from dependency_injector.wiring import Provide, inject

from mpt_api_client import AsyncMPTClient
from mpt_api_client.resources.accounts.buyers import Buyer
from seed.container import Container
from seed.context import Context
from seed.helper import init_resource, require_context_id
from seed.static.static import ICON

logger = logging.getLogger(__name__)


@inject
async def get_buyer(
context: Context = Provide[Container.context],
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
) -> Buyer | None:
"""Get buyer from context or fetch from API."""
buyer_id = context.get_string("accounts.buyer.id")
if not buyer_id:
return None
try:
buyer = context.get_resource("accounts.buyer", buyer_id)
except ValueError:
buyer = None
if not isinstance(buyer, Buyer):
buyer = await mpt_operations.accounts.buyers.get(buyer_id)
context.set_resource("accounts.buyer", buyer)
context["accounts.buyer.id"] = buyer.id
return buyer
return buyer


@inject
def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str, object]:
"""Build buyer data dictionary for creation."""
buyer_account_id = os.getenv("CLIENT_ACCOUNT_ID")
if not buyer_account_id:
raise ValueError("CLIENT_ACCOUNT_ID environment variable is required")
seller_id = context.get_string("accounts.seller.id")
if not seller_id:
raise ValueError("accounts.seller.id missing from context; seed seller before buyer.")
client_account_id = require_context_id(context, "accounts.client_account.id", "creating buyer")
seller_id = require_context_id(context, "accounts.seller.id", "creating buyer")

return {
"name": "E2E Seeded Buyer",
"account": {"id": buyer_account_id},
"account": {"id": client_account_id},
"sellers": [{"id": seller_id}],
"contact": {
"firstName": "first",
Expand All @@ -62,31 +38,19 @@ def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str,


@inject
async def init_buyer(
async def create_buyer(
context: Context = Provide[Container.context],
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
) -> Buyer:
"""Get or create buyer."""
buyer = await get_buyer(context=context, mpt_operations=mpt_operations)
if buyer is None:
buyer_data = build_buyer_data(context=context)
logger.debug("Creating buyer ...")
with ICON.open("rb") as icon_fd:
created = await mpt_operations.accounts.buyers.create(buyer_data, file=icon_fd)
if isinstance(created, Buyer):
context.set_resource("accounts.buyer", created)
context["accounts.buyer.id"] = created.id
logger.info("Buyer created: %s", created.id)
return created
logger.warning("Buyer creation failed") # type: ignore[unreachable]
raise ValueError("Buyer creation failed")
logger.info("Buyer found: %s", buyer.id)
return buyer
"""Creates a buyer."""
buyer_data = build_buyer_data(context=context)
logger.debug("Creating buyer ...")
with ICON.open("rb") as icon_fd:
return await mpt_operations.accounts.buyers.create(buyer_data, file=icon_fd)


@inject
async def seed_buyer() -> None:
"""Seed buyer."""
logger.debug("Seeding buyer ...")
await init_buyer()
await init_resource("accounts.buyer.id", create_buyer)
logger.debug("Seeding buyer completed.")
69 changes: 15 additions & 54 deletions seed/accounts/licensee.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
import logging
import os

from dependency_injector.wiring import Provide, inject

from mpt_api_client import AsyncMPTClient
from mpt_api_client.resources.accounts.licensees import Licensee
from seed.container import Container
from seed.context import Context
from seed.helper import init_resource, require_context_id
from seed.static.static import ICON

logger = logging.getLogger(__name__)


@inject
async def get_licensee(
context: Context = Provide[Container.context],
mpt_client: AsyncMPTClient = Provide[Container.mpt_client],
) -> Licensee | None:
"""Get licensee from context or fetch from API."""
licensee_id = context.get_string("accounts.licensee.id")
if not licensee_id:
return None
try:
licensee = context.get_resource("accounts.licensee", licensee_id)
except ValueError:
licensee = None
if not isinstance(licensee, Licensee):
licensee = await mpt_client.accounts.licensees.get(licensee_id)
context.set_resource("accounts.licensee", licensee)
context["accounts.licensee.id"] = licensee.id
return licensee
return licensee


@inject
def build_licensee_data( # noqa: WPS238
context: Context = Provide[Container.context],
) -> dict[str, object]:
"""Get licensee data dictionary for creation."""
account_id = os.getenv("CLIENT_ACCOUNT_ID")
if not account_id:
raise ValueError("CLIENT_ACCOUNT_ID environment variable is required")
seller_id = context.get_string("accounts.seller.id")
if not seller_id:
raise ValueError("Seller ID is required in context")
buyer_id = context.get_string("accounts.buyer.id")
if not buyer_id:
raise ValueError("Buyer ID is required in context")
group = context.get_resource("accounts.user_group")
if group is None:
raise ValueError("User group is required in context")
account_id = require_context_id(context, "accounts.client_account.id", "creating licensee")
seller_id = require_context_id(context, "accounts.seller.id", "creating licensee")
buyer_id = require_context_id(context, "accounts.buyer.id", "creating licensee")
group_id = require_context_id(context, "accounts.user_group.id", "creating licensee")

licensee_type = "Client"

return {
"name": "E2E Seeded Licensee",
"address": {
Expand All @@ -65,39 +38,27 @@ def build_licensee_data( # noqa: WPS238
"buyer": {"id": buyer_id},
"account": {"id": account_id},
"eligibility": {"client": True, "partner": False},
"groups": [{"id": group.id}],
"groups": [{"id": group_id}],
"type": licensee_type,
"status": "Enabled",
"defaultLanguage": "en-US",
}


@inject
async def init_licensee(
async def create_licensee(
context: Context = Provide[Container.context],
mpt_client: AsyncMPTClient = Provide[Container.mpt_client],
) -> Licensee:
"""Get or create licensee."""
licensee = await get_licensee(context=context, mpt_client=mpt_client)
if licensee is None:
licensee_data = build_licensee_data(context=context)
logger.debug("Creating licensee ...")
with ICON.open("rb") as icon_file:
created = await mpt_client.accounts.licensees.create(licensee_data, file=icon_file)
if isinstance(created, Licensee):
context.set_resource("accounts.licensee", created)
context["accounts.licensee.id"] = created.id
logger.info("Licensee created: %s", created.id)
return created
logger.warning("Licensee creation failed") # type: ignore[unreachable]
raise ValueError("Licensee creation failed")
logger.info("Licensee found: %s", licensee.id)
return licensee
"""Creates a licensee."""
licensee_data = build_licensee_data(context=context)
logger.debug("Creating licensee ...")
with ICON.open("rb") as icon_fd:
return await mpt_client.accounts.licensees.create(licensee_data, file=icon_fd)


@inject
async def seed_licensee() -> None:
"""Seed licensee."""
logger.debug("Seeding licensee ...")
await init_licensee()
await init_resource("accounts.licensee.id", create_licensee)
logger.info("Seeding licensee completed.")
Loading