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

Fix failing resolver for transactionItem #12807

Merged
merged 1 commit into from May 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion saleor/graphql/payment/resolvers.py
Expand Up @@ -10,4 +10,8 @@ def resolve_payments(info):


def resolve_transaction(id):
return models.TransactionItem.objects.filter(id=id).first()
if id.isdigit():
query_params = {"id": id, "use_old_id": True}
else:
query_params = {"token": id}
return models.TransactionItem.objects.filter(**query_params).first()
2 changes: 2 additions & 0 deletions saleor/graphql/payment/schema.py
Expand Up @@ -73,6 +73,8 @@ def resolve_payments(_root, info: ResolveInfo, **kwargs):
@staticmethod
def resolve_transaction(_root, info: ResolveInfo, **kwargs):
_, id = from_global_id_or_error(kwargs["id"], TransactionItem)
if not id:
return None
return resolve_transaction(id)


Expand Down
160 changes: 148 additions & 12 deletions saleor/graphql/payment/tests/queries/test_transaction.py
Expand Up @@ -138,7 +138,42 @@ def test_transaction_created_by_app_query_by_app(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = app_api_client.post_graphql(
TRANSACTION_QUERY, variables, permissions=[permission_manage_payments]
)

# then
content = get_graphql_content(response)
_assert_transaction_fields_created_by(
content,
transaction_item_created_by_app,
event,
app,
)


def test_transaction_created_by_app_query_by_app_with_old_id(
app_api_client, transaction_item_created_by_app, permission_manage_payments, app
):
# given
transaction_item_created_by_app.use_old_id = True
transaction_item_created_by_app.save()
event = transaction_item_created_by_app.events.filter(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.id
)
}

# when
response = app_api_client.post_graphql(
Expand All @@ -155,6 +190,26 @@ def test_transaction_created_by_app_query_by_app(
)


def test_transaction_created_with_old_id_for_new_transaction(
app_api_client, transaction_item_created_by_app, permission_manage_payments, app
):
# given
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.id
)
}

# when
response = app_api_client.post_graphql(
TRANSACTION_QUERY, variables, permissions=[permission_manage_payments]
)

# then
content = get_graphql_content(response)
assert not content["data"]["transaction"]


def test_transaction_creted_by_app_query_no_order(
app_api_client, transaction_item_created_by_app, permission_manage_payments, app
):
Expand All @@ -166,7 +221,11 @@ def test_transaction_creted_by_app_query_no_order(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = app_api_client.post_graphql(
Expand All @@ -191,7 +250,11 @@ def test_transaction_created_by_app_query_by_staff(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = staff_api_client.post_graphql(
Expand All @@ -212,7 +275,11 @@ def test_transaction_create_by_app_query_no_permission(
app_api_client, transaction_item_created_by_app
):
# given
variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = app_api_client.post_graphql(TRANSACTION_QUERY, variables)
Expand All @@ -232,7 +299,11 @@ def test_transaction_created_by_user_query_by_app(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = app_api_client.post_graphql(
Expand Down Expand Up @@ -265,7 +336,11 @@ def test_transaction_creted_by_user_query_no_order(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = app_api_client.post_graphql(
Expand Down Expand Up @@ -295,7 +370,48 @@ def test_transaction_created_by_user_query_by_staff(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = staff_api_client.post_graphql(
TRANSACTION_QUERY,
variables,
permissions=[permission_manage_payments, permission_manage_staff],
)

# then
content = get_graphql_content(response)
_assert_transaction_fields_created_by(
content,
transaction_item_created_by_user,
event,
transaction_item_created_by_user.user,
)


def test_transaction_created_by_user_with_old_id(
staff_api_client,
transaction_item_created_by_user,
permission_manage_payments,
permission_manage_staff,
):
# given
transaction_item_created_by_user.use_old_id = True
transaction_item_created_by_user.save()

event = transaction_item_created_by_user.events.filter(
type=TransactionEventType.CHARGE_SUCCESS
).get()

variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.id
)
}

# when
response = staff_api_client.post_graphql(
Expand All @@ -318,7 +434,11 @@ def test_transaction_create_by_user_query_no_permission(
app_api_client, transaction_item_created_by_user
):
# given
variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = app_api_client.post_graphql(TRANSACTION_QUERY, variables)
Expand Down Expand Up @@ -350,7 +470,11 @@ def test_transaction_with_pending_amount(
setattr(transaction_item_created_by_user, db_field, expected_value)
transaction_item_created_by_user.save(update_fields=[db_field])

variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = staff_api_client.post_graphql(
Expand Down Expand Up @@ -385,7 +509,11 @@ def test_transaction_event_by_user(
user=staff_api_client.user,
)

variables = {"id": to_global_id_or_none(transaction_item_created_by_user)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_user.token
)
}

# when
response = staff_api_client.post_graphql(
Expand Down Expand Up @@ -435,7 +563,11 @@ def test_transaction_event_by_app(
app=app_api_client.app,
)

variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = app_api_client.post_graphql(
Expand Down Expand Up @@ -485,7 +617,11 @@ def test_transaction_event_by_reinstalled_app(
app=None,
)

variables = {"id": to_global_id_or_none(transaction_item_created_by_app)}
variables = {
"id": graphene.Node.to_global_id(
"TransactionItem", transaction_item_created_by_app.token
)
}

# when
response = app_api_client.post_graphql(
Expand Down