def register(): payload = flask.request.get_json() registration_pw = payload.pop("registration_password") if registration_pw != os.environ.get("REGISTRATION_PASSWORD", ""): raise InvalidUsage("Registration password incorrect", 400, payload) user = models.User(email=payload["email"]) if user.email: raise InvalidUsage(f"User {user.email} already exists", 400, payload) try: user.update(payload) except (AttributeError, ValueError) as error: raise InvalidUsage(str(error), 400, payload) return flask.jsonify({"success": True})
def test_user(mock_mongo): assert db.collections.users.count_documents({}) == 1 user = models.User() user.email = "*****@*****.**" user.password = "******" assert user.password != "hellohello" assert user.check_password("hellohello") assert not user.check_password("foobar") assert user.get_id() is None user.save() assert db.collections.users.count_documents({}) == 2 export = dict(user) assert not [k for k in export.keys() if k.startswith("_")] assert not db.collections.users.count_documents({"admin": True}) user2 = models.User(email="*****@*****.**") user2.admin = True assert not isinstance(user2._id, str) assert isinstance(user2.get_id(), str) user2.save() assert db.collections.users.count_documents({"admin": True})
def login(): payload = flask.request.get_json() if not payload: user = flask_login.current_user voter_id = flask.request.cookies.get("voter_id") or str(uuid.uuid1()) response = flask.jsonify({ "is_authenticated": user.is_authenticated, "email": getattr(user, "email", False), "voter_id": voter_id, }) if not flask.request.cookies.get("voter_id"): response.set_cookie("voter_id", voter_id) return response user = models.User(email=payload["email"]) if not user.email or not user.check_password(payload["password"]): raise InvalidUsage("Incorrect email or password") flask_login.login_user(user, remember=payload.get("remember", False)) return flask.jsonify({"success": True})
def delete_test_user(): if "MONGO_URL" not in os.environ: user = models.User(email="*****@*****.**") user.delete() return "", 204
def load_user(identifier): user = models.User(_id=identifier) return user if user.email else None