forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits

test_demo_school.py190 lines, 8.2 KiBHistoryRawDownload
1"""Sahte okul — backend biçiminde tam veri kümesi + yetki taklidi.
2
3NEDEN YETKİ TAKLİDİ ŞART: sahte okuyucu her kullanıcıya her şeyi döndürseydi
4kasa izolasyonu testlerimiz **hiçbir şey ölçmezdi** — gerçek bir sızıntı olsa
5da yeşil kalırlardı. Bu yüzden `FakeSchool` backend'in görünürlük kurallarını
6taklit eder ve bu dosya o taklidin kendisini sınar.
7"""
8import os
9import unittest
10
11from src.bridge.student import build_context
12from src.demo.school import School, FakeSchool, build_school
13from src.guard.roles import can_access
14
15_VAR = os.path.isdir(os.path.join("data", "lise", "10"))
16
17
18@unittest.skipUnless(_VAR, "data/lise/10 yok")
19class OkulKurmaTests(unittest.TestCase):
20 @classmethod
21 def setUpClass(cls):
22 cls.o = build_school()
23 cls.s = FakeSchool(cls.o)
24
25 def _uid(self, username):
26 return self.o.user_by_name(username)["id"]
27
28 def test_school_has_every_record_kind(self):
29 summary = self.o.summary()
30 for alan in ("user_by_name", "sube", "uyelik", "ders", "kayit",
31 "kisisel_not", "ders_notu", "veli_bagi"):
32 self.assertGreater(summary[alan], 0, f"{alan} bos")
33
34 def test_roles_cover_the_product_hierarchy(self):
35 roller = {u["role"] for u in self.o.users}
36 self.assertEqual(roller, {"student", "teacher", "parent"})
37
38 def test_ids_are_deterministic(self):
39 self.assertEqual(self.o.to_dict()["users"], build_school().to_dict()["users"])
40
41 def test_every_enrollment_points_at_a_real_course_and_user(self):
42 kurslar = {c["id"] for c in self.o.courses}
43 kisiler = {u["id"] for u in self.o.users}
44 for e in self.o.enrollments:
45 self.assertIn(e["course"], kurslar)
46 self.assertIn(e["user"], kisiler)
47
48 def test_every_course_note_points_at_a_real_course(self):
49 kurslar = {c["id"] for c in self.o.courses}
50 for n in self.o.course_notes:
51 self.assertIn(n["course"], kurslar)
52
53 def test_sections_differ_so_isolation_is_measurable(self):
54 """Tek ders kümesi olsaydı 'aynı sınıf, farklı ders' durumu hiç
55 kurulmaz ve izolasyonun ders boyutu ölçülemezdi."""
56 a = build_context(self.s, self._uid("ogrenci1")).dersler # 10-A
57 b = build_context(self.s, self._uid("ogrenci2")).dersler # 10-B
58 self.assertNotEqual(set(a), set(b))
59 self.assertTrue(set(a) & set(b), "şubeler hiç örtüşmüyor")
60
61
62@unittest.skipUnless(_VAR, "data/lise/10 yok")
63class GorunurlukTests(unittest.TestCase):
64 @classmethod
65 def setUpClass(cls):
66 cls.o = build_school()
67 cls.s = FakeSchool(cls.o)
68
69 def _uid(self, username):
70 return self.o.user_by_name(username)["id"]
71
72 def test_ai_principal_alone_sees_nothing(self):
73 """`on_behalf_of` yoksa istek servisin kendi (`ai`) kimliğiyle koşar ve
74 `ai` okul verisini göremez — backend'in kendi ifadesi."""
75 for path in ("/users/me", "/classes", "/courses", "/notes"):
76 self.assertEqual(self.s.get(path)[0], 403, path)
77
78 def test_student_sees_only_their_own_notes(self):
79 a = build_context(self.s, self._uid("ogrenci1"))
80 b = build_context(self.s, self._uid("ogrenci2"))
81 self.assertTrue(a.notlar)
82 self.assertTrue(b.notlar)
83 self.assertEqual({n.id for n in a.notlar} & {n.id for n in b.notlar}, set())
84
85 def test_student_cannot_read_a_course_they_are_not_in(self):
86 b = build_context(self.s, self._uid("ogrenci2")) # 10-B
87 a_dersleri = {c["id"] for c in self.o.courses
88 if c["title"] == "Biyoloji"}
89 for kurs in a_dersleri:
90 self.assertEqual(
91 self.s.get("/course-notes", query=f"course={kurs}",
92 on_behalf_of=b.user_id)[0], 403)
93
94 def test_vault_isolation_follows_enrollment_not_just_grade(self):
95 """İki öğrenci de 10. sınıfta ama farklı derslerde — sınıf eşitse bile
96 ders kayıtsızsa erişim REDDEDİLMELİ."""
97 a = build_context(self.s, self._uid("ogrenci1")).role_context()
98 b = build_context(self.s, self._uid("ogrenci2")).role_context()
99 self.assertTrue(can_access(a, sinif="10", ders="biyoloji"))
100 self.assertFalse(can_access(b, sinif="10", ders="biyoloji"))
101 self.assertTrue(can_access(b, sinif="10", ders="fl-kimya"))
102 self.assertFalse(can_access(a, sinif="10", ders="fl-kimya"))
103
104 def test_no_student_can_reach_another_grade(self):
105 for kul in ("ogrenci1", "ogrenci2"):
106 ctx = build_context(self.s, self._uid(kul)).role_context()
107 for sinif in ("9", "11", "12"):
108 self.assertFalse(can_access(ctx, sinif=sinif, ders="biyoloji"),
109 f"{kul} sinif {sinif}'e erisebildi")
110
111 def test_teacher_sees_their_own_section(self):
112 t = build_context(self.s, self._uid("ogretmen1"))
113 self.assertEqual(t.rol, "teacher")
114 self.assertEqual(t.sinif, "10")
115 self.assertTrue(t.dersler)
116
117 def test_parent_path_is_explicitly_unimplemented(self):
118 """Veli yolu KURULMADI ve bu test onu kayda geçirir.
119
120 `parent_links` üretiliyor ama veli hiçbir şey göremiyor. Sebep:
121 velinin çocuğunun verisine hangi uçtan eriştiği backend'de henüz
122 doğrulanmadı. Uydurulmuş bir veli görünürlüğü, üzerine kurulacak her
123 izolasyon ölçümünü sessizce geçersiz kılardı. Veli yolu açıldığında
124 bu test DEĞİŞMELİ (BL-011)."""
125 veli = self._uid("veli1")
126 self.assertTrue(self.o.parent_links)
127 for path in ("/classes", "/courses", "/notes"):
128 durum, govde = self.s.get(path, on_behalf_of=veli)
129 self.assertEqual(durum, 200)
130 self.assertEqual(govde["total"], 0, f"{path} veli icin dolu geldi")
131
132 def test_unknown_user_is_404_not_an_open_door(self):
133 self.assertEqual(self.s.get("/users/me", on_behalf_of="01YOK")[0], 404)
134
135 def test_course_notes_without_course_param_is_a_bad_request(self):
136 self.assertEqual(
137 self.s.get("/course-notes", on_behalf_of=self._uid("ogrenci1"))[0], 400)
138
139 def test_reads_are_recorded_with_the_identity_used(self):
140 s = FakeSchool(self.o)
141 uid = self._uid("ogrenci1")
142 build_context(s, uid)
143 self.assertTrue(s.calls)
144 for _path, _q, obo in s.calls:
145 self.assertEqual(obo, uid)
146
147
148@unittest.skipUnless(_VAR, "data/lise/10 yok")
149class DiskeYazmaTests(unittest.TestCase):
150 def test_writes_full_dataset_and_per_user_snapshots(self):
151 import tempfile
152 from src.bridge.client import FakeReader
153 from src.demo.school import write
154 o = build_school()
155 with tempfile.TemporaryDirectory() as d:
156 yazilan = write(o, d)
157 self.assertIn(os.path.join(d, "okul.json"), yazilan)
158 anlik = os.path.join(d, "okuma-ogrenci1.json")
159 self.assertTrue(os.path.isfile(anlik))
160 # anlık görüntüden kurulan bağlam, canlı sahteyle AYNI olmalı
161 b1 = build_context(FakeReader.from_snapshot(anlik),
162 o.user_by_name("ogrenci1")["id"])
163 b2 = build_context(FakeSchool(o), o.user_by_name("ogrenci1")["id"])
164 self.assertEqual(b1.dersler, b2.dersler)
165 self.assertEqual([n.id for n in b1.notlar], [n.id for n in b2.notlar])
166
167
168class KorpussuzTests(unittest.TestCase):
169 def test_refuses_a_grade_with_no_corpus(self):
170 """Korpusta veri yoksa senaryo UYDURULMAZ, hata verilir.
171
172 DİKKAT: bu test önce `sinif="11"` kullanıyordu ve "bu makinede 11.
173 sınıf verisi yok" gibi **geçici bir ortam gerçeğini** sözleşme sanıyordu.
174 EBA indirmesi 11. sınıfı getirince test kırıldı. Artık boş bir geçici
175 kök kullanılıyor: koşul veriden bağımsız."""
176 import tempfile
177 with tempfile.TemporaryDirectory() as bos:
178 with self.assertRaises(ValueError):
179 build_school(kok=bos, sinif="10")
180
181 def test_refuses_an_out_of_band_grade(self):
182 with self.assertRaises(ValueError):
183 build_school(sinif="13")
184
185 def test_empty_school_summary_is_all_zero(self):
186 self.assertEqual(School(slug="x").summary()["user_by_name"], 0)
187
188
189if __name__ == "__main__":
190 unittest.main()