Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add tests for ServiceAccount

Michał Sipae1e1107ba6a2
diff --git a/tests/api/test_account_service_account.py b/tests/api/test_account_service_account.py
index 1c845c3fba..3174f685a4 100644
--- a/tests/api/test_account_service_account.py
+++ b/tests/api/test_account_service_account.py
@@ -269,6 +269,108 @@ def test_service_accounts_query_no_permission(
assert_no_permission(response)
+def service_accounts_for_pagination():
+ service_accounts = ServiceAccount.objects.bulk_create(
+ [
+ ServiceAccount(name="Account1", is_active=True),
+ ServiceAccount(name="AccountAccount1", is_active=True),
+ ServiceAccount(name="AccountAccount2", is_active=True),
+ ServiceAccount(name="Account2", is_active=True),
+ ServiceAccount(name="Account3", is_active=True),
+ ]
+ )
+ return service_accounts
+
+
+QUERY_SERVICE_ACCOUNT_PAGINATION = """
+ query (
+ $first: Int, $last: Int, $after: String, $before: String,
+ $sortBy: ServiceAccountSortingInput, $filter: ServiceAccountFilterInput
+ ){
+ serviceAccounts(
+ first: $first, last: $last, after: $after, before: $before,
+ sortBy: $sortBy, filter: $filter
+ ) {
+ edges{
+ node{
+ name
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+ "sort_by, service_account_order",
+ [
+ ({"field": "NAME", "direction": "ASC"}, ["Account1", "Account2", "Account3"]),
+ (
+ {"field": "NAME", "direction": "DESC"},
+ ["AccountAccount2", "AccountAccount1", "Account3"],
+ ),
+ ],
+)
+def test_attributes_pagination_with_sorting(
+ sort_by,
+ service_account_order,
+ staff_api_client,
+ service_accounts_for_pagination,
+ permission_manage_service_accounts,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_service_accounts)
+ page_size = 3
+ variables = {"first": page_size, "after": None, "sortBy": sort_by}
+ response = staff_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PAGINATION, variables,
+ )
+ content = get_graphql_content(response)
+ nodes = content["data"]["serviceAccounts"]["edges"]
+ assert service_account_order[0] == nodes[0]["node"]["name"]
+ assert service_account_order[1] == nodes[1]["node"]["name"]
+ assert service_account_order[2] == nodes[2]["node"]["name"]
+ assert len(nodes) == page_size
+
+
+ "filter_by, service_account_order",
+ [
+ ({"search": "Account"}, ["Account1", "AccountAccount1"]),
+ ({"search": "AccountAccount"}, ["AccountAccount1", "AccountAccount2"]),
+ ({"search": "accountaccount"}, ["AccountAccount1", "AccountAccount2"]),
+ ({"search": "Account1"}, ["Account1", "AccountAccount1"]),
+ ],
+)
+def test_attributes_pagination_with_filtering(
+ filter_by,
+ service_account_order,
+ staff_api_client,
+ service_accounts_for_pagination,
+ permission_manage_service_accounts,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_service_accounts)
+ page_size = 2
+ variables = {"first": page_size, "after": None, "filter": filter_by}
+ response = staff_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PAGINATION, variables,
+ )
+ content = get_graphql_content(response)
+ from pprint import pprint
+
+ pprint(content)
+ nodes = content["data"]["serviceAccounts"]["edges"]
+ assert service_account_order[0] == nodes[0]["node"]["name"]
+ assert service_account_order[1] == nodes[1]["node"]["name"]
+ assert len(nodes) == page_size
+
+
QUERY_SERVICE_ACCOUNT = """
query ($id: ID! ){
serviceAccount(id: $id){