def login(self, credentials: dict) -> dict: schema = AdminLoginSchema() try: credentials = schema.load(credentials) storedAdmin = Admin.query.filter_by(username=credentials["username"]).first() if storedAdmin is None: return None isLogged = Utils.verify_password( storedAdmin.password[0], storedAdmin.password[1], credentials["password"] ) if not isLogged: return None data = AdminCreateSchema().dump(storedAdmin) tokens = Auth.generate_session_token("ADMIN", **data) except ValidationError as e: raise InvalidParametersError(e.data, e.messages, "Error occurred when trying to loggin admin", e) return tokens
def login(self, credentials: dict) -> dict: schema = UserLoginSchema() try: credentials = schema.load(credentials) storedUser = User.query.filter_by(email=credentials["email"]).first() if storedUser is None: return None isLogged = Utils.verify_password( storedUser.password[0], storedUser.password[1], credentials["password"] ) if not isLogged: return None data = UserCreateSchema().dump(storedUser) tokens = Auth.generate_session_token("USER", **data) except ValidationError as e: raise InvalidParametersError(e.data, e.messages, "Error occurred when trying to loggin user", e) return tokens
def test_generate_session_token(self): with patch("app.src.common.auth.Auth.session_token", return_value="session token"): with patch("app.src.common.auth.Auth.token_encode", return_value="token jwt"): with patch("app.src.common.auth.Auth.refresh_token", return_value="refresh token"): with patch("app.src.common.auth.Auth.save_session", return_value=None): tokens = Auth.generate_session_token( self.role, **self.data) self.assertIsNotNone(tokens) self.assertDictEqual( dict(token_id="token jwt", refresh_token="refresh token"), tokens)