Ejemplo n.º 1
0
    def test_no_tos_exists(self):
        # Delete TOS
        tos = Tos.objects.all().first()
        tos.delete()
        response = self.question_get()
        # This will raise error in tos models.py as no TOS exists and we want
        # custom error page to be rendered with message from app
        self.assertTemplateUsed(response, "500.html")
        self.assertContains(response,
                            "There is no terms of service yet.",
                            status_code=500)

        # create role
        role = Role.objects.get(role="student")

        # Add TOS
        new_TOS = Tos(version=1, text="Test", current=True, role=role)
        new_TOS.save()
        response = self.question_get()
        self.assertRedirects(
            response,
            reverse("tos:tos_consent", kwargs={"role": "student"}) + "?next=" +
            self.question_url,
            status_code=302,
            target_status_code=200,
        )
Ejemplo n.º 2
0
    def setUp(self):
        self.validated_teacher = ready_user(1)
        self.other_teacher = ready_user(2)
        self.inactive_user = ready_user(3)
        self.guest = ready_user(11)

        # Skip TOS interactions
        role = Role.objects.get(role="teacher")
        tos = Tos(version=1, text="Test", current=True, role=role)
        tos.save()
        consent = Consent(
            user=self.validated_teacher, accepted=True, tos=Tos.objects.first()
        )
        consent.save()

        self.group = Group.objects.get(name="Teacher")
        self.assertFalse(self.group.permissions.all())

        permission = Permission.objects.get(codename="add_question")
        self.group.permissions.add(permission)
        permission = Permission.objects.get(codename="change_question")
        self.group.permissions.add(permission)
        self.assertEqual(self.group.permissions.count(), 2)

        self.assertFalse(self.validated_teacher.get_all_permissions())
Ejemplo n.º 3
0
    def setUp(self):

        if hasattr(settings, "TESTING_BROWSER"):
            browser = settings.TESTING_BROWSER.lower()
        else:
            browser = "firefox"

        if hasattr(settings, "HEADLESS_TESTING") and settings.HEADLESS_TESTING:
            os.environ["MOZ_HEADLESS"] = "1"
            options = webdriver.ChromeOptions()
            options.add_argument("headless")
        else:
            options = webdriver.ChromeOptions()

        if browser == "firefox":
            try:
                self.browser = webdriver.Firefox()
            except WebDriverException:
                self.browser = webdriver.Chrome(options=options)
        elif browser == "chrome":
            try:
                self.browser = webdriver.Chrome(options=options)
            except WebDriverException:
                self.browser = webdriver.Firefox()
        else:
            raise ValueError(
                "The TESTING_BROWSER setting in local_settings.py must either "
                "be firefox or chrome."
            )

        self.browser.implicitly_wait(10)

        self.validated_teacher = ready_user(1)
        self.inactive_user = ready_user(3)

        self.group = Group.objects.get(name="Teacher")
        self.assertFalse(self.group.permissions.all())

        permission = Permission.objects.get(codename="add_question")
        self.group.permissions.add(permission)
        permission = Permission.objects.get(codename="change_question")
        self.group.permissions.add(permission)
        self.assertEqual(self.group.permissions.count(), 2)

        self.assertFalse(self.validated_teacher.get_all_permissions())

        # Add TOS for teachers
        role = Role.objects.get(role="teacher")
        new_TOS = Tos(version=1, text="Test", current=True, role=role)
        new_TOS.save()
Ejemplo n.º 4
0
def test_get_tos(toss_multiple_roles, roles):
    tests = [{
        "role": role.role,
        "version": version
    } for role in roles for version in [None, 0]]

    for test in tests:
        tos, err = Tos.get(**test)
        assert isinstance(tos, Tos)
        assert err is None
        if test["version"] is None:
            correct = [
                t for t in toss_multiple_roles
                if t.role.role == test["role"] and t.current
            ][0]
        else:
            correct = [
                t for t in toss_multiple_roles
                if t.role.role == test["role"] and t.version == test["version"]
            ][0]
        assert tos.version == correct.version
        assert tos.role == correct.role
        assert tos.version == correct.version
        assert tos.hash == correct.hash
        assert tos.text == correct.text
        assert tos.created == correct.created
Ejemplo n.º 5
0
def test_get_tos_version_doesnt_exist(tos):
    tests = [{"role": tos.role.role, "version": 1}]

    for test in tests:
        tos_, err = Tos.get(**test)
        assert tos_ is None
        assert err == ("There is no terms of service with version "
                       "{} for role {}".format(test["version"], test["role"]))
Ejemplo n.º 6
0
def test_get_tos__no_version(role):
    tests = [{"role": role.role, "version": None}]

    for test in tests:
        tos_, err = Tos.get(**test)
        assert tos_ is None
        assert err == ("No terms of service exist yet for role {}.".format(
            test["role"]))
Ejemplo n.º 7
0
    def setUp(self):
        super(QuestionViewTestCase, self).setUp()

        UsesCriterion.objects.filter(
            quality__quality_type__type="global").delete()

        # TOS integration
        from tos.models import Consent, Role, Tos

        role = Role.objects.get(role="student")

        tos = Tos(version=1, text="Test", current=True, role=role)
        tos.save()
        no_share_user = User(username="******", email="*****@*****.**")
        no_share_user.save()
        # To test latest consent is used
        consent = Consent(user=no_share_user,
                          accepted=True,
                          tos=Tos.objects.first())
        consent.save()
        no_consent = Consent(user=no_share_user,
                             accepted=False,
                             tos=Tos.objects.first())
        no_consent.save()

        self.user = factories.UserFactory()
        self.assignment = factories.AssignmentFactory()
        self.set_question(
            factories.QuestionFactory(choices=5,
                                      choices__correct=[2, 4],
                                      choices__rationales=4))
        self.add_user_to_even_answers()
        self.addCleanup(mock.patch.stopall)
        signal_patcher = mock.patch(
            "django_lti_tool_provider.signals.Signals.Grade.updated.send")
        self.mock_send_grade_signal = signal_patcher.start()
        grade_patcher = mock.patch("peerinst.models.Answer.grade",
                                   new_callable=mock.PropertyMock)
        self.mock_grade = grade_patcher.start()
        self.mock_grade.return_value = Grade.CORRECT
Ejemplo n.º 8
0
def test_new_teacher(browser, assert_, new_teacher, tos_teacher):

    # Teacher can login and access account
    browser.get(browser.server_url + "/login")

    inputbox = browser.find_element_by_id("id_username")
    inputbox.send_keys(new_teacher.user.username)

    inputbox = browser.find_element_by_id("id_password")
    inputbox.send_keys("default_password")

    browser.find_element_by_id("submit-btn").click()

    # Redirected to dashboard
    assert browser.current_url.endswith("dashboard/")

    browser.find_element_by_id("accept-cookies").click()

    go_to_account(browser)

    # Access to account redirected to TOS if no TOS registered
    browser.wait_for(lambda: assert_("Terms of Service" in browser.
                                     find_elements_by_tag_name("h1")[0].text))

    browser.find_element_by_id("tos-accept").click()

    # Redirected to My Account and show TOS status
    browser.wait_for(lambda: assert_("My Account" in browser.
                                     find_elements_by_tag_name("h1")[0].text))
    assert "Terms of service: Sharing" in browser.page_source

    # Welcome authenticated user on landing page
    browser.get(browser.server_url)
    welcome = browser.find_element_by_id("link-to-login-or-welcome")
    browser.wait_for(lambda: assert_("Welcome back " + new_teacher.user.
                                     username in welcome.text))

    # Logout and log back in -> skip tos step
    browser.get(browser.server_url + "/logout")
    browser.get(browser.server_url + "/login")

    inputbox = browser.find_element_by_id("id_username")
    inputbox.send_keys(new_teacher.user.username)

    inputbox = browser.find_element_by_id("id_password")
    inputbox.send_keys("default_password")

    browser.find_element_by_id("submit-btn").click()

    go_to_account(browser)

    browser.wait_for(lambda: assert_("My Account" in browser.
                                     find_elements_by_tag_name("h1")[0].text))

    # Add a new current TOS for teachers and refresh account -> back to tos
    role = Role.objects.get(role="teacher")
    new_TOS = Tos(version=2, text="Test 2", current=True, role=role)
    new_TOS.save()

    browser.get(browser.server_url + "/login")

    go_to_account(browser)

    browser.wait_for(lambda: assert_("Terms of Service" in browser.
                                     find_elements_by_tag_name("h1")[0].text))
    browser.find_element_by_id("tos-accept").click()

    # Teacher generally redirected to welcome page if logged in
    browser.get(browser.server_url + "/login")

    # Redirected to dashboard
    assert browser.current_url.endswith("dashboard/")
Ejemplo n.º 9
0
    def test_teacher(self):
        # Teacher can login and access account
        self.browser.get(self.live_server_url + "/login")
        inputbox = self.browser.find_element_by_id("id_username")
        inputbox.send_keys(self.validated_teacher.username)

        inputbox = self.browser.find_element_by_id("id_password")
        inputbox.send_keys(self.validated_teacher.text_pwd)

        inputbox.submit()

        try:
            account_link = WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//a[text()='Go to My Account']")
                )
            )
        except TimeoutException:
            assert False

        self.browser.get(account_link.get_attribute("href"))

        try:
            WebDriverWait(self.browser, timeout).until(
                text_to_be_present_in_element(
                    (By.TAG_NAME, "h1"), "Terms of Service"
                )
            )
        except TimeoutException:
            assert False

        button = self.browser.find_element_by_id("tos-accept")
        button.click()

        assert "My Account" in self.browser.page_source
        assert "Terms of service: Sharing" in self.browser.page_source

        # Welcome authenticated user on landing pages
        self.browser.get(self.live_server_url)
        welcome = self.browser.find_element_by_id("link-to-login-or-welcome")
        assert (
            "Welcome back " + self.validated_teacher.username in welcome.text
        )

        # Logout and log back in -> skip tos
        self.browser.get(self.live_server_url + "/logout")
        self.browser.get(self.live_server_url + "/login")
        inputbox = self.browser.find_element_by_id("id_username")
        inputbox.send_keys(self.validated_teacher.username)

        inputbox = self.browser.find_element_by_id("id_password")
        inputbox.send_keys(self.validated_teacher.text_pwd)

        inputbox.submit()

        account_link = self.browser.find_element_by_xpath(
            "//a[text()='Go to My Account']"
        )
        self.browser.get(account_link.get_attribute("href"))

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h1[text()='My Account']")
                )
            )
        except TimeoutException:
            assert False

        # Add a new current TOS for teachers and refresh account -> tos
        role = Role.objects.get(role="teacher")
        new_TOS = Tos(version=2, text="Test 2", current=True, role=role)
        new_TOS.save()

        self.browser.get(self.live_server_url + "/login")

        account_link = self.browser.find_element_by_xpath(
            "//a[text()='Go to My Account']"
        )
        self.browser.get(account_link.get_attribute("href"))

        assert "Terms of Service" in self.browser.page_source

        button = self.browser.find_element_by_id("tos-accept")
        button.click()

        # Teacher generally redirected to account if logged in
        self.browser.get(self.live_server_url + "/login")

        account_link = self.browser.find_element_by_xpath(
            "//a[text()='Go to My Account']"
        )
        self.browser.get(account_link.get_attribute("href"))

        assert "My Account" in self.browser.page_source

        # Teacher can create a question
        self.browser.find_element_by_id("question-section").click()
        self.browser.find_element_by_link_text("Create new").click()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h2[contains(text(), 'Step 1')]")
                )
            )
        except TimeoutException:
            assert False

        assert "Step 1" in self.browser.find_element_by_tag_name("h2").text

        inputbox = self.browser.find_element_by_id("id_title")
        inputbox.send_keys("Test title")

        tinymce_embed = self.browser.find_element_by_tag_name("iframe")
        self.browser.switch_to.frame(tinymce_embed)
        ifrinputbox = self.browser.find_element_by_id("tinymce")
        ifrinputbox.send_keys("Test text")
        self.browser.switch_to.default_content()

        inputbox.submit()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h2[contains(text(), 'Step 2')]")
                )
            )
        except TimeoutException:
            assert False

        tinymce_embed = self.browser.find_element_by_id(
            "id_answerchoice_set-0-text_ifr"
        )
        self.browser.switch_to.frame(tinymce_embed)
        ifrinputbox = self.browser.find_element_by_id("tinymce")
        ifrinputbox.send_keys("Answer 1")
        self.browser.switch_to.default_content()

        tinymce_embed = self.browser.find_element_by_id(
            "id_answerchoice_set-1-text_ifr"
        )
        self.browser.switch_to.frame(tinymce_embed)
        ifrinputbox = self.browser.find_element_by_id("tinymce")
        ifrinputbox.send_keys("Answer 2")
        self.browser.switch_to.default_content()

        self.browser.find_element_by_id(
            "id_answerchoice_set-0-correct"
        ).click()

        inputbox = self.browser.find_element_by_id("answer-choice-form")

        inputbox.submit()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h2[contains(text(), 'Step 3')]")
                )
            )
        except TimeoutException:
            assert False

        self.browser.find_element_by_id("add_question_to_assignment").submit()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h1[text()='My Account']")
                )
            )
        except TimeoutException:
            assert False
        assert "Test title" in self.browser.page_source

        # Teacher can edit their questions
        try:
            WebDriverWait(self.browser, timeout).until(
                element_to_be_clickable((By.ID, "question-section"))
            ).click()
        except TimeoutException:
            assert False
        question = Question.objects.get(title="Test title")

        try:
            WebDriverWait(self.browser, timeout).until(
                element_to_be_clickable(
                    (By.ID, "edit-question-{}".format(question.id))
                )
            ).click()
        except TimeoutException:
            assert False

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h2[contains(text(), 'Step 1')]")
                )
            )
        except TimeoutException:
            assert False

        assert "Step 1" in self.browser.find_element_by_tag_name("h2").text

        tinymce_embed = self.browser.find_element_by_tag_name("iframe")
        self.browser.switch_to.frame(tinymce_embed)
        ifrinputbox = self.browser.find_element_by_id("tinymce")
        ifrinputbox.send_keys("Edited: ")
        self.browser.switch_to.default_content()

        inputbox = self.browser.find_element_by_id("id_title")
        inputbox.submit()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (By.XPATH, "//h2[contains(text(), 'Step 2')]")
                )
            )
        except TimeoutException:
            assert False

        question.refresh_from_db()

        assert "Edited: Test text" in question.text

        # Teacher cannot edit another teacher's questions
        self.browser.get(
            self.live_server_url
            + reverse("question-update", kwargs={"pk": 43})
        )
        assert "Forbidden" in self.browser.page_source

        # Teacher can create an assignment
        self.browser.get(
            self.live_server_url + reverse("teacher", kwargs={"pk": 1})
        )
        self.browser.find_element_by_id("assignment-section").click()
        self.browser.find_element_by_link_text("Manage assignments").click()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (
                        By.XPATH,
                        "//h2[contains(text(), 'Create a new assignment')]",
                    )
                )
            )
        except TimeoutException:
            assert False

        inputbox = self.browser.find_element_by_id("id_identifier")
        inputbox.send_keys("new-unique-assignment-identifier")

        inputbox = self.browser.find_element_by_id("id_title")
        inputbox.send_keys("New assignment title")

        inputbox.submit()

        try:
            WebDriverWait(self.browser, timeout).until(
                presence_of_element_located(
                    (
                        By.XPATH,
                        "//*[contains(text(), "
                        "'new-unique-assignment-identifier')]",
                    )
                )
            )
        except TimeoutException:
            assert False

        assert (
            Assignment.objects.filter(
                identifier="new-unique-assignment-identifier"
            ).count()
            == 1
        )

        # Teacher can edit an assignment

        # Teacher can create a blink assignment

        # Teacher can delete a blink assignment

        # Teacher can edit a blink assignment

        # Access account from link in top right corner

        # Teacher cannot access other teacher accounts
        self.browser.get(
            self.live_server_url + reverse("teacher", kwargs={"pk": 2})
        )
        assert "Forbidden" in self.browser.page_source