Skip to content

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

Fix test after rebase

Adam Janikcc0fc152c3ec
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index 2074f535fe..0a8bb57324 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -204,3 +204,34 @@ def test_model_form_update_mutation(model_form_class):
meta = TestUpdateMutation._meta
assert 'id' in meta.arguments
+
+
+def test_create_token_mutation(admin_client, staff_user):
+ query = '''
+ mutation {
+ tokenCreate(email: "%(email)s", password: "%(password)s") {
+ token
+ errors {
+ field
+ message
+ }
+ }
+ }
+ '''
+ success_query = query % {'email': staff_user.email, 'password': 'password'}
+ response = admin_client.post(reverse('api'), {'query': success_query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ token_data = content['data']['tokenCreate']
+ assert token_data['token']
+ assert not token_data['errors']
+
+ error_query = query % {'email': staff_user.email, 'password': 'wat'}
+ response = admin_client.post(reverse('api'), {'query': error_query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ token_data = content['data']['tokenCreate']
+ assert not token_data['token']
+ errors = token_data['errors']
+ assert errors
+ assert not errors[0]['field']
diff --git a/tests/api/test_page.py b/tests/api/test_page.py
index 608981bf31..4eb0ce2978 100644
--- a/tests/api/test_page.py
+++ b/tests/api/test_page.py
@@ -5,6 +5,8 @@ import pytest
from django.shortcuts import reverse
from tests.utils import get_graphql_content
+from saleor.page.models import Page
+
def test_page_query(client, page):
page.is_visible = True
@@ -98,3 +100,38 @@ def test_page_delete_mutation(admin_client, page):
assert data['page']['title'] == page.title
with pytest.raises(page._meta.model.DoesNotExist):
page.refresh_from_db()
+
+
+def test_paginate_pages(client, page):
+ page.is_visible = True
+ data_02 = {
+ 'slug': 'test02-url',
+ 'title': 'Test page',
+ 'content': 'test content',
+ 'is_visible': True}
+ data_03 = {
+ 'slug': 'test03-url',
+ 'title': 'Test page',
+ 'content': 'test content',
+ 'is_visible': True}
+
+ page2 = Page.objects.create(**data_02)
+ page3 = Page.objects.create(**data_03)
+ query = """
+ query PagesQuery {
+ pages(first: 2) {
+ edges {
+ node {
+ id
+ title
+ }
+ }
+ }
+ }
+ """
+ response = client.post(
+ reverse('api'), {'query': query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ pages_data = content['data']['pages']
+ assert len(pages_data['edges']) == 2
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 09a48ff86d..b75352f15d 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -2,82 +2,13 @@ import json
import graphene
import pytest
-from django.forms.models import model_to_dict
from django.shortcuts import reverse
from django.utils.text import slugify
from prices import Money
from tests.utils import get_graphql_content
-from saleor.product.models import Category, Product, ProductAttribute
-
-
-def test_create_token_mutation(admin_client, staff_user):
- query = '''
- mutation {
- tokenCreate(email: "%(email)s", password: "%(password)s") {
- token
- errors {
- field
- message
- }
- }
- }
- '''
- success_query = query % {'email': staff_user.email, 'password': 'password'}
- response = admin_client.post(reverse('api'), {'query': success_query})
- content = get_graphql_content(response)
- assert 'errors' not in content
- token_data = content['data']['tokenCreate']
- assert token_data['token']
- assert not token_data['errors']
-
- error_query = query % {'email': staff_user.email, 'password': 'wat'}
- response = admin_client.post(reverse('api'), {'query': error_query})
- content = get_graphql_content(response)
- assert 'errors' not in content
- token_data = content['data']['tokenCreate']
- assert not token_data['token']
- errors = token_data['errors']
- assert errors
- assert not errors[0]['field']
-
-
-def test_category_query(client, product):
- category = Category.objects.first()
- query = '''
- query {
- category(id: "%(category_pk)s") {
- id
- name
- ancestors {
- edges {
- node {
- name
- }
- }
- }
- children {
- edges {
- node {
- name
- }
- }
- }
- }
- }
- ''' % {'category_pk': graphene.Node.to_global_id('Category', category.pk)}
- response = client.post(reverse('api'), {'query': query})
- content = get_graphql_content(response)
- assert 'errors' not in content
- category_data = content['data']['category']
- assert category_data is not None
- assert category_data['name'] == category.name
- assert (
- len(category_data['ancestors']['edges']) ==
- category.get_ancestors().count())
- assert (
- len(category_data['children']['edges']) ==
- category.get_children().count())
+from saleor.product.models import (
+ Category, Product, ProductAttribute, ProductType)
def test_fetch_all_products(client, product):
@@ -412,256 +343,6 @@ def test_attributes_in_category_query(client, product):
assert len(attributes_data) == ProductAttribute.objects.count()
-def test_real_query(admin_client, product):
- product_attr = product.product_type.product_attributes.first()
- category = product.category
- attr_value = product_attr.values.first()
- filter_by = '%s:%s' % (product_attr.slug, attr_value.slug)
- query = '''
- query Root($categoryId: ID!, $sortBy: String, $first: Int, $attributesFilter: [AttributeScalar], $minPrice: Float, $maxPrice: Float) {
- category(id: $categoryId) {
- ...CategoryPageFragmentQuery
- __typename
- }
- attributes(inCategory: $categoryId) {
- edges {
- node {
- ...ProductFiltersFragmentQuery
- __typename
- }
- }
- }
- }
-
- fragment CategoryPageFragmentQuery on Category {
- id
- name
- url
- ancestors {
- edges {
- node {
- name
- id
- url
- __typename
- }
- }
- }
- children {
- edges {
- node {
- name
- id
- url
- slug
- __typename
- }
- }
- }
- products(first: $first, sortBy: $sortBy, attributes: $attributesFilter, price_Gte: $minPrice, price_Lte: $maxPrice) {
- ...ProductListFragmentQuery
- __typename
- }
- __typename
- }
-
- fragment ProductListFragmentQuery on ProductCountableConnection {
- edges {
- node {
- ...ProductFragmentQuery
- __typename
- }
- __typename
- }
- pageInfo {
- hasNextPage
- __typename
- }
- __typename
- }
-
- fragment ProductFragmentQuery on Product {
- id
- name
- price {
- amount
- currency
- localized
- __typename
- }
- availability {
- ...ProductPriceFragmentQuery
- __typename
- }
- thumbnailUrl1x: thumbnailUrl(size: "255x255")
- thumbnailUrl2x: thumbnailUrl(size: "510x510")
- url
- __typename
- }
-
- fragment ProductPriceFragmentQuery on ProductAvailability {
- available
- discount {
- gross {
- amount
- currency
- __typename
- }
- __typename
- }
- priceRange {
- stop {
- gross {
- amount
- currency
- localized
- __typename
- }
- currency
- __typename
- }
- start {
- gross {
- amount
- currency
- localized
- __typename
- }
- currency
- __typename
- }
- __typename
- }
- __typename
- }
-
- fragment ProductFiltersFragmentQuery on ProductAttribute {
- id
- name
- slug
- values {
- id
- name
- slug
- __typename
- }
- __typename
- }
- '''
- response = admin_client.post(
- reverse('api'), {
- 'query': query,
- 'variables': json.dumps(
- {
- 'categoryId': graphene.Node.to_global_id(
- 'Category', category.id),
- 'sortBy': 'name',
- 'first': 1,
- 'attributesFilter': [filter_by]})})
- content = get_graphql_content(response)
- assert 'errors' not in content
-
-def test_page_query(client, page):
- page.is_visible = True
- query = """
- query PageQuery($id: ID!) {
- page(id: $id) {
- title
- slug
- }
- }
- """
- variables = json.dumps({
- 'id': graphene.Node.to_global_id('Page', page.id)})
- response = client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- page_data = content['data']['page']
- assert page_data['title'] == page.title
- assert page_data['slug'] == page.slug
-
-
-def test_paginate_pages(client, page):
- page.is_visible = True
- data_02 = {
- 'slug': 'test02-url',
- 'title': 'Test page',
- 'content': 'test content',
- 'is_visible': True}
- data_03 = {
- 'slug': 'test03-url',
- 'title': 'Test page',
- 'content': 'test content',
- 'is_visible': True}
-
- page2 = Page.objects.create(**data_02)
- page3 = Page.objects.create(**data_03)
- query = """
- query PagesQuery {
- pages(first: 2) {
- edges {
- node {
- id
- title
- }
- }
- }
- }
- """
- response = client.post(
- reverse('api'), {'query': query})
- content = get_graphql_content(response)
- assert 'errors' not in content
- pages_data = content['data']['pages']
- assert len(pages_data['edges']) == 2
-
-@patch('saleor.graphql.core.mutations.convert_form_fields')
-@patch('saleor.graphql.core.mutations.convert_form_field')
-def test_model_form_mutation(
- mocked_convert_form_field, mocked_convert_form_fields,
- model_form_class):
-
- mocked_convert_form_fields.return_value = {
- model_form_class._meta.fields: mocked_convert_form_field.return_value}
-
- class TestMutation(ModelFormMutation):
- test_field = graphene.String()
-
- class Arguments:
- test_input = graphene.String()
-
- class Meta:
- form_class = model_form_class
- return_field_name = 'test_return_field'
-
- meta = TestMutation._meta
- assert meta.form_class == model_form_class
- assert meta.model == 'test_model'
- assert meta.return_field_name == 'test_return_field'
- arguments = meta.arguments
- # check if declarative arguments are present
- assert 'test_input' in arguments
- # check if model form field is present
- mocked_convert_form_fields.assert_called_with(model_form_class, None)
- assert 'test_field' in arguments
-
- output_fields = meta.fields
- assert 'test_return_field' in output_fields
- assert 'errors' in output_fields
-
-
-@patch('saleor.graphql.core.mutations')
-def test_model_form_update_mutation(model_form_class):
- class TestUpdateMutation(ModelFormUpdateMutation):
- class Meta:
- form_class = model_form_class
- return_field_name = 'test_return_field'
-
- meta = TestUpdateMutation._meta
- assert 'id' in meta.arguments
-
-
def test_create_product(
admin_client, product_type, default_category, size_attribute):
query = """
@@ -892,209 +573,6 @@ def test_delete_product(admin_client, product):
product.refresh_from_db()
-def test_category_create_mutation(admin_client):
- query = """
- mutation($name: String!, $description: String, $parentId: ID) {
- categoryCreate(
- name: $name
- description: $description
- parentId: $parentId
- ) {
- category {
- id
- name
- slug
- description
- parent {
- name
- id
- }
- }
- errors {
- field
- message
- }
- }
- }
- """
-
- category_name = 'Test category'
- category_description = 'Test description'
-
- # test creating root category
- variables = json.dumps({
- 'name': category_name, 'description': category_description})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['categoryCreate']
- assert data['errors'] == []
- assert data['category']['name'] == category_name
- assert data['category']['description'] == category_description
- assert not data['category']['parent']
-
- # test creating subcategory
- parent_id = data['category']['id']
- variables = json.dumps({
- 'name': category_name, 'description': category_description,
- 'parentId': parent_id})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['categoryCreate']
- assert data['errors'] == []
- assert data['category']['parent']['id'] == parent_id
-
-
-def test_category_update_mutation(admin_client, default_category):
- query = """
- mutation($id: ID, $name: String!, $description: String) {
- categoryUpdate(
- id: $id
- name: $name
- description: $description
- ) {
- category {
- id
- name
- description
- parent {
- id
- }
- }
- errors {
- field
- message
- }
- }
- }
- """
- # create child category and test that the update mutation won't change
- # it's parent
- child_category = default_category.children.create(name='child')
-
- category_name = 'Updated name'
- category_description = 'Updated description'
-
- category_id = graphene.Node.to_global_id('Category', child_category.pk)
- variables = json.dumps({
- 'name': category_name, 'description': category_description,
- 'id': category_id})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['categoryUpdate']
- assert data['errors'] == []
- assert data['category']['id'] == category_id
- assert data['category']['name'] == category_name
- assert data['category']['description'] == category_description
-
- parent_id = graphene.Node.to_global_id('Category', default_category.pk)
- assert data['category']['parent']['id'] == parent_id
-
-
-def test_category_delete_mutation(admin_client, default_category):
- query = """
- mutation($id: ID!) {
- categoryDelete(id: $id) {
- category {
- name
- }
- errors {
- field
- message
- }
- }
- }
- """
- variables = json.dumps({
- 'id': graphene.Node.to_global_id('Category', default_category.id)})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['categoryDelete']
- assert data['category']['name'] == default_category.name
- with pytest.raises(default_category._meta.model.DoesNotExist):
- default_category.refresh_from_db()
-
-
-def test_page_create_mutation(admin_client):
- query = """
- mutation CreatePage(
- $slug: String!,
- $title: String!,
- $content: String!,
- $isVisible: Boolean!) {
- pageCreate(slug: $slug,
- title: $title,
- content: $content,
- isVisible: $isVisible) {
- page {
- id
- title
- content
- slug
- isVisible
- }
- errors {
- message
- field
- }
- }
- }
- """
- page_slug = 'test-slug'
- page_content = 'test content'
- page_title = 'test title'
- page_isVisible = True
-
- # test creating root page
- variables = json.dumps({
- 'title': page_title, 'content': page_content,
- 'isVisible': page_isVisible, 'slug': page_slug})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['pageCreate']
- assert data['errors'] == []
- assert data['page']['title'] == page_title
- assert data['page']['content'] == page_content
- assert data['page']['slug'] == page_slug
- assert data['page']['isVisible'] == page_isVisible
-
-
-def test_page_delete_mutation(admin_client, page):
- query = """
- mutation DeletePage($id: ID!) {
- pageDelete(id: $id) {
- page {
- title
- id
- }
- errors {
- field
- message
- }
- }
- }
- """
- variables = json.dumps({
- 'id': graphene.Node.to_global_id('Page', page.id)})
- response = admin_client.post(
- reverse('api'), {'query': query, 'variables': variables})
- content = get_graphql_content(response)
- assert 'errors' not in content
- data = content['data']['pageDelete']
- assert data['page']['title'] == page.title
- with pytest.raises(page._meta.model.DoesNotExist):
- page.refresh_from_db()
-
-
def test_product_type(client, product_type):
query = """
query {