테스트 케이스 추가, update_or_create 추가
This commit is contained in:
56
.gitea/workflows/code_check.yml
Normal file
56
.gitea/workflows/code_check.yml
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
name: Code Check
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
run_pytest:
|
||||||
|
description: "Run pytest tests"
|
||||||
|
required: true
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
|
||||||
|
- name: ruff check
|
||||||
|
uses: astral-sh/ruff-action@v3
|
||||||
|
# with:
|
||||||
|
# src: "./src"
|
||||||
|
|
||||||
|
test:
|
||||||
|
needs: lint
|
||||||
|
if: ${{ inputs.run_pytest == true }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
|
||||||
|
- name: Install the default version of uv
|
||||||
|
id: setup-uv
|
||||||
|
uses: astral-sh/setup-uv@v5
|
||||||
|
with:
|
||||||
|
enable-cache: true
|
||||||
|
cache-dependency-glob: "**/uv.lock"
|
||||||
|
|
||||||
|
- name: Cache uv venv
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: .venv
|
||||||
|
key: ${{ runner.os }}-uv-${{ hashFiles('**/uv.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-uv-
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: uv sync --locked --all-extras
|
||||||
|
|
||||||
|
- name: Run tests with pytest
|
||||||
|
run: .venv/bin/pytest -v -s tests/test_main.py
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ build/
|
|||||||
dist/
|
dist/
|
||||||
wheels/
|
wheels/
|
||||||
*.egg-info
|
*.egg-info
|
||||||
|
.pytest_cache/
|
||||||
|
|
||||||
# Virtual environments
|
# Virtual environments
|
||||||
.venv
|
.venv
|
||||||
|
|||||||
73
main.py
73
main.py
@@ -66,9 +66,17 @@ def csv_from_google_sheet_url(url: str) -> list[dict]:
|
|||||||
def _prepare_user_data(user, expiry_timestamp=None):
|
def _prepare_user_data(user, expiry_timestamp=None):
|
||||||
"""
|
"""
|
||||||
사용자 데이터로부터 Descope API에 필요한 테넌트, 커스텀 속성 등을 준비합니다.
|
사용자 데이터로부터 Descope API에 필요한 테넌트, 커스텀 속성 등을 준비합니다.
|
||||||
|
미리 정의된 필드 외의 모든 키-값 쌍은 custom_attributes에 포함됩니다.
|
||||||
"""
|
"""
|
||||||
login_id = user.get('login_id')
|
login_id = user.get('login_id')
|
||||||
|
|
||||||
|
# Descope 사용자 객체의 최상위 레벨 필드 또는 스크립트에서 특별히 처리되는 필드 목록
|
||||||
|
known_fields = {
|
||||||
|
'login_id', 'email', 'display_name', 'tenants', 'role_name',
|
||||||
|
'status', 'new_password', 'custom_attributes_key', 'custom_attributes_value',
|
||||||
|
'company', 'egBimLExpiryDate'
|
||||||
|
}
|
||||||
|
|
||||||
user_tenants = []
|
user_tenants = []
|
||||||
tenants_str = user.get('tenants')
|
tenants_str = user.get('tenants')
|
||||||
if tenants_str:
|
if tenants_str:
|
||||||
@@ -85,6 +93,11 @@ def _prepare_user_data(user, expiry_timestamp=None):
|
|||||||
print(f"경고: {login_id}의 'tenants' 필드를 파싱할 수 없습니다: {tenants_str}")
|
print(f"경고: {login_id}의 'tenants' 필드를 파싱할 수 없습니다: {tenants_str}")
|
||||||
|
|
||||||
custom_attributes = {}
|
custom_attributes = {}
|
||||||
|
# user 딕셔너리의 모든 항목을 순회하며 custom_attributes 구성
|
||||||
|
for key, value in user.items():
|
||||||
|
if key not in known_fields and value:
|
||||||
|
custom_attributes[key] = value
|
||||||
|
|
||||||
if user.get('custom_attributes_key') and user.get('custom_attributes_value'):
|
if user.get('custom_attributes_key') and user.get('custom_attributes_value'):
|
||||||
custom_attributes[user['custom_attributes_key']] = user['custom_attributes_value']
|
custom_attributes[user['custom_attributes_key']] = user['custom_attributes_value']
|
||||||
|
|
||||||
@@ -94,9 +107,8 @@ def _prepare_user_data(user, expiry_timestamp=None):
|
|||||||
custom_attributes['completeForm'] = True
|
custom_attributes['completeForm'] = True
|
||||||
|
|
||||||
# egBimLExpiryDate 처리
|
# egBimLExpiryDate 처리
|
||||||
final_expiry_date = expiry_timestamp # CLI 인자가 우선순위가 가장 높음
|
final_expiry_date = expiry_timestamp
|
||||||
if final_expiry_date is None and user.get('egBimLExpiryDate'):
|
if final_expiry_date is None and user.get('egBimLExpiryDate'):
|
||||||
# CLI 인자가 없고 CSV에 값이 있을 경우, 해당 값을 변환하여 사용
|
|
||||||
final_expiry_date = convert_to_timestamp(user.get('egBimLExpiryDate'))
|
final_expiry_date = convert_to_timestamp(user.get('egBimLExpiryDate'))
|
||||||
|
|
||||||
if final_expiry_date is not None:
|
if final_expiry_date is not None:
|
||||||
@@ -112,16 +124,13 @@ def convert_to_timestamp(date_value):
|
|||||||
if not date_value:
|
if not date_value:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Already an integer timestamp
|
|
||||||
if isinstance(date_value, int):
|
if isinstance(date_value, int):
|
||||||
return date_value
|
return date_value
|
||||||
|
|
||||||
if isinstance(date_value, str):
|
if isinstance(date_value, str):
|
||||||
# Check if it's a string representation of an integer
|
|
||||||
try:
|
try:
|
||||||
return int(date_value)
|
return int(date_value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# If not, try to parse it as a YYYY-MM-DD date string
|
|
||||||
try:
|
try:
|
||||||
dt = datetime.strptime(date_value, '%Y-%m-%d')
|
dt = datetime.strptime(date_value, '%Y-%m-%d')
|
||||||
dt_utc = dt.replace(tzinfo=timezone.utc)
|
dt_utc = dt.replace(tzinfo=timezone.utc)
|
||||||
@@ -134,11 +143,13 @@ def convert_to_timestamp(date_value):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def create_users(users_data, expiry_timestamp=None):
|
def create_users(users_data, expiry_timestamp=None, dry_run=False):
|
||||||
"""
|
"""
|
||||||
CSV에서 읽어온 사용자 데이터를 기반으로 Descope를 통해 계정을 일괄 생성합니다.
|
CSV에서 읽어온 사용자 데이터를 기반으로 Descope를 통해 계정을 일괄 생성합니다.
|
||||||
"""
|
"""
|
||||||
print("--- 사용자 계정 일괄 생성을 시작합니다 ---")
|
print("--- 사용자 계정 일괄 생성을 시작합니다 ---")
|
||||||
|
if dry_run:
|
||||||
|
print("*** DRY RUN 모드로 실행됩니다. 실제 데이터는 변경되지 않습니다. ***")
|
||||||
try:
|
try:
|
||||||
descope_client = get_descope_client()
|
descope_client = get_descope_client()
|
||||||
for user in users_data:
|
for user in users_data:
|
||||||
@@ -149,9 +160,14 @@ def create_users(users_data, expiry_timestamp=None):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
user_tenants, custom_attributes = _prepare_user_data(user, expiry_timestamp)
|
user_tenants, custom_attributes = _prepare_user_data(user, expiry_timestamp)
|
||||||
|
|
||||||
status = user.get('status', 'activated')
|
status = user.get('status', 'activated')
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
print(f"[DRY RUN] 사용자 생성 예정: {login_id}, 속성: {custom_attributes}")
|
||||||
|
if status != 'activated':
|
||||||
|
print(f"[DRY RUN] 사용자 상태 업데이트 예정: {login_id} -> {status}")
|
||||||
|
continue
|
||||||
|
|
||||||
print(f"사용자 생성 시도: {login_id}")
|
print(f"사용자 생성 시도: {login_id}")
|
||||||
descope_client.mgmt.user.create(
|
descope_client.mgmt.user.create(
|
||||||
login_id=login_id,
|
login_id=login_id,
|
||||||
@@ -182,11 +198,13 @@ def create_users(users_data, expiry_timestamp=None):
|
|||||||
print("--- 사용자 계정 일괄 생성이 완료되었습니다 ---")
|
print("--- 사용자 계정 일괄 생성이 완료되었습니다 ---")
|
||||||
|
|
||||||
|
|
||||||
def update_or_create_users(users_data, expiry_timestamp=None):
|
def update_or_create_users(users_data, expiry_timestamp=None, dry_run=False):
|
||||||
"""
|
"""
|
||||||
사용자 이메일(login_id)을 확인하여 존재하면 정보를 업데이트하고, 존재하지 않으면 새로 생성합니다.
|
사용자 이메일(login_id)을 확인하여 존재하면 정보를 업데이트하고, 존재하지 않으면 새로 생성합니다.
|
||||||
"""
|
"""
|
||||||
print("--- 사용자 정보 업데이트 또는 생성을 시작합니다 ---")
|
print("--- 사용자 정보 업데이트 또는 생성을 시작합니다 ---")
|
||||||
|
if dry_run:
|
||||||
|
print("*** DRY RUN 모드로 실행됩니다. 실제 데이터는 변경되지 않습니다. ***")
|
||||||
try:
|
try:
|
||||||
descope_client = get_descope_client()
|
descope_client = get_descope_client()
|
||||||
for user in users_data:
|
for user in users_data:
|
||||||
@@ -199,20 +217,20 @@ def update_or_create_users(users_data, expiry_timestamp=None):
|
|||||||
user_tenants, custom_attributes = _prepare_user_data(user, expiry_timestamp)
|
user_tenants, custom_attributes = _prepare_user_data(user, expiry_timestamp)
|
||||||
display_name = user.get('display_name')
|
display_name = user.get('display_name')
|
||||||
|
|
||||||
# 사용자 존재 여부 확인
|
if dry_run:
|
||||||
|
print(f"[DRY RUN] 사용자 업데이트 또는 생성 예정: {login_id}, 이름: {display_name}, 속성: {custom_attributes}")
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
descope_client.mgmt.user.load(login_id=login_id)
|
descope_client.mgmt.user.load(login_id=login_id)
|
||||||
user_exists = True
|
user_exists = True
|
||||||
except AuthException as e:
|
except AuthException as e:
|
||||||
# Descope SDK는 사용자를 찾지 못하면 AuthException을 발생시킵니다.
|
|
||||||
# 오류 메시지에 "not found"가 포함되어 있는지 확인하여 사용자가 없는 경우를 특정합니다.
|
|
||||||
if "user not found" in str(e).lower() or "user not found" in str(e.args).lower():
|
if "user not found" in str(e).lower() or "user not found" in str(e.args).lower():
|
||||||
user_exists = False
|
user_exists = False
|
||||||
else:
|
else:
|
||||||
raise e # 다른 종류의 AuthException은 다시 발생시킴
|
raise e
|
||||||
|
|
||||||
if user_exists:
|
if user_exists:
|
||||||
# 사용자 업데이트
|
|
||||||
print(f"사용자 업데이트 시도: {login_id}")
|
print(f"사용자 업데이트 시도: {login_id}")
|
||||||
descope_client.mgmt.user.update(
|
descope_client.mgmt.user.update(
|
||||||
login_id=login_id,
|
login_id=login_id,
|
||||||
@@ -223,7 +241,6 @@ def update_or_create_users(users_data, expiry_timestamp=None):
|
|||||||
)
|
)
|
||||||
print(f"성공: {login_id} 사용자 정보가 업데이트되었습니다.")
|
print(f"성공: {login_id} 사용자 정보가 업데이트되었습니다.")
|
||||||
else:
|
else:
|
||||||
# 사용자 생성
|
|
||||||
print(f"사용자 생성 시도: {login_id}")
|
print(f"사용자 생성 시도: {login_id}")
|
||||||
descope_client.mgmt.user.create(
|
descope_client.mgmt.user.create(
|
||||||
login_id=login_id,
|
login_id=login_id,
|
||||||
@@ -234,10 +251,7 @@ def update_or_create_users(users_data, expiry_timestamp=None):
|
|||||||
)
|
)
|
||||||
print(f"성공: {login_id} 사용자가 생성되었습니다.")
|
print(f"성공: {login_id} 사용자가 생성되었습니다.")
|
||||||
|
|
||||||
# 상태 업데이트 (생성/업데이트 공통)
|
|
||||||
status = user.get('status', 'activated')
|
status = user.get('status', 'activated')
|
||||||
# 'activated' 상태는 기본값이므로 별도로 업데이트할 필요가 없습니다.
|
|
||||||
# 다른 상태일 경우에만 상태 업데이트를 시도합니다.
|
|
||||||
if status != 'activated':
|
if status != 'activated':
|
||||||
try:
|
try:
|
||||||
print(f"사용자 상태 업데이트 시도: {login_id} -> {status}")
|
print(f"사용자 상태 업데이트 시도: {login_id} -> {status}")
|
||||||
@@ -258,11 +272,13 @@ def update_or_create_users(users_data, expiry_timestamp=None):
|
|||||||
print("--- 사용자 정보 업데이트 또는 생성이 완료되었습니다 ---")
|
print("--- 사용자 정보 업데이트 또는 생성이 완료되었습니다 ---")
|
||||||
|
|
||||||
|
|
||||||
def change_passwords(users_data):
|
def change_passwords(users_data, dry_run=False):
|
||||||
"""
|
"""
|
||||||
CSV에서 읽어온 사용자 데이터를 기반으로 Descope를 통해 비밀번호를 일괄 변경합니다.
|
CSV에서 읽어온 사용자 데이터를 기반으로 Descope를 통해 비밀번호를 일괄 변경합니다.
|
||||||
"""
|
"""
|
||||||
print("--- 사용자 비밀번호 일괄 변경을 시작합니다 ---")
|
print("--- 사용자 비밀번호 일괄 변경을 시작합니다 ---")
|
||||||
|
if dry_run:
|
||||||
|
print("*** DRY RUN 모드로 실행됩니다. 실제 데이터는 변경되지 않습니다. ***")
|
||||||
try:
|
try:
|
||||||
descope_client = get_descope_client()
|
descope_client = get_descope_client()
|
||||||
for user in users_data:
|
for user in users_data:
|
||||||
@@ -274,6 +290,10 @@ def change_passwords(users_data):
|
|||||||
print(f"경고: 'login_id' 또는 'new_password'가 없는 행을 건너뜁니다: {user}")
|
print(f"경고: 'login_id' 또는 'new_password'가 없는 행을 건너뜁니다: {user}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
print(f"[DRY RUN] 비밀번호 변경 예정: {login_id}")
|
||||||
|
continue
|
||||||
|
|
||||||
print(f"비밀번호 변경 시도: {login_id}")
|
print(f"비밀번호 변경 시도: {login_id}")
|
||||||
descope_client.mgmt.user.set_active_password(
|
descope_client.mgmt.user.set_active_password(
|
||||||
login_id=login_id,
|
login_id=login_id,
|
||||||
@@ -297,7 +317,7 @@ def change_passwords(users_data):
|
|||||||
|
|
||||||
print("--- 사용자 비밀번호 일괄 변경이 완료되었습니다 ---")
|
print("--- 사용자 비밀번호 일괄 변경이 완료되었습니다 ---")
|
||||||
|
|
||||||
def test_create_single_user():
|
def test_create_single_user(dry_run=False):
|
||||||
"""
|
"""
|
||||||
단일 사용자 생성을 테스트하는 함수입니다.
|
단일 사용자 생성을 테스트하는 함수입니다.
|
||||||
"""
|
"""
|
||||||
@@ -306,14 +326,13 @@ def test_create_single_user():
|
|||||||
'login_id': 'testuser.gemini@example.com',
|
'login_id': 'testuser.gemini@example.com',
|
||||||
'email': 'testuser.gemini@example.com',
|
'email': 'testuser.gemini@example.com',
|
||||||
'display_name': 'Gemini Test User',
|
'display_name': 'Gemini Test User',
|
||||||
'tenants': '["T31ZmUcwOZbwk0y3YmMxrPCpzpQR"]', # 실제 테스트용 Tenant ID로 변경 필요
|
'tenants': '["T31ZmUcwOZbwk0y3YmMxrPCpzpQR"]',
|
||||||
'company': 'Gemini Test Inc.',
|
'company': 'Gemini Test Inc.',
|
||||||
'egBimLExpiryDate': 1798729200,
|
'egBimLExpiryDate': 1798729200,
|
||||||
'status': 'activated',
|
'status': 'activated',
|
||||||
'verifiedEmail': True,
|
|
||||||
'is_test_user': True
|
'is_test_user': True
|
||||||
}]
|
}]
|
||||||
create_users(test_user_data)
|
create_users(test_user_data, dry_run=dry_run)
|
||||||
print("--- 단일 사용자 생성 테스트가 완료되었습니다 ---")
|
print("--- 단일 사용자 생성 테스트가 완료되었습니다 ---")
|
||||||
|
|
||||||
|
|
||||||
@@ -330,11 +349,12 @@ def main():
|
|||||||
help="수행할 작업 (create: 생성, change_password: 비밀번호 변경, update_or_create: 업데이트 또는 생성, test: 테스트)"
|
help="수행할 작업 (create: 생성, change_password: 비밀번호 변경, update_or_create: 업데이트 또는 생성, test: 테스트)"
|
||||||
)
|
)
|
||||||
parser.add_argument("--expiry-date", help="사용자 라이선스 만료일을 'YYYY-MM-DD' 형식으로 지정합니다. 이 값을 지정하면 CSV 파일의 모든 'egBimLExpiryDate' 값을 덮어씁니다.")
|
parser.add_argument("--expiry-date", help="사용자 라이선스 만료일을 'YYYY-MM-DD' 형식으로 지정합니다. 이 값을 지정하면 CSV 파일의 모든 'egBimLExpiryDate' 값을 덮어씁니다.")
|
||||||
|
parser.add_argument("--dry-run", action='store_true', help="실제 API를 호출하지 않고 실행할 작업만 출력합니다.")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.action == 'test':
|
if args.action == 'test':
|
||||||
test_create_single_user()
|
test_create_single_user(dry_run=args.dry_run)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not args.source:
|
if not args.source:
|
||||||
@@ -353,16 +373,15 @@ def main():
|
|||||||
if args.expiry_date:
|
if args.expiry_date:
|
||||||
expiry_timestamp = convert_to_timestamp(args.expiry_date)
|
expiry_timestamp = convert_to_timestamp(args.expiry_date)
|
||||||
if expiry_timestamp is None:
|
if expiry_timestamp is None:
|
||||||
# convert_to_timestamp 함수 내부에서 이미 경고 메시지를 출력하므로 여기서는 종료만 합니다.
|
|
||||||
return
|
return
|
||||||
print(f"전체 만료 날짜가 {args.expiry_date}로 설정되었습니다 (UTC 타임스탬프: {expiry_timestamp}).")
|
print(f"전체 만료 날짜가 {args.expiry_date}로 설정되었습니다 (UTC 타임스탬프: {expiry_timestamp}).")
|
||||||
|
|
||||||
if args.action == 'create':
|
if args.action == 'create':
|
||||||
create_users(users_data, expiry_timestamp)
|
create_users(users_data, expiry_timestamp, dry_run=args.dry_run)
|
||||||
elif args.action == 'change_password':
|
elif args.action == 'change_password':
|
||||||
change_passwords(users_data)
|
change_passwords(users_data, dry_run=args.dry_run)
|
||||||
elif args.action == 'update_or_create':
|
elif args.action == 'update_or_create':
|
||||||
update_or_create_users(users_data, expiry_timestamp)
|
update_or_create_users(users_data, expiry_timestamp, dry_run=args.dry_run)
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"오류: 파일을 찾을 수 없습니다 - {args.source}")
|
print(f"오류: 파일을 찾을 수 없습니다 - {args.source}")
|
||||||
|
|||||||
@@ -6,4 +6,17 @@ readme = "README.md"
|
|||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"descope>=1.7.9",
|
"descope>=1.7.9",
|
||||||
|
"pytest>=8.4.2",
|
||||||
|
"requests>=2.32.3",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"pytest>=8.2.2",
|
||||||
|
"pytest-mock>=3.14.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
pythonpath = [
|
||||||
|
"."
|
||||||
]
|
]
|
||||||
|
|||||||
104
tests/test_main.py
Normal file
104
tests/test_main.py
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import pytest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
import main
|
||||||
|
|
||||||
|
# convert_to_timestamp 함수 테스트
|
||||||
|
@pytest.mark.parametrize("input_date, expected_timestamp", [
|
||||||
|
("2025-12-25", int(datetime(2025, 12, 25, tzinfo=timezone.utc).timestamp())),
|
||||||
|
(1735084800, 1735084800),
|
||||||
|
("1735084800", 1735084800),
|
||||||
|
(None, None),
|
||||||
|
("", None),
|
||||||
|
("invalid-date", None),
|
||||||
|
])
|
||||||
|
def test_convert_to_timestamp(input_date, expected_timestamp):
|
||||||
|
"""
|
||||||
|
다양한 형식의 날짜 입력에 대해 convert_to_timestamp가 정확한 타임스탬프를 반환하는지 테스트합니다.
|
||||||
|
"""
|
||||||
|
assert main.convert_to_timestamp(input_date) == expected_timestamp
|
||||||
|
|
||||||
|
# _prepare_user_data 함수 테스트
|
||||||
|
def test_prepare_user_data():
|
||||||
|
"""
|
||||||
|
_prepare_user_data 함수가 사용자 정보를 올바르게 파싱하고,
|
||||||
|
특히 egBimLExpiryDate를 CLI 인자와 CSV 값에 따라 우선순위를 부여하여 처리하는지 테스트합니다.
|
||||||
|
"""
|
||||||
|
user_data = {
|
||||||
|
'login_id': 'test@example.com',
|
||||||
|
'tenants': '["tenant1", "tenant2"]',
|
||||||
|
'role_name': 'editor',
|
||||||
|
'company': 'Test Inc.',
|
||||||
|
'egBimLExpiryDate': '2024-12-31'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 1. CLI expiry_timestamp가 주어질 경우
|
||||||
|
cli_timestamp = int(datetime(2025, 1, 1, tzinfo=timezone.utc).timestamp())
|
||||||
|
tenants, custom_attrs = main._prepare_user_data(user_data, expiry_timestamp=cli_timestamp)
|
||||||
|
assert custom_attrs['egBimLExpiryDate'] == cli_timestamp
|
||||||
|
assert custom_attrs['company'] == 'Test Inc.'
|
||||||
|
assert len(tenants) == 2
|
||||||
|
assert tenants[0].tenant_id == 'tenant1'
|
||||||
|
assert tenants[0].role_names == ['editor']
|
||||||
|
|
||||||
|
# 2. CLI expiry_timestamp가 없고, CSV에 값이 있을 경우
|
||||||
|
csv_timestamp = int(datetime(2024, 12, 31, tzinfo=timezone.utc).timestamp())
|
||||||
|
tenants, custom_attrs = main._prepare_user_data(user_data, expiry_timestamp=None)
|
||||||
|
assert custom_attrs['egBimLExpiryDate'] == csv_timestamp
|
||||||
|
|
||||||
|
# 3. 두 값 모두 없을 경우
|
||||||
|
user_data_no_date = user_data.copy()
|
||||||
|
del user_data_no_date['egBimLExpiryDate']
|
||||||
|
tenants, custom_attrs = main._prepare_user_data(user_data_no_date, expiry_timestamp=None)
|
||||||
|
assert 'egBimLExpiryDate' not in custom_attrs
|
||||||
|
|
||||||
|
# update_or_create_users 함수 테스트 (DescopeClient 모킹)
|
||||||
|
@patch('main.get_descope_client')
|
||||||
|
def test_update_or_create_users_flow(mock_get_client):
|
||||||
|
"""
|
||||||
|
update_or_create_users 함수가 사용자의 존재 여부에 따라
|
||||||
|
user.update 또는 user.create를 올바르게 호출하는지 테스트합니다.
|
||||||
|
"""
|
||||||
|
# 가짜 DescopeClient 및 mgmt.user 객체 설정
|
||||||
|
mock_descope_client = MagicMock()
|
||||||
|
mock_user_mgmt = MagicMock()
|
||||||
|
mock_descope_client.mgmt.user = mock_user_mgmt
|
||||||
|
mock_get_client.return_value = mock_descope_client
|
||||||
|
|
||||||
|
users_to_process = [
|
||||||
|
{'login_id': 'existing.user@example.com', 'display_name': 'Existing User Updated'},
|
||||||
|
{'login_id': 'new.user@example.com', 'display_name': 'New User Created'},
|
||||||
|
]
|
||||||
|
|
||||||
|
# 'existing.user@example.com'에 대해서는 load가 성공하고,
|
||||||
|
# 'new.user@example.com'에 대해서는 AuthException을 발생시키도록 설정
|
||||||
|
def load_user_side_effect(login_id):
|
||||||
|
if login_id == 'existing.user@example.com':
|
||||||
|
return {'user': 'details'} # 성공 시 반환값 (내용은 중요하지 않음)
|
||||||
|
elif login_id == 'new.user@example.com':
|
||||||
|
# Descope SDK는 사용자를 찾지 못하면 AuthException 발생
|
||||||
|
from descope import AuthException
|
||||||
|
raise AuthException("user not found", "E062103", 404)
|
||||||
|
|
||||||
|
mock_user_mgmt.load.side_effect = load_user_side_effect
|
||||||
|
|
||||||
|
# 테스트 실행
|
||||||
|
main.update_or_create_users(users_to_process)
|
||||||
|
|
||||||
|
# 검증: update가 'existing.user@example.com'에 대해 한 번 호출되었는지 확인
|
||||||
|
mock_user_mgmt.update.assert_called_once_with(
|
||||||
|
login_id='existing.user@example.com',
|
||||||
|
email=None,
|
||||||
|
display_name='Existing User Updated',
|
||||||
|
user_tenants=[],
|
||||||
|
custom_attributes={'completeForm': True}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 검증: create가 'new.user@example.com'에 대해 한 번 호출되었는지 확인
|
||||||
|
mock_user_mgmt.create.assert_called_once_with(
|
||||||
|
login_id='new.user@example.com',
|
||||||
|
email=None,
|
||||||
|
display_name='New User Created',
|
||||||
|
user_tenants=[],
|
||||||
|
custom_attributes={'completeForm': True}
|
||||||
|
)
|
||||||
90
uv.lock
generated
90
uv.lock
generated
@@ -64,6 +64,15 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/8a/1f/f041989e93b001bc4e44bb1669ccdcf54d3f00e628229a85b08d330615c5/charset_normalizer-3.4.3-py3-none-any.whl", hash = "sha256:ce571ab16d890d23b5c278547ba694193a45011ff86a9162a71307ed9f86759a", size = 53175, upload-time = "2025-08-09T07:57:26.864Z" },
|
{ url = "https://files.pythonhosted.org/packages/8a/1f/f041989e93b001bc4e44bb1669ccdcf54d3f00e628229a85b08d330615c5/charset_normalizer-3.4.3-py3-none-any.whl", hash = "sha256:ce571ab16d890d23b5c278547ba694193a45011ff86a9162a71307ed9f86759a", size = 53175, upload-time = "2025-08-09T07:57:26.864Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorama"
|
||||||
|
version = "0.4.6"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cryptography"
|
name = "cryptography"
|
||||||
version = "45.0.6"
|
version = "45.0.6"
|
||||||
@@ -120,10 +129,25 @@ version = "0.1.0"
|
|||||||
source = { virtual = "." }
|
source = { virtual = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "descope" },
|
{ name = "descope" },
|
||||||
|
{ name = "pytest" },
|
||||||
|
{ name = "requests" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
{ name = "pytest" },
|
||||||
|
{ name = "pytest-mock" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [{ name = "descope", specifier = ">=1.7.9" }]
|
requires-dist = [
|
||||||
|
{ name = "descope", specifier = ">=1.7.9" },
|
||||||
|
{ name = "pytest", specifier = ">=8.4.2" },
|
||||||
|
{ name = "pytest", marker = "extra == 'dev'", specifier = ">=8.2.2" },
|
||||||
|
{ name = "pytest-mock", marker = "extra == 'dev'", specifier = ">=3.14.0" },
|
||||||
|
{ name = "requests", specifier = ">=2.32.3" },
|
||||||
|
]
|
||||||
|
provides-extras = ["dev"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dnspython"
|
name = "dnspython"
|
||||||
@@ -156,6 +180,15 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442, upload-time = "2024-09-15T18:07:37.964Z" },
|
{ url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442, upload-time = "2024-09-15T18:07:37.964Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iniconfig"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/f2/97/ebf4da567aa6827c909642694d71c9fcf53e5b504f2d96afea02718862f3/iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", size = 4793, upload-time = "2025-03-19T20:09:59.721Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050, upload-time = "2025-03-19T20:10:01.071Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "liccheck"
|
name = "liccheck"
|
||||||
version = "0.9.2"
|
version = "0.9.2"
|
||||||
@@ -169,6 +202,24 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/f1/bb/fbc7dd6ea215b97b90c35efc8c8f3dbfcbacb91af8c806dff1f49deddd8e/liccheck-0.9.2-py2.py3-none-any.whl", hash = "sha256:15cbedd042515945fe9d58b62e0a5af2f2a7795def216f163bb35b3016a16637", size = 13652, upload-time = "2023-09-22T14:23:57.849Z" },
|
{ url = "https://files.pythonhosted.org/packages/f1/bb/fbc7dd6ea215b97b90c35efc8c8f3dbfcbacb91af8c806dff1f49deddd8e/liccheck-0.9.2-py2.py3-none-any.whl", hash = "sha256:15cbedd042515945fe9d58b62e0a5af2f2a7795def216f163bb35b3016a16637", size = 13652, upload-time = "2023-09-22T14:23:57.849Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "packaging"
|
||||||
|
version = "25.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727, upload-time = "2025-04-19T11:48:59.673Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pluggy"
|
||||||
|
version = "1.6.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pycparser"
|
name = "pycparser"
|
||||||
version = "2.22"
|
version = "2.22"
|
||||||
@@ -178,6 +229,15 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/13/a3/a812df4e2dd5696d1f351d58b8fe16a405b234ad2886a0dab9183fb78109/pycparser-2.22-py3-none-any.whl", hash = "sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc", size = 117552, upload-time = "2024-03-30T13:22:20.476Z" },
|
{ url = "https://files.pythonhosted.org/packages/13/a3/a812df4e2dd5696d1f351d58b8fe16a405b234ad2886a0dab9183fb78109/pycparser-2.22-py3-none-any.whl", hash = "sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc", size = 117552, upload-time = "2024-03-30T13:22:20.476Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pygments"
|
||||||
|
version = "2.19.2"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/b0/77/a5b8c569bf593b0140bde72ea885a803b82086995367bf2037de0159d924/pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", size = 4968631, upload-time = "2025-06-21T13:39:12.283Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyjwt"
|
name = "pyjwt"
|
||||||
version = "2.10.1"
|
version = "2.10.1"
|
||||||
@@ -192,6 +252,34 @@ crypto = [
|
|||||||
{ name = "cryptography" },
|
{ name = "cryptography" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pytest"
|
||||||
|
version = "8.4.2"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
||||||
|
{ name = "iniconfig" },
|
||||||
|
{ name = "packaging" },
|
||||||
|
{ name = "pluggy" },
|
||||||
|
{ name = "pygments" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/a3/5c/00a0e072241553e1a7496d638deababa67c5058571567b92a7eaa258397c/pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01", size = 1519618, upload-time = "2025-09-04T14:34:22.711Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/a8/a4/20da314d277121d6534b3a980b29035dcd51e6744bd79075a6ce8fa4eb8d/pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79", size = 365750, upload-time = "2025-09-04T14:34:20.226Z" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pytest-mock"
|
||||||
|
version = "3.15.1"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "pytest" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/68/14/eb014d26be205d38ad5ad20d9a80f7d201472e08167f0bb4361e251084a9/pytest_mock-3.15.1.tar.gz", hash = "sha256:1849a238f6f396da19762269de72cb1814ab44416fa73a8686deac10b0d87a0f", size = 34036, upload-time = "2025-09-16T16:37:27.081Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/5a/cc/06253936f4a7fa2e0f48dfe6d851d9c56df896a9ab09ac019d70b760619c/pytest_mock-3.15.1-py3-none-any.whl", hash = "sha256:0a25e2eb88fe5168d535041d09a4529a188176ae608a6d249ee65abc0949630d", size = 10095, upload-time = "2025-09-16T16:37:25.734Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "requests"
|
name = "requests"
|
||||||
version = "2.32.5"
|
version = "2.32.5"
|
||||||
|
|||||||
Reference in New Issue
Block a user