def test_url_uniqueness(rf): page = create_page(url='bacon', shop=get_default_shop()) with pytest.raises(ValidationError): page = create_page(url='bacon', shop=get_default_shop()) with transaction.atomic(): mpage = create_multilanguage_page(url="cheese", shop=get_default_shop()) with pytest.raises(ValidationError): mpage = create_multilanguage_page(url="cheese", shop=get_default_shop())
def test_superuser_can_see_invisible_page(rf, admin_user): page = create_page(shop=get_default_shop()) view_func = PageView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view_func(request, url=page.url) response.render() assert "<h1>Bacon ipsum" in response.rendered_content
def test_none_dates_page_not_visible(): # create page that is not anymore visible page = create_page(shop=get_default_shop()) assert not Page.objects.visible( get_default_shop()).filter(pk=page.pk).exists() assert not page.is_visible()
def test_anon_cant_see_invisible_page(rf): page = create_page(shop=get_default_shop()) get_default_shop() view_func = PageView.as_view() request = apply_request_middleware(rf.get("/")) assert request.user.is_anonymous() with pytest.raises(Http404): response = view_func(request, url=page.url)
def test_future_page_not_visible(): today = now() page = create_page(available_from=(today + datetime.timedelta(days=1)), available_to=(today + datetime.timedelta(days=2)), shop=get_default_shop()) assert not Page.objects.visible( get_default_shop()).filter(pk=page.pk).exists() assert not page.is_visible()
def test_visible_page_has_right_content(rf): page = create_page(available_from=datetime.date(1988, 1, 1), shop=get_default_shop()) view_func = PageView.as_view() request = apply_request_middleware(rf.get("/")) assert request.user.is_anonymous() response = view_func(request, url=page.url) response.render() assert "<h1>Bacon ipsum" in response.rendered_content
def test_page_without_visibility_end_is_visible(): today = now() page = create_page(available_from=today, available_to=None, shop=get_default_shop()) assert Page.objects.visible(get_default_shop(), today).filter(pk=page.pk).exists() assert page.is_visible(today)
def test_current_page_is_visible(): today = now() page = create_page(available_from=today, available_to=today, shop=get_default_shop()) assert Page.objects.visible(get_default_shop(), today).filter(pk=page.pk).exists() assert page.is_visible(today)
def test_invisible_children(rf): shop = get_default_shop() request = apply_request_middleware(rf.get("/")) parent_content = "Parent content" page = create_page(available_from=datetime.date(1988, 1, 1), content=parent_content, shop=shop) children_content = "Children content" create_page(content=children_content, parent=page, shop=shop) # Create invisible children assert page.list_children_on_page == False check_children_content(request, page, children_content, False) page.list_children_on_page = True page.save() check_children_content(request, page, children_content, False)
def test_visible_children(rf): shop = get_default_shop() request = apply_request_middleware(rf.get("/")) assert request.user.is_anonymous() parent_content = "Parent content" page = create_page(available_from=datetime.date(1988, 1, 1), content=parent_content, shop=shop) children_content = "Children content" # Visible child create_page(available_from=datetime.date(2000, 1, 1), content=children_content, parent=page, shop=shop) assert page.list_children_on_page == False check_children_content(request, page, children_content, False) page.list_children_on_page = True page.save() check_children_content(request, page, children_content, True)
def test_slide_links(): test_carousel = Carousel.objects.create(name="test") test_image_1 = Image.objects.create(original_filename="slide1.jpg") with translation.override("en"): test_slide = Slide.objects.create(carousel=test_carousel, name="test", image=test_image_1) # Test external link assert len(test_carousel.slides.all()) == 1 test_link = "http://example.com" test_slide.external_link = test_link test_slide.save() assert test_slide.get_translated_field("external_link") == test_link assert test_slide.get_link_url() == test_link # Test Product url and link priorities test_product = get_default_product() test_slide.product_link = test_product test_slide.save() assert test_slide.get_link_url() == test_link test_slide.external_link = None test_slide.save() assert test_slide.get_link_url().startswith("/p/") # Close enough... # Test Category url and link priorities test_category = get_default_category() test_slide.category_link = test_category test_slide.save() assert test_slide.get_link_url().startswith("/p/") # Close enough... test_slide.product_link = None test_slide.save() assert test_slide.get_link_url().startswith("/c/") # Close enough... # Test CMS page url and link priorities attrs = {"url": "test", "shop": get_default_shop()} test_page = create_page(**attrs) test_slide.cms_page_link = test_page test_slide.save() assert test_slide.get_link_url().startswith("/c/") # Close enough... test_slide.category_link = None test_slide.save() assert test_slide.get_link_url().startswith("/test/") # Check that external link overrides everything test_slide.external_link = test_link test_slide.save() assert test_slide.get_link_url() == test_link
def test_page_different_shops(rf): shop1 = get_shop(status=ShopStatus.ENABLED, identifier="shop-1", name="Shop 1", domain="shop1") shop2 = get_shop(status=ShopStatus.ENABLED, identifier="shop-2", name="Shop 2", domain="shop2") # dreate page only for shop2 page = create_page(available_from=datetime.date(1988, 1, 1), shop=shop2) view_func = PageView.as_view() request = apply_request_middleware(rf.get("/", HTTP_HOST=shop1.domain)) with pytest.raises(Http404): response = view_func(request, url=page.url) request = apply_request_middleware(rf.get("/", HTTP_HOST=shop2.domain)) response = view_func(request, url=page.url) assert response.status_code == 200 response.render() assert "<h1>Bacon ipsum" in response.rendered_content
def test_page_form(rf, admin_user): shop = get_default_shop() request = rf.get("/") request.user = admin_user request.shop = shop view = PageEditView(request=request) form_class = view.get_form_class() form = form_class(**view.get_form_kwargs()) assert not form.is_bound data = get_form_data(form) data.update({ "available_from": "", "available_to": "", "content__en": "", "content__fi": "suomi", "content__ja": "", "identifier": "", "title__en": "", "title__fi": "", "title__ja": "", "url__en": "", "url__fi": "suomi", "url__ja": "", }) form = form_class(**dict(view.get_form_kwargs(), data=data)) form.full_clean() assert "title__fi" in form.errors # We get an error because all of a given language's fields must be filled if any are data["title__fi"] = "suomi" form = form_class(**dict(view.get_form_kwargs(), data=data)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == { "fi" } # The page should be only in Finnish # Let's edit that page original_url = "errrnglish" data.update({ "title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(view.get_form_kwargs(), data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() assert set(page.get_available_languages()) == {"fi", "en"} # English GET # Try to make page a child of itself data.update({"parent": page.pk}) form = form_class(**dict(view.get_form_kwargs(), data=data, instance=page)) form.full_clean() assert form.errors del data["parent"] # add dummy page with simple url, page is in english dummy = create_page(url="test", shop=get_default_shop()) # edit page again and try to set duplicate url data.update({ "title__en": "englaish", "url__en": "test", "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(view.get_form_kwargs(), data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__en" in form.errors assert form.errors["url__en"].as_data()[0].code == "invalid_url" # it should be possible to change back to the original url data.update({ "title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n" }) form = form_class(**dict(view.get_form_kwargs(), data=data, instance=page)) form.full_clean() assert not form.errors page = form.save() # add finnish urls, it should not be possible to enter original url data.update({ "title__fi": "englaish", "url__fi": original_url, "content__fi": "ennnn ennnn ennnnnnn-nn-n-n" }) assert data["url__fi"] == data[ "url__en"] # both urls are same, should raise two errors form = form_class(**dict(view.get_form_kwargs(), data=data, instance=page)) form.full_clean() assert len(form.errors) == 1 assert "url__fi" in form.errors error_data = form.errors["url__fi"].as_data() assert error_data[0].code == "invalid_url" assert error_data[1].code == "invalid_unique_url"