class ChangeLocalUserPassword(unittest.TestCase): def setUp(self): self.facility = Facility(name="Test Facility") self.facility.save() self.group = FacilityGroup(facility=self.facility, name="Test Class") self.group.full_clean() self.group.save() self.user = FacilityUser(facility=self.facility, username="******", first_name="Firstname", last_name="Lastname", group=self.group) self.user.clear_text_password = "******" # not used anywhere but by us, for testing purposes self.user.set_password(self.user.clear_text_password) self.user.full_clean() self.user.save() def test_change_password(self): # Now, re-retrieve the user, to check. (out, err, val) = call_command_with_output("changelocalpassword", self.user.username, noinput=True) self.assertEquals(err, "", "no output on stderr") self.assertNotEquals(out, "", "some output on stderr") self.assertEquals(val, 0, "Exit code is zero") match = re.match( r"^.*Generated new password for user '([^']+)': '([^']+)'", out.replace("\n", ""), re.MULTILINE) self.assertFalse(match is None, "could not parse stdout") user = FacilityUser.objects.get(facility=self.facility, username=self.user.username) self.assertEquals(user.username, match.groups()[0], "Username reported correctly") self.assertTrue(user.check_password(match.groups()[1]), "New password works") self.assertFalse(user.check_password(self.user.clear_text_password), "NOT the old password") def test_no_user(self): fake_username = self.user.username + "xxxxx" #with self.assertRaises(FacilityUser.DoesNotExist): (out, err, val) = call_command_with_output("changelocalpassword", fake_username, noinput=True) self.assertNotIn("Generated new password for user", out, "Did not set password") self.assertNotEquals(err, "", "some output on stderr") match = re.match(r"^.*Error: user '([^']+)' does not exist$", err.replace("\n", ""), re.M) self.assertFalse(match is None, "could not parse stderr") self.assertEquals(match.groups()[0], fake_username, "Verify printed fake username") self.assertNotEquals(val, 0, "Verify exit code is non-zero")
def generate_fake_facility_users(nusers=20, facilities=None, facility_groups=None, password="******"): """Add the given fake facility users to each of the given fake facilities. If no facilities are given, they are created.""" if not facility_groups: (facility_groups, facilities) = generate_fake_facility_groups(facilities=facilities) facility_users = [] cur_usernum = 0 users_per_group = nusers / len(facility_groups) for facility in facilities: for facility_group in facility_groups: for i in range(0, users_per_group): user_data = { "first_name": random.choice(firstnames), "last_name": random.choice(lastnames), } user_data["username"] = username_from_name( user_data["first_name"], user_data["last_name"]) try: facility_user = FacilityUser.objects.get( facility=facility, username=user_data["username"]) facility_user.group = facility_group facility_user.save() logging.info("Retrieved facility user '%s/%s'" % (facility.name, user_data["username"])) except FacilityUser.DoesNotExist as e: notes = json.dumps(sample_user_settings()) facility_user = FacilityUser( facility=facility, username=user_data["username"], first_name=user_data["first_name"], last_name=user_data["last_name"], notes=notes, group=facility_group, ) facility_user.set_password( password) # set same password for every user facility_user.full_clean() facility_user.save() logging.info("Created facility user '%s/%s'" % (facility.name, user_data["username"])) facility_users.append(facility_user) cur_usernum += 1 # this is messy and could be done more intelligently; # could also randomize to add more users, as this function # seems to be generic, but really is not. return (facility_users, facility_groups, facilities)
def handle(self, *args, **options): if settings.CENTRAL_SERVER: raise CommandError("Don't run this on the central server!! Data not linked to any zone on the central server is BAD.") facility = Facility(name="Wilson Elementary") facility.save() group1 = FacilityGroup(facility=facility, name="Class 4E") group1.full_clean() group1.save() group2 = FacilityGroup(facility=facility, name="Class 5B") group2.full_clean() group2.save() facilityusers = [] for i in range(0,10): newuser1 = FacilityUser(facility=facility, username=usernames[i], first_name=firstnames[i], last_name=lastnames[i], group=group1) if settings.DEBUG: newuser1.set_password(hashed_password = hashed_blah)#"blah") else: newuser1.set_password("blah") newuser1.full_clean() newuser1.save() facilityusers.append(newuser1) newuser2 = FacilityUser(facility=facility, username=usernames[i+10], first_name=firstnames[i+10], last_name=lastnames[i+10], group=group2) if settings.DEBUG: newuser2.set_password(hashed_password = hashed_blah)#"blah") else: newuser2.set_password("blah") newuser2.full_clean() newuser2.save() facilityusers.append(newuser2) for topic in topics: exercises = get_topic_exercises(topic_id=topic) exercises_a = [random.random() for i in range(len(exercises))] exercises_b = [float(i) / len(exercises) for i in range(len(exercises))] for i, user in enumerate(facilityusers): for j, exercise in enumerate(exercises): sig = sigmoid(proficiency[i], exercises_a[j], exercises_b[j]) if random.random() < 0.05 * (1-sig) and j > 2: break if random.random() < 0.15: continue attempts = random.random() * 40 + 10 streak_progress = max(10, min(100, 10 * random.random() + 10 * attempts * sig)) print int(attempts), int(streak_progress), user.first_name, exercise["name"] log = ExerciseLog(user=user, exercise_id=exercise["name"], attempts=attempts, streak_progress=streak_progress) log.full_clean() log.save()
def generate_fake_facility_users(nusers=20, facilities=None, facility_groups=None, password="******"): """Add the given fake facility users to each of the given fake facilities. If no facilities are given, they are created.""" if not facility_groups: (facility_groups, facilities) = generate_fake_facility_groups(facilities=facilities) facility_users = [] cur_usernum = 0 users_per_group = nusers / len(facility_groups) for facility in facilities: for facility_group in facility_groups: for i in range(0, users_per_group): user_data = { "first_name": random.choice(firstnames), "last_name": random.choice(lastnames), } user_data["username"] = username_from_name(user_data["first_name"], user_data["last_name"]) try: facility_user = FacilityUser.objects.get(facility=facility, username=user_data["username"]) facility_user.group = facility_group facility_user.save() logging.info("Retrieved facility user '%s/%s'" % (facility.name, user_data["username"])) except FacilityUser.DoesNotExist as e: notes = json.dumps(sample_user_settings()) facility_user = FacilityUser( facility=facility, username=user_data["username"], first_name=user_data["first_name"], last_name=user_data["last_name"], notes=notes, group=facility_group, ) facility_user.set_password(password) # set same password for every user facility_user.full_clean() facility_user.save() logging.info("Created facility user '%s/%s'" % (facility.name, user_data["username"])) facility_users.append(facility_user) cur_usernum += 1 # this is messy and could be done more intelligently; # could also randomize to add more users, as this function # seems to be generic, but really is not. return (facility_users, facility_groups, facilities)
def handle(self, *args, **options): facility = Facility(name="Wilson Elementary") facility.save() group1 = FacilityGroup(facility=facility, name="Class 4E") group1.full_clean() group1.save() group2 = FacilityGroup(facility=facility, name="Class 5B") group2.full_clean() group2.save() facilityusers = [] for i in range(0,10): newuser1 = FacilityUser(facility=facility, username=usernames[i], first_name=firstnames[i], last_name=lastnames[i], group=group1) if settings.DEBUG: newuser1.set_password(hashed_password = hashed_blah)#"blah") else: newuser1.set_password("blah") newuser1.full_clean() newuser1.save() facilityusers.append(newuser1) newuser2 = FacilityUser(facility=facility, username=usernames[i+10], first_name=firstnames[i+10], last_name=lastnames[i+10], group=group2) if settings.DEBUG: newuser2.set_password(hashed_password = hashed_blah)#"blah") else: newuser2.set_password("blah") newuser2.full_clean() newuser2.save() facilityusers.append(newuser2) for topic in topics: exercises = get_topic_exercises(topic_id=topic) exercises_a = [random.random() for i in range(len(exercises))] exercises_b = [float(i) / len(exercises) for i in range(len(exercises))] for i, user in enumerate(facilityusers): for j, exercise in enumerate(exercises): sig = sigmoid(proficiency[i], exercises_a[j], exercises_b[j]) if random.random() < 0.05 * (1-sig) and j > 2: break if random.random() < 0.15: continue attempts = random.random() * 40 + 10 streak_progress = max(10, min(100, 10 * random.random() + 10 * attempts * sig)) print int(attempts), int(streak_progress), user.first_name, exercise["name"] log = ExerciseLog(user=user, exercise_id=exercise["name"], attempts=attempts, streak_progress=streak_progress) log.full_clean() log.save()
class ChangeLocalUserPassword(unittest.TestCase): def setUp(self): self.facility = Facility(name="Test Facility") self.facility.save() self.group = FacilityGroup(facility=self.facility, name="Test Class") self.group.full_clean() self.group.save() self.user = FacilityUser(facility=self.facility, username="******", first_name="Firstname", last_name="Lastname", group=self.group) self.user.clear_text_password = "******" # not used anywhere but by us, for testing purposes self.user.set_password(self.user.clear_text_password) self.user.full_clean() self.user.save() def test_change_password(self): # Now, re-retrieve the user, to check. (out,err,val) = call_command_with_output("changelocalpassword", self.user.username, noinput=True) self.assertEquals(err, "", "no output on stderr") self.assertNotEquals(out, "", "some output on stderr") self.assertEquals(val, 0, "Exit code is zero") match = re.match(r"^.*Generated new password for user '([^']+)': '([^']+)'", out.replace("\n",""), re.MULTILINE) self.assertFalse(match is None, "could not parse stdout") user = FacilityUser.objects.get(facility=self.facility, username=self.user.username) self.assertEquals(user.username, match.groups()[0], "Username reported correctly") self.assertTrue(user.check_password(match.groups()[1]), "New password works") self.assertFalse(user.check_password(self.user.clear_text_password), "NOT the old password") def test_no_user(self): fake_username = self.user.username + "xxxxx" #with self.assertRaises(FacilityUser.DoesNotExist): (out,err,val) = call_command_with_output("changelocalpassword", fake_username, noinput=True) self.assertNotIn("Generated new password for user", out, "Did not set password") self.assertNotEquals(err, "", "some output on stderr") match = re.match(r"^.*Error: user '([^']+)' does not exist$", err.replace("\n",""), re.M) self.assertFalse(match is None, "could not parse stderr") self.assertEquals(match.groups()[0], fake_username, "Verify printed fake username") self.assertNotEquals(val, 0, "Verify exit code is non-zero")
def handle(self, *args, **options): facility = Facility(name="Wilson Elementary") facility.save() group1 = FacilityGroup(facility=facility, name="Class 4E") group1.full_clean() group1.save() group2 = FacilityGroup(facility=facility, name="Class 5B") group2.full_clean() group2.save() facilityusers = [] for i in range(0,10): newuser1 = FacilityUser(facility=facility, username=usernames[i], first_name=firstnames[i], last_name=lastnames[i], group=group1) newuser1.set_password("blah") newuser1.full_clean() newuser1.save() facilityusers.append(newuser1) newuser2 = FacilityUser(facility=facility, username=usernames[i+10], first_name=firstnames[i+10], last_name=lastnames[i+10], group=group2) newuser2.set_password("blah") newuser2.full_clean() newuser2.save() facilityusers.append(newuser2) for topic in topics: exercises = json.load(open("./static/data/topicdata/" + topic + ".json","r")) exercises = sorted(exercises, key = lambda k: (k["h_position"], k["v_position"])) exercises_a = [random.random() for i in range(len(exercises))] exercises_b = [float(i) / len(exercises) for i in range(len(exercises))] for i, user in enumerate(facilityusers): for j, exercise in enumerate(exercises): sig = sigmoid(proficiency[i], exercises_a[j], exercises_b[j]) if random.random() < 0.05 * (1-sig) and j > 2: break if random.random() < 0.15: continue attempts = random.random() * 40 + 10 streak_progress = max(10, min(100, 10 * random.random() + 10 * attempts * sig)) print int(attempts), int(streak_progress), user.first_name, exercise["name"] log = ExerciseLog(user=user, exercise_id=exercise["name"], attempts=attempts, streak_progress=streak_progress) log.full_clean() log.save()