Refactor login and tests