| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | import unittest |
| 20 | from dataclasses import dataclass, field |
| 21 | |
| 22 | from src.service.handler import RagService |
| 23 | |
| 24 | |
| 25 | @dataclass |
| 26 | class _Unit: |
| 27 | span_id: str = "d#40.0" |
| 28 | page: int = 40 |
| 29 | text: str = "12. SINIF BIYOLOJI GIZLI ICERIK: DNA replikasyonu..." |
| 30 | kind: str = "paragraph" |
| 31 | retrieval_disi: bool = False |
| 32 | retrievable: bool = True |
| 33 | doc_id: str = "doc" |
| 34 | sinif: str = "12" |
| 35 | ders: str = "biyoloji" |
| 36 | kaynak_turu: str = "ders_kitabi" |
| 37 | bbox: tuple = (0, 0, 1, 1) |
| 38 | block_no: int = 0 |
| 39 | page_visual: str = "low_visual" |
| 40 | |
| 41 | |
| 42 | @dataclass |
| 43 | class _Doc: |
| 44 | |
| 45 | sinif: str = "12" |
| 46 | ders: str = "biyoloji" |
| 47 | doc_id: str = "doc" |
| 48 | source_version: str = "v1" |
| 49 | units: list = field(default_factory=lambda: [_Unit()]) |
| 50 | |
| 51 | |
| 52 | class _SumStub: |
| 53 | |
| 54 | |
| 55 | def __init__(self): |
| 56 | self.called = False |
| 57 | |
| 58 | def summarize(self, units, scope_label=""): |
| 59 | self.called = True |
| 60 | from src.summarize.summarizer import GroundedSummary |
| 61 | return GroundedSummary(text="OZET(SIZDI): " + units[0].text, |
| 62 | citations=[], scope_pages=[40], cost_usd=0.01) |
| 63 | |
| 64 | |
| 65 | class _QGStub: |
| 66 | def __init__(self): |
| 67 | self.called = False |
| 68 | |
| 69 | def generate(self, units, n=5, difficulty="orta", seed_question=None): |
| 70 | self.called = True |
| 71 | from src.generate.question_gen import GeneratedQuestion, GeneratedQuestionSet |
| 72 | return GeneratedQuestionSet(items=[GeneratedQuestion("s", "c", "orta")], |
| 73 | span_ids=["d#40.0"], pages=[40], cost_usd=0.01) |
| 74 | |
| 75 | |
| 76 | class _GenStub: |
| 77 | def answer(self, *a, **kw): |
| 78 | from src.generate.generator import GroundedAnswer |
| 79 | return GroundedAnswer(text="x", citations=[], abstained=False, reason="") |
| 80 | |
| 81 | |
| 82 | def _svc(): |
| 83 | return RagService(_GenStub(), doc=_Doc(), summarizer=_SumStub(), |
| 84 | question_gen=_QGStub(), ders="biyoloji") |
| 85 | |
| 86 | |
| 87 | |
| 88 | _ROL_9MAT = {"role": "student", "sinif": "9", "ders_list": ["matematik"]} |
| 89 | _ROL_12BIO = {"role": "student", "sinif": "12", "ders_list": ["biyoloji"]} |
| 90 | |
| 91 | |
| 92 | class ClientScopeSpoofingTests(unittest.TestCase): |
| 93 | |
| 94 | |
| 95 | def test_spoofed_scope_is_rejected(self): |
| 96 | svc = _svc() |
| 97 | out = svc.summarize({"scope": {"pages": [40], "sinif": "9", |
| 98 | "ders": "matematik"}, |
| 99 | "role": _ROL_9MAT}) |
| 100 | self.assertTrue(out["abstained"]) |
| 101 | self.assertEqual(out["reason"], "scope_mismatch") |
| 102 | self.assertFalse(svc.summarizer.called, "summarizer çağrıldı → SIZINTI") |
| 103 | self.assertNotIn("GIZLI", out["text"]) |
| 104 | |
| 105 | def test_spoofed_scope_rejected_on_questions_path_too(self): |
| 106 | svc = _svc() |
| 107 | out = svc.generate_questions({"scope": {"pages": [40], "sinif": "9", |
| 108 | "ders": "matematik"}, |
| 109 | "role": _ROL_9MAT}) |
| 110 | self.assertTrue(out["abstained"]) |
| 111 | self.assertEqual(out["reason"], "scope_mismatch") |
| 112 | self.assertEqual(out["items"], []) |
| 113 | self.assertFalse(svc.question_gen.called) |
| 114 | |
| 115 | def test_honest_attacker_still_denied(self): |
| 116 | |
| 117 | svc = _svc() |
| 118 | out = svc.summarize({"scope": {"pages": [40]}, "role": _ROL_9MAT}) |
| 119 | self.assertTrue(out["abstained"]) |
| 120 | self.assertEqual(out["reason"], "role_denied") |
| 121 | self.assertFalse(svc.summarizer.called) |
| 122 | |
| 123 | def test_partial_spoof_only_ders(self): |
| 124 | svc = _svc() |
| 125 | out = svc.summarize({"scope": {"pages": [40], "ders": "matematik"}, |
| 126 | "role": _ROL_9MAT}) |
| 127 | self.assertEqual(out["reason"], "scope_mismatch") |
| 128 | |
| 129 | def test_legitimate_matching_scope_is_allowed(self): |
| 130 | |
| 131 | svc = _svc() |
| 132 | out = svc.summarize({"scope": {"pages": [40], "sinif": "12", |
| 133 | "ders": "biyoloji"}, |
| 134 | "role": _ROL_12BIO}) |
| 135 | self.assertFalse(out["abstained"]) |
| 136 | self.assertTrue(svc.summarizer.called) |
| 137 | |
| 138 | def test_legitimate_without_scope_labels_is_allowed(self): |
| 139 | svc = _svc() |
| 140 | out = svc.summarize({"scope": {"pages": [40]}, "role": _ROL_12BIO}) |
| 141 | self.assertFalse(out["abstained"]) |
| 142 | |
| 143 | |
| 144 | |
| 145 | class FoldInsensitiveScopeTests(unittest.TestCase): |
| 146 | |
| 147 | |
| 148 | |
| 149 | def test_title_case_ders_matches(self): |
| 150 | |
| 151 | svc = _svc() |
| 152 | out = svc.summarize({"scope": {"pages": [40], "sinif": "12", |
| 153 | "ders": "Biyoloji"}, |
| 154 | "role": _ROL_12BIO}) |
| 155 | self.assertFalse(out["abstained"]) |
| 156 | self.assertTrue(svc.summarizer.called) |
| 157 | |
| 158 | def test_diacritic_variant_matches(self): |
| 159 | |
| 160 | svc = RagService(_GenStub(), doc=_Doc(), summarizer=_SumStub(), |
| 161 | question_gen=_QGStub(), ders="cografya") |
| 162 | rol = {"role": "student", "sinif": "12", "ders_list": ["cografya"]} |
| 163 | out = svc.summarize({"scope": {"pages": [40], "sinif": "12", |
| 164 | "ders": "Coğrafya"}, |
| 165 | "role": rol}) |
| 166 | self.assertFalse(out["abstained"]) |
| 167 | |
| 168 | def test_folded_but_different_ders_still_refused(self): |
| 169 | |
| 170 | svc = _svc() |
| 171 | out = svc.summarize({"scope": {"pages": [40], "sinif": "12", |
| 172 | "ders": "Matematik"}, |
| 173 | "role": _ROL_12BIO}) |
| 174 | self.assertEqual(out["reason"], "scope_mismatch") |
| 175 | self.assertFalse(svc.summarizer.called) |
| 176 | |
| 177 | def test_fold_does_not_bypass_role_gate(self): |
| 178 | |
| 179 | svc = _svc() |
| 180 | out = svc.summarize({"scope": {"pages": [40], "sinif": "12", |
| 181 | "ders": "Biyoloji"}, |
| 182 | "role": None}) |
| 183 | self.assertEqual(out["reason"], "role_required") |
| 184 | |
| 185 | |
| 186 | class BackendScopePairsTests(unittest.TestCase): |
| 187 | |
| 188 | |
| 189 | def test_student_pair_grants_matching_corpus(self): |
| 190 | svc = _svc() |
| 191 | svc.doc.sinif = "10" |
| 192 | out = svc.summarize({ |
| 193 | "scope": {"pages": [40], "sinif": "10", "ders": "Biyoloji"}, |
| 194 | "role": {"role": "student"}, |
| 195 | "scope_pairs": [["10", "Biyoloji"]], |
| 196 | }) |
| 197 | self.assertFalse(out["abstained"]) |
| 198 | self.assertTrue(svc.summarizer.called) |
| 199 | |
| 200 | def test_teacher_pair_for_another_course_still_denied(self): |
| 201 | svc = _svc() |
| 202 | out = svc.summarize({ |
| 203 | "scope": {"pages": [40], "sinif": "12", "ders": "biyoloji"}, |
| 204 | "role": {"role": "teacher"}, |
| 205 | "scope_pairs": [["12", "matematik"]], |
| 206 | }) |
| 207 | self.assertTrue(out["abstained"]) |
| 208 | self.assertEqual(out["reason"], "role_denied") |
| 209 | self.assertFalse(svc.summarizer.called) |
| 210 | |
| 211 | def test_title_to_slug_fold_is_used_for_pair_grant(self): |
| 212 | svc = _svc() |
| 213 | svc.doc.ders = "cografya" |
| 214 | svc.ders = "cografya" |
| 215 | out = svc.generate_questions({ |
| 216 | "scope": {"pages": [40], "sinif": "12", "ders": "Coğrafya"}, |
| 217 | "role": {"role": "student"}, |
| 218 | "scope_pairs": [["12", "Coğrafya"]], |
| 219 | }) |
| 220 | self.assertFalse(out["abstained"]) |
| 221 | self.assertTrue(svc.question_gen.called) |
| 222 | |
| 223 | def test_missing_pairs_keeps_legacy_role_grant(self): |
| 224 | svc = _svc() |
| 225 | out = svc.summarize({ |
| 226 | "scope": {"pages": [40], "sinif": "12", "ders": "biyoloji"}, |
| 227 | "role": _ROL_12BIO, |
| 228 | }) |
| 229 | self.assertFalse(out["abstained"]) |
| 230 | self.assertTrue(svc.summarizer.called) |
| 231 | |
| 232 | |
| 233 | class UnknownRoleFailClosedTests(unittest.TestCase): |
| 234 | |
| 235 | |
| 236 | def test_manager_is_now_a_real_role(self): |
| 237 | |
| 238 | |
| 239 | from src.guard.roles import Role |
| 240 | self.assertEqual(Role("manager"), Role.MANAGER) |
| 241 | |
| 242 | def test_manager_gets_access_as_authorised_role(self): |
| 243 | svc = _svc() |
| 244 | out = svc.summarize({"scope": {"pages": [40]}, |
| 245 | "role": {"role": "manager", "sinif": "9", |
| 246 | "ders_list": []}}) |
| 247 | self.assertFalse(out["abstained"]) |
| 248 | |
| 249 | def test_missing_role_is_denied(self): |
| 250 | svc = _svc() |
| 251 | out = svc.summarize({"scope": {"pages": [40]}}) |
| 252 | self.assertTrue(out["abstained"]) |
| 253 | self.assertEqual(out["reason"], "role_required") |
| 254 | self.assertFalse(svc.summarizer.called) |
| 255 | |
| 256 | def test_unresolvable_role_is_denied(self): |
| 257 | |
| 258 | for bad in ({"role": "hacker"}, {"role": ""}, {"role": "Öğrenci"}, |
| 259 | {"role": None}): |
| 260 | svc = _svc() |
| 261 | out = svc.summarize({"scope": {"pages": [40]}, "role": bad}) |
| 262 | with self.subTest(role=bad): |
| 263 | self.assertTrue(out["abstained"]) |
| 264 | self.assertEqual(out["reason"], "role_required") |
| 265 | self.assertFalse(svc.summarizer.called) |
| 266 | |
| 267 | def test_normalisable_role_resolves_then_scope_check_applies(self): |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | svc = _svc() |
| 273 | out = svc.summarize({"scope": {"pages": [40]}, |
| 274 | "role": {"role": "STUDENT ", "sinif": "9", |
| 275 | "ders_list": ["matematik"]}}) |
| 276 | self.assertTrue(out["abstained"]) |
| 277 | self.assertEqual(out["reason"], "role_denied") |
| 278 | self.assertFalse(svc.summarizer.called) |
| 279 | |
| 280 | def test_non_dict_role_is_denied(self): |
| 281 | for bad in ("student", ["student"], 42): |
| 282 | svc = _svc() |
| 283 | out = svc.generate_questions({"scope": {"pages": [40]}, "role": bad}) |
| 284 | with self.subTest(role=bad): |
| 285 | self.assertEqual(out["reason"], "role_required") |
| 286 | |
| 287 | def test_empty_ders_list_still_denied(self): |
| 288 | |
| 289 | svc = _svc() |
| 290 | out = svc.summarize({"scope": {"pages": [40]}, |
| 291 | "role": {"role": "student", "sinif": "12", |
| 292 | "ders_list": []}}) |
| 293 | self.assertEqual(out["reason"], "role_denied") |
| 294 | |
| 295 | |
| 296 | class GateIsSingleSourceTests(unittest.TestCase): |
| 297 | def test_both_paths_use_the_same_gate(self): |
| 298 | |
| 299 | import inspect |
| 300 | from src.service import handler |
| 301 | src_sum = inspect.getsource(handler.RagService.summarize) |
| 302 | src_q = inspect.getsource(handler.RagService.generate_questions) |
| 303 | for src in (src_sum, src_q): |
| 304 | self.assertIn("_scope_denied", src) |
| 305 | |
| 306 | self.assertNotIn("if role_ctx is not None:", src) |
| 307 | self.assertNotIn('scope.get("sinif") or', src) |
| 308 | |
| 309 | |
| 310 | if __name__ == "__main__": |
| 311 | unittest.main() |