예제 #1
0
def test_login_as_staff_member(rf):
    shop = get_default_shop()
    staff_user = UserFactory(is_staff=True)
    permission_group = get_default_permission_group()
    staff_user.groups.add(permission_group)
    shop.staff_members.add(staff_user)

    view_func = LoginAsUserView.as_view()
    request = apply_request_middleware(rf.post("/"),
                                       user=staff_user,
                                       skip_session=True)

    # log in as self
    with pytest.raises(Problem):
        view_func(request, pk=staff_user.pk)

    user = UserFactory()
    get_person_contact(user)

    request = apply_request_middleware(rf.post("/"), user=staff_user)
    user.is_superuser = True
    user.save()
    # user is trying to login as another superuser
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_superuser = False
    user.is_staff = True
    user.save()
    # user is trying to login as a staff user
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_staff = False
    user.is_active = False
    user.save()
    # user is trying to login as an inactive user
    with pytest.raises(Problem):
        view_func(request, pk=user.pk)

    user.is_active = True
    user.save()

    # staff user without "user.login-as" permission trying to login as valid user
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    permission_group = staff_user.groups.first()
    set_permissions_for_group(permission_group, ["user.login-as"])
    response = view_func(request, pk=user.pk)
    assert response["location"] == reverse("shuup:index")
    assert get_user(request) == user
예제 #2
0
def test_login_as_user_errors(rf, admin_user, regular_user):
    get_default_shop()
    view_func = LoginAsUserView.as_view()
    request = apply_request_middleware(rf.post("/"), user=regular_user)

    # log in as self
    with pytest.raises(Problem):
        view_func(request, pk=regular_user.pk)

    user = UserFactory()
    get_person_contact(user)
    # non superuser trying to login as someone else
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    request = apply_request_middleware(rf.post("/"), user=admin_user)
    user.is_superuser = True
    user.save()
    # user is trying to login as another superuser
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_superuser = False
    user.is_staff = True
    user.save()
    # user is trying to login as a staff user
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_staff = False
    user.is_active = False
    user.save()
    # user is trying to login as an inactive user
    with pytest.raises(Problem):
        view_func(request, pk=user.pk)
예제 #3
0
def test_login_as_user_errors(rf, admin_user, regular_user):
    get_default_shop()
    view_func = LoginAsUserView.as_view()
    request = apply_request_middleware(rf.post("/"), user=regular_user, skip_session=True)

    # log in as self
    with pytest.raises(Problem):
        view_func(request, pk=regular_user.pk)

    user = UserFactory()
    get_person_contact(user)
    # non superuser trying to login as someone else
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    request = apply_request_middleware(rf.post("/"), user=admin_user)
    user.is_superuser = True
    user.save()
    # user is trying to login as another superuser
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_superuser = False
    user.is_staff = True
    user.save()
    # user is trying to login as a staff user
    with pytest.raises(PermissionDenied):
        view_func(request, pk=user.pk)

    user.is_staff = False
    user.is_active = False
    user.save()
    # user is trying to login as an inactive user
    with pytest.raises(Problem):
        view_func(request, pk=user.pk)
예제 #4
0
def test_contact_filters(rf, admin_user):
    shop = get_default_shop()
    products_per_order = 5

    request = rf.get('/')
    request.shop = shop
    apply_request_middleware(request)
    product = get_default_product()

    customer = get_person_contact(admin_user)
    create_order(request,
                 creator=admin_user,
                 customer=customer,
                 product=product)
    order_one = Order.objects.first()

    user = UserFactory()
    second_customer = get_person_contact(user)
    create_order(request,
                 creator=admin_user,
                 customer=second_customer,
                 product=product)
    order_two = Order.objects.first()

    user = UserFactory()
    user.is_staff = True
    user.save()

    create_order(request,
                 creator=user,
                 customer=second_customer,
                 product=product)
    order_three = Order.objects.first()
    order_three.orderer = customer
    order_three.save()

    # test that admin user gets two orders as he created two
    expected_taxful_total_price = order_one.taxful_total_price + order_two.taxful_total_price
    expected_taxless_total_price = order_one.taxless_total_price + order_two.taxless_total_price
    expected_order_count = 2
    test_info = initialize_simple_report(
        SalesReport, data_overrides={"creator": [admin_user.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count, expected_taxful_total_price,
                            expected_taxless_total_price, products_per_order,
                            return_data)

    # test that new admin user gets one order
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport,
                                         data_overrides={"creator": [user.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count, expected_taxful_total_price,
                            expected_taxless_total_price, products_per_order,
                            return_data)

    # test that new admin user and second_customer gets one order
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport,
                                         data_overrides={
                                             "creator": [user.pk],
                                             "customer": [second_customer.pk]
                                         })
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count, expected_taxful_total_price,
                            expected_taxless_total_price, products_per_order,
                            return_data)

    # test that second_customer gets two orders
    expected_taxful_total_price = order_three.taxful_total_price + order_two.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price + order_two.taxless_total_price
    expected_order_count = 2
    test_info = initialize_simple_report(
        SalesReport, data_overrides={"customer": [second_customer.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count, expected_taxful_total_price,
                            expected_taxless_total_price, products_per_order,
                            return_data)

    # test that second_customer gets two orders
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport,
                                         data_overrides={
                                             "customer": [second_customer.pk],
                                             "orderer": [customer.pk]
                                         })
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count, expected_taxful_total_price,
                            expected_taxless_total_price, products_per_order,
                            return_data)
예제 #5
0
def test_contact_filters(rf, admin_user):
    shop = get_default_shop()
    products_per_order = 5

    request = rf.get('/')
    request.shop = shop
    apply_request_middleware(request)
    product = get_default_product()

    customer = get_person_contact(admin_user)
    create_order(request, creator=admin_user, customer=customer, product=product)
    order_one = Order.objects.first()

    user = UserFactory()
    second_customer = get_person_contact(user)
    create_order(request, creator=admin_user, customer=second_customer, product=product)
    order_two = Order.objects.first()

    user = UserFactory()
    user.is_staff = True
    user.save()

    create_order(request, creator=user, customer=second_customer, product=product)
    order_three = Order.objects.first()
    order_three.orderer = customer
    order_three.save()

    # test that admin user gets two orders as he created two
    expected_taxful_total_price = order_one.taxful_total_price + order_two.taxful_total_price
    expected_taxless_total_price = order_one.taxless_total_price + order_two.taxless_total_price
    expected_order_count = 2
    test_info = initialize_simple_report(SalesReport, data_overrides={"creator": [admin_user.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count,
                            expected_taxful_total_price,
                            expected_taxless_total_price,
                            products_per_order,
                            return_data)

    # test that new admin user gets one order
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport, data_overrides={"creator": [user.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count,
                            expected_taxful_total_price,
                            expected_taxless_total_price,
                            products_per_order,
                            return_data)

    # test that new admin user and second_customer gets one order
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport,
                                         data_overrides={"creator": [user.pk], "customer": [second_customer.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count,
                            expected_taxful_total_price,
                            expected_taxless_total_price,
                            products_per_order,
                            return_data)

    # test that second_customer gets two orders
    expected_taxful_total_price = order_three.taxful_total_price + order_two.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price + order_two.taxless_total_price
    expected_order_count = 2
    test_info = initialize_simple_report(SalesReport, data_overrides={"customer": [second_customer.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count,
                            expected_taxful_total_price,
                            expected_taxless_total_price,
                            products_per_order,
                            return_data)

    # test that second_customer gets two orders
    expected_taxful_total_price = order_three.taxful_total_price
    expected_taxless_total_price = order_three.taxless_total_price
    expected_order_count = 1
    test_info = initialize_simple_report(SalesReport,
                                         data_overrides={"customer": [second_customer.pk], "orderer": [customer.pk]})
    return_data = test_info.json_data.get("tables")[0].get("data")
    _assert_expected_values(expected_order_count,
                            expected_taxful_total_price,
                            expected_taxless_total_price,
                            products_per_order,
                            return_data)