Testing Authentication Systems
1. Understanding Authentication Testing
| Type | Detail |
|---|---|
| Unit | Functions (hash, token verify) |
| Integration | Login flow end-to-end |
| Security | Pen test, fuzzing |
| Property-based | Invariants under random input |
2. Implementing Login Testing
Example: Playwright
test("login + protected route", async ({ page }) => {
await page.goto("/login");
await page.fill("[name=email]", "test@example.com");
await page.fill("[name=password]", process.env.TEST_PWD);
await page.click("button[type=submit]");
await expect(page).toHaveURL("/dashboard");
await expect(page.locator("h1")).toContainText("Welcome");
});
3. Testing Password Reset Flow
| Case | Expected |
|---|---|
| Valid email | 200, email sent |
| Unknown email | 200 (no enumeration) |
| Expired token | 410 or error page |
| Reused token | 410 |
| Weak new password | 422 |
4. Implementing Session Testing
| Scenario | Verify |
|---|---|
| Idle timeout | Session expires after N min |
| Logout | Token invalidated, cookie cleared |
| Concurrent sessions | Per policy (allow / single) |
| Session fixation | New ID issued post-login |
5. Testing Token Validation
| Test | Expected |
|---|---|
| Expired token | 401 |
| Tampered signature | 401 |
alg=none | Rejected |
| Wrong audience | 401 |
| Revoked token | 401 |
6. Implementing MFA Testing
| Test | Detail |
|---|---|
| Wrong code | Reject, counter++ |
| Replay | Used code rejected |
| Time skew | ±1 step tolerated |
| Backup codes | One-time use |
7. Testing Authorization Rules
Example: Jest authz tests
describe("orders authorization", () => {
it("owner can read own order", async () => {
expect(await can(owner, "read", order)).toBe(true);
});
it("other user cannot read", async () => {
expect(await can(stranger, "read", order)).toBe(false);
});
it("admin can read any", async () => {
expect(await can(admin, "read", order)).toBe(true);
});
});
8. Using Security Testing Tools
| Tool | Purpose |
|---|---|
| OWASP ZAP | DAST |
| Burp Suite | Manual + auto pentest |
| SQLMap | SQL injection |
| Nuclei | Template-based vuln scanning |
| Semgrep / CodeQL | SAST |
9. Implementing Automated Security Scans
| CI Stage | Tool |
|---|---|
| Pre-commit | gitleaks, secret scan |
| Build | SAST (Semgrep, Snyk Code) |
| After deploy | DAST (ZAP), Nuclei |
| Runtime | RASP, anomaly detection |