-
Notifications
You must be signed in to change notification settings - Fork 0
[MPT-15443] Seeded accounts e2e data #144
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
Merged
robcsegal
merged 1 commit into
main
from
MPT-15443-seed-data-required-for-accounts-e-2-e-tests
Dec 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import logging | ||
|
|
||
| from seed.accounts.api_tokens import seed_api_token | ||
| from seed.accounts.buyer import seed_buyer | ||
| from seed.accounts.licensee import seed_licensee | ||
| from seed.accounts.module import seed_module | ||
| from seed.accounts.seller import seed_seller | ||
| from seed.accounts.user_group import seed_user_group | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def seed_accounts() -> None: # noqa: WPS217 | ||
| """Seed accounts data including account.""" | ||
| logger.debug("Seeding accounts ...") | ||
| await seed_seller() | ||
| await seed_buyer() | ||
| await seed_module() | ||
| await seed_api_token() | ||
| await seed_user_group() | ||
| await seed_licensee() | ||
|
|
||
| logger.debug("Seeded accounts completed.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from dependency_injector.wiring import inject | ||
|
|
||
| from mpt_api_client import AsyncMPTClient | ||
| from mpt_api_client.resources.accounts.api_tokens import ApiToken | ||
| from seed.context import Context | ||
| from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @inject | ||
| async def get_api_token( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_ops: AsyncMPTClient = DEFAULT_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 | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return api_token | ||
|
|
||
|
|
||
| @inject | ||
| def build_api_token_data( | ||
| context: Context = DEFAULT_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") | ||
| return { | ||
| "account": {"id": account_id}, | ||
| "name": "E2E Seeded API Token", | ||
| "description": "This is a seeded API token for end-to-end testing.", | ||
| "icon": "", | ||
| "modules": [{"id": module_id}], | ||
| } | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @inject | ||
| async def init_api_token( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_ops: AsyncMPTClient = DEFAULT_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 | ||
|
|
||
|
|
||
| @inject | ||
| async def seed_api_token() -> None: | ||
| """Seed API token.""" | ||
| logger.debug("Seeding API token ...") | ||
| await init_api_token() | ||
| logger.debug("Seeding API token completed.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import logging | ||
| import os | ||
| import pathlib | ||
|
|
||
| from dependency_injector.wiring import inject | ||
|
|
||
| from mpt_api_client import AsyncMPTClient | ||
| from mpt_api_client.resources.accounts.buyers import Buyer | ||
| from seed.context import Context | ||
| from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| icon = pathlib.Path("seed/data/logo.png").resolve() | ||
|
|
||
|
|
||
| @inject | ||
| async def get_buyer( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_operations: AsyncMPTClient = DEFAULT_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 | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return buyer | ||
|
|
||
|
|
||
| @inject | ||
| def build_buyer_data(context: Context = DEFAULT_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") | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not seller_id: | ||
| raise ValueError("accounts.seller.id missing from context; seed seller before buyer.") | ||
| return { | ||
| "name": "E2E Seeded Buyer", | ||
| "account": {"id": buyer_account_id}, | ||
| "sellers": [{"id": seller_id}], | ||
| "contact": { | ||
| "firstName": "first", | ||
| "lastName": "last", | ||
| "email": "created.buyer@example.com", | ||
| }, | ||
| "address": { | ||
| "addressLine1": "123 Main St", | ||
| "city": "Los Angeles", | ||
| "state": "CA", | ||
| "postCode": "12345", | ||
| "country": "US", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @inject | ||
| async def init_buyer( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_operations: AsyncMPTClient = DEFAULT_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 open(str(icon), "rb") as icon_file: # noqa: PTH123 | ||
| created = await mpt_operations.accounts.buyers.create(buyer_data, file=icon_file) | ||
| 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") | ||
| raise ValueError("Buyer creation failed") | ||
| logger.info("Buyer found: %s", buyer.id) | ||
| return buyer | ||
|
|
||
|
|
||
| @inject | ||
| async def seed_buyer() -> None: | ||
| """Seed buyer.""" | ||
| logger.debug("Seeding buyer ...") | ||
| await init_buyer() | ||
| logger.debug("Seeding buyer completed.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import logging | ||
| import os | ||
| import pathlib | ||
|
|
||
| from dependency_injector.wiring import inject | ||
|
|
||
| from mpt_api_client import AsyncMPTClient | ||
| from mpt_api_client.resources.accounts.licensees import Licensee | ||
| from seed.context import Context | ||
| from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_CLIENT | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| icon = pathlib.Path("seed/data/logo.png").resolve() | ||
|
|
||
|
|
||
| @inject | ||
| async def get_licensee( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_client: AsyncMPTClient = DEFAULT_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 | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return licensee | ||
|
|
||
|
|
||
| @inject | ||
| def build_licensee_data( # noqa: WPS238 | ||
| context: Context = DEFAULT_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") | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if group is None: | ||
| raise ValueError("User group is required in context") | ||
| licensee_type = "Client" | ||
robcsegal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| "name": "E2E Seeded Licensee", | ||
| "address": { | ||
| "addressLine1": "123 Main St", | ||
| "city": "Los Angeles", | ||
| "state": "CA", | ||
| "postCode": "67890", | ||
| "country": "US", | ||
| }, | ||
| "useBuyerAddress": False, | ||
| "seller": {"id": seller_id}, | ||
| "buyer": {"id": buyer_id}, | ||
| "account": {"id": account_id}, | ||
| "eligibility": {"client": True, "partner": False}, | ||
| "groups": [{"id": group.id}], | ||
| "type": licensee_type, | ||
| "status": "Enabled", | ||
| "defaultLanguage": "en-US", | ||
| } | ||
|
|
||
|
|
||
| @inject | ||
| async def init_licensee( | ||
| context: Context = DEFAULT_CONTEXT, | ||
| mpt_client: AsyncMPTClient = DEFAULT_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 open(str(icon), "rb") as icon_file: # noqa: PTH123 | ||
| 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") | ||
| raise ValueError("Licensee creation failed") | ||
| logger.info("Licensee found: %s", licensee.id) | ||
| return licensee | ||
|
|
||
|
|
||
| @inject | ||
| async def seed_licensee() -> None: | ||
| """Seed licensee.""" | ||
| logger.debug("Seeding licensee ...") | ||
| await init_licensee() | ||
| logger.info("Seeding licensee completed.") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.