def setUp(self):
        # 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="******",
                                 password="******",
                                 facility=self.facility)
        self.user.save()

        # create an initial VideoLog instance so we have something to collide with later
        self.original_videolog = VideoLog(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.")
Exemple #2
0
 def test_set_password_raw_ok(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG
     settings.DEBUG = True
     fu.set_password(raw_password="******")
     settings.DEBUG = dbg_mode
     self.assertTrue(fu.check_password("blah"))
Exemple #3
0
 def test_set_password_hash_ok(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG
     settings.DEBUG = True
     fu.set_password(hashed_password=self.__class__.hashed_blah)
     settings.DEBUG = dbg_mode
     self.assertTrue(fu.check_password("blah"))
Exemple #4
0
 def test_set_password_hash_nodebug_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG
     settings.DEBUG = False
     with self.assertRaises(AssertionError):
         fu.set_password(hashed_password=self.__class__.hashed_blah)
     settings.DEBUG = dbg_mode
Exemple #5
0
    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.")
Exemple #6
0
    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
Exemple #7
0
 def test_set_password_both_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG
     settings.DEBUG = True
     with self.assertRaises(AssertionError):
         fu.set_password(raw_password="******",
                         hashed_password=self.__class__.hashed_blah)
     settings.DEBUG = dbg_mode
    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(39 + 3*UserLog.is_enabled()):
            self.browser_login_teacher("t1", passwd, self.facility)
    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(39 + 3*UserLog.is_enabled()):
            self.browser_login_student("s1", passwd, self.facility)
Exemple #10
0
    def setUp(self):
        """Create a new facility and facility user"""
        super(ChangeLocalUserPassword, self).setUp()

        self.old_password = '******'
        self.username = "******"
        self.facility = Facility(name="Test Facility")
        self.facility.save()
        self.user = FacilityUser(username=self.username, facility=self.facility)
        self.user.set_password(self.old_password)
        self.user.save()
Exemple #11
0
    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()
Exemple #12
0
    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()
        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()
        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.")
Exemple #13
0
 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 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  # reset the group, if needed.
                    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.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 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)
Exemple #16
0
    def setUp(self):
        # 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()
Exemple #17
0
def load_test(request, nusers=None):
    global n_users_created

    if not n_users_created or n_users_created < int(
            request.GET.get("nusers", 1)):  # default 1, as before
        # It's either the first time, or time to add more

        # Make sure there's a facility
        if not Facility.objects.count():
            fac = Facility.objects.create(name="fac")
        fac = Facility.objects.all()[0]

        # Loop over all needed students
        while n_users_created < int(request.GET.get("nusers", 1)):
            n_users_created += 1
            unpw = "s%d" % n_users_created
            (user, _) = FacilityUser.get_or_initialize(username=unpw,
                                                       facility=fac)
            user.set_password(unpw)
            user.save()

    return {
        "pct_videos": request.GET.get("pct_videos", 0.9),
        "pct_logout": request.GET.get("pct_logout", 0.0),
        "nusers": n_users_created,
    }
Exemple #18
0
 def test_users_out_of_group(self):
     group = FacilityGroup(name="Test Group", facility=self.facility)
     group.save()
     FacilityUser(username="******", password="******", facility=self.facility).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 users found.", "Error message with no users available.")
Exemple #19
0
 def test_success_with_group(self):
     group = FacilityGroup(name="Test Group", facility=self.facility)
     group.save()
     FacilityUser(username="******", password="******", facility=self.facility, group=group).save()
     self.browser_login_admin()
     self.browse_to(self.reverse("tabular_view") + "?topic=addition-subtraction&group=" + group.id)
     with self.assertRaises(NoSuchElementException):
         self.browser.find_element_by_css_selector('#error_message')
Exemple #20
0
class ChangeLocalUserPassword(MainTestCase):
    """Tests for the changelocalpassword command"""
    def setUp(self):
        """Create a new facility and facility user"""
        super(ChangeLocalUserPassword, self).setUp()

        self.old_password = '******'
        self.username = "******"
        self.facility = Facility(name="Test Facility")
        self.facility.save()
        self.user = FacilityUser(username=self.username,
                                 facility=self.facility)
        self.user.set_password(self.old_password)
        self.user.save()

    def test_change_password_on_existing_user(self):
        """Change the password on an existing user."""

        # Now, re-retrieve the user, to check.
        (out, err, val) = call_command_with_output("changelocalpassword",
                                                   self.user.username,
                                                   noinput=True)
        self.assertEqual(err, "", "no output on stderr")
        self.assertNotEqual(out, "", "some output on stdout")
        self.assertEqual(val, 0, "Exit code is not zero")

        new_password = re.search(
            r"Generated new password for user .*: '(?P<password>.*)'",
            out).group('password')
        self.assertNotEqual(self.old_password, new_password)

        c = KALiteClient()
        success = c.login(username=self.user.username,
                          password=new_password,
                          facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")

    def test_change_password_on_nonexistent_user(self):
        nonexistent_username = "******"
        with self.assertRaises(CommandError):
            (out, err, val) = call_command_with_output("changelocalpassword",
                                                       nonexistent_username,
                                                       noinput=True)
Exemple #21
0
 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()
Exemple #22
0
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")
Exemple #23
0
 def setUp(self):
     # 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(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()
Exemple #24
0
    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
Exemple #25
0
    def setUp(self):
        # 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()
class ChangeLocalUserPassword(MainTestCase):
    """Tests for the changelocalpassword command"""

    def setUp(self):
        """Create a new facility and facility user"""
        super(ChangeLocalUserPassword, self).setUp()

        self.old_password = '******'
        self.username = "******"
        self.facility = Facility(name="Test Facility")
        self.facility.save()
        self.user = FacilityUser(username=self.username, facility=self.facility)
        self.user.set_password(self.old_password)
        self.user.save()


    def test_change_password_on_existing_user(self):
        """Change the password on an existing user."""

        # Now, re-retrieve the user, to check.
        (out,err,val) = call_command_with_output("changelocalpassword", self.user.username, noinput=True)
        self.assertEqual(err, "", "no output on stderr")
        self.assertNotEqual(out, "", "some output on stdout")
        self.assertEqual(val, 0, "Exit code is not zero")

        new_password =  re.search(r"Generated new password for user .*: '(?P<password>.*)'", out).group('password')
        self.assertNotEqual(self.old_password, new_password)

        c = KALiteClient()
        success = c.login(username=self.user.username, password=new_password, facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")


    def test_change_password_on_nonexistent_user(self):
        nonexistent_username = "******"
        with self.assertRaises(CommandError):
            (out, err, val) = call_command_with_output("changelocalpassword", nonexistent_username, noinput=True)
Exemple #27
0
    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(39 + 3 * UserLog.is_enabled()):
            self.browser_login_teacher("t1", passwd, self.facility)
Exemple #28
0
    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(39 + 3 * UserLog.is_enabled()):
            self.browser_login_student("s1", passwd, self.facility)
Exemple #29
0
    def setUp(self):
        # 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="******", password="******", facility=self.facility)
        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.")
Exemple #30
0
    def setUp(self):
        # 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="******", password="******", facility=self.facility)
        self.user.save()

        # create an initial VideoLog instance so we have something to collide with later
        self.original_videolog = VideoLog(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.")
Exemple #31
0
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")
Exemple #32
0
def load_test(request, nusers=None):
    global n_users_created

    if not n_users_created or n_users_created < int(request.GET.get("nusers", 1)):  # default 1, as before
        # It's either the first time, or time to add more

        # Make sure there's a facility
        if not Facility.objects.count():
            fac = Facility.objects.create(name="fac")
        fac = Facility.objects.all()[0]

        # Loop over all needed students
        while n_users_created < int(request.GET.get("nusers", 1)):
            n_users_created += 1
            unpw = "s%d" % n_users_created
            user = FacilityUser.get_or_initialize(username=unpw, facility=fac)
            user.set_password(unpw)
            user.save()

    return {
        "pct_videos": request.GET.get("pct_videos", 0.9),
        "pct_logout": request.GET.get("pct_logout", 0.0),
        "nusers": n_users_created,
    }
Exemple #33
0
    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,
            password=self.korean_string,
        )
        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()
        self.assertNotIn(
            unicode(elog), "Bad Unicode data",
            "ExerciseLog: Bad conversion to unicode (after saving).")

        vlog = VideoLog(user=user, youtube_id=self.korean_string)
        self.assertNotIn(
            unicode(vlog), "Bad Unicode data",
            "VideoLog: Bad conversion to unicode (before saving).")
        vlog.save()
        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.")
Exemple #34
0
 def test_set_password_neither_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG
     settings.DEBUG = True
     with self.assertRaises(AssertionError):
         fu.set_password()
Exemple #35
0
class TestExerciseLogs(KALiteTestCase):

    ORIGINAL_POINTS = 37
    ORIGINAL_ATTEMPTS = 3
    NEW_POINTS = 22
    NEW_ATTEMPTS = 5
    EXERCISE_ID = "number_line"

    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 test_exerciselog_update(self):

        # get a new reference to the existing ExerciseLog
        exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # update the ExerciseLog
        exerciselog.points = self.NEW_POINTS
        exerciselog.attempts = self.NEW_ATTEMPTS
        exerciselog.save()

        # get a new reference to the existing ExerciseLog
        exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # make sure the ExerciseLog was updated
        self.assertEqual(exerciselog2.points, self.NEW_POINTS, "The ExerciseLog's points were not updated.")
        self.assertEqual(exerciselog2.attempts, self.NEW_ATTEMPTS, "The ExerciseLog's attempts were not updated.")

    @unittest.skip("Auto-merging is not yet automatic, so skip this")
    def test_exerciselog_collision(self):

        # create a new exercise log with the same exercise_id and user, but different points/attempts
        exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user)
        exerciselog.points = self.NEW_POINTS
        exerciselog.attempts = self.NEW_ATTEMPTS

        # try saving the new ExerciseLog: this is where the collision will happen, hopefully leading to a merge
        exerciselog.save()

        # get a new reference to the existing ExerciseLog
        exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # make sure the ExerciseLog has been properly merged
        self.assertEqual(exerciselog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The ExerciseLog's points were not properly merged.")
        self.assertEqual(exerciselog.attempts, max(self.ORIGINAL_ATTEMPTS, self.NEW_ATTEMPTS), "The ExerciseLog's attempts have already changed.")
Exemple #36
0
 def test_set_password_hash_ok(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG; settings.DEBUG=True
     fu.set_password(hashed_password=self.__class__.hashed_blah)	
     settings.DEBUG = dbg_mode 
     self.assertTrue(fu.check_password("blah"))
 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()
Exemple #38
0
 def test_set_password_neither_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG; settings.DEBUG=True
     with self.assertRaises(AssertionError):
         fu.set_password()	
 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()
Exemple #40
0
 def test_set_password_hash_nodebug_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG; settings.DEBUG=False
     with self.assertRaises(AssertionError):
         fu.set_password(hashed_password=self.__class__.hashed_blah)	
     settings.DEBUG = dbg_mode 
Exemple #41
0
class TestExerciseLogs(TestCase):

    ORIGINAL_POINTS = 37
    ORIGINAL_ATTEMPTS = 3
    NEW_POINTS = 22
    NEW_ATTEMPTS = 5
    EXERCISE_ID = "number_line"

    def setUp(self):
        # 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="******",
                                 password="******",
                                 facility=self.facility)
        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 test_exerciselog_update(self):

        # get a new reference to the existing ExerciseLog
        exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # update the ExerciseLog
        exerciselog.points = self.NEW_POINTS
        exerciselog.attempts = self.NEW_ATTEMPTS
        exerciselog.save()

        # get a new reference to the existing ExerciseLog
        exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # make sure the ExerciseLog was updated
        self.assertEqual(exerciselog2.points, self.NEW_POINTS,
                         "The ExerciseLog's points were not updated.")
        self.assertEqual(exerciselog2.attempts, self.NEW_ATTEMPTS,
                         "The ExerciseLog's attempts were not updated.")

    @unittest.skip("Auto-merging is not yet automatic, so skip this")
    def test_exerciselog_collision(self):

        # create a new exercise log with the same exercise_id and user, but different points/attempts
        exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user)
        exerciselog.points = self.NEW_POINTS
        exerciselog.attempts = self.NEW_ATTEMPTS

        # try saving the new ExerciseLog: this is where the collision will happen, hopefully leading to a merge
        exerciselog.save()

        # get a new reference to the existing ExerciseLog
        exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # make sure the ExerciseLog has been properly merged
        self.assertEqual(exerciselog.points,
                         max(self.ORIGINAL_POINTS, self.NEW_POINTS),
                         "The ExerciseLog's points were not properly merged.")
        self.assertEqual(exerciselog.attempts,
                         max(self.ORIGINAL_ATTEMPTS, self.NEW_ATTEMPTS),
                         "The ExerciseLog's attempts have already changed.")
Exemple #42
0
class TestSaveExerciseLog(KALiteTestCase):

    ORIGINAL_POINTS = 37
    ORIGINAL_ATTEMPTS = 3
    ORIGINAL_STREAK_PROGRESS = 20
    NEW_POINTS_LARGER = 22
    NEW_ATTEMPTS = 5
    NEW_STREAK_PROGRESS_LARGER = 10
    NEW_POINTS_SMALLER = 0
    NEW_STREAK_PROGRESS_SMALLER = 0
    EXERCISE_ID = "number_line"
    EXERCISE_ID2 = "radius_diameter_and_circumference"
    USERNAME = "******"
    PASSWORD = "******"

    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_new_exerciselog(self):

        # make sure the target exercise log does not already exist
        exerciselogs = ExerciseLog.objects.filter(
            exercise_id=self.EXERCISE_ID2, user__username=self.USERNAME)
        self.assertEqual(
            exerciselogs.count(), 0,
            "The target exercise log to be newly created already exists")

        c = KALiteClient()

        # login
        success = c.login(username=self.USERNAME,
                          password=self.PASSWORD,
                          facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")

        # save a new exercise log
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID2,
            streak_progress=self.NEW_STREAK_PROGRESS_LARGER,
            points=self.NEW_POINTS_LARGER,
            correct=True,
            attempts=self.NEW_ATTEMPTS,
        )
        self.assertEqual(
            result.status_code, 200,
            "An error (%d) was thrown while saving the exercise log." %
            result.status_code)

        # get a reference to the newly created ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID2,
                                              user__username=self.USERNAME)

        # make sure the ExerciseLog was properly created
        self.assertEqual(exerciselog.points, self.NEW_POINTS_LARGER,
                         "The ExerciseLog's points were not saved correctly.")
        self.assertEqual(
            exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER,
            "The ExerciseLog's streak progress was not saved correctly.")
        self.assertEqual(
            exerciselog.attempts, self.NEW_ATTEMPTS,
            "The ExerciseLog did not have the correct number of attempts (%d)."
            % self.NEW_ATTEMPTS)

    def test_update_exerciselog(self):

        # get a new reference to the existing ExerciseLog
        exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id)

        # make sure the ExerciseLog hasn't already been changed
        self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS,
                         "The ExerciseLog's points have already changed.")
        self.assertEqual(exerciselog.streak_progress,
                         self.ORIGINAL_STREAK_PROGRESS,
                         "The ExerciseLog's streak progress already changed.")
        self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS,
                         "The ExerciseLog's attempts have already changed.")

        c = KALiteClient()

        # login
        success = c.login(username=self.USERNAME,
                          password=self.PASSWORD,
                          facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")

        # save a new record onto the exercise log, with a correct answer (increasing the points and streak)
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID,
            streak_progress=self.NEW_STREAK_PROGRESS_LARGER,
            points=self.NEW_POINTS_LARGER,
            correct=True,
            attempts=self.NEW_ATTEMPTS,
        )
        self.assertEqual(
            result.status_code, 200,
            "An error (%d) was thrown while saving the exercise log." %
            result.status_code)

        # get a reference to the updated ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID,
                                              user__username=self.USERNAME)

        # make sure the ExerciseLog was properly updated
        self.assertEqual(
            exerciselog.points, self.NEW_POINTS_LARGER,
            "The ExerciseLog's points were not updated correctly.")
        self.assertEqual(
            exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER,
            "The ExerciseLog's streak progress was not updated correctly.")
        self.assertEqual(
            exerciselog.attempts, self.NEW_ATTEMPTS,
            "The ExerciseLog did not have the correct number of attempts (%d)."
            % self.NEW_ATTEMPTS)

        # save a new record onto the exercise log, with an incorrect answer (decreasing the points and streak)
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID,
            streak_progress=self.NEW_STREAK_PROGRESS_SMALLER,
            points=self.NEW_POINTS_SMALLER,
            correct=False,
            attempts=self.NEW_ATTEMPTS + 1,
        )
        self.assertEqual(
            result.status_code, 200,
            "An error (%d) was thrown while saving the exercise log." %
            result.status_code)

        # get a reference to the updated ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID,
                                              user__username=self.USERNAME)

        # make sure the ExerciseLog was properly updated
        self.assertEqual(exerciselog.points, self.NEW_POINTS_SMALLER,
                         "The ExerciseLog's points were not saved correctly.")
        self.assertEqual(
            exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_SMALLER,
            "The ExerciseLog's streak progress was not saved correctly.")
        self.assertEqual(
            exerciselog.attempts, self.NEW_ATTEMPTS + 1,
            "The ExerciseLog did not have the correct number of attempts.")
Exemple #43
0
class TestVideoLogs(TestCase):

    ORIGINAL_POINTS = 37
    ORIGINAL_SECONDS_WATCHED = 3
    NEW_POINTS = 22
    NEW_SECONDS_WATCHED = 5
    YOUTUBE_ID = "aNqG4ChKShI"

    def setUp(self):
        # 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="******",
                                 password="******",
                                 facility=self.facility)
        self.user.save()

        # create an initial VideoLog instance so we have something to collide with later
        self.original_videolog = VideoLog(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 test_videolog_update(self):

        # get a new reference to the existing VideoLog
        videolog = VideoLog.objects.get(id=self.original_videolog.id)

        # update the VideoLog
        videolog.points = self.NEW_POINTS
        videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED
        videolog.save()

        # get a new reference to the existing VideoLog
        videolog2 = VideoLog.objects.get(id=self.original_videolog.id)

        # make sure the VideoLog was updated
        self.assertEqual(videolog2.points, self.NEW_POINTS,
                         "The VideoLog's points were not updated.")
        self.assertEqual(
            videolog2.total_seconds_watched, self.NEW_SECONDS_WATCHED,
            "The VideoLog's total seconds watched were not updated.")

    @unittest.skip("Auto-merging is not yet automatic, so skip this")
    def test_videolog_collision(self):

        # create a new video log with the same youtube_id and user, but different points/total seconds watched
        videolog = VideoLog(youtube_id=self.YOUTUBE_ID, user=self.user)
        videolog.points = self.NEW_POINTS
        videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED

        # try saving the new VideoLog: this is where the collision will happen, hopefully leading to a merge
        videolog.save()

        # get a new reference to the existing VideoLog
        videolog2 = VideoLog.objects.get(id=self.original_videolog.id)

        # make sure the VideoLog has been properly merged
        self.assertEqual(videolog.points,
                         max(self.ORIGINAL_POINTS, self.NEW_POINTS),
                         "The VideoLog's points were not properly merged.")
        self.assertEqual(
            videolog.total_seconds_watched,
            max(self.ORIGINAL_ATTEMPTS, self.NEW_SECONDS_WATCHED),
            "The VideoLog's total seconds watched have already changed.")
Exemple #44
0
class TestSaveVideoLog(KALiteTestCase):
    
    ORIGINAL_POINTS = 84
    ORIGINAL_SECONDS_WATCHED = 32
    NEW_POINTS = 32
    NEW_SECONDS_WATCHED = 15
    YOUTUBE_ID = "aNqG4ChKShI"
    YOUTUBE_ID2 = "b22tMEc6Kko"
    USERNAME = "******"
    PASSWORD = "******"
    
    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(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_new_videolog(self):
        
        # make sure the target video log does not already exist
        videologs = VideoLog.objects.filter(youtube_id=self.YOUTUBE_ID2, user__username=self.USERNAME)
        self.assertEqual(videologs.count(), 0, "The target video log to be newly created already exists")

        c = KALiteClient()
        
        # login
        success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")
        
        # save a new video log
        result = c.save_video_log(
            youtube_id=self.YOUTUBE_ID2,
            total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED,
            points=self.NEW_POINTS,
        )
        self.assertEqual(result.status_code, 200, "An error (%d) was thrown while saving the video log." % result.status_code)
        
        # get a reference to the newly created VideoLog
        videolog = VideoLog.objects.get(youtube_id=self.YOUTUBE_ID2, user__username=self.USERNAME)
        
        # make sure the VideoLog was properly created
        self.assertEqual(videolog.points, self.NEW_POINTS, "The VideoLog's points were not saved correctly.")
        self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's seconds watched was not saved correctly.")

    def test_update_videolog(self):

        # get a new reference to the existing VideoLog
        videolog = VideoLog.objects.get(id=self.original_videolog.id)
        
        # make sure the VideoLog hasn't already been changed
        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 seconds watched already changed.")
        
        c = KALiteClient()
        
        # login
        success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")
        
        # save a new record onto the video log, with a correct answer (increasing the points and streak)
        result = c.save_video_log(
            youtube_id=self.YOUTUBE_ID,
            total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED + self.NEW_SECONDS_WATCHED,
            points=self.ORIGINAL_POINTS + self.NEW_POINTS,
        )
        self.assertEqual(result.status_code, 200, "An error (%d) was thrown while saving the video log." % result.status_code)

        # get a reference to the updated VideoLog
        videolog = VideoLog.objects.get(youtube_id=self.YOUTUBE_ID, user__username=self.USERNAME)
        
        # make sure the VideoLog was properly updated
        self.assertEqual(videolog.points, self.ORIGINAL_POINTS + self.NEW_POINTS, "The VideoLog's points were not updated correctly.")
        self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED + self.NEW_SECONDS_WATCHED, "The VideoLog's seconds watched was not updated correctly.")
Exemple #45
0
class TestSaveExerciseLog(KALiteTestCase):
    
    ORIGINAL_POINTS = 37
    ORIGINAL_ATTEMPTS = 3
    ORIGINAL_STREAK_PROGRESS = 20
    NEW_POINTS_LARGER = 22
    NEW_ATTEMPTS = 5
    NEW_STREAK_PROGRESS_LARGER = 10
    NEW_POINTS_SMALLER = 0
    NEW_STREAK_PROGRESS_SMALLER = 0
    EXERCISE_ID = "number_line"
    EXERCISE_ID2 = "radius_diameter_and_circumference"
    USERNAME = "******"
    PASSWORD = "******"
    
    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_new_exerciselog(self):
        
        # make sure the target exercise log does not already exist
        exerciselogs = ExerciseLog.objects.filter(exercise_id=self.EXERCISE_ID2, user__username=self.USERNAME)
        self.assertEqual(exerciselogs.count(), 0, "The target exercise log to be newly created already exists")

        c = KALiteClient()
        
        # login
        success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")
        
        # save a new exercise log
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID2,
            streak_progress=self.NEW_STREAK_PROGRESS_LARGER,
            points=self.NEW_POINTS_LARGER,
            correct=True,
            attempts=self.NEW_ATTEMPTS,
        )
        self.assertEqual(result.status_code, 200, "An error (%d) was thrown while saving the exercise log." % result.status_code)
        
        # get a reference to the newly created ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID2, user__username=self.USERNAME)
        
        # make sure the ExerciseLog was properly created
        self.assertEqual(exerciselog.points, self.NEW_POINTS_LARGER, "The ExerciseLog's points were not saved correctly.")
        self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER, "The ExerciseLog's streak progress was not saved correctly.")
        self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS, "The ExerciseLog did not have the correct number of attempts (%d)." % self.NEW_ATTEMPTS)        

    def test_update_exerciselog(self):

        # get a new reference to the existing ExerciseLog
        exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id)
        
        # make sure the ExerciseLog hasn't already been changed
        self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS, "The ExerciseLog's points have already changed.")
        self.assertEqual(exerciselog.streak_progress, self.ORIGINAL_STREAK_PROGRESS, "The ExerciseLog's streak progress already changed.")
        self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS, "The ExerciseLog's attempts have already changed.")
        
        c = KALiteClient()
        
        # login
        success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")
        
        # save a new record onto the exercise log, with a correct answer (increasing the points and streak)
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID,
            streak_progress=self.NEW_STREAK_PROGRESS_LARGER,
            points=self.NEW_POINTS_LARGER,
            correct=True,
            attempts=self.NEW_ATTEMPTS,
        )
        self.assertEqual(result.status_code, 200, "An error (%d) was thrown while saving the exercise log." % result.status_code)
        
        # get a reference to the updated ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID, user__username=self.USERNAME)
        
        # make sure the ExerciseLog was properly updated
        self.assertEqual(exerciselog.points, self.NEW_POINTS_LARGER, "The ExerciseLog's points were not updated correctly.")
        self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER, "The ExerciseLog's streak progress was not updated correctly.")
        self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS, "The ExerciseLog did not have the correct number of attempts (%d)." % self.NEW_ATTEMPTS)

        # save a new record onto the exercise log, with an incorrect answer (decreasing the points and streak)
        result = c.save_exercise_log(
            exercise_id=self.EXERCISE_ID,
            streak_progress=self.NEW_STREAK_PROGRESS_SMALLER,
            points=self.NEW_POINTS_SMALLER,
            correct=False,
            attempts=self.NEW_ATTEMPTS + 1,
        )
        self.assertEqual(result.status_code, 200, "An error (%d) was thrown while saving the exercise log." % result.status_code)
        
        # get a reference to the updated ExerciseLog
        exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID, user__username=self.USERNAME)
        
        # make sure the ExerciseLog was properly updated
        self.assertEqual(exerciselog.points, self.NEW_POINTS_SMALLER, "The ExerciseLog's points were not saved correctly.")
        self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_SMALLER, "The ExerciseLog's streak progress was not saved correctly.")
        self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS + 1, "The ExerciseLog did not have the correct number of attempts.")
Exemple #46
0
class TestSaveVideoLog(KALiteTestCase):

    ORIGINAL_POINTS = 84
    ORIGINAL_SECONDS_WATCHED = 32
    NEW_POINTS = 32
    NEW_SECONDS_WATCHED = 15
    YOUTUBE_ID = "aNqG4ChKShI"
    YOUTUBE_ID2 = "b22tMEc6Kko"
    USERNAME = "******"
    PASSWORD = "******"

    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(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_new_videolog(self):

        # make sure the target video log does not already exist
        videologs = VideoLog.objects.filter(youtube_id=self.YOUTUBE_ID2,
                                            user__username=self.USERNAME)
        self.assertEqual(
            videologs.count(), 0,
            "The target video log to be newly created already exists")

        c = KALiteClient()

        # login
        success = c.login(username=self.USERNAME,
                          password=self.PASSWORD,
                          facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")

        # save a new video log
        result = c.save_video_log(
            youtube_id=self.YOUTUBE_ID2,
            total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED,
            points=self.NEW_POINTS,
        )
        self.assertEqual(
            result.status_code, 200,
            "An error (%d) was thrown while saving the video log." %
            result.status_code)

        # get a reference to the newly created VideoLog
        videolog = VideoLog.objects.get(youtube_id=self.YOUTUBE_ID2,
                                        user__username=self.USERNAME)

        # make sure the VideoLog was properly created
        self.assertEqual(videolog.points, self.NEW_POINTS,
                         "The VideoLog's points were not saved correctly.")
        self.assertEqual(
            videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED,
            "The VideoLog's seconds watched was not saved correctly.")

    def test_update_videolog(self):

        # get a new reference to the existing VideoLog
        videolog = VideoLog.objects.get(id=self.original_videolog.id)

        # make sure the VideoLog hasn't already been changed
        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 seconds watched already changed.")

        c = KALiteClient()

        # login
        success = c.login(username=self.USERNAME,
                          password=self.PASSWORD,
                          facility=self.facility.id)
        self.assertTrue(success, "Was not able to login as the test user")

        # save a new record onto the video log, with a correct answer (increasing the points and streak)
        result = c.save_video_log(
            youtube_id=self.YOUTUBE_ID,
            total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED +
            self.NEW_SECONDS_WATCHED,
            points=self.ORIGINAL_POINTS + self.NEW_POINTS,
        )
        self.assertEqual(
            result.status_code, 200,
            "An error (%d) was thrown while saving the video log." %
            result.status_code)

        # get a reference to the updated VideoLog
        videolog = VideoLog.objects.get(youtube_id=self.YOUTUBE_ID,
                                        user__username=self.USERNAME)

        # make sure the VideoLog was properly updated
        self.assertEqual(videolog.points,
                         self.ORIGINAL_POINTS + self.NEW_POINTS,
                         "The VideoLog's points were not updated correctly.")
        self.assertEqual(
            videolog.total_seconds_watched,
            self.ORIGINAL_SECONDS_WATCHED + self.NEW_SECONDS_WATCHED,
            "The VideoLog's seconds watched was not updated correctly.")
Exemple #47
0
 def test_set_password_raw_ok(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG; settings.DEBUG=True
     fu.set_password(raw_password="******")
     settings.DEBUG = dbg_mode 
     self.assertTrue(fu.check_password("blah"))
Exemple #48
0
class TestVideoLogs(KALiteTestCase):

    ORIGINAL_POINTS = 37
    ORIGINAL_SECONDS_WATCHED = 3
    NEW_POINTS = 22
    NEW_SECONDS_WATCHED = 5
    YOUTUBE_ID = "aNqG4ChKShI"
    VIDEO_ID = i18n.get_video_id(YOUTUBE_ID) or "dummy"

    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 test_videolog_update(self):

        # get a new reference to the existing VideoLog
        videolog = VideoLog.objects.get(id=self.original_videolog.id)

        # update the VideoLog
        videolog.points = self.NEW_POINTS
        videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED
        videolog.save()

        # get a new reference to the existing VideoLog
        videolog2 = VideoLog.objects.get(id=self.original_videolog.id)

        # make sure the VideoLog was updated
        self.assertEqual(videolog2.points, self.NEW_POINTS, "The VideoLog's points were not updated.")
        self.assertEqual(videolog2.total_seconds_watched, self.NEW_SECONDS_WATCHED, "The VideoLog's total seconds watched were not updated.")

    @unittest.skip("Auto-merging is not yet automatic, so skip this")
    def test_videolog_collision(self):

        # create a new video log with the same youtube_id and user, but different points/total seconds watched
        videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user)
        videolog.points = self.NEW_POINTS
        videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED

        # try saving the new VideoLog: this is where the collision will happen, hopefully leading to a merge
        videolog.save()

        # get a new reference to the existing VideoLog
        videolog2 = VideoLog.objects.get(id=self.original_videolog.id)

        # make sure the VideoLog has been properly merged
        self.assertEqual(videolog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The VideoLog's points were not properly merged.")
        self.assertEqual(videolog.total_seconds_watched, max(self.ORIGINAL_ATTEMPTS, self.NEW_SECONDS_WATCHED), "The VideoLog's total seconds watched have already changed.")
Exemple #49
0
    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()
Exemple #50
0
 def test_set_password_both_bad(self):
     fu = FacilityUser(username="******")
     dbg_mode = settings.DEBUG; settings.DEBUG=True
     with self.assertRaises(AssertionError):
         fu.set_password(raw_password="******", hashed_password=self.__class__.hashed_blah)
     settings.DEBUG = dbg_mode