def test_create(self): rolename = uuid.uuid4().hex role_id = Role.create(rolename).id db.session.remove() role = Role.query.filter_by(id=role_id).first() self.assertEqual(role.name, rolename) self.assertIs(Role.create(role), role) new_role = Role.create(rolename) self.assertEqual(new_role.id, role.id)
def test_roles(self): username = uuid.uuid4().hex password = uuid.uuid4().hex user = User.create(username, password) self.assertTrue(user.has_roles()) username = uuid.uuid4().hex password = uuid.uuid4().hex user = User(username=username, password=password) role_names = map(lambda i: uuid.uuid4().hex, range(3)) roles = [] for role_name in role_names: role = Role.create(role_name) user.roles.append(role) roles.append(role) db.session.add(user) db.session.commit() self.assertTrue(user.has_roles(allowed=[roles[0].name, roles[1].name])) self.assertTrue(user.has_roles(allowed=[roles[0].name, "foo"])) self.assertTrue(user.has_roles(allowed=["foo", roles[0].name])) self.assertFalse(user.has_roles(allowed=["foo"])) self.assertTrue( user.has_roles(required=[roles[0].name, roles[1].name])) self.assertFalse( user.has_roles(required=[roles[0].name, roles[1].name, "foo"]))
def test_roles(self): username = uuid.uuid4().hex password = uuid.uuid4().hex user = User.create(username, password) self.assertTrue(user.has_roles()) username = uuid.uuid4().hex password = uuid.uuid4().hex user = User(username=username, password=password) role_names = map(lambda i: uuid.uuid4().hex, range(3)) roles = [] for role_name in role_names: role = Role.create(role_name) user.roles.append(role) roles.append(role) db.session.add(user) db.session.commit() self.assertTrue( user.has_roles(allowed=[roles[0].name, roles[1].name])) self.assertTrue( user.has_roles(allowed=[roles[0].name, "foo"])) self.assertTrue( user.has_roles(allowed=["foo", roles[0].name])) self.assertFalse( user.has_roles(allowed=["foo"])) self.assertTrue( user.has_roles(required=[roles[0].name, roles[1].name])) self.assertFalse( user.has_roles(required=[roles[0].name, roles[1].name, "foo"]))
def setup_page(): form = NewUserForm(request.form) if request.method == "GET": # create the admin role if it does not exist and # find any existing administrators admin_role = Role.query.filter_by(name="admin").first() if admin_role is None: admin_role = Role.create("admin") admin_users = [] else: admin_users = admin_role.users return render_template("pyfarm/setup.html", form=form, admin_role=admin_role, admin_users=admin_users) elif request.method == "POST": if form.validate(): # make sure we're still the only admin admin_role = Role.query.filter_by(name="admin").first() if admin_role and admin_role.users.first(): return render_template( "pyfarm/setup.html", error="someone else created an administrator") user = User.create(request.form["username"], request.form["password"], email=request.form["email"]) user.roles.append(Role.create("admin")) db.session.add(user) db.session.commit() return render_template("pyfarm/setup.html", finished=True, redirect_seconds=5) return render_template("pyfarm/setup.html", form=form)
def setup_page(): form = NewUserForm(request.form) if request.method == "GET": # create the admin role if it does not exist and # find any existing administrators admin_role = Role.query.filter_by(name="admin").first() if admin_role is None: admin_role = Role.create("admin") admin_users = [] else: admin_users = admin_role.users return render_template( "pyfarm/setup.html", form=form, admin_role=admin_role, admin_users=admin_users) elif request.method == "POST": if form.validate(): # make sure we're still the only admin admin_role = Role.query.filter_by(name="admin").first() if admin_role and admin_role.users.first(): return render_template( "pyfarm/setup.html", error="someone else created an administrator") user = User.create( request.form["username"], request.form["password"], email=request.form["email"]) user.roles.append(Role.create("admin")) db.session.add(user) db.session.commit() return render_template( "pyfarm/setup.html", finished=True, redirect_seconds=5) return render_template("pyfarm/setup.html", form=form)
def test_active(self): rolename = uuid.uuid4().hex role = Role.create(rolename) role.active = False self.assertFalse(role.is_active()) role.active = True self.assertTrue(role.is_active()) role.expiration = datetime.now() - timedelta(days=1) self.assertFalse(role.is_active()) role.expiration = datetime.now() + timedelta(days=1) self.assertTrue(role.is_active()) role.expiration = datetime.now() + timedelta(seconds=.5) self.assertTrue(role.is_active()) time.sleep(1) self.assertFalse(role.is_active())
def setUp(self): super(TestLogin, self).setUp() # create a normal user self.normal_username = uuid.uuid4().hex self.normal_password = uuid.uuid4().hex self.normal_user = User.create(self.normal_username, self.normal_password) # create an admin self.admin_username = uuid.uuid4().hex self.admin_password = uuid.uuid4().hex self.admin_user = User.create(self.admin_username, self.admin_password) self.admin_user.roles.append(Role.create("admin")) # commit the changes to the database db.session.commit()
def setUp(self): super(TestLogin, self).setUp() # create a normal user self.normal_username = uuid.uuid4().hex self.normal_password = uuid.uuid4().hex self.normal_user = User.create( self.normal_username, self.normal_password) # create an admin self.admin_username = uuid.uuid4().hex self.admin_password = uuid.uuid4().hex self.admin_user = User.create( self.admin_username, self.admin_password) self.admin_user.roles.append(Role.create("admin")) # commit the changes to the database db.session.commit()