def setUp(self): super(TestVideoLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial VideoLog instance so we have something to collide with later self.original_videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) self.original_videolog.points = self.ORIGINAL_POINTS self.original_videolog.total_seconds_watched = self.ORIGINAL_SECONDS_WATCHED self.original_videolog.save() # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog was created correctly self.assertEqual(videolog.points, self.ORIGINAL_POINTS, "The VideoLog's points have already changed.") self.assertEqual( videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's total seconds watched have already changed.")
def setUp(self): super(TestExerciseLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial ExerciseLog instance so we have something to collide with later self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog was saved as intended self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS, "The ExerciseLog's points have already changed.") self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS, "The ExerciseLog's attempts have already changed.")
def create_student(self, username=default_username, password=default_password, facility_name=default_facility_name): facilities = Facility.objects.filter(name=facility_name) facility = facilities[0] if facilities else self.create_facility() student = FacilityUser(username=username, facility=facility) student.set_password(raw_password=password) student.save() return student
def test_facility_user_save(self): # only perform test if we are ourselves a trusted (i.e. central server) device if Device.get_own_device().is_trusted(): user = FacilityUser(username="******", facility=self.facility) user.set_password("blahblah") user.save() assert user.zone_fallback is not None, "Centrally created FacilityUser was not assigned a zone."
def test_query_login_student(self): """Check the # of queries when logging in as a student.""" student = FacilityUser(is_teacher=False, username="******", facility=self.facility) passwd = self._gen_valid_password() student.set_password(passwd) student.save() with self.assertNumQueries(23 + 3*UserLog.is_enabled()): self.browser_login_student("s1", passwd, self.facility)
def test_query_login_teacher(self): """Check the # of queries when logging in as a teacher.""" teacher = FacilityUser(is_teacher=True, username="******", facility=self.facility) passwd = self._gen_valid_password() teacher.set_password(passwd) teacher.save() with self.assertNumQueries(26 + 3*UserLog.is_enabled()): self.browser_login_teacher("t1", passwd, self.facility)
def create_student(cls, password='******', **kwargs): fields = CreateStudentMixin.DEFAULTS.copy() fields.update(**kwargs) fields['facility'] = (fields.get('facility') or cls.create_facility()) user = FacilityUser(**fields) user.set_password(password) user.real_password = password user.save() return user
def test_success_no_group(self): fu = FacilityUser(username="******", facility=self.facility) fu.set_password(raw_password="******") fu.save() self.browser_login_admin() self.browse_to( self.reverse("tabular_view") + "?topic=addition-subtraction") with self.assertRaises(NoSuchElementException): self.browser.find_element_by_css_selector('#error_message')
def generate_fake_facility_users(nusers=20, facilities=None, facility_groups=None, password="******", is_teacher=False): """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 if not is_teacher else None facility_user.is_teacher = is_teacher 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 if not is_teacher else None, is_teacher=is_teacher, ) facility_user.set_password(password) # set same password for every user try: facility_user.save() logging.info("Created facility user '%s/%s'" % (facility.name, user_data["username"])) except Exception as e: logging.error("Error saving facility user: %s" % e) continue 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 test_success_no_group(self): fu = FacilityUser(username="******", facility=self.facility) fu.set_password(raw_password="******") fu.save() self.browser_login_admin(**self.admin_data) url = "%s?topic=%s" % ( self.url, self.topic, ) self.browse_to(url) with self.assertRaises(NoSuchElementException): self.browser.find_element_by_css_selector('.alert-danger')
def create_teacher(self, username=default_teacher_username, password=default_password, facility_name=default_facility_name): facilities = Facility.objects.filter(name=facility_name) facility = facilities[0] if facilities else self.create_facility() teacher = FacilityUser(username=username, facility=facility, is_teacher=True) teacher.set_password(raw_password=password) teacher.save() return teacher
def setUp(self): '''Performed before every test''' #a brand new user self.facility = Facility(name=self.FACILITY) self.facility.save() self.user_with_no_activity = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user_with_no_activity.set_password(self.PASSWORD) self.user_with_no_activity.save() #a user with valid exercises self.user_with_activity = FacilityUser(username=self.USERNAME2, facility=self.facility) self.user_with_activity.set_password(self.PASSWORD) self.user_with_activity.save() #add some exercises for second user (both incomplete) self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user_with_activity, complete=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user=self.user_with_activity, complete=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.save()
def setUp(self): super(TestSaveVideoLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial VideoLog instance so we have something to update later self.original_videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) self.original_videolog.points = self.ORIGINAL_POINTS self.original_videolog.total_seconds_watched = self.ORIGINAL_SECONDS_WATCHED self.original_videolog.save()
def test_groups_two_groups_one_user_in_group_no_ungrouped_group_selected_move( self): facility = self.facility params = { "zone_id": None, "facility_id": facility.id, } group_name_1 = "From Group" group1 = FacilityGroup(name=group_name_1, facility=self.facility) group1.save() group_name_2 = "To Group" group2 = FacilityGroup(name=group_name_2, facility=self.facility) group2.save() user = FacilityUser(username="******", facility=self.facility, group=group1) user.set_password(raw_password="******") user.save() self.browser_login_admin(**self.admin_data) self.browse_to(self.reverse("facility_management", kwargs=params)) self.browser.find_element_by_xpath( "//div[@id='students']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr/td[1]/input[@type='checkbox'][1]" ).click() Select( self.browser.find_element_by_css_selector( "div#students select.movegrouplist")).select_by_visible_text( "To Group") self.browser.find_element_by_css_selector("button.movegroup").click() if self.is_phantomjs: alert = self.browser_click_and_accept('button.movegroup') else: alert = self.browser.switch_to_alert() self.assertNotEqual(alert.text, None, "Does not produce alert of group movement.") self.assertEqual( alert.text, "You are about to move selected users to another group.", "Does not warn that users are about to be moved.") alert.accept() WebDriverWait(self.browser, 5).until( EC.presence_of_element_located((By.ID, "students"))) self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='groups']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr[1]/td[5]" ).text, "0", "Does not report no user for From Group.") self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='groups']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr[2]/td[5]" ).text, "1", "Does not report one user for To Group.")
def setUp(self): super(TestSaveContentLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Default Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial ContentLog instance so we have something to update later self.contentlog = ContentLog(content_id=self.CONTENT_ID, user=self.user) self.contentlog.points = self.POINTS self.contentlog.content_kind = self.CONTENT_KIND self.contentlog.content_source = self.CONTENT_SOURCE self.contentlog.save()
def setUp(self): super(TestSaveExerciseLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial ExerciseLog instance so we have something to update later self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.save()
def test_users_out_of_group(self): group = FacilityGroup(name="Test Group", facility=self.facility) group.save() fu = FacilityUser(username="******", facility=self.facility) # Ungrouped fu.set_password(raw_password="******") fu.save() self.browser_login_admin() self.browse_to( self.reverse("tabular_view") + "?topic=addition-subtraction&group=" + group.id) self.browser.find_element_by_css_selector('#error_message') self.assertEqual( self.browser.find_element_by_css_selector('#error_message').text, _("No student accounts in this group have been created."), "Error message with no users available.")
def generate_fake_coachreport_logs(password="******"): try: t = FacilityUser.objects.get( facility=Facility.objects.all()[0], username=random.choice(firstnames), ) except FacilityUser.DoesNotExist as e: t = FacilityUser( facility=Facility.objects.all()[0], username=random.choice(firstnames), ) t.set_password(password) t.save() # TODO: create flags later num_logs = 20 logs = [] for _ in xrange(num_logs): date_logged_in = datetime.datetime.now() - datetime.timedelta( days=random.randint(1, 10)) date_viewed_coachreport = date_logged_in + datetime.timedelta( minutes=random.randint(0, 30)) date_logged_out = date_viewed_coachreport + datetime.timedelta( minutes=random.randint(0, 30)) login_log = UserLog.objects.create( user=t, activity_type=UserLog.get_activity_int("login"), start_datetime=date_logged_in, last_active_datetime=date_viewed_coachreport, end_datetime=date_logged_out, ) logging.info("created login log for teacher %s" % t.username) coachreport_log = UserLog.objects.create( user=t, activity_type=UserLog.get_activity_int("coachreport"), start_datetime=date_viewed_coachreport, last_active_datetime=date_viewed_coachreport, end_datetime=date_viewed_coachreport, ) logs.append((login_log, coachreport_log)) logging.info("created coachreport log for teacher %s" % t.username) return logs
def test_users_out_of_group(self): fu = FacilityUser(username="******", facility=self.facility) # Ungrouped fu.set_password(raw_password="******") fu.save() self.browser_login_admin(**self.admin_data) url = "%s?topic=%s&group_id=%s" % ( self.url, self.topic, self.group.id, ) self.browse_to(url) try: elem = WebDriverWait(self.browser, 3).until( EC.presence_of_element_located( (By.CLASS_NAME, "alert-warning"))) # Check if error message is contained in the alert bubble container. self.assertTrue( "No learner accounts in this group have been created." in elem.text) except TimeoutException: self.fail("Didn't find the error message with users out of group.")
def test_groups_one_group_one_user_in_group_no_ungrouped_no_group_selected( self): facility = self.facility params = { "zone_id": None, "facility_id": facility.id, } group_name = "Test Group" group = FacilityGroup(name=group_name, facility=self.facility) group.save() user = FacilityUser(username="******", facility=self.facility, group=group) user.set_password(raw_password="******") user.save() self.browser_login_admin() self.browse_to(self.reverse("facility_management", kwargs=params)) self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='groups']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr/td[2]/a[1]" ).text.strip()[:len(group.name)], "Test Group", "Does not show group in list.") self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='groups']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr/td[5]" ).text.strip()[:len(group.name)], "1", "Does not report one user for group.") self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='students']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr/td[2]" ).text.strip()[:len(user.username)], "test_user", "Does not show user in list.") self.assertEqual( self.browser.find_element_by_xpath( "//div[@id='students']/div[@class='col-md-12']/div[@class='table-responsive']/table/tbody/tr/td[5]" ).text.strip()[:len(user.group.name)], "Test Group", "Does not report user in group.")
def setUp(self): '''Performed before every test''' # create a facility and user that can be referred to in models across tests self.facility = Facility(name=self.FACILITY) self.facility.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user1.set_password(self.PASSWORD) self.user1.save() #add one exercise self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.latest_activity_timestamp = self.TIMESTAMP self.original_exerciselog.completion_timestamp = self.TIMESTAMP self.original_exerciselog.save() #create a request factory for later instantiation of request self.factory = RequestFactory()
def setUp(self): '''Performed before every test''' # create a facility and user that can be referred to in models across tests self.facility = Facility(name=self.FACILITY) self.facility.save() self.facilitygroup = FacilityGroup(name=self.GROUP, description="", facility=self.facility) self.facilitygroup.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility, group=self.facilitygroup) self.user1.set_password(self.PASSWORD) self.user1.save() self.user2 = FacilityUser(username=self.USERNAME2, facility=self.facility, group=self.facilitygroup) self.user2.set_password(self.PASSWORD) self.user2.save() #user 1 - now add some mock data into exercise log self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog.save() #user 2 self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user2, struggling=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user=self.user2, struggling=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.save() self.original_exerciselog3 = ExerciseLog( exercise_id=self.EXERCISE_ID_STRUGGLE, user=self.user2, struggling=True) self.original_exerciselog3.points = self.ORIGINAL_POINTS self.original_exerciselog3.attempts = self.ORIGINAL_POINTS #intentionally made larger to trigger struggling self.original_exerciselog3.streak_progress = 0 self.original_exerciselog3.attempts = 100 self.original_exerciselog3.latest_activity_timestamp = self.TIMESTAMP_STRUGGLE self.original_exerciselog3.completion_timestamp = self.TIMESTAMP_STRUGGLE self.original_exerciselog3.save()
def test_unicode_string(self): # Dependencies dev = Device.get_own_device() self.assertNotIn(unicode(dev), "Bad Unicode data", "Device: Bad conversion to unicode.") fac = Facility(name=self.korean_string) fac.save() self.assertNotIn(unicode(fac), "Bad Unicode data", "Facility: Bad conversion to unicode.") fg = FacilityGroup(facility=fac, name=self.korean_string) fg.save() self.assertNotIn(unicode(fg), "Bad Unicode data", "FacilityGroup: Bad conversion to unicode.") user = FacilityUser( facility=fac, group=fg, first_name=self.korean_string, last_name=self.korean_string, username=self.korean_string, notes=self.korean_string, ) user.set_password(self.korean_string * settings.PASSWORD_CONSTRAINTS["min_length"]) user.save() self.assertNotIn(unicode(user), "Bad Unicode data", "FacilityUser: Bad conversion to unicode.") known_classes = [ExerciseLog, UserLog, UserLogSummary, VideoLog] # elog = ExerciseLog(user=user, exercise_id=self.korean_string) self.assertNotIn( unicode(elog), "Bad Unicode data", "ExerciseLog: Bad conversion to unicode (before saving).") elog.save(update_userlog=False) self.assertNotIn( unicode(elog), "Bad Unicode data", "ExerciseLog: Bad conversion to unicode (after saving).") vlog = VideoLog(user=user, video_id=self.korean_string, youtube_id=self.korean_string) self.assertNotIn( unicode(vlog), "Bad Unicode data", "VideoLog: Bad conversion to unicode (before saving).") vlog.save(update_userlog=False) self.assertNotIn( unicode(vlog), "Bad Unicode data", "VideoLog: Bad conversion to unicode (after saving).") ulog = UserLog(user=user) self.assertNotIn(unicode(ulog), "Bad Unicode data", "UserLog: Bad conversion to unicode.") ulogsum = UserLogSummary( user=user, device=dev, activity_type=1, start_datetime=datetime.now(), end_datetime=datetime.now(), ) self.assertNotIn(unicode(ulogsum), "Bad Unicode data", "UserLogSummary: Bad conversion to unicode.")