コード例 #1
0
ファイル: test_digital_content.py プロジェクト: zdlm/saleor
def test_digital_content_create_mutation_default_settings(
    monkeypatch, staff_api_client, variant, permission_manage_products, media_root
):
    query = """
    mutation digitalCreate($variant: ID!,
        $input: DigitalContentUploadInput!) {
        digitalContentCreate(variantId: $variant, input: $input) {
            variant {
                id
            }
        }
    }
    """

    image_file, image_name = create_image()

    variables = {
        "variant": graphene.Node.to_global_id("ProductVariant", variant.id),
        "input": {"useDefaultSettings": True, "contentFile": image_name},
    }

    body = get_multipart_request_body(query, variables, image_file, image_name)
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products]
    )
    get_graphql_content(response)
    variant.refresh_from_db()
    assert variant.digital_content.content_file
    assert variant.digital_content.use_default_settings
コード例 #2
0
def test_collection_image_query(user_api_client, collection, media_root):
    alt_text = "Alt text for an image."
    image_file, image_name = create_image()
    collection.background_image = image_file
    collection.background_image_alt = alt_text
    collection.save()
    collection_id = graphene.Node.to_global_id("Collection", collection.pk)
    variables = {"id": collection_id}
    response = user_api_client.post_graphql(FETCH_COLLECTION_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["collection"]
    thumbnail_url = collection.background_image.thumbnail["120x120"].url
    assert thumbnail_url in data["backgroundImage"]["url"]
    assert data["backgroundImage"]["alt"] == alt_text
コード例 #3
0
def test_category_image_query(user_api_client, non_default_category,
                              media_root):
    alt_text = "Alt text for an image."
    category = non_default_category
    image_file, image_name = create_image()
    category.background_image = image_file
    category.background_image_alt = alt_text
    category.save()
    category_id = graphene.Node.to_global_id("Category", category.pk)
    variables = {"id": category_id}
    response = user_api_client.post_graphql(FETCH_CATEGORY_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["category"]
    thumbnail_url = category.background_image.thumbnail["120x120"].url
    assert thumbnail_url in data["backgroundImage"]["url"]
    assert data["backgroundImage"]["alt"] == alt_text
コード例 #4
0
def test_category_update_mutation(monkeypatch, staff_api_client, category,
                                  permission_manage_products, media_root):
    mock_create_thumbnails = Mock(return_value=None)
    monkeypatch.setattr(
        ("saleor.product.thumbnails."
         "create_category_background_image_thumbnails.delay"),
        mock_create_thumbnails,
    )

    # create child category and test that the update mutation won't change
    # it's parent
    child_category = category.children.create(name="child")

    category_name = "Updated name"
    category_slug = slugify(category_name)
    category_description = "Updated description"
    image_file, image_name = create_image()
    image_alt = "Alt text for an image."

    category_id = graphene.Node.to_global_id("Category", child_category.pk)
    variables = {
        "name": category_name,
        "description": category_description,
        "backgroundImage": image_name,
        "backgroundImageAlt": image_alt,
        "id": category_id,
        "slug": category_slug,
    }
    body = get_multipart_request_body(MUTATION_CATEGORY_UPDATE_MUTATION,
                                      variables, image_file, image_name)
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products])
    content = get_graphql_content(response)
    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", category.pk)
    assert data["category"]["parent"]["id"] == parent_id
    category = Category.objects.get(name=category_name)
    assert category.background_image.file
    mock_create_thumbnails.assert_called_once_with(category.pk)
    assert data["category"]["backgroundImage"]["alt"] == image_alt
コード例 #5
0
ファイル: test_digital_content.py プロジェクト: zdlm/saleor
def test_digital_content_create_mutation_custom_settings(
    monkeypatch, staff_api_client, variant, permission_manage_products, media_root
):
    query = """
    mutation createDigitalContent($variant: ID!,
        $input: DigitalContentUploadInput!) {
        digitalContentCreate(variantId: $variant, input: $input) {
            variant {
                id
            }
        }
    }
    """

    image_file, image_name = create_image()
    url_valid_days = 3
    max_downloads = 5

    variables = {
        "variant": graphene.Node.to_global_id("ProductVariant", variant.id),
        "input": {
            "useDefaultSettings": False,
            "maxDownloads": max_downloads,
            "urlValidDays": url_valid_days,
            "automaticFulfillment": True,
            "contentFile": image_name,
        },
    }

    body = get_multipart_request_body(query, variables, image_file, image_name)
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products]
    )
    get_graphql_content(response)
    variant.refresh_from_db()
    assert variant.digital_content.content_file
    assert variant.digital_content.max_downloads == max_downloads
    assert variant.digital_content.url_valid_days == url_valid_days
    assert variant.digital_content.automatic_fulfillment
    assert not variant.digital_content.use_default_settings
コード例 #6
0
def test_update_collection_with_background_image(monkeypatch, staff_api_client,
                                                 collection,
                                                 permission_manage_products,
                                                 media_root):
    mock_create_thumbnails = Mock(return_value=None)
    monkeypatch.setattr(
        ("saleor.product.thumbnails."
         "create_collection_background_image_thumbnails.delay"),
        mock_create_thumbnails,
    )

    image_file, image_name = create_image()
    image_alt = "Alt text for an image."
    variables = {
        "name": "new-name",
        "slug": "new-slug",
        "id": to_global_id("Collection", collection.id),
        "backgroundImage": image_name,
        "backgroundImageAlt": image_alt,
        "isPublished": True,
    }
    body = get_multipart_request_body(
        MUTATION_UPDATE_COLLECTION_WITH_BACKGROUND_IMAGE,
        variables,
        image_file,
        image_name,
    )
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products])
    content = get_graphql_content(response)
    data = content["data"]["collectionUpdate"]
    assert not data["errors"]
    slug = data["collection"]["slug"]
    collection = Collection.objects.get(slug=slug)
    assert collection.background_image
    mock_create_thumbnails.assert_called_once_with(collection.pk)
    assert data["collection"]["backgroundImage"]["alt"] == image_alt
コード例 #7
0
ファイル: test_order_actions.py プロジェクト: zdlm/saleor
def test_fulfill_digital_lines(mock_digital_settings, mock_email_fulfillment,
                               order_with_lines, media_root):
    mock_digital_settings.return_value = {"automatic_fulfillment": True}
    line = order_with_lines.lines.all()[0]

    image_file, image_name = create_image()
    variant = line.variant
    digital_content = DigitalContent.objects.create(content_file=image_file,
                                                    product_variant=variant,
                                                    use_default_settings=True)

    line.variant.digital_content = digital_content
    line.is_shipping_required = False
    line.save()

    order_with_lines.refresh_from_db()
    automatically_fulfill_digital_lines(order_with_lines)
    line.refresh_from_db()
    fulfillment = Fulfillment.objects.get(order=order_with_lines)
    fulfillment_lines = fulfillment.lines.all()

    assert fulfillment_lines.count() == 1
    assert line.digital_content_url
    assert mock_email_fulfillment.delay.called
コード例 #8
0
def test_create_collection(monkeypatch, staff_api_client, product_list,
                           media_root, permission_manage_products):
    query = """
        mutation createCollection(
                $name: String!, $slug: String!, $description: String,
                $descriptionJson: JSONString, $products: [ID],
                $backgroundImage: Upload!, $backgroundImageAlt: String,
                $isPublished: Boolean!, $publicationDate: Date) {
            collectionCreate(
                input: {
                    name: $name,
                    slug: $slug,
                    description: $description,
                    descriptionJson: $descriptionJson,
                    products: $products,
                    backgroundImage: $backgroundImage,
                    backgroundImageAlt: $backgroundImageAlt,
                    isPublished: $isPublished,
                    publicationDate: $publicationDate}) {
                collection {
                    name
                    slug
                    description
                    descriptionJson
                    products {
                        totalCount
                    }
                    publicationDate
                    backgroundImage{
                        alt
                    }
                }
            }
        }
    """

    mock_create_thumbnails = Mock(return_value=None)
    monkeypatch.setattr(
        ("saleor.product.thumbnails."
         "create_collection_background_image_thumbnails.delay"),
        mock_create_thumbnails,
    )

    product_ids = [
        to_global_id("Product", product.pk) for product in product_list
    ]
    image_file, image_name = create_image()
    image_alt = "Alt text for an image."
    name = "test-name"
    slug = "test-slug"
    description = "test-description"
    description_json = json.dumps({"content": "description"})
    publication_date = date.today()
    variables = {
        "name": name,
        "slug": slug,
        "description": description,
        "descriptionJson": description_json,
        "products": product_ids,
        "backgroundImage": image_name,
        "backgroundImageAlt": image_alt,
        "isPublished": True,
        "publicationDate": publication_date,
    }
    body = get_multipart_request_body(query, variables, image_file, image_name)
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products])
    content = get_graphql_content(response)
    data = content["data"]["collectionCreate"]["collection"]
    assert data["name"] == name
    assert data["slug"] == slug
    assert data["description"] == description
    assert data["descriptionJson"] == description_json
    assert data["publicationDate"] == publication_date.isoformat()
    assert data["products"]["totalCount"] == len(product_ids)
    collection = Collection.objects.get(slug=slug)
    assert collection.background_image.file
    mock_create_thumbnails.assert_called_once_with(collection.pk)
    assert data["backgroundImage"]["alt"] == image_alt
コード例 #9
0
def test_category_create_mutation(monkeypatch, staff_api_client,
                                  permission_manage_products, media_root):
    query = """
        mutation(
                $name: String, $slug: String, $description: String,
                $descriptionJson: JSONString, $backgroundImage: Upload,
                $backgroundImageAlt: String, $parentId: ID) {
            categoryCreate(
                input: {
                    name: $name
                    slug: $slug
                    description: $description
                    descriptionJson: $descriptionJson
                    backgroundImage: $backgroundImage
                    backgroundImageAlt: $backgroundImageAlt
                },
                parent: $parentId
            ) {
                category {
                    id
                    name
                    slug
                    description
                    descriptionJson
                    parent {
                        name
                        id
                    }
                    backgroundImage{
                        alt
                    }
                }
                errors {
                    field
                    message
                }
            }
        }
    """

    mock_create_thumbnails = Mock(return_value=None)
    monkeypatch.setattr(
        ("saleor.product.thumbnails."
         "create_category_background_image_thumbnails.delay"),
        mock_create_thumbnails,
    )

    category_name = "Test category"
    category_slug = slugify(category_name)
    category_description = "Test description"
    category_description_json = json.dumps({"content": "description"})
    image_file, image_name = create_image()
    image_alt = "Alt text for an image."

    # test creating root category
    variables = {
        "name": category_name,
        "description": category_description,
        "descriptionJson": category_description_json,
        "backgroundImage": image_name,
        "backgroundImageAlt": image_alt,
        "slug": category_slug,
    }
    body = get_multipart_request_body(query, variables, image_file, image_name)
    response = staff_api_client.post_multipart(
        body, permissions=[permission_manage_products])
    content = get_graphql_content(response)
    data = content["data"]["categoryCreate"]
    assert data["errors"] == []
    assert data["category"]["name"] == category_name
    assert data["category"]["description"] == category_description
    assert data["category"]["descriptionJson"] == category_description_json
    assert not data["category"]["parent"]
    category = Category.objects.get(name=category_name)
    assert category.background_image.file
    mock_create_thumbnails.assert_called_once_with(category.pk)
    assert data["category"]["backgroundImage"]["alt"] == image_alt

    # test creating subcategory
    parent_id = data["category"]["id"]
    variables = {
        "name": category_name,
        "description": category_description,
        "parentId": parent_id,
        "slug": f"{category_slug}-2",
    }
    response = staff_api_client.post_graphql(query, variables)
    content = get_graphql_content(response)
    data = content["data"]["categoryCreate"]
    assert data["errors"] == []
    assert data["category"]["parent"]["id"] == parent_id