async def test_get_users_state_filter(app, state):
    db = app.db

    # has_one_active: one active, one inactive, zero ready
    has_one_active = add_user(db, app=app, name='has_one_active')
    # has_two_active: two active, ready servers
    has_two_active = add_user(db, app=app, name='has_two_active')
    # has_two_inactive: two spawners, neither active
    has_two_inactive = add_user(db, app=app, name='has_two_inactive')
    # has_zero: no Spawners registered at all
    has_zero = add_user(db, app=app, name='has_zero')
    total_users = db.query(orm.User).count()

    test_usernames = {
        "has_one_active",
        "has_two_active",
        "has_two_inactive",
        "has_zero",
    }

    user_states = {
        "inactive": ["has_two_inactive", "has_zero"],
        "ready": ["has_two_active"],
        "active": ["has_one_active", "has_two_active"],
        "invalid": [],
    }
    expected = user_states[state]

    def add_spawner(user, name='', active=True, ready=True):
        """Add a spawner in a requested state

        If active, should turn up in an active query
        If active and ready, should turn up in a ready query
        If not active, should turn up in an inactive query
        """
        spawner = user.spawners[name]
        db.commit()
        if active:
            orm_server = orm.Server()
            db.add(orm_server)
            db.commit()
            spawner.server = Server(orm_server=orm_server)
            db.commit()
            if not ready:
                spawner._spawn_pending = True
        return spawner

    for name in ("", "secondary"):
        add_spawner(has_two_active, name, active=True)
        add_spawner(has_two_inactive, name, active=False)

    add_spawner(has_one_active, active=True, ready=False)
    add_spawner(has_one_active, "inactive", active=False)

    r = await api_request(
        app, f'users?state={state}', headers={"Accept": PAGINATION_MEDIA_TYPE}
    )
    if state == "invalid":
        assert r.status_code == 400
        return
    assert r.status_code == 200

    response = r.json()
    users = response["items"]
    page = response["_pagination"]

    usernames = sorted(u["name"] for u in users if u["name"] in test_usernames)
    assert usernames == expected
    if state == "ready":
        # "ready" can't actually get a correct count because it has post-filtering applied
        # but it has an upper bound
        assert page["total"] >= len(users)
    else:
        assert page["total"] == len(users)
Add a spawner in a requested state

If active, should turn up in an active query
If active and ready, should turn up in a ready query
If not active, should turn up in an inactive query
jupyterhub/tests/test_api.py