def test_retrieve_users(superuser_token_headers): server_api = get_server_api() username = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=username, email=username, password=password) bucket = get_default_bucket() user = upsert_user(bucket, user_in, persist_to=1) username2 = random_lower_string() password2 = random_lower_string() user_in2 = UserInCreate(username=username2, email=username2, password=password2) user2 = upsert_user(bucket, user_in, persist_to=1) r = requests.get(f"{server_api}{config.API_V1_STR}/users/", headers=superuser_token_headers) all_users = r.json() assert len(all_users) > 1 for user in all_users: assert "username" in user assert "admin_roles" in user
async def create_user(conn: AsyncIOMotorClient, user: UserInCreate) -> UserInDB: salt = generate_salt() hashed_password = get_password_hash(salt + user.password) db_user = user.dict() db_user['salt'] = salt db_user['hashed_password'] = hashed_password del db_user['password'] row = await conn[database_name][user_collection_name].insert_one(db_user) return UserInDB(**user.dict())
def route_users_post( *, username, password, admin_channels=[], admin_roles=[], disabled=False, email=None, full_name=None, ): current_user = get_current_user() if not current_user: abort(400, "Could not authenticate user with provided token") elif not check_if_user_is_active(current_user): abort(400, "Inactive user") elif not check_if_user_is_superuser(current_user): abort(400, "The user doesn't have enough privileges") bucket = get_default_bucket() user = get_user(bucket, username) if user: return abort(400, f"The user with this username already exists in the system.") user_in = UserInCreate( username=username, password=password, admin_channels=admin_channels, admin_roles=admin_roles, disabled=disabled, email=email, full_name=full_name, ) user = upsert_user(bucket, user_in) if config.EMAILS_ENABLED: send_new_account_email(email_to=email, username=username, password=password) return user
def test_check_if_user_is_active(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=email, password=password) user = crud.user.create(db_session, user_in=user_in) is_active = crud.user.is_active(user) assert is_active is True
def route_users_post_open( *, username: str = Body(...), password: str = Body(...), email: EmailStr = Body(None), full_name: str = Body(None), ): """ Create new user without the need to be logged in """ if not config.USERS_OPEN_REGISTRATION: raise HTTPException( status_code=403, detail="Open user resgistration is forbidden on this server", ) bucket = get_default_bucket() user = get_user(bucket, username) if user: raise HTTPException( status_code=400, detail="The user with this username already exists in the system", ) user_in = UserInCreate( username=username, password=password, email=email, full_name=full_name ) user = upsert_user(bucket, user_in, persist_to=1) return user
def route_users_post( *, name, password, admin_channels=[], admin_roles=[], disabled=False, email=None, human_name=None, ): current_user = get_current_user() if not current_user: abort(400, "Could not authenticate user with provided token") elif not check_if_user_is_active(current_user): abort(400, "Inactive user") elif not check_if_user_is_admin_or_superuser(current_user): abort(400, "The user doesn't have enough privileges") user = get_user(bucket, name) if user: return abort( 400, f"The user with this username already exists in the system.") user_in = UserInCreate( name=name, password=password, admin_channels=admin_channels, admin_roles=admin_roles, disabled=disabled, email=email, human_name=human_name, ) user = upsert_user(bucket, user_in) return user
def test_check_if_user_is_superuser(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=email, password=password, is_superuser=True) user = crud.user.create(db_session, user_in=user_in) is_superuser = crud.user.is_superuser(user) assert is_superuser is True
def create_user_open( *, username: str = Body(...), password: str = Body(...), email: EmailStr = Body(None), full_name: str = Body(None), ): """ Create new user without the need to be logged in """ if not config.USERS_OPEN_REGISTRATION: raise HTTPException( status_code=403, detail="Open user resgistration is forbidden on this server", ) bucket = get_default_bucket() user = crud.user.get(bucket, username=username) if user: raise HTTPException( status_code=400, detail="The user with this username already exists in the system", ) user_in = UserInCreate( username=username, password=password, email=email, full_name=full_name ) user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) if config.EMAILS_ENABLED and user_in.email: send_new_account_email( email_to=user_in.email, username=user_in.username, password=user_in.password ) return user
def test_check_if_user_is_superuser_normal_user(): username = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=username, email=username, password=password) user = upsert_user(bucket, user_in) is_superuser = check_if_user_is_superuser(user) assert is_superuser is False
def test_check_if_user_is_active(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=email, email=email, password=password) user = upsert_user(bucket, user_in) is_active = check_if_user_is_active(user) assert is_active is True
def test_create_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=email, password=password) user = crud.user.create(db_session, user_in=user_in) assert user.email == email assert hasattr(user, "hashed_password")
def test_check_if_user_is_superuser_normal_user(): username = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=username, password=password) user = crud.user.create(db_session, user_in=user_in) is_superuser = crud.user.is_superuser(user) assert is_superuser is False
def test_authenticate_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=email, email=email, password=password) user = upsert_user(bucket, user_in) authenticated_user = authenticate_user(bucket, email, password) assert authenticated_user assert user.Meta.key == authenticated_user.Meta.key
def test_check_if_user_is_active(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=email, email=email, password=password) bucket = get_default_bucket() user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) is_active = crud.user.is_active(user) assert is_active is True
def upsert_user_in_db(bucket: Bucket, user_in: UserInCreate): user_doc_id = get_user_doc_id(user_in.name) passwordhash = get_password_hash(user_in.password) user = UserStored(**user_in.json_dict(), hashed_password=passwordhash) doc_data = user.json_dict() bucket.upsert(user_doc_id, doc_data) return user
def test_authenticate_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=email, email=email, password=password) bucket = get_default_bucket() user = upsert_user(bucket, user_in, persist_to=1) authenticated_user = authenticate_user(bucket, email, password) assert authenticated_user assert user.username == authenticated_user.username
def test_check_if_user_is_superuser_normal_user(): username = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=username, email=username, password=password) bucket = get_default_bucket() user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) is_superuser = crud.user.is_superuser(user) assert is_superuser is False
def test_retrieve_users(superuser_token_headers): server_api = get_server_api() username = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=username, password=password) user = crud.user.create(db_session, user_in=user_in) username2 = random_lower_string() password2 = random_lower_string() user_in2 = UserInCreate(email=username2, password=password2) user2 = crud.user.create(db_session, user_in=user_in2) r = requests.get(f"{server_api}{config.API_V1_STR}/users/", headers=superuser_token_headers) all_users = r.json() assert len(all_users) > 1 for user in all_users: assert "email" in user
def test_check_if_user_is_active_inactive(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=email, password=password, disabled=True) print(user_in) user = crud.user.create(db_session, user_in=user_in) print(user) is_active = crud.user.is_active(user) print(is_active) assert is_active
def test_create_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=email, email=email, password=password) user = upsert_user(bucket, user_in) assert hasattr(user, "name") assert user.name == email assert hasattr(user, "hashed_password") assert hasattr(user, "type") assert user.type == "userprofile"
def test_authenticate_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(email=email, password=password) user = crud.user.create(db_session, user_in=user_in) authenticated_user = crud.user.authenticate(db_session, email=email, password=password) assert authenticated_user assert user.email == authenticated_user.email
def upsert_user_in_db(bucket: Bucket, user_in: UserInCreate, persist_to=0): user_doc_id = get_user_doc_id(user_in.username) passwordhash = get_password_hash(user_in.password) user = UserInDB(**user_in.dict(), hashed_password=passwordhash) doc_data = jsonable_encoder(user) with bucket.durability(persist_to=persist_to, timeout=COUCHBASE_DURABILITY_TIMEOUT_SECS): bucket.upsert(user_doc_id, doc_data) return user
def test_get_user(): password = random_lower_string() username = random_lower_string() user_in = UserInCreate(email=username, password=password, is_superuser=True) user = crud.user.create(db_session, user_in=user_in) user_2 = crud.user.get(db_session, user_id=user.id) assert user.email == user_2.email assert jsonable_encoder(user) == jsonable_encoder(user_2)
def test_check_if_user_is_superuser(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=email, email=email, password=password, admin_roles=[RoleEnum.superuser]) user = upsert_user(bucket, user_in) is_superuser = check_if_user_is_superuser(user) assert is_superuser is True
def init_db(): logging.info("before config_couchbase") config_couchbase(COUCHBASE_USER, COUCHBASE_PASSWORD, host=COUCHBASE_HOST, port=COUCHBASE_PORT) logging.info("after config_couchbase") # COUCHBASE_USER="******" # COUCHBASE_PASSWORD="******" logging.info("before ensure_create_bucket") ensure_create_bucket( COUCHBASE_USER, COUCHBASE_PASSWORD, COUCHBASE_BUCKET_NAME, host=COUCHBASE_HOST, port=COUCHBASE_PORT, ) logging.info("after ensure_create_bucket") logging.info("before get_bucket") bucket = get_bucket( COUCHBASE_USER, COUCHBASE_PASSWORD, COUCHBASE_BUCKET_NAME, host=COUCHBASE_HOST, port=COUCHBASE_PORT, ) logging.info("after get_bucket") logging.info("before ensure_create_primary_index") ensure_create_primary_index(bucket) logging.info("after ensure_create_primary_index") logging.info("before ensure_create_type_index") ensure_create_type_index(bucket) logging.info("after ensure_create_type_index") logging.info("before ensure_create_couchbase_app_user sync") ensure_create_couchbase_app_user( COUCHBASE_USER, COUCHBASE_PASSWORD, COUCHBASE_SYNC_GATEWAY_USER, COUCHBASE_SYNC_GATEWAY_PASSWORD, COUCHBASE_BUCKET_NAME, host=COUCHBASE_HOST, port=COUCHBASE_PORT, ) logging.info("after ensure_create_couchbase_app_user sync") logging.info("before upsert_user first superuser") in_user = UserInCreate( name=FIRST_SUPERUSER, password=FIRST_SUPERUSER_PASSWORD, email=FIRST_SUPERUSER, admin_roles=[RoleEnum.superuser, RoleEnum.admin], admin_channels=[FIRST_SUPERUSER, RoleEnum.admin], ) upsert_user(bucket, in_user) logging.info("after upsert_user first superuser")
def test_get_user(): password = random_lower_string() username = random_lower_string() user_in = UserInCreate(name=username, email=username, password=password, admin_roles=[RoleEnum.superuser]) user = upsert_user(bucket, user_in) user_2 = get_user(bucket, username) assert user.Meta.key == user_2.Meta.key assert user.json_dict() == user_2.json_dict()
def test_check_if_user_is_active_inactive(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=email, email=email, password=password, disabled=True) bucket = get_default_bucket() user = upsert_user(bucket, user_in, persist_to=1) is_active = check_if_user_is_active(user) assert is_active is False
def test_check_if_user_is_superuser(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=email, email=email, password=password, admin_roles=[RoleEnum.superuser]) bucket = get_default_bucket() user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) is_superuser = crud.user.is_superuser(user) assert is_superuser is True
def test_create_user(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=email, email=email, password=password) bucket = get_default_bucket() user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) assert hasattr(user, "username") assert user.username == email assert hasattr(user, "hashed_password") assert hasattr(user, "type") assert user.type == "userprofile"
def test_create_user_by_normal_user(): server_api = get_server_api() username = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=username, email=username, password=password) user = upsert_user(bucket, user_in) user_token_headers = user_authentication_headers(server_api, username, password) data = {"name": username, "password": password} r = requests.post( f"{server_api}{config.API_V1_STR}/users/", headers=user_token_headers, json=data ) assert r.status_code == 400