Skip to content
test_validation_tools.py 112 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),

                # Note: can tags include element None?
                self.get_updated_tags(["ts1", None]))

            self.assertEqual(mock_val_func.call_count, 0)

    def test_tag_session_fail(self):
        tag_session = "ts1"
        kwargs = {
            "tag_session": tag_session
        }
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_session_start_rule(
                    vctx, location,
                    self.get_updated_nrule(**kwargs),
                    self.get_updated_tags(["ts2", "ts3"]))

            self.assertEqual(mock_val_func.call_count, 1)
            self.assertEqual(mock_val_func.call_args[0][2], tag_session)

            # make sure validate_identifier only warn if invalid
            self.assertEqual(mock_val_func.call_args[1]["warning_only"], True)

            expected_error_msg = "invalid tag 'ts1'"
            self.assertIn(expected_error_msg, str(cm.exception))

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

    def test_default_expiration_mode_success(self):
        mode = "roll_over"
        kwargs = {
            "default_expiration_mode": mode
        }

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

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

    def test_default_expiration_mode_fail(self):
        mode = "unknown"
        kwargs = {
            "default_expiration_mode": mode
        }

        with self.assertRaises(ValidationError) as cm:
            validation.validate_session_start_rule(
                vctx, location,
                self.get_updated_nrule(**kwargs),
                self.get_updated_tags())

        expected_error_msg = (
                f"invalid default expiration mode '{mode}'")
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertIn(expected_error_msg, str(cm.exception))

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


class ValidateSessionAccessRuleTest(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_session_access_rule

    def get_updated_arule(self, **kwargs):
        rule = {"permissions": ["fp1", "fp2"]}
        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):
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp:
            validation.validate_session_access_rule(
                vctx, location, self.get_updated_arule(), self.get_updated_tags())
        self.assertEqual(mock_vfp.call_count, 2)

    def test_if_after(self):
        datespec = "some_time1"
        kwargs = {
            "if_after": datespec
        }

        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

        self.assertEqual(mock_vfp.call_count, 2)
        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
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

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

    def test_if_completed_before(self):
        datespec = "some_time3"
        kwargs = {
            "if_completed_before": datespec
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

        self.assertEqual(mock_vfp.call_count, 2)
        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_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_role") as mock_val_func:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

        self.assertEqual(mock_val_func.call_count, 3)
        self.assertEqual(mock_vfp.call_count, 2)

    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_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_participationtag"
        ) as mock_val_func:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs1),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 3)
            mock_val_func.reset_mock()
            self.assertEqual(mock_vfp.call_count, 2)
            mock_vfp.reset_mock()

            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs2),
                self.get_updated_tags())
            self.assertEqual(mock_val_func.call_count, 3)
            self.assertEqual(mock_vfp.call_count, 2)

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

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

            self.assertEqual(mock_vfp.call_count, 2)

    def test_if_has_tag_none(self):
        if_has_tag = None
        kwargs = {
            "if_has_tag": if_has_tag
        }

        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

            self.assertEqual(mock_val_func.call_count, 0)

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

            self.assertEqual(mock_vfp.call_count, 2)

    def test_if_has_tag_success(self):
        if_has_tag = "ts1"
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags(["ts1", "ts2"]))

            self.assertEqual(mock_val_func.call_count, 1)
            self.assertEqual(mock_val_func.call_args[0][2], if_has_tag)

            # make sure validate_identifier only warn if invalid
            self.assertEqual(mock_val_func.call_args[1]["warning_only"], True)

            self.assertEqual(mock_vfp.call_count, 2)

    def test_if_has_tag_success_with_none_in_tags(self):
        # Note: can tags include element None?
        if_has_tag = None
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),

                # Note: can tags include element None?
                self.get_updated_tags(["ts1", None]))

            self.assertEqual(mock_val_func.call_count, 0)

            self.assertEqual(mock_vfp.call_count, 2)

    def test_if_has_tag_fail(self):
        if_has_tag = "ts1"
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp, mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_session_access_rule(
                    vctx, location,
                    self.get_updated_arule(**kwargs),
                    self.get_updated_tags(["ts2", "ts3"]))

            self.assertEqual(mock_val_func.call_count, 1)
            self.assertEqual(mock_val_func.call_args[0][2], if_has_tag)

            # make sure validate_identifier only warn if invalid
            self.assertEqual(mock_val_func.call_args[1]["warning_only"], True)

            expected_error_msg = "invalid tag 'ts1'"
            self.assertIn(expected_error_msg, str(cm.exception))

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

            self.assertEqual(mock_vfp.call_count, 0)

    def test_default_expiration_mode_success(self):
        mode = "roll_over"
        kwargs = {
            "if_expiration_mode": mode
        }
        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

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

        self.assertEqual(mock_vfp.call_count, 2)

    def test_if_expiration_mode_fail(self):
        mode = "unknown"
        kwargs = {
            "if_expiration_mode": mode
        }

        with mock.patch(
                "course.validation.validate_flow_permission"
        ) as mock_vfp, self.assertRaises(ValidationError) as cm:
            validation.validate_session_access_rule(
                vctx, location,
                self.get_updated_arule(**kwargs),
                self.get_updated_tags())

        expected_error_msg = (
                f"invalid expiration mode '{mode}'")
Dong Zhuang's avatar
Dong Zhuang committed
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
        self.assertIn(expected_error_msg, str(cm.exception))

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

        self.assertEqual(mock_vfp.call_count, 0)

    def test_if_in_progress_no_warning(self):
        if_in_progress = True
        kwargs = {
            "if_in_progress": if_in_progress,
            "permissions": []
        }
        validation.validate_session_access_rule(
            vctx, location,
            self.get_updated_arule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.add_warning.call_count, 0)

        if_in_progress = False
        kwargs = {
            "if_in_progress": if_in_progress,
            "permissions": []
        }
        validation.validate_session_access_rule(
            vctx, location,
            self.get_updated_arule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.add_warning.call_count, 0)

    def test_if_in_progress_warned_ignore(self):
        if_in_progress = False
        kwargs = {
            "if_in_progress": if_in_progress,
            "permissions": [flow_permission.submit_answer]
        }

        expected_warn_msg = (
            "Rule specifies 'submit_answer' or 'end_session' "
            "permissions for non-in-progress flow. These "
            "permissions will be ignored.")
        validation.validate_session_access_rule(
            vctx, location,
            self.get_updated_arule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.add_warning.call_count, 1)
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])
        vctx.reset_mock()

        kwargs = {
            "if_in_progress": if_in_progress,
            "permissions": [flow_permission.end_session]
        }

        expected_warn_msg = (
            "Rule specifies 'submit_answer' or 'end_session' "
            "permissions for non-in-progress flow. These "
            "permissions will be ignored.")
        validation.validate_session_access_rule(
            vctx, location,
            self.get_updated_arule(**kwargs),
            self.get_updated_tags())

        self.assertEqual(vctx.add_warning.call_count, 1)
        self.assertIn(expected_warn_msg, vctx.add_warning.call_args[0])
        vctx.reset_mock()

        kwargs = {
            "if_in_progress": if_in_progress,
            "permissions": [flow_permission.submit_answer,
                            flow_permission.end_session]
        }

        expected_warn_msg = (
            "Rule specifies 'submit_answer' or 'end_session' "
            "permissions for non-in-progress flow. These "
            "permissions will be ignored.")

        validation.validate_session_access_rule(
            vctx, location,
            self.get_updated_arule(**kwargs),
            self.get_updated_tags())

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


class ValidateSessionGradingRule(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_session_grading_rule

    default_grade_identifier = "my_grade_identifier"

    def get_updated_grule(self, **kwargs):
        rule = {}
        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):
        self.assertEqual(validation.validate_session_grading_rule(
            vctx, location, self.get_updated_grule(), self.get_updated_tags(),
            self.default_grade_identifier), False)

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

    def test_removed_grade_identifier(self):
        kwargs = {
            "grade_identifier": "unknown"
        }

        with self.assertRaises(ValidationError) as cm:
            validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), self.default_grade_identifier)

        expected_error_msg = (
            "'grade_identifier' attribute found. "
            "This attribute is no longer allowed here "
            "and should be moved upward into the 'rules' "
            "block.")

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

    def test_removed_grade_aggregation_strategy(self):
        kwargs = {
            "grade_aggregation_strategy": "unknown"
        }

        with self.assertRaises(ValidationError) as cm:
            validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), self.default_grade_identifier)

        expected_error_msg = (
            "'grade_aggregation_strategy' attribute found. "
            "This attribute is no longer allowed here "
            "and should be moved upward into the 'rules' "
            "block.")

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

    def test_if_started_before(self):
        datespec = "some_time1"
        kwargs = {
            "if_started_before": datespec
        }
        self.assertEqual(validation.validate_session_grading_rule(
            vctx, location,
            self.get_updated_grule(**kwargs),
            self.get_updated_tags(), self.default_grade_identifier), True)

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

    def test_due(self):
        datespec = "some_time3"
        kwargs = {
            "due": datespec
        }
        self.assertEqual(validation.validate_session_grading_rule(
            vctx, location,
            self.get_updated_grule(**kwargs),
            self.get_updated_tags(), self.default_grade_identifier), False)

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

    def test_if_completed_before(self):
        datespec = "some_time2"
        kwargs = {
            "if_completed_before": datespec
        }
        self.assertEqual(validation.validate_session_grading_rule(
            vctx, location,
            self.get_updated_grule(**kwargs),
            self.get_updated_tags(), self.default_grade_identifier), True)

        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:
            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), self.default_grade_identifier), True)
        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:
            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs1),
                self.get_updated_tags(), self.default_grade_identifier), True)
            self.assertEqual(mock_val_func.call_count, 3)
            mock_val_func.reset_mock()

            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs2),
                self.get_updated_tags(), self.default_grade_identifier), True)
            self.assertEqual(mock_val_func.call_count, 3)

    def test_if_has_tag_none(self):
        if_has_tag = None
        kwargs = {
            "if_has_tag": if_has_tag
        }

        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), self.default_grade_identifier), True)

            self.assertEqual(mock_val_func.call_count, 0)

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

    def test_if_has_tag_success(self):
        if_has_tag = "ts1"
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(["ts1", "ts2"]),
                self.default_grade_identifier), True)

            self.assertEqual(mock_val_func.call_count, 1)
            self.assertEqual(mock_val_func.call_args[0][2], if_has_tag)

            # make sure validate_identifier only warn if invalid
            self.assertEqual(mock_val_func.call_args[1]["warning_only"], True)

    def test_if_has_tag_success_with_none_in_tags(self):
        # Note: can tags include element None?
        if_has_tag = None
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            self.assertEqual(validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),

                # Note: can tags include element None?
                self.get_updated_tags(["ts1", None]), self.default_grade_identifier),
                True)

            self.assertEqual(mock_val_func.call_count, 0)

    def test_if_has_tag_fail(self):
        if_has_tag = "ts1"
        kwargs = {
            "if_has_tag": if_has_tag
        }
        with mock.patch(
                "course.validation.validate_identifier") as mock_val_func:
            with self.assertRaises(ValidationError) as cm:
                validation.validate_session_grading_rule(
                    vctx, location,
                    self.get_updated_grule(**kwargs),
                    self.get_updated_tags(["ts2", "ts3"]),
                    self.default_grade_identifier)

            self.assertEqual(mock_val_func.call_count, 1)
            self.assertEqual(mock_val_func.call_args[0][2], if_has_tag)

            # make sure validate_identifier only warn if invalid
            self.assertEqual(mock_val_func.call_args[1]["warning_only"], True)

            expected_error_msg = "invalid tag 'ts1'"
            self.assertIn(expected_error_msg, str(cm.exception))

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

    def test_generates_grade_and_grade_identifier_is_none(self):
        kwargs = {
            "generates_grade": False
        }

        # success when not generates_grade
        self.assertEqual(validation.validate_session_grading_rule(
            vctx, location,
            self.get_updated_grule(**kwargs),
            self.get_updated_tags(), grade_identifier=None), False)

        expected_error_msg = (
            "'generates_grade' is true, but no 'grade_identifier'"
            "is given.")

        kwargs = {
            "generates_grade": True
        }

        # fail when generates_grade
        with self.assertRaises(ValidationError) as cm:
            validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), grade_identifier=None)
        self.assertIn(expected_error_msg, str(cm.exception))

        kwargs = {}

        # fail when generates_grade not configured
        with self.assertRaises(ValidationError) as cm:
            validation.validate_session_grading_rule(
                vctx, location,
                self.get_updated_grule(**kwargs),
                self.get_updated_tags(), grade_identifier=None)
        self.assertIn(expected_error_msg, str(cm.exception))


class ValidateFlowRules(ValidationTestMixin, unittest.TestCase):
    # test validation.validate_flow_rules

    default_grade_identifier = "my_grade_identifier"

    def get_updated_rule(
            self, no_grade_identifier=False,
            no_grade_aggregation_strategy=False,
            no_grading_rules=False,
            **kwargs):
        rule = {"access": ["access_rule1", "access_rule2"]}

        if not no_grade_identifier:
            rule["grade_identifier"] = self.default_grade_identifier
        if not no_grade_aggregation_strategy:
            rule["grade_aggregation_strategy"] = (
                grade_aggregation_strategy.use_latest)
        if not no_grading_rules:
            rule["grading"] = ["grading_rule1", "grading_rule2",
                               "grading_rule3"]
        rule.update(kwargs)
        return dict_to_struct(rule)

    def test_success(self):
        with mock.patch(
                "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]
            validation.validate_flow_rules(
                vctx, location, self.get_updated_rule())

        self.assertEqual(mock_vi.call_count, 1)
        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vsgr.call_count, 3)

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

    def test_success_with_grading_identifier_none(self):
        kwargs = {
            "grade_identifier": None
        }
        with mock.patch(
                "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]
            validation.validate_flow_rules(
                vctx, location,
                self.get_updated_rule(
                    no_grading_rules=True,
                    **kwargs))

        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vi.call_count, 0)
        self.assertEqual(mock_vsgr.call_count, 0)

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

    def test_no_grade_identifier_with_no_grading_rules(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_rules(
                    vctx, location,
                    self.get_updated_rule(
                        no_grade_identifier=True, no_grading_rules=True))

            expected_error_msg = (
                "'rules' block does not have a grade_identifier "
                "attribute.")
            unexpected_error_msg = (
                "This attribute needs to be moved out of "
                "the lower-level 'grading' rules block and into "
                "the 'rules' block itself."
            )
            self.assertIn(expected_error_msg, str(cm.exception))
            self.assertNotIn(unexpected_error_msg, str(cm.exception))

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 0)
        self.assertEqual(mock_vsar.call_count, 0)
        self.assertEqual(mock_vsgr.call_count, 0)

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

    def test_no_grade_identifier_with_grading_rules_with_out_grade_identifier(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_rules(
                    vctx, location,
                    self.get_updated_rule(no_grade_identifier=True))

            expected_error_msg = (
                "'rules' block does not have a grade_identifier "
                "attribute.")
            unexpected_error_msg = (
                "This attribute needs to be moved out of "
                "the lower-level 'grading' rules block and into "
                "the 'rules' block itself."
            )
            self.assertIn(expected_error_msg, str(cm.exception))
            self.assertNotIn(unexpected_error_msg, str(cm.exception))

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 0)
        self.assertEqual(mock_vsar.call_count, 0)
        self.assertEqual(mock_vsgr.call_count, 0)

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

    def test_no_grade_identifier_with_grading_rules_with_grade_identifier(self):

        kwargs = {
            "grading": dict_to_struct(
                {"grade_identifier": self.default_grade_identifier})
        }

        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
                "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_rules(
                    vctx, location,
                    self.get_updated_rule(no_grade_identifier=True, **kwargs))

            expected_error_msg = (
                "'rules' block does not have a grade_identifier "
                "attribute. This attribute needs to be moved out of "
                "the lower-level 'grading' rules block and into "
                "the 'rules' block itself.")
            self.assertIn(expected_error_msg, str(cm.exception))

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 0)
        self.assertEqual(mock_vsar.call_count, 0)
        self.assertEqual(mock_vsgr.call_count, 0)

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

    def test_tags(self):

        kwargs = {
            "tags": ["tag1", "tag2"]
        }

        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
                "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            validation.validate_flow_rules(
                vctx, location,
                self.get_updated_rule(**kwargs))

        self.assertEqual(mock_vs.call_count, 1)

        # two extra validate_identifier calls for tags
        self.assertEqual(mock_vi.call_count, 3)
        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vsgr.call_count, 3)

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

    def test_start_rules_validated(self):

        kwargs = {
            "start": ["start_rule1", "start_rule2",
                      "start_rule3", "start_rule4"]
        }

        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_start_rule"
        ) as mock_vssr, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            validation.validate_flow_rules(
                vctx, location,
                self.get_updated_rule(**kwargs))

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 1)
        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vssr.call_count, 4)
        self.assertEqual(mock_vsgr.call_count, 3)

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

    def test_no_grade_aggregation_strategy(self):
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_rules(
                    vctx, location,
                    self.get_updated_rule(no_grade_aggregation_strategy=True))
            expected_error_msg = (
                "flows that have a grade "
                f"identifier ('{self.default_grade_identifier}') "
Dong Zhuang's avatar
Dong Zhuang committed
                "must have grading rules with a "
                "grade_aggregation_strategy")
Dong Zhuang's avatar
Dong Zhuang committed

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

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 1)
        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vsgr.call_count, 0)

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

    def test_unknown_grade_aggregation_strategy(self):
        g_strategy = "unknown"
        kwargs = {
            "grade_aggregation_strategy": g_strategy
        }
        with mock.patch(
                "course.validation.validate_struct"
        ) as mock_vs, mock.patch(
            "course.validation.validate_identifier"
        ) as mock_vi, mock.patch(
            "course.validation.validate_session_access_rule"
        ) as mock_vsar, mock.patch(
            "course.validation.validate_session_grading_rule"
        ) as mock_vsgr:
            mock_vsgr.side_effect = [True, True, False]

            with self.assertRaises(ValidationError) as cm:
                validation.validate_flow_rules(
                    vctx, location,
                    self.get_updated_rule(**kwargs))

            expected_error_msg = (
                f"invalid grade aggregation strategy: {g_strategy}")
Dong Zhuang's avatar
Dong Zhuang committed

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

        self.assertEqual(mock_vs.call_count, 1)
        self.assertEqual(mock_vi.call_count, 1)
        self.assertEqual(mock_vsar.call_count, 2)
        self.assertEqual(mock_vsgr.call_count, 0)

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