Example #1
0
def test_deserialization_datetime():
    json_str = '{"end_date": "2019-12-06T19:25:22+00:00"}'
    dct = json_loads(json_str)
    assert "end_date" in dct
    assert dct["end_date"] == datetime(2019, 12, 6, 19, 25, 22, 0,
                                       timezone.utc)

    dct = {"end_date": datetime(2019, 12, 6, 19, 25, 22, 0, timezone.utc)}
    assert json_loads(json_dumps(dct)) == dct
Example #2
0
def test_map_create_not_authenticated(test_client):
    body = {
        "name": "CreatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new"
    }
    response = test_client.post(
        "/api/maps/",
        data=json_dumps(body),
    )
    assert HTTPStatus.UNAUTHORIZED == response.status_code
Example #3
0
def test_map_create_with_owner(test_client, user_token_headers):
    body = {
        "name": "CreatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new"
    }
    response = test_client.post(
        "/api/maps/",
        data=json_dumps(body),
        headers=user_token_headers,
    )
    assert HTTPStatus.NO_CONTENT == response.status_code
Example #4
0
async def form_error_handler(request: Request, exc: FormException) -> JSONResponse:
    if isinstance(exc, FormValidationError):
        return JSONResponse(
            {
                "type": type(exc).__name__,
                "detail": str(exc),
                "traceback": show_ex(exc),
                "title": "Form not valid",
                # We need to make sure the is nothing the default json.dumps cannot handle
                "validation_errors": json_loads(json_dumps(exc.errors)),
                "status": HTTPStatus.BAD_REQUEST,
            },
            status_code=HTTPStatus.BAD_REQUEST,
        )
    elif isinstance(exc, FormNotCompleteError):
        return JSONResponse(
            {
                "type": type(exc).__name__,
                "detail": str(exc),
                "traceback": show_ex(exc),
                # We need to make sure the is nothing the default json.dumps cannot handle
                "form": json_loads(json_dumps(exc.form)),
                "title": "Form not complete",
                "status": HTTPStatus.NOT_EXTENDED,
            },
            status_code=HTTPStatus.NOT_EXTENDED,
        )
    else:
        return JSONResponse(
            {
                "detail": str(exc),
                "title": "Internal Server Error",
                "status": HTTPStatus.INTERNAL_SERVER_ERROR,
                "type": type(exc).__name__,
            },
            status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
        )
Example #5
0
def test_admin_map_create_with_normal_user(test_client, user_token_headers):
    body = {
        "name": "CreatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new",
        "created_by": None,
    }
    response = test_client.post(
        "/api/maps/admin",
        data=json_dumps(body),
        headers=user_token_headers,
    )
    assert HTTPStatus.FORBIDDEN == response.status_code
Example #6
0
def test_admin_map_create(test_client, superuser_token_headers):
    body = {
        "name": "CreatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new",
        "created_by": None,
    }
    response = test_client.post(
        "/api/maps/admin",
        data=json_dumps(body),
        headers=superuser_token_headers,
    )
    assert HTTPStatus.NO_CONTENT == response.status_code
def test_product_create(test_client):
    p_id = uuid4()
    body = {
        "product_id": str(p_id),
        "name": "Product",
        "description": "Product description",
    }

    response = test_client.post(
        "/api/products/",
        data=json_dumps(body),
        headers={"Content_Type": "application/json"},
    )
    assert HTTPStatus.NO_CONTENT == response.status_code
    products = test_client.get("/api/products").json()
    assert 1 == len(products)
Example #8
0
def test_map_update_with_not_owner(test_client, superuser_token_headers,
                                   map_1):
    body = {
        "name": "UpdatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new",
        "end_date": "2021-10-22T17:14:41.016Z",
    }

    response = test_client.put(
        f"/api/maps/{map_1}",
        data=json_dumps(body),
        headers=superuser_token_headers,
    )
    assert HTTPStatus.FORBIDDEN == response.status_code
Example #9
0
def test_admin_map_update(test_client, superuser_token_headers, map_1):
    body = {
        "name": "UpdatedMap",
        "description": "desc",
        "size_x": 10,
        "size_y": 10,
        "status": "new",
        "end_date": "2021-10-22T17:14:41.016Z",
        "created_by": None,
    }

    response = test_client.put(
        f"/api/maps/admin/{map_1}",
        data=json_dumps(body),
        headers=superuser_token_headers,
    )
    assert HTTPStatus.NO_CONTENT == response.status_code
Example #10
0
def test_serialization_datetime():
    json_str = json_dumps({"end_date": nowtz()})
    assert re.search(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:00", json_str)