Skip to content

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

Add test for query own gift cards

fowczarekb7eb6f36c3fd
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index b45eeb6c8a..23fc87f964 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -1,40 +1,36 @@
+from datetime import date
+
import graphene
from tests.api.utils import get_graphql_content
-from .utils import assert_no_permission
-
-def test_query_gift_cards(
- staff_api_client, gift_card, permission_manage_giftcard):
+def test_query_own_gift_card(user_api_client, gift_card):
query = """
- query giftCards{
- giftCards(first: 1) {
- edges {
- node {
- code
- creator {
- email
- }
- created
- startDate
- expirationDate
- lastUsedOn
- isActive
- initialBalance {
- amount
- }
- currentBalance {
- amount
- }
- }
+ query giftCard($id: ID!) {
+ giftCard(id: $id){
+ code
+ creator {
+ email
+ }
+ created
+ startDate
+ expirationDate
+ lastUsedOn
+ isActive
+ initialBalance {
+ amount
+ }
+ currentBalance {
+ amount
}
}
}
"""
- response = staff_api_client.post_graphql(
- query, permissions=[permission_manage_giftcard])
+ gift_card_id = graphene.Node.to_global_id('GiftCard', gift_card.pk)
+ variables = {'id': gift_card_id}
+ response = user_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
- data = content['data']['giftCards']['edges'][0]['node']
+ data = content['data']['giftCard']
assert data['code'] == gift_card.code
assert data['creator']['email'] == gift_card.creator.email
assert data['created'] == gift_card.created.isoformat()
@@ -46,25 +42,30 @@ def test_query_gift_cards(
assert data['currentBalance']['amount'] == gift_card.current_balance
-def test_query_gift_card(
- staff_api_client, gift_card, permission_manage_giftcard):
+def test_query_gift_card_with_premissions(
+ staff_api_client, gift_card, permission_manage_gift_card):
query = """
query giftCard($id: ID!) {
giftCard(id: $id){
code
+ creator {
+ email
+ }
}
}
"""
gift_card_id = graphene.Node.to_global_id('GiftCard', gift_card.pk)
variables = {'id': gift_card_id}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_giftcard])
+ staff_api_client.user.user_permissions.add(permission_manage_gift_card)
+ response = staff_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
data = content['data']['giftCard']
assert data['code'] == gift_card.code
+ assert data['creator']['email'] == gift_card.creator.email
-def test_query_gift_card_without_premissions(staff_api_client, gift_card):
+def test_query_gift_card_without_premissions(
+ user_api_client, gift_card_created_by_staff):
query = """
query giftCard($id: ID!) {
giftCard(id: $id){
@@ -72,7 +73,54 @@ def test_query_gift_card_without_premissions(staff_api_client, gift_card):
}
}
"""
- gift_card_id = graphene.Node.to_global_id('GiftCard', gift_card.pk)
+ gift_card_id = graphene.Node.to_global_id(
+ 'GiftCard', gift_card_created_by_staff.pk)
variables = {'id': gift_card_id}
- response = staff_api_client.post_graphql(query, variables)
- assert_no_permission(response)
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ assert not content['data']['giftCard']
+
+
+def test_query_gift_cards(
+ staff_api_client, gift_card, gift_card_created_by_staff,
+ permission_manage_gift_card):
+ query = """
+ query giftCards{
+ giftCards(first: 10) {
+ edges {
+ node {
+ code
+ }
+ }
+ }
+ }
+ """
+ response = staff_api_client.post_graphql(
+ query, permissions=[permission_manage_gift_card])
+ content = get_graphql_content(response)
+ data = content['data']['giftCards']['edges']
+ assert data[0]['node']['code'] == gift_card.code
+ assert data[1]['node']['code'] == gift_card_created_by_staff.code
+
+
+def test_query_own_gift_cards(
+ user_api_client, gift_card, gift_card_created_by_staff):
+ query = """
+ query giftCards{
+ me {
+ giftCards(first: 10) {
+ edges {
+ node {
+ code
+ }
+ }
+ totalCount
+ }
+ }
+ }
+ """
+ response = user_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ data = content['data']['me']['giftCards']
+ assert data['edges'][0]['node']['code'] == gift_card.code
+ assert data['totalCount'] == 1
diff --git a/tests/conftest.py b/tests/conftest.py
index 454b9b516d..d2c67285ed 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -348,7 +348,7 @@ def permission_manage_discounts():
@pytest.fixture
-def permission_manage_giftcard():
+def permission_manage_gift_card():
return Permission.objects.get(codename='manage_gift_card')
@@ -617,12 +617,19 @@ def order_line(order, variant, vatlayer):
@pytest.fixture
-def gift_card(customer_user): # pylint: disable=W0613
+def gift_card(customer_user):
return GiftCard.objects.create(
code='mirumee', creator=customer_user, initial_balance=10,
current_balance=10)
+def gift_card_created_by_staff(staff_user):
+ return GiftCard.objects.create(
+ code='mirumee_staff', creator=staff_user, initial_balance=100,
+ current_balance=100)
+
+
@pytest.fixture()
def order_with_lines(order, product_type, category, shipping_zone, vatlayer):
taxes = vatlayer