Skip to content
test_validation_tools.py 112 KiB
Newer Older
        tree.add(ATTRIBUTES_FILENAME.encode(), stat.S_IFREG,
                 b".attributes.yml_content")

        fake_repo = FakeRepo()
        fake_repo[b".attributes.yml_content"] = FakeBlob(INVALID_ATTRIBUTES_YML)
        self.mock_get_true_repo_and_path.return_value = (fake_repo, "")

        with self.assertRaises(ValidationError) as cm:
            validation.check_attributes_yml(
                vctx, repo, path, tree, default_access_kinds)

        expected_error_msg = "entry 2 in 'student' is not a string"
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_attributes_yml_failed_with_public_and_unenrolled(self):
        path = ""
        repo = vctx.repo
        tree = Tree()
        tree.add(ATTRIBUTES_FILENAME.encode(), stat.S_IFREG,
                 b".attributes.yml_content")

        fake_repo = FakeRepo()
        fake_repo[b".attributes.yml_content"] = (
            FakeBlob(INVALID_ATTRIBUTES_WITH_PUBLIC_AND_UNENROLLED_YML))
        self.mock_get_true_repo_and_path.return_value = (fake_repo, "")

        with self.assertRaises(ValidationError) as cm:
            validation.check_attributes_yml(
                vctx, repo, path, tree, default_access_kinds)

        expected_error_msg = ("access classes 'public' and 'unenrolled' may not "
                              "exist simultaneously.")
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_attributes_yml_gitignore_subfolder(self):
        path = ""
        repo = vctx.repo
        tree = Tree()

        tree.add(b".gitignore", stat.S_IFREG, b".gitignore_content")
        tree.add(b"a_dir", stat.S_IFDIR, b"a_dir_content")

        fake_repo = FakeRepo()
        fake_repo[b".gitignore_content"] = (
            FakeBlob(GITIGNORE))

        fake_repo[b"a_dir_content"] = Tree()
        self.mock_get_true_repo_and_path.return_value = (fake_repo, "")

        validation.check_attributes_yml(
            vctx, repo, path, tree, default_access_kinds)
        self.assertEqual(vctx.add_warning.call_count, 0)

        # make sure check_attributes_yml is not called recursively
        self.assertEqual(
            self.mock_get_true_repo_and_path.call_count, 1,
            "check_attributes_yml is expected to be called only once")

    def test_attributes_yml_gitignore_subfolder_commented(self):
        path = ""
        repo = vctx.repo
        tree = Tree()

        tree.add(b".gitignore", stat.S_IFREG, b".gitignore_content")
        tree.add(b"a_dir", stat.S_IFDIR, b"a_dir_content")

        fake_repo = FakeRepo()
        fake_repo[b".gitignore_content"] = (
            FakeBlob(GITIGNORE_COMMENTED))

        fake_repo[b"a_dir_content"] = Tree()
        self.mock_get_true_repo_and_path.return_value = (fake_repo, "")

        validation.check_attributes_yml(
            vctx, repo, path, tree, default_access_kinds)
        self.assertEqual(vctx.add_warning.call_count, 0)

        # make sure check_attributes_yml is called recursively
        self.assertEqual(
            self.mock_get_true_repo_and_path.call_count, 2,
            "check_attributes_yml is expected to be called twice")

    def test_attributes_yml_failed_with_public_and_unenrolled_in_subfolder(self):
        path = ""
        repo = vctx.repo
        tree = Tree()

        tree.add(b"a_dir", stat.S_IFDIR, b"a_dir_content")

        fake_repo = FakeRepo()
        fake_sub_repo = FakeRepo()

        sub_tree = Tree()
        sub_tree.add(b"some_file", stat.S_IFREG, b"some_content")
        sub_tree.add(ATTRIBUTES_FILENAME.encode(), stat.S_IFREG,
                 b".attributes.yml_content")

        fake_sub_repo[b".attributes.yml_content"] = FakeBlob(
            INVALID_ATTRIBUTES_WITH_PUBLIC_AND_UNENROLLED_YML)

        fake_repo[b"a_dir_content"] = sub_tree
        self.mock_get_true_repo_and_path.side_effect = [
            (fake_repo, ""), (fake_sub_repo, "a_dir")]

        with self.assertRaises(ValidationError) as cm:
            validation.check_attributes_yml(
                vctx, repo, path, tree, default_access_kinds)

        expected_error_msg = ("access classes 'public' and 'unenrolled' may not "
                              "exist simultaneously.")
        self.assertIn(expected_error_msg, str(cm.exception))

        # make sure check_attributes_yml is called recursively
        self.assertEqual(
            self.mock_get_true_repo_and_path.call_count, 2,
            "check_attributes_yml is expected to be called twice")