Files
dbx-main/tests/test_project_meeting_board.py
king 46dc038e26 fix(project): show OAuth error code when meeting board token refresh fails
RefreshError showed only a generic message; include Google's error code
and explain client ID/secret mismatch (unauthorized_client).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 12:59:33 +09:00

122 lines
5.3 KiB
Python

"""회의록 보드(Google 슬라이드) — 가짜 Drive·Slides 로 찾기·빈 슬라이드 만들기·API 를 검증한다. 네트워크·DB 없이 실행."""
import importlib
import unittest
from datetime import date
from unittest.mock import patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.integrations import google_slides as gs
api = importlib.import_module("app.modules.project.router")
class Call:
def __init__(self, result):
self.result = result
def execute(self):
return self.result
class FakeDrive:
def __init__(self, existing=None):
self.existing, self.created, self.queries = existing, [], []
def files(self):
return self
def list(self, **kw):
self.queries.append(kw)
return Call({"files": [{"id": self.existing}] if self.existing else []})
def create(self, body, fields, supportsAllDrives):
assert supportsAllDrives is True
self.created.append(body)
return Call({"id": "new-file"})
class FakeSlides:
def __init__(self):
self.batches = []
def presentations(self):
return self
def get(self, presentationId, fields):
return Call({"slides": [{"objectId": "title-slide"}]})
def batchUpdate(self, presentationId, body):
self.batches.append((presentationId, body))
return Call({})
def client_with(drive, slides):
c = gs.GoogleSlidesClient.__new__(gs.GoogleSlidesClient)
c.enabled, c.reason, c.auth_mode = True, "", "oauth"
c._drive, c._slides = drive, slides
return c
class MeetingBoardTests(unittest.TestCase):
def test_existing_board_is_reused_without_creating(self):
drive, slides = FakeDrive(existing="abc"), FakeSlides()
out = client_with(drive, slides).open_week_board(folder_id="F", week_key="2026-10-05", title="10월 2주차 (10/5~10/11)")
self.assertEqual(out, {"id": "abc", "url": "https://docs.google.com/presentation/d/abc/edit", "created": False})
self.assertEqual(drive.created, [])
q = drive.queries[0]["q"]
self.assertIn("'F' in parents", q)
self.assertIn("key='dbxMeetingWeek' and value='2026-10-05'", q)
self.assertTrue(drive.queries[0]["supportsAllDrives"] and drive.queries[0]["includeItemsFromAllDrives"])
def test_new_board_is_blank_slide_in_folder(self):
drive, slides = FakeDrive(), FakeSlides()
out = client_with(drive, slides).open_week_board(folder_id="F", week_key="2026-10-05", title="10월 2주차 (10/5~10/11)")
self.assertTrue(out["created"])
body = drive.created[0]
self.assertEqual(body["parents"], ["F"])
self.assertEqual(body["name"], "10월 2주차 (10/5~10/11)")
self.assertEqual(body["mimeType"], gs.PRESENTATION_MIME)
self.assertEqual(body["appProperties"], {"dbxMeetingWeek": "2026-10-05"})
# 제목·부제목 틀이 있는 첫 장은 지우고 틀 없는 빈 장(BLANK) 하나만
reqs = slides.batches[0][1]["requests"]
self.assertEqual(reqs[0]["createSlide"]["slideLayoutReference"], {"predefinedLayout": "BLANK"})
self.assertEqual(reqs[1:], [{"deleteObject": {"objectId": "title-slide"}}])
def test_disabled_client_and_api_errors_have_safe_messages(self):
off = gs.GoogleSlidesClient.__new__(gs.GoogleSlidesClient)
off.enabled, off.reason = False, "Google 인증 미설정"
with self.assertRaises(gs.GoogleSlidesError):
off.open_week_board(folder_id="F", week_key="2026-10-05", title="t")
self.assertIn("토큰을 다시 발급", gs._friendly(RuntimeError("invalid_scope: Bad Request")))
self.assertIn("OAuth 클라이언트", gs._friendly(RuntimeError("('unauthorized_client: Unauthorized', {'error': 'unauthorized_client'})")))
self.assertIn("만료·폐기", gs._friendly(RuntimeError("('invalid_grant: Bad Request', {})")))
def test_title_uses_month_week_and_period(self):
self.assertEqual(api.meeting_board_title(date(2026, 10, 5)), "10월 2주차 (10/5~10/11)")
self.assertEqual(api.meeting_board_title(date(2026, 11, 1)), "11월 1주차 (11/1~11/8)")
def test_api_returns_url_and_maps_errors(self):
app = FastAPI()
app.include_router(api.router)
client = TestClient(app)
fake = client_with(FakeDrive(existing="abc"), FakeSlides())
with patch.object(api, "_require_user", return_value={"email": "u@test"}), \
patch.object(gs, "get_client", return_value=fake):
r = client.post("/project/api/goals/meetings/2026-10-05/board")
self.assertEqual(r.status_code, 200, r.text)
self.assertEqual(r.json()["url"], "https://docs.google.com/presentation/d/abc/edit")
self.assertEqual(client.post("/project/api/goals/meetings/2026-10-06/board").status_code, 400) # 주차 시작일 아님
off = gs.GoogleSlidesClient.__new__(gs.GoogleSlidesClient)
off.enabled, off.reason = False, "Google 인증 미설정"
with patch.object(api, "_require_user", return_value={"email": "u@test"}), \
patch.object(gs, "get_client", return_value=off):
r = client.post("/project/api/goals/meetings/2026-10-05/board")
self.assertEqual(r.status_code, 503)
self.assertIn("Google 인증 미설정", r.json()["detail"])
if __name__ == "__main__":
unittest.main()