Example #1
0
async def test_positive_register_w_email_and_name(test_cli):
    global username
    username = None
    while username is None:
        i = random.randint(1, 10000)
        username = f"amichay.oren+{i}@gmail.com"
        if User.username_exists(username):
            username = None

    data = {
        "username": username,
        "password": "******",
        "email": username,
        "name": "Amichay Oren",
    }
    resp = await test_cli.post("/users", data=json.dumps(data))

    assert resp.status == 201

    data = {"username": username, "password": "******"}
    resp = await test_cli.post("/auth", data=json.dumps(data))
    resp_json = await resp.json()
    print(resp_json)
    global access_token
    access_token = resp_json["access_token"]
    global refresh_token
    refresh_token = resp_json["refresh_token"]
    assert access_token is not None
    assert refresh_token is not None
    assert resp.status == 200
Example #2
0
def test_cli(loop, app, sanic_client):
    global username
    while username is None:
        i = random.randint(1, 10000)
        username = f"amichay.oren+{i}@gmail.com"
        if User.username_exists(username):
            username = None

    return loop.run_until_complete(sanic_client(app))
Example #3
0
async def test_positive_register_w_email(test_cli):
    global username
    username = None
    while username is None:
        i = random.randint(1, 10000)
        username = f"amichay.oren+{i}@gmail.com"
        if User.username_exists(username):
            username = None

    data = {"username": username, "password": "******", "email": username}
    resp = await test_cli.post("/users", data=json.dumps(data))
    assert resp.status == 201
Example #4
0
async def register(request, *args, **kwargs):
    if (
        request.json is None
        or "username" not in request.json
        or "password" not in request.json
    ):
        raise InvalidUsage("invalid payload (should be {username, password})")

    password = request.json["password"]
    if not password_validator(password):
        raise InvalidUsage("password does not match minimum requirements")

    username = request.json["username"]
    if User.username_exists(username):
        raise Conflict(f"username {username} already exists")

    email = request.json["email"] if "email" in request.json else None
    name = request.json["name"] if "name" in request.json else None
    user = User(
        None, username, encrypt(request.json["password"]), ["user"], email, name
    )
    user.save()

    return response.HTTPResponse(status=201)
Example #5
0
async def test_positive_admin_update_user(test_cli):
    global access_token
    # check who i am
    headers = {"Authorization": f"Bearer {access_token}"}
    resp = await test_cli.get("/auth/me", headers=headers)
    resp_json = await resp.json()
    print(resp_json)
    assert resp.status == 200

    # update me
    my_user_id = resp_json["me"]["user_id"]
    data = {"scopes": ["user", "manager", "admin"]}
    headers = {"Authorization": f"Bearer {access_token}"}
    resp = await test_cli.patch(f"/users/{my_user_id}/scopes",
                                headers=headers,
                                data=json.dumps(data))
    assert resp.status == 204
    manager_access_token = access_token

    # register second
    global username
    username = None
    while username is None:
        i = random.randint(1, 10000)
        username = f"amichay.oren+{i}"
        if User.username_exists(username):
            username = None

    data = {
        "username": username,
        "password": "******",
        "name": "Amichay Oren",
        "email": f"{username}@gmail.com",
    }
    resp = await test_cli.post("/users", data=json.dumps(data))
    assert resp.status == 201

    # login second
    data = {"username": username, "password": "******"}
    resp = await test_cli.post("/auth", data=json.dumps(data))
    resp_json = await resp.json()
    print(resp_json)
    access_token = resp_json["access_token"]
    refresh_token = resp_json["refresh_token"]
    assert access_token is not None
    assert refresh_token is not None
    assert resp.status == 200

    # check who is second
    headers = {"Authorization": f"Bearer {access_token}"}
    resp = await test_cli.get("/auth/me", headers=headers)
    resp_json = await resp.json()
    print(resp_json)
    assert resp.status == 200
    new_manager_user_id = resp_json["me"]["user_id"]

    # first admin update second user
    new_name = "some new name"
    data = {"name": new_name}
    headers = {"Authorization": f"Bearer {manager_access_token}"}
    resp = await test_cli.patch(f"/users/{new_manager_user_id}",
                                headers=headers,
                                data=json.dumps(data))
    assert resp.status == 204