Skip to content
test_validation_tools.py 112 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2018 Dong Zhuang"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

Andreas Klöckner's avatar
Andreas Klöckner committed
import stat
import unittest

Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase
Andreas Klöckner's avatar
Andreas Klöckner committed
from dulwich.repo import Tree
Dong Zhuang's avatar
Dong Zhuang committed

from course import validation
from course.constants import (
    ATTRIBUTES_FILENAME,
    flow_permission,
    grade_aggregation_strategy,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.content import dict_to_struct
from course.validation import ValidationError
Dong Zhuang's avatar
Dong Zhuang committed
from tests import factories
Andreas Klöckner's avatar
Andreas Klöckner committed
from tests.base_test_mixins import CoursesTestMixinBase
from tests.utils import mock, suppress_stdout_decorator
Dong Zhuang's avatar
Dong Zhuang committed


location = "some_where"
vctx = mock.MagicMock()
vctx.repo = mock.MagicMock()
class ValidationTestMixin:
Dong Zhuang's avatar
Dong Zhuang committed
    def setUp(self):
        super().setUp()
Dong Zhuang's avatar
Dong Zhuang committed
        self.addCleanup(vctx.reset_mock)


class ValidateIdentifierTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_identifier

    def test_success(self):
        identifier = "identifier"
        validation.validate_identifier(
            vctx, location, identifier, warning_only=True)

        validation.validate_identifier(vctx, location, identifier)
        self.assertEqual(vctx.add_warning.call_count, 0)

    def test_id_re_not_matched(self):
        identifier = "test identifier"
        expected_warn_msg = expected_error_msg = (
            "invalid identifier '%s'" % identifier)
        validation.validate_identifier(
            vctx, location, identifier, warning_only=True)
        self.assertEqual(vctx.add_warning.call_count, 1)
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])
        with self.assertRaises(ValidationError) as cm:
            validation.validate_identifier(vctx, location, identifier)
        self.assertIn(expected_error_msg, str(cm.exception))


class ValidateRoleTest(ValidationTestMixin, CoursesTestMixinBase, TestCase):
    # test validation.validate_role

    def setUp(self):
        super().setUp()
Dong Zhuang's avatar
Dong Zhuang committed
        course = factories.CourseFactory()
        vctx.course = course
        factories.ParticipationRoleFactory(course=course, identifier="role1")
        factories.ParticipationRoleFactory(course=course, identifier="role2")

    def test_vctx_no_course(self):
        vctx.course = None
        validation.validate_role(vctx, location, "some_role")
        self.assertEqual(vctx.add_warning.call_count, 0)

    def test_success(self):
        validation.validate_role(vctx, location, "role1")

    def test_role_not_found(self):
        expected_error_msg = "invalid role 'some_role'"
        with self.assertRaises(ValidationError) as cm:
            validation.validate_role(vctx, location, "some_role")
        self.assertIn(expected_error_msg, str(cm.exception))


class ValidateFacilityTest(ValidationTestMixin, unittest.TestCase):
    # test validate_facility

    def test_validate_facility_no_facility(self):
        with mock.patch("course.utils.get_facilities_config") as mock_get_f:
            mock_get_f.return_value = None
            validation.validate_facility(vctx, location, "some_facility")

    def test_validate_facility_not_found(self):
        with mock.patch("course.utils.get_facilities_config") as mock_get_f:
            mock_get_f.return_value = ["f1", "f2"]
            expected_warn_msg = (
                "Name of facility not recognized: 'some_facility'. "
                "Known facility names: 'f1, f2'")
            validation.validate_facility(vctx, location, "some_facility")
            self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

    def test_success(self):
        with mock.patch("course.utils.get_facilities_config") as mock_get_f:
            mock_get_f.return_value = ["f1", "f2"]
            validation.validate_facility(vctx, location, "f1")
            self.assertEqual(vctx.add_warning.call_count, 0)


class ValidateParticipationtagTest(CoursesTestMixinBase, TestCase):
    # test validate_participationtag

    def setUp(self):
        # we don't use mock vctx, because there is a memoize_in decorator
        # which won't function when the vctx itself is mocked.
        super().setUp()
Dong Zhuang's avatar
Dong Zhuang committed
        course = factories.CourseFactory()

        repo = mock.MagicMock()
        self.vctx = validation.ValidationContext(repo, "some_sha", course)

        factories.ParticipationTagFactory(course=course, name="tag1")
        factories.ParticipationTagFactory(course=course, name="tag2")

    def test_vctx_no_course(self):
        self.vctx.course = None
        with mock.patch(
                "course.validation.ValidationContext.add_warning") as mock_add_warn:
            validation.validate_participationtag(self.vctx, location, "my_tag")
            self.assertEqual(mock_add_warn.call_count, 0)

    def test_tag_not_found(self):
        with mock.patch(
                "course.validation.ValidationContext.add_warning") as mock_add_warn:
            validation.validate_participationtag(self.vctx, location, "my_tag")
            expected_warn_msg = (
                "Name of participation tag not recognized: 'my_tag'. "
                "Known participation tag names: 'tag1, tag2'")
            self.assertIn(expected_warn_msg, mock_add_warn.call_args[0])

    def test_success_and_memoized(self):
        with mock.patch(
                "course.validation.ValidationContext.add_warning") as mock_add_warn:
            validation.validate_participationtag(self.vctx, location, "tag1")
            self.assertEqual(mock_add_warn.call_count, 0)

        with mock.patch(
                "course.models.ParticipationTag.objects.filter"
        ) as mock_ptag_filter_func:
            validation.validate_participationtag(self.vctx, location, "tag2")
            self.assertEqual(mock_ptag_filter_func.call_count, 0)


required_attrs = [("ra1", int), ("ra2", str)]
allowed_attrs = [("aa1", float), ("aa2", bool), "aa3", ("aa4", "markup")]
rule1 = dict_to_struct({"ra1": 1, "ra2": "abcd"})
rule2 = dict_to_struct({"ra2": "abcd"})
rule3 = dict_to_struct(
    {"ra1": 1, "ra2": "abcd",
     "unknown1": "some_value", "unknown2": "some_value2"})
rule4 = dict_to_struct(
    {"ra1": 0.5, "ra2": "abcd"})
rule5 = dict_to_struct({"ra1": 1, "ra2": "abcd", "aa4": "[href](abcd)"})


class ValidateStructTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_struct

    def test_target_not_struct(self):
        with self.assertRaises(ValidationError) as cm:
            validation.validate_struct(
                vctx, location, "some string", required_attrs, allowed_attrs)

        expected_error_msg = "not a key-value map"
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_success(self):
        with mock.patch("course.validation.validate_markup") as mock_val_markup:
            validation.validate_struct(
                vctx, location, rule1, required_attrs, allowed_attrs)
            self.assertEqual(mock_val_markup.call_count, 0)

            validation.validate_struct(
Loading
Loading full blame...