def test_update_user_empty_json(test_app, test_database):
    utils.recreate_db()
    public_id = utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )
    client = test_app.test_client()
    resp = client.put(
        f"/users/{public_id}",
        data=json.dumps({}),
        content_type="application/json",
    )
    data = json.loads(resp.data.decode())
    assert resp.status_code == 400
    assert "Empty payload" in data["message"]
    assert "fail" in data["status"]
def test_update_user_wrong_permission(test_app, test_database):
    utils.recreate_db()
    public_id = utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )
    client = test_app.test_client()
    resp = client.put(
        f"/users/{public_id}",
        data=json.dumps({"public_id": "123"}),
        content_type="application/json",
    )
    data = json.loads(resp.data.decode())
    assert resp.status_code == 403
    assert "Can not modify public_id attribute" in data["message"]
    assert "fail" in data["status"]
def test_remove_user(test_app, test_database):
    utils.recreate_db()
    public_id = utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )
    client = test_app.test_client()
    resp_one = client.get("/users")
    data = json.loads(resp_one.data.decode())
    assert resp_one.status_code == 200
    assert len(data["data"]["users"]) == 1
    resp_two = client.delete(f"/users/{public_id}")
    data = json.loads(resp_two.data.decode())
    assert resp_two.status_code == 200
    assert "[email protected] was removed!" in data["message"]
    assert "success" in data["status"]
    resp_three = client.get("/users")
    data = json.loads(resp_three.data.decode())
    assert resp_three.status_code == 200
    assert len(data["data"]["users"]) == 0
def test_update_user(test_app, test_database):
    utils.recreate_db()
    public_id = utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )
    client = test_app.test_client()
    resp_one = client.put(
        f"/users/{public_id}",
        data=json.dumps({"username": "******", "email": "*****@*****.**"}),
        content_type="application/json",
    )
    data = json.loads(resp_one.data.decode())
    assert resp_one.status_code == 200
    assert f"{public_id} was updated!" in data["message"]
    assert "success" in data["status"]
    resp_two = client.get(f"/users/{public_id}")
    data = json.loads(resp_two.data.decode())
    assert resp_two.status_code == 200
    assert "me" in data["data"]["username"], data["data"]
    assert "*****@*****.**" in data["data"]["email"]
    assert "success" in data["status"]
def test_get_users(test_app, test_database):
    utils.recreate_db()
    utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )
    utils.add_user(
        {"username": "******", "email": "*****@*****.**"}
    )

    client = test_app.test_client()

    response = client.get("/users")
    assert response.status_code == 200

    data = json.loads(response.data.decode())
    assert "success" in data["status"]
    assert len(data["data"]["users"]) == 2
    assert "mayankdcoder" in data["data"]["users"][0]["username"]
    assert "mayankdcoder1" in data["data"]["users"][1]["username"]
    assert "*****@*****.**" in data["data"]["users"][0]["email"]
    assert "*****@*****.**" in data["data"]["users"][1]["email"]