Skip to content
test_validation_tools.py 115 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
from __future__ import division

__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.
"""

import unittest
import stat
import hashlib
from dulwich.repo import Tree
Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase

from course import validation
from course.validation import ValidationError
from course.content import dict_to_struct
from course.constants import (
    flow_permission, grade_aggregation_strategy, ATTRIBUTES_FILENAME)
Dong Zhuang's avatar
Dong Zhuang committed

from tests.utils import mock, suppress_stdout_decorator
from tests.base_test_mixins import CoursesTestMixinBase
from tests import factories


location = "some_where"
vctx = mock.MagicMock()
vctx.repo = mock.MagicMock()
Dong Zhuang's avatar
Dong Zhuang committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774


class ValidationTestMixin(object):
    def setUp(self):
        super(ValidationTestMixin, self).setUp()
        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(ValidateRoleTest, self).setUp()
        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(ValidateParticipationtagTest, self).setUp()
        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(
                vctx, location, rule5, required_attrs, allowed_attrs)
            self.assertEqual(mock_val_markup.call_count, 1)

    def test_required_missing(self):
        with self.assertRaises(ValidationError) as cm:
            validation.validate_struct(
                vctx, location, rule2, required_attrs, allowed_attrs)

        expected_error_msg = "attribute 'ra1' missing"
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_extraneous(self):
        with self.assertRaises(ValidationError) as cm:
            validation.validate_struct(
                vctx, location, rule3, required_attrs, allowed_attrs)

        expected_error_msg1 = "extraneous attribute(s) 'unknown1,unknown2'"
        expected_error_msg2 = "extraneous attribute(s) 'unknown2,unknown1'"
        self.assertTrue(
            expected_error_msg1 in str(cm.exception)
            or expected_error_msg2 in str(cm.exception))

    def test_instance_wrong(self):
        with self.assertRaises(ValidationError) as cm:
            validation.validate_struct(
                vctx, location, rule4, required_attrs, allowed_attrs)

        expected_error_msg = (
            "attribute 'ra1' has wrong type: got 'float', expected ")

        self.assertIn(expected_error_msg, str(cm.exception))


class ValidateMarkupTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_markup

    def test_success(self):
        with mock.patch("course.content.markup_to_html") as mock_mth:
            mock_mth.return_value = None
            validation.validate_markup(vctx, location, "some string")

    @suppress_stdout_decorator(suppress_stderr=True)
    def test_markup_to_html_failure(self):
        with mock.patch("course.content.markup_to_html") as mock_mth:
            mock_mth.side_effect = RuntimeError("my error")
            with self.assertRaises(ValidationError) as cm:
                validation.validate_markup(
                    vctx, location, "some problematic string")

            expected_error_msg = ("RuntimeError: my error")
            self.assertIn(expected_error_msg, str(cm.exception))


chunk_rule2_dict = {
    "weight": 1,
    "if_after": "some_date1",
    "if_before": "some_date2",
    "if_in_facility": "some_facility",
    "if_has_role": ["r1", "r2"],
    "if_has_participation_tags_any": ["tag1", "tag2"],
    "if_has_participation_tags_all": ["tag3", "tag4"],
}
chunk_rule3 = dict_to_struct({
    "weight": 1,
    "if_after": "some_date1",
    "if_before": "some_date2",
    "if_in_facility": "some_facility",
    "if_has_role": ["r1", "r2"],
    "if_has_participation_tags_any": ["tag1", "tag2"],
    "if_has_participation_tags_all": ["tag3", "tag4"],
})


class ValidateChunkRuleTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_chunk_rule

    chunk_rule_dict_base = {"weight": 1}

    def get_updated_rule(self, **kwargs):
        rule = self.chunk_rule_dict_base.copy()
        rule.update(kwargs)
        return dict_to_struct(rule)

    def test_base_success(self):
        with mock.patch("course.validation.validate_struct") as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location, self.get_updated_rule())
            self.assertEqual(mock_val_func.call_count, 1)

    def test_encounter_datespec_call(self):
        validation.validate_chunk_rule(
            vctx, location,
            self.get_updated_rule(if_after="some_date1"))
        self.assertEqual(vctx.encounter_datespec.call_count, 1)

        vctx.reset_mock()

        validation.validate_chunk_rule(
            vctx, location,
            self.get_updated_rule(if_before="some_date1"))
        self.assertEqual(vctx.encounter_datespec.call_count, 1)

    def test_if_has_role(self):
        with mock.patch("course.validation.validate_role") as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location,
                self.get_updated_rule(if_has_role=["r1", "r2"]))
            self.assertEqual(mock_val_func.call_count, 2)

    def test_if_has_participation_tags_any(self):
        with mock.patch("course.validation.validate_participationtag"
                        ) as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location,
                self.get_updated_rule(
                    if_has_participation_tags_any=["tag1", "tag2", "tag3"]))
            self.assertEqual(mock_val_func.call_count, 3)

    def test_if_has_participation_tags_all(self):
        with mock.patch("course.validation.validate_participationtag"
                        ) as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location,
                self.get_updated_rule(
                    if_has_participation_tags_all=["tag1", "tag2"]))
            self.assertEqual(mock_val_func.call_count, 2)

    def test_if_in_facility(self):
        with mock.patch("course.validation.validate_facility"
                        ) as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location,
                self.get_updated_rule(
                    if_in_facility="some_facility"))
            self.assertEqual(mock_val_func.call_count, 1)

    def test_deprecated_time_attribute(self):
        expected_warn_msg = ("Uses deprecated 'start' attribute--"
                             "use 'if_after' instead")
        validation.validate_chunk_rule(
            vctx, location,
            self.get_updated_rule(start="some_time"))
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

        vctx.reset_mock()

        expected_warn_msg = ("Uses deprecated 'end' attribute--"
                             "use 'if_before' instead")
        validation.validate_chunk_rule(
            vctx, location,
            self.get_updated_rule(end="some_time"))
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

    def test_deprecated_roles(self):
        expected_warn_msg = ("Uses deprecated 'roles' attribute--"
                             "use 'if_has_role' instead")

        with mock.patch("course.validation.validate_role") as mock_val_func:
            validation.validate_chunk_rule(
                vctx, location,
                self.get_updated_rule(
                    roles=["r1", "r2", "r3"]))
            self.assertEqual(mock_val_func.call_count, 3)
            self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])


class ValidatePageChunkTest(ValidationTestMixin, unittest.TestCase):
    def get_updated_chunk(self, **kwargs):
        chunk = {"id": "my_id", "content": "some content"}
        chunk.update(kwargs)
        return dict_to_struct(chunk)

    def test_success(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.extract_title_from_markup"
        ) as mock_etfm, mock.patch(
            "course.validation.validate_chunk_rule"
        ) as mock_vcr, mock.patch(
            "course.validation.validate_markup"
        ) as mock_vm:
            validation.validate_page_chunk(
                vctx, location,
                self.get_updated_chunk(
                    title="The title"))
            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_etfm.call_count, 0)
            self.assertEqual(mock_vcr.call_count, 0)
            self.assertEqual(mock_vm.call_count, 1)

    def test_not_title(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.extract_title_from_markup"
        ) as mock_etfm, mock.patch(
            "course.validation.validate_chunk_rule"
        ) as mock_vcr, mock.patch(
            "course.validation.validate_markup"
        ) as mock_vm:
            mock_etfm.return_value = None

            with self.assertRaises(ValidationError) as cm:
                validation.validate_page_chunk(
                    vctx, location,
                    self.get_updated_chunk())

            expected_error_msg = "no title present"
            self.assertIn(expected_error_msg, str(cm.exception))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_etfm.call_count, 1)

            self.assertEqual(mock_vcr.call_count, 0)
            self.assertEqual(mock_vm.call_count, 0)

    def test_chunk_has_rule(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.extract_title_from_markup"
        ) as mock_etfm, mock.patch(
            "course.validation.validate_chunk_rule"
        ) as mock_vcr, mock.patch(
            "course.validation.validate_markup"
        ) as mock_vm:
            mock_etfm.return_value = "Some title"

            validation.validate_page_chunk(
                vctx, location,
                self.get_updated_chunk(rules=["r1", "r2", "r3"]))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_etfm.call_count, 1)

            self.assertEqual(mock_vcr.call_count, 3)
            self.assertEqual(mock_vm.call_count, 1)


class ValidateStaticpageDesc(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_staticpage_desc

    def get_updated_page_desc(self, **kwargs):
        page_desc = {}
        page_desc.update(kwargs)
        return dict_to_struct(page_desc)

    def test_success_with_content(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.validation.validate_page_chunk"
        ) as mock_vpc:
            validation.validate_staticpage_desc(
                vctx, location,
                self.get_updated_page_desc(content="blabla"))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_vpc.call_count, 1)

    def test_success_with_chunks(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.normalize_page_desc"
        ) as mock_npd, mock.patch(
            "course.validation.validate_page_chunk"
        ) as mock_vpc:
            validation.validate_staticpage_desc(
                vctx, location,
                self.get_updated_page_desc(
                    chunks=(
                        dict_to_struct(
                            {"id": "my_id", "content": "some content"}),
                        dict_to_struct(
                            {"id": "my_id2", "content": "some content2"}),
                        dict_to_struct(
                            {"id": "my_id3", "content": "some content2"}),
                    )
                ))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_npd.call_count, 0)
            self.assertEqual(mock_vpc.call_count, 3)

    def test_neither_chunks_nor_content(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.normalize_page_desc"
        ) as mock_npd, mock.patch(
            "course.validation.validate_page_chunk"
        ) as mock_vpc:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_staticpage_desc(
                    vctx, location,
                    self.get_updated_page_desc())

            expected_error_msg = "must have either 'chunks' or 'content'"
            self.assertIn(expected_error_msg, str(cm.exception))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_npd.call_count, 0)
            self.assertEqual(mock_vpc.call_count, 0)

    def test_both_chunks_and_content(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.normalize_page_desc"
        ) as mock_npd, mock.patch(
            "course.validation.validate_page_chunk"
        ) as mock_vpc:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_staticpage_desc(
                    vctx, location,
                    self.get_updated_page_desc(chunks="blabla", content="blabal2"))

            expected_error_msg = "must have either 'chunks' or 'content'"
            self.assertIn(expected_error_msg, str(cm.exception))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_npd.call_count, 0)
            self.assertEqual(mock_vpc.call_count, 0)

    def test_success_with_chunks_id_not_unique(self):
        with mock.patch("course.validation.validate_struct"
                        ) as mock_vs, mock.patch(
            "course.content.normalize_page_desc"
        ) as mock_npd, mock.patch(
            "course.validation.validate_page_chunk"
        ) as mock_vpc:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_staticpage_desc(
                    vctx, location,
                    self.get_updated_page_desc(
                        chunks=(
                            dict_to_struct(
                                {"id": "my_id", "content": "some content"}),
                            dict_to_struct(
                                {"id": "my_id2", "content": "some content2"}),
                            dict_to_struct(
                                {"id": "my_id2", "content": "some content2"}),
                        )
                    ))

            expected_error_msg = "chunk id 'my_id2' not unique"
            self.assertIn(expected_error_msg, str(cm.exception))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_vpc.call_count, 3)
            self.assertEqual(mock_npd.call_count, 0)


class FakeCustomRepoPageType1(object):
    def __init__(self, vctx, location, page_desc):
        return


class FakePageType1(object):
    def __init__(self, vctx, location, page_desc):
        raise ValidationError("This is a faked validation error")


class FakePageType2(object):
    def __init__(self, vctx, location, page_desc):
        raise RuntimeError("This is a faked RuntimeError")


class ValidateFlowPageTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_flow_page

    def get_updated_page_desc(self, **kwargs):
        page_desc = {"id": "my_page_id"}
        page_desc.update(kwargs)
        return dict_to_struct(page_desc)

    def test_no_id(self):
        with self.assertRaises(ValidationError) as cm:
            validation.validate_flow_page(vctx, location, dict_to_struct({}))
        expected_error_msg = "flow page has no ID"
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_custom_page_types_stop_support_deadline(self):
        from datetime import datetime, timedelta
        deadline = datetime(2019, 1, 1, 0, 0, 0, 0)
        one_month_before_deadline = deadline - timedelta(days=30)
        one_month_after_deadline = deadline + timedelta(days=30)

        with mock.patch("course.validation.validate_identifier"
                        ) as mock_vi, mock.patch(
            "course.utils.get_custom_page_types_stop_support_deadline"
        ) as mock_gcptssd, mock.patch(
            "relate.utils.local_now"
        ) as mock_local_now, mock.patch(
            "course.content.get_flow_page_class"
        ) as mock_gfpc:
            mock_vi.return_value = None
            mock_gfpc.return_value = FakeCustomRepoPageType1

            from relate.utils import localize_datetime

            # passed deadline
            mock_gcptssd.return_value = localize_datetime(deadline)
            mock_local_now.return_value = (
                localize_datetime(one_month_after_deadline))

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_page(
                    vctx, location,
                    self.get_updated_page_desc(type="repo:my_custom_page_type"))
            expected_error_msg = (
                "Custom page type \'repo:my_custom_page_type\' specified. "
                "Custom page types were no longer supported in RELATE since "
                "Jan. 1, 2019, midnight.")
            self.assertIn(expected_error_msg, str(cm.exception))

            # not passing deadline
            mock_local_now.return_value = (
                localize_datetime(one_month_before_deadline))

            validation.validate_flow_page(
                vctx, location,
                self.get_updated_page_desc(type="repo:my_custom_page_type"))

            expected_warn_msg = (
                "Custom page type \'repo:my_custom_page_type\' specified. "
                "Custom page types will stop being supported in RELATE at "
                "Jan. 1, 2019, midnight.")
            self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

    def test_flow_page_fail_validation(self):
        with mock.patch("course.validation.validate_identifier"
                        ) as mock_vi, mock.patch(
            "course.content.get_flow_page_class"
        ) as mock_gfpc:
            mock_vi.return_value = None
            mock_gfpc.return_value = FakePageType1

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_page(
                    vctx, location,
                    self.get_updated_page_desc(type="PageType1"))

            expected_error_msg = (
                "This is a faked validation error")
            self.assertIn(expected_error_msg, str(cm.exception))

    def test_flow_page_instantiate_error(self):
        with mock.patch("course.validation.validate_identifier"
                        ) as mock_vi, mock.patch(
            "course.content.get_flow_page_class"
        ) as mock_gfpc:
            mock_vi.return_value = None
            mock_gfpc.return_value = FakePageType2

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_page(
                    vctx, location,
                    self.get_updated_page_desc(type="PageType2"))

            expected_error_msg = (
                "could not instantiate flow page: RuntimeError: This is a "
                "faked RuntimeError")
            self.assertIn(expected_error_msg, str(cm.exception))


class ValidateFlowGroupTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_flow_group

    def get_updated_group(self, **kwargs):
        group_desc = {"id": "my_page_id",
                      "pages": [
                          dict_to_struct({"id": "page1"}),
                          dict_to_struct({"id": "page2"}),
                      ]}
        group_desc.update(kwargs)
        return dict_to_struct(group_desc)

    def test_success(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_flow_page"
        ) as mock_vfp, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi:
            mock_vs.return_value = None
            mock_vfp.return_value = None
            mock_vi.return_value = None

            validation.validate_flow_group(
                vctx, location,
                self.get_updated_group()
            )

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_vfp.call_count, 2)
            self.assertEqual(mock_vi.call_count, 1)

            self.assertIn("my_page_id", mock_vi.call_args[0])

    def test_empty_group(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_flow_page"
        ) as mock_vfp, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi:
            mock_vs.return_value = None
            mock_vfp.return_value = None
            mock_vi.return_value = None

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_group(
                    vctx, location,
                    self.get_updated_group(pages=[])
                )

            expected_error_msg = (
                "group 'my_page_id': group is empty")
            self.assertIn(expected_error_msg, str(cm.exception))

            self.assertEqual(mock_vs.call_count, 1)
            self.assertEqual(mock_vfp.call_count, 0)

    def test_empty_non_positive_max_page_count(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_flow_page"
        ) as mock_vfp, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi:
            mock_vs.return_value = None
            mock_vfp.return_value = None
            mock_vi.return_value = None

            expected_error_msg = (
                "group 'my_page_id': max_page_count is not positive")

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_group(
                    vctx, location,
                    self.get_updated_group(max_page_count=0)
                )
            self.assertIn(expected_error_msg, str(cm.exception))

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_group(
                    vctx, location,
                    self.get_updated_group(max_page_count=-2)
                )
            self.assertIn(expected_error_msg, str(cm.exception))

    def test_pages_in_group_id_not_unique(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_flow_page"
        ) as mock_vfp, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi:
            mock_vs.return_value = None
            mock_vfp.return_value = None
            mock_vi.return_value = None

            expected_error_msg = ("page id 'page2' not unique")

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_group(
                    vctx, location,
                    self.get_updated_group(pages=[
                        dict_to_struct({"id": "page1"}),
                        dict_to_struct({"id": "page2"}),
                        dict_to_struct({"id": "page2"}),
                    ])
                )
            self.assertIn(expected_error_msg, str(cm.exception))

    def test_max_page_count_with_shuffle_not_given(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_flow_page"
        ) as mock_vfp, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi:
            mock_vs.return_value = None
            mock_vfp.return_value = None
            mock_vi.return_value = None

            validation.validate_flow_group(
                vctx, location,
                self.get_updated_group(max_page_count=1, shuffle=True)
            )
            self.assertEqual(vctx.add_warning.call_count, 0)

            expected_warn_msg = (
                "shuffle attribute will be required for groups with"
                "max_page_count in a future version. set "
                "'shuffle: False' to match current behavior.")

            validation.validate_flow_group(
                vctx, location,
                self.get_updated_group(max_page_count=1)
            )
            self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])
            vctx.add_warning.reset_mock()

Dong Zhuang's avatar
Dong Zhuang committed

class ValidateSessionStartRuleTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_session_start_rule

    def get_updated_nrule(self, no_may_start_new_session=False,
                          no_may_list_existing_sessions=False, **kwargs):
        rule = {}
        if not no_may_start_new_session:
            rule["may_start_new_session"] = True
        if not no_may_list_existing_sessions:
            rule["may_list_existing_sessions"] = True
        rule.update(kwargs)
        return dict_to_struct(rule)

    def get_updated_tags(self, extra=None):
        tags = []

        if extra is not None:
            assert isinstance(extra, list)
        else:
            extra = []

        tags.extend(extra)
        return tags

    def test_success(self):
        validation.validate_session_start_rule(
            vctx, location, self.get_updated_nrule(), self.get_updated_tags())

        validation.validate_session_start_rule(
            vctx, location, self.get_updated_nrule(
                may_start_new_session=False), self.get_updated_tags())
        validation.validate_session_start_rule(
            vctx, location, self.get_updated_nrule(
                may_start_new_session=True), self.get_updated_tags())

        validation.validate_session_start_rule(
            vctx, location, self.get_updated_nrule(
                may_list_existing_sessions=False), self.get_updated_tags())
        validation.validate_session_start_rule(
            vctx, location, self.get_updated_nrule(
                may_list_existing_sessions=True), self.get_updated_tags())

        # no warnings
        self.assertEqual(vctx.add_warning.call_count, 0)

    def test_success_may_start_new_session_not_present(self):
        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(no_may_start_new_session=True),
            self.get_updated_tags())

        expected_warn_msg = (
            "attribute 'may_start_new_session' is not present")
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

    def test_success_may_list_existing_sessions_not_present(self):
        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(no_may_list_existing_sessions=True),
            self.get_updated_tags())

        expected_warn_msg = (
            "attribute 'may_list_existing_sessions' is not present")
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

    def test_if_after(self):
        datespec = "some_time1"
        kwargs = {
            "if_after": datespec
        }
        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.encounter_datespec.call_count, 1)
        self.assertIn(datespec, vctx.encounter_datespec.call_args[0])

    def test_if_before(self):
        datespec = "some_time2"
        kwargs = {
            "if_before": datespec
        }
        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.encounter_datespec.call_count, 1)
        self.assertIn(datespec, vctx.encounter_datespec.call_args[0])

    def test_if_has_role(self):
        roles = ["r1", "r2", "r3"]
        kwargs = {
            "if_has_role": roles
        }
        with mock.patch("course.validation.validate_role") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),
                self.get_updated_tags())
        self.assertEqual(mock_val_func.call_count, 3)

    def test_if_has_participation_tags(self):
        ptags = ["t1", "t2", "t3"]
        kwargs1 = {
            "if_has_participation_tags_any": ptags
        }
        kwargs2 = {
            "if_has_participation_tags_all": ptags
        }
        with mock.patch(
                "course.validation.validate_participationtag") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs1),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 3)
            mock_val_func.reset_mock()

            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs2),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 3)

    def test_if_in_facility(self):
        faclity = "f1"
        kwargs = {
            "if_in_facility": faclity
        }

        with mock.patch(
                "course.validation.validate_facility") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 1)
            self.assertIn(faclity, mock_val_func.call_args[0])

    def test_if_has_session_tagged(self):
        s_tag = None
        kwargs = {
            "if_has_session_tagged": s_tag
        }

        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 0)

        s_tag = "s_tag1"
        kwargs = {
            "if_has_session_tagged": s_tag
        }
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 1)
            self.assertIn(s_tag, mock_val_func.call_args[0])

    def test_deprecated_lock_down_as_exam_session(self):

        expected_warn_msg = (
            "Attribute 'lock_down_as_exam_session' is deprecated "
            "and non-functional. Use the access permission flag "
            "'lock_down_as_exam_session' instead.")

        kwargs = {
            "lock_down_as_exam_session": True
        }

        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(**kwargs),
            self.get_updated_tags())

        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])

        kwargs = {
            "lock_down_as_exam_session": False
        }

        validation.validate_session_start_rule(
            vctx, location,
            self.get_updated_nrule(**kwargs),
            self.get_updated_tags())