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
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
def test_category_update_mutation_invalid_background_image( staff_api_client, category, permission_manage_products): image_file, image_name = create_pdf_file_with_image_ext() image_alt = "Alt text for an image." variables = { "name": "new-name", "slug": "new-slug", "id": to_global_id("Category", category.id), "backgroundImage": image_name, "backgroundImageAlt": image_alt, "isPublished": True, } 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"][0]["field"] == "backgroundImage" assert data["errors"][0]["message"] == "Invalid file type"
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
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
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
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