def check_user(self): u = User() u.set_user_id('foo') db = UserDB() self.test_stmt("db.add_user(u)") self.test_val("db.get_user('foo')", u) self.test_exc("db.add_user(u)", KeyError) self.test_stmt("db.remove_user('foo')") self.test_stmt("db.add_user(u)") self.test_val("db.get_user('foo')", u)
def add_real_data(db): for group in ['admin', 'staff', 'users']: if not db.usergroup_db.has_key(group): ug = UserGroup(group) db.usergroup_db.add_usergroup(ug) group_admin = db.usergroup_db['admin'] group_staff = db.usergroup_db['staff'] group_users = db.usergroup_db['users'] for (user_id, group_id, name, email) in [ ('admin', 'admin', "Administrative User", "*****@*****.**"), ('jimc', 'users', "Jim Carroll", "*****@*****.**"), ]: if not db.user_db.has_key(user_id): user = User(user_id) user.set_password(user_id) user.contact_name = name user.add_email(email) user.add_to_group(db.usergroup_db[group_id]) db.user_db.add_user(user) admin = db.user_db['admin'] admin.add_to_group(group_staff) admin.add_to_group(group_users) jimc = db.user_db['jimc'] jimc.add_to_group(group_staff) # set database owners db.user_db.set_owner(admin) db.user_db.set_owning_group(group_admin) db.user_db.set_group_perms(['read']) db.user_db.set_other_perms(['read']) db.usergroup_db.set_owner(admin) db.usergroup_db.set_owning_group(group_admin) db.usergroup_db.set_group_perms(['read']) db.usergroup_db.set_other_perms(['read']) db.group_db.set_owner(admin) db.group_db.set_owning_group(group_admin) db.group_db.add_owning_group([group_staff, group_users]) db.group_db.set_group_perms(['read','write']) db.group_db.set_other_perms(['read']) # create initial groups import qon.group_db qon.group_db.create_initial_groups()
def check_HasOwnership(self): self.test_stmt("HasOwnership()") ho = HasOwnership() foo = User() foo.set_user_id('foo') staff = UserGroup() staff.set_group_id('staff') self.test_stmt("ho.add_owner(foo)") self.test_stmt("ho.add_owning_group(staff)") self.test_stmt("ho.set_owner_perms(['read', 'write'])") self.test_stmt("ho.set_group_perms('read')") self.test_exc("ho.test_perm('fooble', User())", KeyError) u = User('foo') nobody = User('nobody') self.test_false("ho.can_read(u)") self.test_true("ho.can_read(foo)") self.test_false("ho.can_read(nobody)") self.test_false("ho.can_write(u)") self.test_true("ho.can_write(foo)") self.test_false("ho.can_write(nobody)") nobody.add_to_group(staff) self.test_true("ho.can_read(nobody)") self.test_false("ho.can_write(nobody)") self.test_stmt("ho.remove_owner(foo)") self.test_false("ho.can_read(foo)") self.test_false("ho.can_write(foo)") # check 'other' perms nobody = User() ho = HasOwnership() self.test_false("ho.can_read(nobody)") self.test_stmt("ho.set_other_perms('read')") self.test_true("ho.can_read(nobody)") self.test_false("ho.can_write(nobody)") self.test_stmt("ho.set_other_perms('write')") self.test_true("ho.can_write(nobody)") self.test_false("ho.can_manage(nobody)") self.test_stmt("ho.set_other_perms('manage')") self.test_true("ho.can_manage(nobody)")
def create_test_users(db, how_many, print_progress=False, sequential=False): """ Create a bunch of test users with userid tu______ and add them to the database howmany = how many users to attempt to create sequential = True to number the userids in sequence; otherwise, random userids are used """ # don't bother doing it if we already have enough users # if len(db.user_db) >= how_many: # return progress_interval = calculate_progress_interval(how_many) for x in range(1, how_many+1): if print_progress and (x % progress_interval == 0): print "Creating user %s of %s" % (x, how_many) if sequential: userid = "tu%s" % x if db.user_db.has_key(userid): continue else: user = User("tu%s" % x) else: user = User() while db.user_db.has_key(user.get_user_id()): user.generate_user_id("tu") user.set_password("password") user.contact_name = "%s Smith" % user.get_user_id() user.add_email("*****@*****.**" % user.get_user_id()) # add user to the 'users' UserGroup user.add_to_group(db.usergroup_db['users']) # add user to the database db.user_db.add_user(user) get_transaction().commit()