def test_menu_reset_view(rf):
    with override_settings(
            SHUUP_ADMIN_SHOP_PROVIDER_SPEC=
            "shuup.testing.shop_provider.TestingAdminShopProvider",
            SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC=
            "shuup.testing.supplier_provider.FirstSupplierProvider",
    ):
        supplier_user = get_supplier_user()
        arrange_url = reverse("shuup_admin:menu.arrange_supplier")
        menu_request = apply_request_middleware(rf.get(arrange_url),
                                                user=supplier_user)
        admin_menu_before_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        new_data = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ][::-1]
        new_data[0]["entries"][0]["name"] = "Menu Arrange"
        data = {"menus": json.dumps(new_data)}
        SupplierMenuArrangeView.as_view()(apply_request_middleware(
            rf.post(arrange_url, data=data), user=supplier_user))
        admin_menu_after_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_save == new_data

        reset_url = reverse("shuup_admin:menu.reset_supplier")
        request = apply_request_middleware(rf.get(reset_url),
                                           user=supplier_user)
        response = SupplierMenuResetView.as_view()(request)
        assert response.status_code == 302
        admin_menu_after_reset = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_reset == admin_menu_before_save
def test_menu_arrange_view(rf):
    with override_settings(
            SHUUP_ADMIN_SHOP_PROVIDER_SPEC=
            "shuup.testing.shop_provider.TestingAdminShopProvider",
            SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC=
            "shuup.testing.supplier_provider.FirstSupplierProvider"):
        supplier_user = get_supplier_user()
        url = reverse('shuup_admin:menu.arrange_supplier')
        request = apply_request_middleware(rf.get(url), user=supplier_user)
        response = SupplierMenuArrangeView.as_view()(request)
        assert response.status_code == 200
def test_menu_save_arrange_view(rf):
    with override_settings(
            SHUUP_ADMIN_SHOP_PROVIDER_SPEC=
            "shuup.testing.shop_provider.TestingAdminShopProvider",
            SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC=
            "shuup.testing.supplier_provider.FirstSupplierProvider",
    ):
        supplier_user = get_supplier_user()
        url = reverse("shuup_admin:menu.arrange_supplier")

        menu_request = apply_request_middleware(rf.get(url),
                                                user=supplier_user)
        admin_menu_before_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        new_data = admin_menu_before_save[::-1]
        new_data[0]["entries"][0]["name"] = "Menu Arrange"
        data = {"menus": json.dumps(new_data)}

        request = apply_request_middleware(rf.post(url, data=data),
                                           user=supplier_user)
        response = SupplierMenuArrangeView.as_view()(request)
        assert response.status_code == 302

        menu_request = apply_request_middleware(rf.get(url),
                                                user=supplier_user)
        admin_menu_after_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_save == new_data

        # Make sure other staff has same menu after save
        another_supplier_user = get_supplier_user()
        menu_request = apply_request_middleware(rf.get(url),
                                                user=supplier_user)
        admin_menu_after_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_save == new_data

        # Test that different languages are also customizable
        activate("fi")
        new_data[0]["entries"][0]["name"] = "Listan jarjestaminen"
        data = {"menus": json.dumps(new_data)}
        request = apply_request_middleware(rf.post(url, data=data),
                                           user=supplier_user)
        response = SupplierMenuArrangeView.as_view()(request)
        assert response.status_code == 302

        menu_request = apply_request_middleware(rf.get(url),
                                                user=supplier_user)
        admin_menu_after_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_save == new_data

        # Back in english menu title should still be "Menu Arrange"
        activate("en")
        menu_request = apply_request_middleware(rf.get(url),
                                                user=supplier_user)
        admin_menu_after_save = [
            m.to_dict() for m in get_menu_entry_categories(menu_request)
        ]
        assert admin_menu_after_save[0]["entries"][0]["name"] == "Menu Arrange"

    # Make sure staff does not receive this menu as long as the get_supplier provider
    # returns None which is the default
    staff = get_staff_user()
    menu_request = apply_request_middleware(rf.get(url), user=staff)
    staff_menu = [m.to_dict() for m in get_menu_entry_categories(menu_request)]
    assert staff_menu != admin_menu_after_save