Beispiel #1
0
    def obj_get_list(self, bundle, **kwargs):
        """
        Default options will be "All" groups and if a facility has ungrouped students, also add an "Ungrouped" option.
        Then we add the list of facilities after the above default options.

        Must return no groups if there are no groups for the user.
        """

        # Call with facility=None argument to get all facilities and to determine if has ungrouped students.
        (groups, facilities,
         ungrouped_available) = get_accessible_objects_from_logged_in_user(
             bundle.request, facility=None)

        # Filter records only for all facilities accessible for the user
        qs = []
        if facilities:
            facility_ids = facilities.values_list("id", flat=True)
            # Get the facility requested by client, if there is any, and validate it
            # from the list of facilities for the user.
            facility_id = bundle.request.GET.get('facility_id')
            if facility_id and facility_id != ID_NONE:
                if facility_id in facility_ids:
                    facility = Facility.objects.get(id=facility_id)
                    ungrouped_available = facility.has_ungrouped_students
                    qs = FacilityGroup.objects.filter(facility__id=facility_id)
                else:
                    raise BadRequest(
                        "No facility found with that facility_id.")
            else:
                qs = FacilityGroup.objects.filter(
                    facility__id__in=facility_ids)

        # Flag to return only the FacilityGroup objects and not including the "All" and "Ungrouped" options.
        # TODO(cpauya): how to convert this into a kwargs above instead of a request.GET?
        groups_only = bundle.request.GET.get("groups_only", True)
        if groups_only:
            group_list = list(qs)
        else:
            default_list = []
            if qs or ungrouped_available:
                # add the "All" option
                default_list = [FacilityGroup(id=ALL_KEY, name=_("All"))]

                # add the "Ungrouped" option
                if ungrouped_available:
                    fg = FacilityGroup(id=UNGROUPED_KEY, name=_("Ungrouped"))
                    default_list.append(fg)
            # add all the facility group options for the user
            group_list = default_list + list(qs)

        # call super to trigger auth
        return super(FacilityGroupResource,
                     self).authorized_read_list(group_list, bundle)
Beispiel #2
0
 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.")
Beispiel #3
0
    def test_facility_group_save(self):

        # only perform test if we are ourselves a trusted (i.e. central server) device
        if Device.get_own_device().is_trusted():
            group = FacilityGroup(name="MyGroup", facility=self.facility)
            group.save()
            assert group.zone_fallback is not None, "Centrally created FacilityGroup was not assigned a zone."
Beispiel #4
0
	def setUp(self):
		'''Performed before every test'''
	
		super(TestNextMethods, self).setUp()
	
		self.EXERCISE_ID = self.content_exercises[0].id
		self.EXERCISE_ID2 = self.content_exercises[1].id
		self.EXERCISE_ID_STRUGGLE = self.content_exercises[2].id
		
		# 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()
Beispiel #5
0
 def test_groups_group_selected_no_topic_selected(self):
     group = FacilityGroup(name="Test Group", facility=self.facility)
     group.save()
     self.browser_login_admin()
     self.browse_to(self.reverse("tabular_view") + "?group=" + group.id)
     self.browser.find_element_by_css_selector('#error_message')
     self.assertEqual(
         self.browser.find_element_by_css_selector('#error_message').text,
         _("Please select a topic above."),
         "Error message with no topic selected.")
Beispiel #6
0
    def test_description_exclusion_regression_bug_2470(self):
        """FacilityGroup.__init__ used to append "description" to _unhashable_fields, which affected other classes as well.
        This test ensures that the description is not being excluded from Device._hashable_representation, even after
        instantiating a FacilityGroup."""

        d = Device(name="Test", description="Test")
        possibly_bad_serialization = d._hashable_representation()
        self.assertIn("description=Test", possibly_bad_serialization, "Hashable representation of Device did not include description")

        g = FacilityGroup()
        possibly_worse_serialization = d._hashable_representation()
        self.assertIn("description=Test", possibly_worse_serialization, "Instantiating a FacilityGroup changed hashable representation of Device")
Beispiel #7
0
 def test_groups_group_selected_topic_selected_no_users(self):
     group = FacilityGroup(name="Test Group", facility=self.facility)
     group.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.")
Beispiel #8
0
    def setUp(self):
        super(TestTabularViewErrors, self).setUp()

        self.admin_data = {"username": "******", "password": "******"}
        self.admin = self.create_admin(**self.admin_data)

        self.facility = self.create_facility()

        self.group = FacilityGroup(name="Test Group", facility=self.facility)
        self.group.save()

        self.url = self.reverse("tabular_view")
        self.topic = "addition-subtraction"
Beispiel #9
0
 def test_success_with_group(self):
     group = FacilityGroup(name="Test Group", facility=self.facility)
     group.save()
     fu = FacilityUser(username="******",
                       facility=self.facility,
                       group=group)
     fu.set_password(raw_password="******")
     fu.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')
Beispiel #10
0
 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.")
Beispiel #11
0
 def test_groups_one_group_no_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()
     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, "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, "0", "Does not report zero users for empty group.")
Beispiel #12
0
def generate_fake_facility_groups(names=("Class 4E", "Class 5B"), facilities=None):
    """Add the given fake facility groups to the given fake facilities"""

    if not facilities:
        facilities = generate_fake_facilities()

    facility_groups = []
    for facility in facilities:
        for name in names:
            found_facility_groups = FacilityGroup.objects.filter(facility=facility, name=name)
            if found_facility_groups:
                facility_group = found_facility_groups[0]
                logging.info("Retrieved facility group '%s'" % name)
            else:
                facility_group = FacilityGroup(facility=facility, name=name)
                facility_group.save()
                logging.info("Created facility group '%s'" % name)

            facility_groups.append(facility_group)

    return (facility_groups, facilities)
Beispiel #13
0
 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.")
Beispiel #14
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(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.")