Files
Power BI DevandClaude Opus 5.5 602a9c98b9 Menu "Ganti password" menjadi "Pengaturan" + panduan menghubungkan akun Google
- Menu profil kini "Pengaturan" (ikon settings); halaman berjudul Pengaturan dengan
  kartu Akun Google di atas kartu Ganti password.
- Masuk dengan Google yang belum terhubung tidak lagi berupa galat merah: halaman login
  menampilkan pengumuman langkah (masuk NIP/NPM → Pengaturan → Hubungkan akun Google),
  dan masuk NIP/NPM dari halaman itu langsung diteruskan ke Pengaturan #akun-google.
- Uji: pengumuman, penerusan ke Pengaturan, label menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 08:44:23 +07:00

151 lines
8.6 KiB
Python

"""Uji login Google (salinan DB): tersembunyi tanpa env, hubungkan dari Akun, masuk dengan
Google, tautan otomatis via email, domain kampus, state/nonce, satu Google satu akun, putus.
Jawaban endpoint token Google ditiru (tanpa jaringan)."""
import base64, json, os, sys, time
from urllib.parse import parse_qs, urlparse, unquote_plus
SP = os.path.dirname(os.path.abspath(__file__))
os.environ["DB_PATH"] = os.path.join(SP, "uji.db")
sys.path.insert(0, sys.argv[1])
from fastapi.testclient import TestClient
from app import main, db, auth, google_auth
gagal = []
def cek(ok, pesan):
print(("OK " if ok else "GAGAL"), pesan)
if not ok: gagal.append(pesan)
kap = next(p for p in db.daftar_pengguna() if p["role"] == "kaprodi")
dsn = next(p for p in db.daftar_pengguna() if p["role"] == "dosen")
for k in ("GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"):
os.environ.pop(k, None)
def klien(u=None):
c = TestClient(main.app)
if u:
t = auth.token_baru(); db.buat_sesi(u["id"], t); c.cookies.set("sesi", t)
return c
# 1) tanpa env: tombol tersembunyi, rute menolak
c = klien()
cek("Masuk dengan Google" not in c.get("/login").text, "tanpa GOOGLE_CLIENT_ID: tombol login Google tersembunyi")
cek("Hubungkan akun Google" not in klien(dsn).get("/akun").text, "tanpa env: kartu Akun Google tersembunyi")
cek("/login" in c.get("/auth/google", follow_redirects=False).headers["location"], "tanpa env: /auth/google dialihkan ke login")
os.environ["GOOGLE_CLIENT_ID"] = "uji.apps.googleusercontent.com"
os.environ["GOOGLE_CLIENT_SECRET"] = "rahasia-uji"
cek("Masuk dengan Google" in c.get("/login").text, "dengan env: tombol login Google tampil")
# tiruan Google: klaim id_token untuk kode berikutnya
KLAIM = {}
def tiru(code, redirect):
k = {"iss": "https://accounts.google.com", "aud": os.environ["GOOGLE_CLIENT_ID"],
"exp": time.time() + 300, "email_verified": True, **KLAIM}
bagian = base64.urlsafe_b64encode(json.dumps(k).encode()).decode().rstrip("=")
return {"id_token": f"x.{bagian}.y"}
google_auth._minta_token = tiru
def alur(c, tujuan="masuk", **klaim):
"""Jalankan /auth/google → callback; kembalikan respons callback."""
r = c.get(f"/auth/google?tujuan={tujuan}", follow_redirects=False)
if "accounts.google.com" not in r.headers.get("location", ""):
return r
q = parse_qs(urlparse(r.headers["location"]).query)
KLAIM.clear(); KLAIM.update({"nonce": q["nonce"][0], **klaim})
return c.get(f"/auth/google/callback?code=abc&state={q['state'][0]}", follow_redirects=False)
# 2) URL otorisasi
r = c.get("/auth/google", follow_redirects=False)
q = parse_qs(urlparse(r.headers["location"]).query)
cek(q["redirect_uri"][0].endswith("/auth/google/callback") and q["hd"][0] == "untan.ac.id"
and q["scope"][0] == "openid email profile" and "g_oauth" in r.headers.get("set-cookie", ""),
"URL Google: redirect_uri, hd=untan.ac.id, scope dasar, cookie state")
# 3) belum terhubung & email tak terdaftar → ditolak, tidak membuat akun
n0 = len(db.daftar_pengguna(True))
r = alur(klien(), sub="g-asing", email="orang@informatika.untan.ac.id")
cek("/login" in r.headers["location"] and "belum terhubung" in unquote_plus(r.headers["location"])
and len(db.daftar_pengguna(True)) == n0, "email tak terdaftar: ditolak, tanpa akun baru")
cek("info=hubung" in r.headers["location"], "belum terhubung → login dengan pengumuman langkah menghubungkan")
h = klien().get(r.headers["location"]).text
cek("Akun Google belum terhubung" in h and "Pengaturan" in h and 'name="lanjut" value="pengaturan"' in h,
"pengumuman: langkah ke menu Pengaturan + form meneruskan ke Pengaturan")
r = klien().post("/login", data={"nip": dsn["username"], "password": dsn["username"], "lanjut": "pengaturan"},
follow_redirects=False)
cek(r.headers.get("location", "").startswith("/akun?") and r.headers["location"].endswith("#akun-google"),
"masuk NIP dari pengumuman → langsung ke Pengaturan #akun-google")
cek("Pengaturan" in klien(dsn).get("/akun").text and 'tabler-settings' in klien(dsn).get("/").text,
"menu profil & judul halaman bernama Pengaturan")
# 4) domain bukan kampus / email belum verifikasi
r = alur(klien(), sub="g-gmail", email="dosen@gmail.com")
cek("akun Google kampus" in unquote_plus(r.headers["location"]), "Gmail pribadi ditolak")
r = alur(klien(), sub="g-x", email="x@untan.ac.id.evil.com")
cek("akun Google kampus" in unquote_plus(r.headers["location"]), "domain tiruan (untan.ac.id.evil.com) ditolak")
r = alur(klien(), sub="g-nv", email="nv@untan.ac.id", email_verified=False)
cek("belum terverifikasi" in unquote_plus(r.headers["location"]), "email belum terverifikasi ditolak")
# 5) state & nonce
c5 = klien(); r = c5.get("/auth/google", follow_redirects=False)
r = c5.get("/auth/google/callback?code=abc&state=PALSU", follow_redirects=False)
cek("tidak valid" in unquote_plus(r.headers["location"]), "state tidak cocok ditolak")
r = alur(klien(), sub="g1", email="a@untan.ac.id", nonce="lain")
cek("kedaluwarsa" in unquote_plus(r.headers["location"]), "nonce tidak cocok ditolak")
r = klien().get("/auth/google/callback?error=access_denied&state=x", follow_redirects=False)
cek("dibatalkan" in unquote_plus(r.headers["location"]), "pengguna membatalkan di Google")
# 6) hubungkan dari menu Akun (sudah login), lalu masuk dengan Google
cd = klien(dsn)
cek("Hubungkan akun Google" in cd.get("/akun").text, "Akun: tombol Hubungkan tampil")
r = alur(cd, "hubung", sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek(r.headers["location"].startswith("/akun?pesan=") and db.pengguna_by_id(dsn["id"])["google_sub"] == "g-dosen",
"hubungkan: sub tersimpan di akun dosen")
cek(db.pengguna_by_id(dsn["id"])["email"] in ("dosen.uji@informatika.untan.ac.id", dsn.get("email") or "x"),
"hubungkan: email akun terisi bila sebelumnya kosong")
cn = klien(); r = alur(cn, sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek(r.status_code == 303 and r.headers["location"] == "/" and "sesi=" in r.headers.get("set-cookie", ""),
"masuk dengan Google → sesi dosen")
cek(cn.get("/akun").status_code == 200 and dsn["nama"] in cn.get("/akun").text, "sesi Google = akun dosen yang benar")
cek(klien().get("/auth/google?tujuan=hubung", follow_redirects=False).headers["location"].startswith("/login"),
"hubung tanpa login → diminta masuk dulu")
# 7) satu akun Google hanya untuk satu akun aplikasi
r = alur(klien(kap), "hubung", sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek("sudah terhubung ke akun lain" in unquote_plus(r.headers["location"])
and db.pengguna_by_id(kap["id"])["google_sub"] == "", "Google yang sama tidak bisa ditautkan ke akun kedua")
# 8) tautan otomatis lewat email terdaftar (login pertama)
db.ubah_dosen(kap["id"], email="kaprodi.uji@untan.ac.id")
r = alur(klien(), sub="g-kap", email="Kaprodi.Uji@untan.ac.id")
cek(r.headers["location"] == "/" and db.pengguna_by_id(kap["id"])["google_sub"] == "g-kap",
"email terdaftar (beda huruf besar/kecil) → tertaut otomatis & masuk")
# 9) akun nonaktif tidak bisa masuk dengan Google
db.ubah_dosen(dsn["id"], aktif=False)
r = alur(klien(), sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek("/login" in r.headers["location"], "akun nonaktif: masuk dengan Google ditolak")
db.ubah_dosen(dsn["id"], aktif=True)
# 10) putus (pengguna) & putus oleh kaprodi
cd = klien(dsn)
cd.post("/akun/google/putus", follow_redirects=False)
cek(not db.google_terhubung(db.pengguna_by_id(dsn["id"])), "pengguna memutus akun Google sendiri")
r = alur(klien(), sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek("/login" in r.headers["location"], "diputus pemilik: Google lama tidak tertaut otomatis lagi")
cek("Hubungkan akun Google" in klien(dsn).get("/akun").text, "diputus pemilik: Akun kembali menawarkan Hubungkan")
r = alur(klien(dsn), "hubung", sub="g-dosen", email="dosen.uji@informatika.untan.ac.id")
cek(db.google_terhubung(db.pengguna_by_id(dsn["id"])), "pemilik bisa menghubungkan lagi secara sengaja")
ck = klien(kap)
cek("terhubung" in ck.get("/master/dosen").text, "Data Master Dosen: lencana Google terhubung tampil")
ck.post(f"/master/dosen/{kap['id']}", data={"aksi": "putus_google"}, follow_redirects=False)
cek(db.pengguna_by_id(kap["id"])["google_sub"] == "", "kaprodi memutus tautan Google dari Data Master")
r = alur(klien(), sub="g-kap2", email="kaprodi.uji@untan.ac.id")
cek(db.pengguna_by_id(kap["id"])["google_sub"] == "g-kap2", "diputus kaprodi: tautan otomatis via email kembali boleh")
# 11) login NIP tetap jalan
r = klien().post("/login", data={"nip": kap["username"], "password": kap["username"]}, follow_redirects=False)
cek(r.status_code in (200, 303), "login NIP/password tetap tersedia")
print("TOTAL GAGAL:", len(gagal))
sys.exit(1 if gagal else 0)