def test_admin_teacher_can_see_all_classes(self):
        """An admin should be able to see all classes, not just the ones they teach"""
        normal_teacher = Teacher.objects.factory("Mr", "Normal", "Teacher",
                                                 "*****@*****.**",
                                                 "secretpa$sword")
        admin_teacher = Teacher.objects.factory("Mrs", "Admin", "Admin",
                                                "*****@*****.**",
                                                "secretpa$sword2")

        admin_teacher.is_admin = True
        admin_teacher.save()

        klass1, name1, access_code1 = create_class_directly(
            admin_teacher.user.user.email, class_name="Class 1")
        klass2, name2, access_code2 = create_class_directly(
            admin_teacher.user.user.email, class_name="Class 2")
        klass3, name3, access_code3 = create_class_directly(
            normal_teacher.user.user.email, class_name="Class 3")

        c = Client()
        c.login(username=admin_teacher.user.user.email,
                password="******")

        url = reverse("scoreboard")
        response = c.get(url)

        choices_in_form = [
            v for (k, v) in response.context["form"]["classes"].field.choices
        ]
        assert_that(choices_in_form,
                    contains_inanyorder("Class 1", "Class 2", "Class 3"))
Beispiel #2
0
    def setUpTestData(cls):
        cls.user: User = User.objects.create_user("test", "*****@*****.**",
                                                  "password")
        cls.user.is_staff = True
        cls.user.save()
        user_profile: UserProfile = UserProfile(user=cls.user)
        user_profile.save()
        teacher: Teacher = Teacher.objects.create(user=user_profile,
                                                  new_user=cls.user,
                                                  title="Mx")
        teacher.save()
        cls.klass, _, _ = create_class_directly(cls.user.email)
        cls.klass.save()
        cls.worksheet = Worksheet.objects.create(name="test worksheet",
                                                 starter_code="test code")
        cls.game = models.Game(id=1,
                               name="test",
                               game_class=cls.klass,
                               worksheet=cls.worksheet)
        cls.game.save()

        cls.EXPECTED_GAME_DETAIL = {
            "era": "1",
            "name": "test",
            "status": "r",
            "settings":
            '{"GENERATOR": "Main", "OBSTACLE_RATIO": 0.1, "PICKUP_SPAWN_CHANCE": 0.1, "SCORE_DESPAWN_CHANCE": 0.05, "START_HEIGHT": 31, "START_WIDTH": 31, "TARGET_NUM_CELLS_PER_AVATAR": 16.0, "TARGET_NUM_PICKUPS_PER_AVATAR": 1.2, "TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR": 0.5}',
            "worksheet_id": str(cls.worksheet.id),
        }
        cls.EXPECTED_GAME_LIST = {
            "1": cls.EXPECTED_GAME_DETAIL,
            "2": cls.EXPECTED_GAME_DETAIL
        }
    def test_anonymous_level_saving_school_student(self):
        email, password = signup_teacher_directly()
        create_organisation_directly(email)
        _, class_name, access_code = create_class_directly(email)
        student_name, student_password, _ = create_school_student_directly(
            access_code)

        self.student_login(student_name, access_code, student_password)
        url = reverse("save_level_for_editor")
        data1 = {
            u"origin":
            u'{"coordinate":[3,5],"direction":"S"}',
            u"pythonEnabled":
            False,
            u"decor": [],
            u"blocklyEnabled":
            True,
            u"blocks": [
                {
                    u"type": u"move_forwards"
                },
                {
                    u"type": u"turn_left"
                },
                {
                    u"type": u"turn_right"
                },
            ],
            u"max_fuel":
            u"50",
            u"pythonViewEnabled":
            False,
            u"character":
            u"3",
            u"name":
            u"abc",
            u"theme":
            1,
            u"anonymous":
            True,
            u"cows":
            u"[]",
            u"path":
            u'[{"coordinate":[3,5],"connectedNodes":[1]},{"coordinate":[3,4],'
            u'"connectedNodes":[0]}]',
            u"traffic_lights":
            u"[]",
            u"destinations":
            u"[[3,4]]",
        }
        response = self.client.post(url, {"data": json.dumps(data1)})

        assert_that(response.status_code, equal_to(200))
        sharing_info1 = json.loads(
            self.get_sharing_information(json.loads(
                response.content)["id"]).getvalue())
        assert_that(sharing_info1["teacher"]["shared"], equal_to(True))
        assert_that(len(mail.outbox), equal_to(0))
    def test_non_admin_teacher_can_only_see_their_own_classes(self):
        """A teacher who is not an admin should only be able to see their classes, not ones taught by others"""
        teacher1 = Teacher.objects.factory("Mr", "First", "Teacher",
                                           "*****@*****.**",
                                           "secretpa$sword")
        teacher2 = Teacher.objects.factory("Mrs", "Second", "Teacher",
                                           "*****@*****.**",
                                           "secretpa$sword2")

        klass1, name1, access_code1 = create_class_directly(
            teacher2.user.user.email, class_name="Class 1")
        klass2, name2, access_code2 = create_class_directly(
            teacher2.user.user.email, class_name="Class 2")
        klass3, name3, access_code3 = create_class_directly(
            teacher1.user.user.email, class_name="Class 3")

        c = Client()
        # First teacher logs in.  Should see only Class 3
        c.login(username="******", password="******")

        url = reverse("scoreboard")
        response = c.get(url)

        choices_in_form = [
            v for (k, v) in response.context["form"]["classes"].field.choices
        ]

        assert_that(choices_in_form, contains("Class 3"))
        assert_that(choices_in_form,
                    not_(contains_inanyorder("Class 1", "Class 2")))
        assert_that(choices_in_form, has_length(1))

        # Other teacher logs in.  Should see Class 1 and Class 2
        c.login(username="******", password="******")

        response = c.get(url)
        choices_in_form = [
            v for (k, v) in response.context["form"]["classes"].field.choices
        ]

        assert_that(choices_in_form, not_(contains("Class 3")))
        assert_that(choices_in_form, contains_inanyorder("Class 1", "Class 2"))
        assert_that(choices_in_form, has_length(2))
def set_up_data(classmates_data_viewable=False):
    email, password = signup_teacher_directly()
    clas, class_name, access_code = create_class_directly(email)
    if classmates_data_viewable:
        clas.classmates_data_viewable = True
        clas.save()
    _, _, student = create_school_student_directly(access_code)
    _, _, student2 = create_school_student_directly(access_code)
    create_random_school_data()
    return clas, student, student2
 def test_scoreboard_loads(self):
     email, password = signup_teacher_directly()
     create_organisation_directly(email)
     klass, name, access_code = create_class_directly(email)
     create_school_student_directly(access_code)
     url = reverse("scoreboard")
     c = Client()
     c.login(username=email, password=password)
     response = c.get(url)
     self.assertEqual(200, response.status_code)
    def test_moderation_another_class(self):
        email, password = signup_teacher_directly()

        email2, _ = signup_teacher_directly()
        klass2, _, access_code = create_class_directly(email2)

        student_name, _, student = create_school_student_directly(access_code)

        self.login(email, password)
        response = self.students_of_class(klass2)
        assert_that(response.status_code, equal_to(404))
        assert_that(response.content, empty)
    def test_moderation_teachers_class(self):
        email, password = signup_teacher_directly()
        klass, name, access_code = create_class_directly(email)

        student_name, _, student = create_school_student_directly(access_code)

        self.login(email, password)
        response = self.students_of_class(klass)
        assert_that(response.status_code, equal_to(200))
        assert_that(
            response.content.decode(),
            equal_to('{"%s": "%s"}' % (student.id, student_name)),
        )
Beispiel #9
0
    def test_list_running_games(self):
        self.game.main_user = self.user
        self.game.save()

        klass3, _, _ = create_class_directly(self.user.email)
        klass3.save()
        klass4, _, _ = create_class_directly(self.user.email)
        klass4.save()

        game2 = Game(id=2,
                     name="test",
                     game_class=self.klass2,
                     status=Game.STOPPED)
        game2.save()
        game3 = Game(id=3, name="test", game_class=klass3, status=Game.RUNNING)
        game3.save()
        game4 = Game(id=4, name="test", game_class=klass4, status=Game.STOPPED)
        game3.save()

        def expected_game_detail(class_id, worksheet_id):
            return {
                "era": "1",
                "name": "test",
                "status": "r",
                "settings":
                '{"GENERATOR": "Main", "OBSTACLE_RATIO": 0.1, "PICKUP_SPAWN_CHANCE": 0.1, "SCORE_DESPAWN_CHANCE": 0.05, "START_HEIGHT": 31, "START_WIDTH": 31, "TARGET_NUM_CELLS_PER_AVATAR": 16.0, "TARGET_NUM_PICKUPS_PER_AVATAR": 1.2, "TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR": 0.5}',
                "class_id": str(class_id),
                "worksheet_id": str(worksheet_id),
            }

        expected_game_list = {
            "1": expected_game_detail(self.klass.id, self.worksheet.id),
            "3": expected_game_detail(klass3.id, 1),
        }

        c = Client()
        response = c.get(reverse("game-running"))

        self.assertJSONEqual(response.content, expected_game_list)
Beispiel #10
0
 def setUpTestData(cls):
     cls.user: User = User.objects.create_user("test", "*****@*****.**",
                                               "password")
     cls.user.is_staff = True
     cls.user.save()
     user_profile: UserProfile = UserProfile(user=cls.user)
     user_profile.save()
     teacher: Teacher = Teacher.objects.create(user=user_profile,
                                               new_user=cls.user)
     teacher.save()
     cls.klass, _, _ = create_class_directly(cls.user.email)
     cls.klass.save()
     cls.klass2, _, _ = create_class_directly(cls.user.email)
     cls.klass2.save()
     cls.worksheet: Worksheet = Worksheet.objects.create(
         name="test worksheet", starter_code="test code 1")
     cls.worksheet2: Worksheet = Worksheet.objects.create(
         name="test worksheet 2", starter_code="test code 2")
     cls.game = models.Game(id=1,
                            name="test",
                            game_class=cls.klass,
                            worksheet=cls.worksheet)
     cls.game.save()
Beispiel #11
0
    def login_once(self):
        if not BaseGameTest.already_logged_on:
            email, password = signup_teacher_directly()
            create_organisation_directly(email)
            klass, name, access_code = create_class_directly(email)
            create_school_student_directly(access_code)
            login_page = self.go_to_homepage().go_to_teacher_login_page()
            login_page.login(email, password)
            email = email
            BaseGameTest.user_profile = UserProfile.objects.get(user__email=email)

            BaseGameTest.already_logged_on = True

        return BaseGameTest.user_profile
Beispiel #12
0
    def test_student_can_see_classes(self):
        """A student should be able to see the classes they are in"""
        mr_teacher = Teacher.objects.factory("Mr", "Normal", "Teacher",
                                             "*****@*****.**",
                                             "secretpa$sword")
        klass1, name1, access_code1 = create_class_directly(
            mr_teacher.user.user.email, class_name="Class 1")
        klass2, name2, access_code2 = create_class_directly(
            mr_teacher.user.user.email, class_name="Class 2")
        student = Student.objects.schoolFactory(klass1, "some student",
                                                "secret")

        c = Client()
        c.force_login(student.user.user)

        url = reverse("scoreboard")
        response = c.get(url)

        choices_in_form = [
            v for (k, v) in response.context["form"]["classes"].field.choices
        ]
        assert_that(choices_in_form, contains("Class 1"))
        assert_that(choices_in_form, not_(contains("Class 2")))
        assert_that(choices_in_form, has_length(1))
Beispiel #13
0
def test_cannot_add_game_for_classes_not_given_to_form(class1: Class,
                                                       worksheet: Worksheet,
                                                       teacher1_email: str):
    # Make query set for form
    class_query_set = Class.objects.filter(id=class1.id)

    # Create class not in the query set
    klass, _, _ = create_class_directly(teacher1_email)

    form = AddGameForm(
        class_query_set,
        data={
            "name": "test1",
            "game_class": klass.id,
            "worksheet": worksheet.id
        },
    )

    assert not form.is_valid()
Beispiel #14
0
    def test_delete_games(self):
        # Create a new teacher with a game to make sure it's not affected
        new_user: User = User.objects.create_user("test2", "*****@*****.**",
                                                  "password")
        new_user.is_staff = True
        new_user.save()
        new_user_profile: UserProfile = UserProfile(user=new_user)
        new_user_profile.save()
        new_teacher: Teacher = Teacher.objects.create(user=new_user_profile,
                                                      new_user=new_user)
        new_teacher.save()
        new_klass, _, _ = create_class_directly(new_user.email)
        new_user.save()
        new_game = models.Game(name="test2",
                               game_class=new_klass,
                               worksheet=self.worksheet)
        new_game.save()

        # Create a game for the second class
        game2 = models.Game(name="test",
                            game_class=self.klass2,
                            worksheet=self.worksheet)
        game2.save()

        data = {"game_ids": [self.game.id, game2.id, new_game.id]}

        # Try to login as a student and delete games - they shouldn't have access
        _, student_password, student = create_school_student_directly(
            self.klass.access_code)
        client = self.login(username=student.new_user.username,
                            password=student_password)
        response = client.post(reverse("game-delete-games"), data)
        assert response.status_code == 403
        assert Game.objects.count() == 3

        # Login as initial teacher and delete games - only his games should be deleted
        client = self.login()
        response = client.post(reverse("game-delete-games"), data)
        assert response.status_code == 204
        assert Game.objects.count() == 1
        assert Game.objects.get(pk=new_game.id)
Beispiel #15
0
    def student_row(self, class_name=None):
        email, password = signup_teacher_directly()
        _, class_name, access_code = create_class_directly(email, class_name)
        _, _, student = create_school_student_directly(access_code)

        total_time = timedelta(0, 30)
        scores = [x for x in range(20)]
        total_score = sum(scores)
        progress = (0, 0, 0)

        all_scores = scores + [""] * 89

        row = StudentRow(
            student=student,
            total_time=total_time,
            total_score=total_score,
            start_time=datetime.fromtimestamp(1435305072, tz=utc),
            finish_time=datetime.fromtimestamp(1438305072, tz=utc),
            progress=progress,
            scores=all_scores,
            class_field=Class(name="MyClass"),
        )
        return row
Beispiel #16
0
def class1(db, teacher1_email) -> Class:
    klass, _, _ = create_class_directly(teacher1_email)
    return klass
Beispiel #17
0
def create_random_school_data():
    email, password = signup_teacher_directly()
    clas, class_name, access_code = create_class_directly(email)
    create_school_student_directly(access_code)
    create_school_student_directly(access_code)