def test_forbidden_update_milestone_documents(app, centralized_milestone):
    """
    Plan owner or just broker can't manage milestone documents
    """
    milestone_data, plan_data = centralized_milestone[
        "milestone"], centralized_milestone["plan"]
    milestone, milestone_token = milestone_data["data"], milestone_data[
        "token"]
    plan, plan_token = plan_data["data"], plan_data["token"]
    app.authorization = ("Basic", ("broker", "broker"))

    response = app.post_json(
        "/plans/{}/milestones/{}/documents?acc_token={}".format(
            plan["id"], milestone["id"], plan_token), {"data": {}},
        status=403)
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description": u"Forbidden",
            u"location": u"url",
            u"name": u"permission"
        }]
    }

    # put
    document = milestone["documents"][0]
    response = app.put_json(
        "/plans/{}/milestones/{}/documents/{}?acc_token={}".format(
            plan["id"], milestone["id"], document["id"],
            plan_token), {"data": {}},
        status=403)
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description": u"Forbidden",
            u"location": u"url",
            u"name": u"permission"
        }]
    }

    # patch
    response = app.patch_json(
        "/plans/{}/milestones/{}/documents/{}?acc_token={}".format(
            plan["id"], milestone["id"], document["id"],
            plan_token), {"data": {}},
        status=403)
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description": u"Forbidden",
            u"location": u"url",
            u"name": u"permission"
        }]
    }
def test_update_milestone_documents(app, centralized_milestone):
    """
    as a milestone owner I can manage it's documents
    """
    milestone_data, plan_data = centralized_milestone["milestone"], centralized_milestone["plan"]
    milestone, milestone_token = milestone_data["data"], milestone_data["token"]
    plan, plan_token = plan_data["data"], plan_data["token"]
    app.authorization = ("Basic", ("broker", "broker"))
    plan_date_modified = plan["dateModified"]
    milestone_date_modified = milestone["dateModified"]

    request_data = {
        "title": "sign.p7s",
        "url": generate_docservice_url(app),
        "hash": "md5:" + "0" * 32,
        "format": "application/pk7s",
    }
    response = app.post_json(
        "/plans/{}/milestones/{}/documents?acc_token={}".format(plan["id"], milestone["id"], milestone_token),
        {"data": request_data},
    )
    assert response.status_code == 201

    result_plan = app.app.registry.databases.plans.get(plan["id"])
    result_milestone = result_plan.get("milestones")[0]
    assert len(result_milestone["documents"]) == 2
    new_doc = result_milestone["documents"][1]
    assert new_doc["title"] == request_data["title"]
    assert new_doc["hash"] == request_data["hash"]
    assert new_doc["format"] == request_data["format"]
    assert new_doc["url"].split("Signature")[0] == request_data["url"].split("Signature")[0]
    assert result_plan["dateModified"] > plan_date_modified
    plan_date_modified = result_plan["dateModified"]
    assert result_plan["milestones"][0]["dateModified"] > milestone_date_modified
    milestone_date_modified = result_plan["milestones"][0]["dateModified"]

    # put
    request_data = {
        "title": "sign-2.p7s",
        "url": generate_docservice_url(app),
        "hash": "md5:" + "0" * 32,
        "format": "application/signature",
    }
    response = app.put_json(
        "/plans/{}/milestones/{}/documents/{}?acc_token={}".format(
            plan["id"], milestone["id"], new_doc["id"], milestone_token),
        {"data": request_data},
    )
    assert response.status_code == 200

    result_plan = app.app.registry.databases.plans.get(plan["id"])
    result_milestone = result_plan.get("milestones")[0]
    assert len(result_milestone["documents"]) == 3
    old_doc = new_doc
    new_doc = result_milestone["documents"][2]
    assert new_doc["id"] == old_doc["id"]
    assert new_doc["title"] == request_data["title"]
    assert new_doc["hash"] == request_data["hash"]
    assert new_doc["format"] == request_data["format"]
    assert new_doc["url"].split("Signature")[0] == request_data["url"].split("Signature")[0]
    assert result_plan["dateModified"] > plan_date_modified
    plan_date_modified = result_plan["dateModified"]
    assert result_plan["milestones"][0]["dateModified"] > milestone_date_modified
    milestone_date_modified = result_plan["milestones"][0]["dateModified"]

    # patch
    request_data = {
        "title": "sign-3.p7s",
        "url": generate_docservice_url(app),
        "hash": "md5:" + "0" * 32,
        "format": "ms/sms",
        "documentOf": "my ma",
        "documentType": "notice",
        "language": "c++",
    }
    response = app.patch_json(
        "/plans/{}/milestones/{}/documents/{}?acc_token={}".format(
            plan["id"], milestone["id"], new_doc["id"], milestone_token),
        {"data": request_data},
    )
    assert response.status_code == 200

    result_plan = app.app.registry.databases.plans.get(plan["id"])
    result_milestone = result_plan.get("milestones")[0]
    assert len(result_milestone["documents"]) == 3
    patched_doc = result_milestone["documents"][2]
    assert patched_doc["id"] == new_doc["id"]
    assert patched_doc["hash"] == new_doc["hash"]
    assert patched_doc["url"].split("Signature")[0] == new_doc["url"].split("Signature")[0]
    assert patched_doc["format"] == request_data["format"]
    assert patched_doc["title"] == request_data["title"]
    assert patched_doc["documentOf"] == request_data["documentOf"]
    assert patched_doc["documentType"] == request_data["documentType"]
    assert patched_doc["language"] == request_data["language"]
    assert result_plan["dateModified"] > plan_date_modified
    assert result_plan["milestones"][0]["dateModified"] > milestone_date_modified
예제 #3
0
def test_fail_update_complete_or_cancelled_plan(app, status):
    app.authorization = ("Basic", ("broker", "broker"))
    test_data = deepcopy(test_plan_data)
    test_data["documents"] = [{
        "title": u"укр.doc",
        "url": generate_docservice_url(app),
        "hash": "md5:" + "0" * 32,
        "format": "application/msword",
    }]
    test_data["status"] = status
    if status == "cancelled":
        test_data["cancellation"] = dict(reason="Because", status="active")

    response = app.post_json("/plans", {"data": test_data})
    assert response.status == "201 Created"
    assert response.json["data"].get("status") == status
    plan_id = response.json["data"]["id"]
    doc_id = response.json["data"]["documents"][0]["id"]
    acc_token = response.json["access"]["token"]

    # patch
    response = app.patch_json("/plans/{}?acc_token={}".format(
        plan_id, acc_token), {"data": {}},
                              status=422)
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description":
            u"Can't update plan in '{}' status".format(status),
            u"location":
            u"body",
            u"name":
            u"status",
        }],
    }

    #  docs
    response = app.post_json(
        "/plans/{}/documents?acc_token={}".format(plan_id, acc_token),
        {
            "data": {
                "title": u"укр.doc",
                "url": generate_docservice_url(app),
                "hash": "md5:" + "0" * 32,
                "format": "application/msword",
            }
        },
        status=422,
    )
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description":
            u"Can't update plan in '{}' status".format(status),
            u"location":
            u"body",
            u"name":
            u"status",
        }],
    }

    response = app.put_json(
        "/plans/{}/documents/{}?acc_token={}".format(plan_id, doc_id,
                                                     acc_token),
        {
            "data": {
                "title": u"укр_2.doc",
                "url": generate_docservice_url(app),
                "hash": "md5:" + "0" * 32,
                "format": "application/msword",
            }
        },
        status=422,
    )
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description":
            u"Can't update plan in '{}' status".format(status),
            u"location":
            u"body",
            u"name":
            u"status",
        }],
    }

    response = app.patch_json(
        "/plans/{}/documents/{}?acc_token={}".format(plan_id, doc_id,
                                                     acc_token),
        {"data": {
            "title": u"whatever.doc"
        }},
        status=422,
    )
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description":
            u"Can't update plan in '{}' status".format(status),
            u"location":
            u"body",
            u"name":
            u"status",
        }],
    }

    # tender creation
    response = app.post_json("/plans/{}/tenders".format(plan_id), {"data": {}},
                             status=422)
    assert response.json == {
        u"status":
        u"error",
        u"errors": [{
            u"description":
            u"Can't update plan in '{}' status".format(status),
            u"location":
            u"body",
            u"name":
            u"status",
        }],
    }