refactor tests and redirect to login when state is missing or mismatched