"""会员服务:外链、协会会员档案、会员资讯、培训报名。""" import base64 import hashlib import uuid import pytest from fastapi.testclient import TestClient from app.core.config import get_settings from app.core.security import create_access_token @pytest.fixture def client(monkeypatch): monkeypatch.setenv("JWT_SECRET", "test-secret-must-be-32-characters!") monkeypatch.setenv("API_PAYLOAD_ENCRYPT", "false") key = base64.b64encode(hashlib.sha256(b"membership-field-key").digest()).decode("ascii") monkeypatch.setenv("FIELD_AES_KEY", key) get_settings.cache_clear() if not get_settings().database_url.strip(): pytest.skip("DATABASE_URL 为空") from app.main import create_app with TestClient(create_app()) as api: yield api get_settings.cache_clear() def _staff() -> dict[str, str]: return {"Authorization": f"Bearer {create_access_token('1', 'staff')}"} def _profile(email: str, **extra) -> dict: body = { "email": email, "password": "Member123", "kind": "individual", "personName": "张三", "membershipStatus": "active", "feeStatus": "unpaid", "mobile": "13800138000", "idCard": "620102199001011234", "appraiserRegNo": "1234567890", } body.update(extra) return body def _registration(**extra) -> dict: body = { "region": "甘肃省兰州市", "orgName": "示例估价公司", "personName": "李四", "gender": "female", "position": "估价师", "idCard": "620102199202023214", "mobile": "13900139000", "appraiserRegNo": "1098765432", "email": "attendee@example.com", "lodging": "association", "roomStandard": True, "standardCount": 1, "dine": True, "mealDates": ["7月29日", "8月1日"], } body.update(extra) return body def test_member_notices_follow_column_articles(client: TestClient): staff = _staff() created = [] draft_title = f"章程草稿{uuid.uuid4().hex[:6]}" try: empty = client.get("/api/site/about/articles/member_notices") assert empty.status_code == 200, empty.text draft = client.post( "/api/admin/home/articles", headers=staff, json={ "category": "member_notices", "title": draft_title, "published": False, "bodyHtml": "
未发布
", "attachmentFileIds": [], }, ) assert draft.status_code == 200, draft.text created.append(draft.json()["id"]) published = client.post( "/api/admin/home/articles", headers=staff, json={ "category": "member_notices", "title": f"协会章程{uuid.uuid4().hex[:6]}", "published": True, "bodyHtml": "会员权利
", "attachmentFileIds": [], }, ) assert published.status_code == 200, published.text created.append(published.json()["id"]) listed = client.get("/api/site/about/articles/member_notices") titles = [item["title"] for item in listed.json()["items"]] assert published.json()["id"] assert any(item["id"] == published.json()["id"] for item in listed.json()["items"]) assert draft_title not in titles detail = client.get(f"/api/site/home/articles/{published.json()['id']}") assert "会员权利" in detail.text assert "script" not in detail.text finally: for item_id in created: client.delete(f"/api/admin/home/articles/{item_id}", headers=staff) def test_new_member_password_is_short_and_optional(client: TestClient): staff = _staff() stamp = uuid.uuid4().hex[:8] simple = f"simple-{stamp}@example.com" letters = f"letters-{stamp}@example.com" blank = f"blank-{stamp}@example.com" created_ids: list[int] = [] try: short = client.post( "/api/admin/membership/profiles", headers=staff, json=_profile(simple, password="123456"), ) assert short.status_code == 200, short.text created_ids.append(short.json()["id"]) signed = client.post("/api/site/auth/login", json={"email": simple, "password": "123456"}) assert signed.status_code == 200, signed.text free = client.post( "/api/admin/membership/profiles", headers=staff, json=_profile(letters, password="abc"), ) assert free.status_code == 200, free.text created_ids.append(free.json()["id"]) signed = client.post("/api/site/auth/login", json={"email": letters, "password": "abc"}) assert signed.status_code == 200, signed.text too_long = client.post( "/api/admin/membership/profiles", headers=staff, json=_profile(f"long-{stamp}@example.com", password="12345678901"), ) assert too_long.status_code == 400 assert "10" in too_long.text omitted = _profile(blank) omitted.pop("password") defaulted = client.post("/api/admin/membership/profiles", headers=staff, json=omitted) assert defaulted.status_code == 200, defaulted.text created_ids.append(defaulted.json()["id"]) signed = client.post("/api/site/auth/login", json={"email": blank, "password": "123456"}) assert signed.status_code == 200, signed.text reset = client.put( f"/api/admin/membership/profiles/{created_ids[-1]}", headers=staff, json=_profile(blank, password="8888"), ) assert reset.status_code == 200, reset.text signed = client.post("/api/site/auth/login", json={"email": blank, "password": "8888"}) assert signed.status_code == 200, signed.text finally: for profile_id in created_ids: client.delete(f"/api/admin/membership/profiles/{profile_id}", headers=staff) def test_profile_syncs_portal_account_and_training_meal_dates_are_multi_select(client: TestClient): staff = _staff() stamp = uuid.uuid4().hex[:8] email = f"member-{stamp}@example.com" other = f"other-{stamp}@example.com" profile_id = None other_id = None event_id = None try: created = client.post("/api/admin/membership/profiles", headers=staff, json=_profile(email)) assert created.status_code == 200, created.text profile_id = created.json()["id"] assert created.json()["idCard"] == "620102199001011234" listed = client.get("/api/admin/membership/profiles", headers=staff, params={"keyword": email}) assert listed.status_code == 200, listed.text assert "620102199001011234" not in listed.text assert "**********" in listed.text login = client.post("/api/site/auth/login", json={"email": email, "password": "Member123"}) assert login.status_code == 200, login.text token = {"Authorization": f"Bearer {login.json()['accessToken']}"} own = client.get("/api/site/membership/profile", headers=token) assert own.status_code == 200, own.text assert own.json()["profile"]["personName"] == "张三" stranger = client.get("/api/site/membership/profile") assert stranger.status_code == 401 again = client.post( "/api/admin/membership/profiles", headers=staff, json=_profile(email, personName="不应再建"), ) assert again.status_code == 400 assert "已有会员档案" in again.text event = client.post( "/api/admin/membership/events", headers=staff, json={ "title": f"估价会议{stamp}", "place": "兰州", "startsOn": "2026-07-29", "endsOn": "2026-07-30", "quota": 1, "published": True, "mealNote": "午餐自助餐80元/位", "mealDates": ["7月29日", "8月1日"], "payeeName": "甘肃省房地产价格评估行业协会", "bankName": "招商银行兰州中央广场支行", "bankAccount": "9319000000000000", "bodyHtml": "会议通知
", }, ) assert event.status_code == 200, event.text event_id = event.json()["id"] assert event.json()["mealDates"] == ["7月29日", "8月1日"] hidden = client.get("/api/site/membership/events") assert any(item["id"] == event_id for item in hidden.json()["items"]) bad_date = client.post( f"/api/site/membership/events/{event_id}/registrations", headers=token, json=_registration(email=email, mealDates=["7月30日"]), ) assert bad_date.status_code == 400 assert "用餐日期" in bad_date.text submitted = client.post( f"/api/site/membership/events/{event_id}/registrations", headers=token, json=_registration(email=email, personName="张三", mealDates=["8月1日", "7月29日"]), ) assert submitted.status_code == 200, submitted.text assert submitted.json()["mealDates"] == ["8月1日", "7月29日"] mine = client.get("/api/site/membership/registrations", headers=token) assert mine.status_code == 200, mine.text assert mine.json()["items"][0]["eventId"] == event_id other_profile = client.post("/api/admin/membership/profiles", headers=staff, json=_profile(other, personName="王五")) assert other_profile.status_code == 200, other_profile.text other_id = other_profile.json()["id"] other_login = client.post("/api/site/auth/login", json={"email": other, "password": "Member123"}) other_token = {"Authorization": f"Bearer {other_login.json()['accessToken']}"} full = client.post( f"/api/site/membership/events/{event_id}/registrations", headers=other_token, json=_registration(email=other, personName="王五"), ) assert full.status_code == 400 assert "名额已满" in full.text other_mine = client.get("/api/site/membership/registrations", headers=other_token) assert other_mine.json()["items"] == [] roster = client.get(f"/api/admin/membership/events/{event_id}/registrations", headers=staff) assert roster.status_code == 200, roster.text assert len(roster.json()["items"]) == 1 assert "620102199202023214" not in roster.text finally: if event_id: client.delete(f"/api/admin/membership/events/{event_id}", headers=staff) if profile_id: client.delete(f"/api/admin/membership/profiles/{profile_id}", headers=staff) if other_id: client.delete(f"/api/admin/membership/profiles/{other_id}", headers=staff) for address in (email, other): listed = client.get("/api/admin/site-members", headers=staff, params={"email": address, "current": 1, "size": 10}) if listed.status_code != 200: continue for row in listed.json().get("records") or []: if row.get("email") == address: client.delete(f"/api/admin/site-members/{row['id']}", headers=staff)