Lockout After Failed Logins
Implement a class LoginGuard that locks an account after limit failed logins in a row. Write these methods:
- LoginGuard(limit) starts with no failures and no locked accounts.
- attempt(user, ok) records a login by user; ok says whether the password was right.
- If the account is locked, nothing changes and it returns "locked".
- A right password sets the failures of user back to 0 and returns "welcome".
- A wrong password adds one failure. If user now has limit failures in a row, the account becomes locked and it returns "locked"; otherwise it returns "denied".
- unlock(user) unlocks a locked account, sets its failures back to 0 and returns true. If the account is not locked, nothing changes and it returns false.
- failures_of(user) returns the current failures in a row of user (0 for a user never seen).
- count_locked() returns how many accounts are locked.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
The right password after the first failure resets the count for amy, so it takes two more failures to lock the account. While it is locked even the right password is refused, until unlock.
Constraints
1 <= limit <= 4userhas1to5lowercase letters- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.