Exemplo n.º 1
0
def test_delete_controller_wrong_user(flask_app, auth_user, es_create, willorders_ws_db_session):
    prod_list = es_create("products", 3)
    user_slug = uuid_to_slug(uuid4())
    obj = OrderFactory.create(user_slug=user_slug)
    willorders_ws_db_session.commit()

    slug = obj.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]

    amount = 1
    for es_id in prod_id_list:
        product = ProductFactory.create(es_id=es_id)
        OrderProductFactory.create(order=obj, product=product, amount=amount)
        amount += 1

    willorders_ws_db_session.commit()

    with flask_app.test_client(user=auth_user) as client:
        response = client.delete(
            "api/order/delete/%s" % slug
        )

    data = json.loads(response.data)
    assert data["error"] == {}
    assert response.status_code == 404
Exemplo n.º 2
0
def test_delete_controller(flask_app, auth_user, es_create, willorders_ws_db_session):
    prod_list = es_create("products", 5)
    user_slug = auth_user.uuid_slug
    obj = OrderFactory.create(user_slug=user_slug)
    willorders_ws_db_session.commit()

    slug = obj.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]

    for es_id in prod_id_list:
        product = ProductFactory.create(es_id=es_id)
        OrderProductFactory.create(order=obj, product=product, amount=2)

    willorders_ws_db_session.commit()

    assert len(willorders_ws_db_session.query(Order).all()) == 1
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 5

    with flask_app.test_client(user=auth_user) as client:
        response = client.delete(
            "api/order/delete/%s" % slug
        )

    data = json.loads(response.data)
    assert data == {}
    assert response.status_code == 200
    assert len(willorders_ws_db_session.query(Order).all()) == 0
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 0

    with flask_app.test_client(user=auth_user) as client:
        response = client.delete(
            "api/order/delete/%s" % slug
        )

    data = json.loads(response.data)
    assert data["error"] == {}
    assert response.status_code == 404
    assert len(willorders_ws_db_session.query(Order).all()) == 0
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 0
Exemplo n.º 3
0
def test_select_by_slug_controller(flask_app, auth_user, es_create,
                                   willorders_ws_db_session):
    prod_list = es_create("products", 5)
    user_slug = auth_user.uuid_slug
    obj = OrderFactory.create(user_slug=user_slug)
    willorders_ws_db_session.commit()

    slug = obj.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]

    amount = 1
    for es_id in prod_id_list:
        product = ProductFactory.create(es_id=es_id)
        OrderProductFactory.create(order=obj, product=product, amount=amount)
        amount += 1

    willorders_ws_db_session.commit()

    with flask_app.test_client(user=auth_user) as client:
        response = client.get("api/order/%s" % slug)

    data = json.loads(response.data)
    OrderSchema().load(data)
    assert response.status_code == 200
    assert data["slug"] == slug
    assert data["product_types"] == len(prod_list)
    assert data["items_amount"] == ((1 + len(prod_list)) * len(prod_list)) / 2
    assert len(data["products"]) == len(prod_list)

    for item in [item.to_dict() for item in obj.items]:
        product = next(p for p in data["products"]
                       if p["id"] == item["item_id"])
        assert product["amount"] == item["amount"]

    with flask_app.test_client(user=auth_user) as client:
        response = client.get("api/order/WILLrogerPEREIRAslugBR")

    data = json.loads(response.data)
    assert data["error"] == {}
    assert response.status_code == 404
Exemplo n.º 4
0
def test_delete(domain_url, auth_user, auth_session, es_create, willorders_ws_db_session):
    prod_list = es_create("products", 5)
    user_slug = auth_user.uuid_slug
    obj = OrderFactory.create(user_slug=user_slug)
    willorders_ws_db_session.commit()

    slug = obj.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]

    for es_id in prod_id_list:
        product = ProductFactory.create(es_id=es_id)
        OrderProductFactory.create(order=obj, product=product, amount=2)

    willorders_ws_db_session.commit()

    assert len(willorders_ws_db_session.query(Order).all()) == 1
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 5

    response = auth_session.delete(
        domain_url + "/api/order/delete/%s" % slug
    )

    data = response.json()
    assert data == {}
    assert response.status_code == 200
    assert len(willorders_ws_db_session.query(Order).all()) == 0
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 0

    response = auth_session.delete(
        domain_url + "/api/order/delete/%s" % slug
    )

    data = response.json()
    assert data["error"] == {}
    assert response.status_code == 404
    assert len(willorders_ws_db_session.query(Order).all()) == 0
    assert len(willorders_ws_db_session.query(Product).all()) == 5
    assert len(willorders_ws_db_session.query(OrderProduct).all()) == 0
Exemplo n.º 5
0
def test_select_by_slug_wrong_user(domain_url, auth_session, es_create,
                                   willorders_ws_db_session):
    prod_list = es_create("products", 3)
    user_slug = uuid_to_slug(uuid4())
    obj = OrderFactory.create(user_slug=user_slug)
    willorders_ws_db_session.commit()

    slug = obj.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]

    amount = 1
    for es_id in prod_id_list:
        product = ProductFactory.create(es_id=es_id)
        OrderProductFactory.create(order=obj, product=product, amount=amount)
        amount += 1

    willorders_ws_db_session.commit()

    response = auth_session.get(domain_url + "/api/order/%s" % slug)

    data = response.json()
    assert data["error"] == {}
    assert response.status_code == 404
Exemplo n.º 6
0
def test_select_from_user_controller(flask_app, auth_user, es_create,
                                     willorders_ws_db_session):
    prod_list = es_create("products", 5)
    user_slug = auth_user.uuid_slug
    prod_id_list = [p.meta["id"] for p in prod_list]
    product_list = [
        ProductFactory.create(es_id=es_id) for es_id in prod_id_list
    ]
    willorders_ws_db_session.commit()

    obj_list = OrderFactory.create_batch(2, user_slug=user_slug)

    for product in product_list:
        OrderProductFactory.create(order=obj_list[0],
                                   product=product,
                                   amount=2)

    for product in product_list[0:3]:
        OrderProductFactory.create(order=obj_list[1],
                                   product=product,
                                   amount=5)

    willorders_ws_db_session.commit()

    with flask_app.test_client(user=auth_user) as client:
        response = client.post("api/order/user")

    data = json.loads(response.data)
    UserOrdersSchema().load(data)
    assert response.status_code == 200
    assert len(data["orders"]) == 2
    assert data["total"] == 2
    assert data["pages"] == 1

    order_slug_list = [order["slug"] for order in data["orders"]]
    for slug in order_slug_list:
        assert slug in [obj.uuid_slug for obj in obj_list]

    for order in data["orders"]:
        if order["slug"] == obj_list[0].uuid_slug:
            assert order["product_types"] == 5
            assert order["items_amount"] == 10
        else:
            assert order["product_types"] == 3
            assert order["items_amount"] == 15

    with flask_app.test_client(user=auth_user) as client:
        response = client.post("api/order/user",
                               json={
                                   "page": "1",
                                   "page_size": "1"
                               })

    data = json.loads(response.data)
    UserOrdersSchema().load(data)
    assert response.status_code == 200
    assert len(data["orders"]) == 1
    assert data["total"] == 2
    assert data["pages"] == 2

    with flask_app.test_client(user=auth_user) as client:
        response = client.post("api/order/user",
                               json={
                                   "datespan": {
                                       "start":
                                       str(date.today() - timedelta(days=1)),
                                       "end":
                                       str(date.today() + timedelta(days=1))
                                   }
                               })

    data = json.loads(response.data)
    UserOrdersSchema().load(data)
    assert response.status_code == 200
    assert len(data["orders"]) == 2
    assert data["total"] == 2
    assert data["pages"] == 1