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

__doc__ = """
This is testing uncovering part of course.models by other tests
"""

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

from datetime import datetime, timedelta
Dong Zhuang's avatar
Dong Zhuang committed
import six
import unittest
import pytz

from django.conf import settings
from django.test import TestCase, override_settings
from django.core.exceptions import ValidationError
from django.utils.timezone import now

from course import models
from course.constants import participation_permission as pperm
from course import constants
from course.content import dict_to_struct

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


class CourseTest(CoursesTestMixinBase, unittest.TestCase):
    def setUp(self):
        self.course1 = factories.CourseFactory()
        self.course2 = factories.CourseFactory(identifier="another-course")

    def test_get_absolute_url(self):
        self.assertEqual(
            self.course1.get_absolute_url(),
            self.get_course_page_url(self.course1.identifier)
        )

        self.assertEqual(
            self.course2.get_absolute_url(),
            self.get_course_page_url(self.course2.identifier)
        )

    def test_get_from_email(self):
        with override_settings(RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True):
            self.assertEqual(self.course1.get_from_email(), self.course1.from_email)
            self.assertEqual(self.course2.get_from_email(), self.course2.from_email)

        notification_email_from = "expected_email_from@example.com"
        robot_email_from = "robot@example.com"
Dong Zhuang's avatar
Dong Zhuang committed
        with override_settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
                NOTIFICATION_EMAIL_FROM=notification_email_from,
                ROBOT_EMAIL_FROM=robot_email_from):
            self.assertEqual(self.course1.get_from_email(), notification_email_from)
            self.assertEqual(self.course2.get_from_email(), notification_email_from)

            del settings.NOTIFICATION_EMAIL_FROM
            self.assertEqual(self.course1.get_from_email(), robot_email_from)
            self.assertEqual(self.course2.get_from_email(), robot_email_from)

    def test_get_reply_to_email(self):
        with override_settings(RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True):
            self.assertEqual(
                self.course1.get_reply_to_email(), self.course1.from_email)
            self.assertEqual(
                self.course2.get_reply_to_email(), self.course2.from_email)

        with override_settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False):
            self.assertEqual(
                self.course1.get_reply_to_email(), self.course1.notify_email)
            self.assertEqual(
                self.course2.get_reply_to_email(), self.course2.notify_email)

    def test_add_default_roles_and_permissions(self):
        # make sure add_default_roles_and_permissions is called after
        # a course is created, and not called when updated.
        with mock.patch(
                "course.models.add_default_roles_and_permissions"
        ) as mock_add_default_roles_and_permissions:
            new_course = factories.CourseFactory(identifier="yet-another-course")
            self.assertEqual(mock_add_default_roles_and_permissions.call_count, 1)
            self.assertIn(
                new_course, mock_add_default_roles_and_permissions.call_args[0])

            mock_add_default_roles_and_permissions.reset_mock()

            new_course.is_hidden = False
            new_course.save()
            self.assertEqual(mock_add_default_roles_and_permissions.call_count, 0)


class RelateModelTestMixin(object):
    def setUp(self):
        self.course = factories.CourseFactory()


class EventTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        event1 = factories.EventFactory(course=self.course, kind="my_event",
                                        ordinal=None)
        event2 = factories.EventFactory(course=self.course, kind="my_event2",
                                        ordinal=None)
        self.assertNotEqual(str(event1), str(event2))

        event3 = factories.EventFactory(course=self.course, kind="my_event3",
                                        ordinal=1)
        event4 = factories.EventFactory(course=self.course, kind="my_event3",
                                        ordinal=2)
        self.assertNotEqual(str(event3), str(event4))

    def test_clean_end_time(self):
        with self.assertRaises(ValidationError) as cm:
            factories.EventFactory(
                course=self.course, time=now(), kind="some_kind",
                end_time=now() - timedelta(seconds=1))

        expected_error_msg = "End time must not be ahead of start time."
        self.assertIn(expected_error_msg, cm.exception.message_dict["end_time"])

        # make sure end_time >= time is valid
        factories.EventFactory(
            course=self.course, time=now(), kind="some_kind",
            end_time=now())

        factories.EventFactory(
            course=self.course, time=now(), kind="some_kind",
            end_time=now() + timedelta(seconds=1))

    def test_event_with_no_ordinal_uniqueness(self):
        kwargs = {"course": self.course, "time": now(),
                  "kind": "some_kind", "ordinal": None}
        event = factories.EventFactory(**kwargs)

        # make sure it can be updated
        event.time = now() - timedelta(days=1)
        event.save()

        from django.db import IntegrityError
        with self.assertRaises(IntegrityError):
            factories.EventFactory(**kwargs)

Dong Zhuang's avatar
Dong Zhuang committed
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 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

class ParticipationTagTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        tag1 = factories.ParticipationTagFactory(course=self.course, name="tag1")
        tag2 = factories.ParticipationTagFactory(course=self.course, name="tag2")
        self.assertNotEqual(str(tag1), str(tag2))

    def test_clean_success(self):
        tag = models.ParticipationTag(course=self.course, name="abcd")
        tag.clean()

    def test_clean_failure(self):
        tag = models.ParticipationTag(course=self.course, name="~abcd")
        with self.assertRaises(ValidationError) as cm:
            tag.clean()

        expected_error_msg = "'name' contains invalid characters."
        self.assertIn(expected_error_msg, cm.exception.message_dict["name"])


class ParticipationRoleTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        tag1 = factories.ParticipationRoleFactory(course=self.course,
                                                  identifier="ta")
        tag2 = factories.ParticipationRoleFactory(course=self.course,
                                                  identifier="stu")
        self.assertNotEqual(str(tag1), str(tag2))

    def test_clean_success(self):
        role = models.ParticipationRole(
            course=self.course, name="role 1", identifier="role1")
        role.clean()

    def test_clean_failure(self):
        role = models.ParticipationRole(
            course=self.course, name="role 1", identifier="role 1")
        with self.assertRaises(ValidationError) as cm:
            role.clean()

        expected_error_msg = "'identifier' contains invalid characters."
        self.assertIn(expected_error_msg, cm.exception.message_dict["identifier"])

    def test_has_permission(self):
        student_pr = models.ParticipationRole.objects.get(
            course=self.course, identifier="student")
        self.assertFalse(
            student_pr.has_permission(pperm.access_files_for, "ta"))
        self.assertTrue(
            student_pr.has_permission(pperm.access_files_for, "student"))
        self.assertFalse(
            student_pr.has_permission(pperm.view_gradebook))

        instructor_pr = models.ParticipationRole.objects.get(
            course=self.course, identifier="instructor")
        self.assertTrue(
            instructor_pr.has_permission(pperm.access_files_for, "instructor"))
        self.assertTrue(
            instructor_pr.has_permission(pperm.view_gradebook))

    def test_permission_tuples_cached(self):
        student_pr = models.ParticipationRole.objects.get(
            course=self.course, identifier="student")
        self.assertFalse(
            student_pr.has_permission(pperm.access_files_for, "ta"))

        with mock.patch(
                "course.models.ParticipationRolePermission.objects.filter"
        ) as mock_filter:
            self.assertFalse(
                student_pr.has_permission(pperm.access_files_for, "instructor"))
            self.assertTrue(
                student_pr.has_permission(pperm.access_files_for, "unenrolled"))

            self.assertEqual(
                mock_filter.call_count, 0,
                "permission_tuples is expected to be cached.")


class ParticipationRolePermissionTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        all_objects = models.ParticipationRolePermission.objects.all()
        count = all_objects.count()
        self.assertGreater(count, 0)

        prp_unicode_list = []
        for prp in all_objects:
            self.assertNotIn(
                str(prp), prp_unicode_list,
                "ParticipationRolePermission objects are expected to "
                "have different stringified result")
            prp_unicode_list.append(str(prp))


class ParticipationTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        course2 = factories.CourseFactory(identifier="another-course")
        user = factories.UserFactory()

        participation1 = factories.ParticipationFactory(course=self.course,
                                                        user=user)
        participation2 = factories.ParticipationFactory(course=course2,
                                                        user=user)

        self.assertNotEqual(str(participation1), str(participation2))

    def test_has_permission(self):
        user = factories.UserFactory()
        participation = factories.ParticipationFactory(course=self.course,
                                                       user=user)

        self.assertTrue(
            participation.has_permission(pperm.access_files_for, "unenrolled"))
        self.assertFalse(
            participation.has_permission(pperm.view_gradebook))

        instructor = factories.UserFactory()
        instructor_role = factories.ParticipationRoleFactory(
            course=self.course,
            identifier="instructor"
        )
        instructor_participation = factories.ParticipationFactory(
            course=self.course,
            user=instructor)
        instructor_participation.roles.set([instructor_role])

        self.assertTrue(
            participation.has_permission(pperm.access_files_for, "unenrolled"))
        self.assertTrue(
            participation.has_permission(pperm.access_files_for, "student"))

    def test_get_role_desc(self):
        course2 = factories.CourseFactory(identifier="another-course")
        user = factories.UserFactory()

        participation1 = factories.ParticipationFactory(course=self.course,
                                                        user=user)
        participation2 = factories.ParticipationFactory(course=course2,
                                                        user=user)

        self.assertIsInstance(participation1.get_role_desc(), six.text_type)
        self.assertEqual(
            participation1.get_role_desc(), participation2.get_role_desc())

        instructor_role = factories.ParticipationRoleFactory(
            course=self.course,
            identifier="instructor"
        )
        participation2.roles.set([instructor_role])
        self.assertNotEqual(
            participation1.get_role_desc(), participation2.get_role_desc())

    def test_permission_cached(self):
        user = factories.UserFactory()
        participation = factories.ParticipationFactory(course=self.course,
                                                       user=user)

        self.assertTrue(
            participation.has_permission(pperm.access_files_for, "unenrolled"))

        with mock.patch(
                "course.models.ParticipationRolePermission.objects.filter"
        ) as mock_filter:
            self.assertFalse(
                participation.has_permission(pperm.view_gradebook))

            self.assertEqual(
                mock_filter.call_count, 0,
                "participation permissions is expected to be cached.")


class ParticipationPreapprovalTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        paprv1 = factories.ParticipationPreapprovalFactory(
            course=self.course, institutional_id=None
        )
        paprv2 = factories.ParticipationPreapprovalFactory(
            course=self.course, institutional_id=None
        )
        self.assertNotEqual(str(paprv1), str(paprv2))

        paprv3 = factories.ParticipationPreapprovalFactory(
            course=self.course, email=None
        )
        paprv4 = factories.ParticipationPreapprovalFactory(
            course=self.course, email=None
        )
        self.assertNotEqual(str(paprv3), str(paprv4))

        paprv5 = factories.ParticipationPreapprovalFactory(
            course=self.course, email=None, institutional_id=None,
        )
        paprv6 = factories.ParticipationPreapprovalFactory(
            course=self.course, email=None, institutional_id=None,
        )
        self.assertNotEqual(str(paprv5), str(paprv6))


class AuthenticationTokenTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        participation1 = factories.ParticipationFactory(
            course=self.course,
            user=factories.UserFactory())

        participation2 = factories.ParticipationFactory(
            course=self.course,
            user=factories.UserFactory())

        token1 = factories.AuthenticationTokenFactory(
            participation=participation1)

        token2 = factories.AuthenticationTokenFactory(
            participation=participation2)

        self.assertNotEqual(str(token1), str(token2))


class InstantFlowRequestTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        ifr1 = factories.InstantFlowRequestFactory(course=self.course)
        ifr2 = factories.InstantFlowRequestFactory(course=self.course,
                                                   flow_id="another-flow")
        self.assertNotEqual(str(ifr1), str(ifr2))


class FlowSessionTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(FlowSessionTest, self).setUp()
        self.user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=self.user)

    def test_unicode(self):
        fs1 = factories.FlowSessionFactory(
            participation=self.participation)
        fs2 = factories.FlowSessionFactory(
            participation=self.participation)

        self.assertNotEqual(str(fs1), str(fs2))

        fs3 = factories.FlowSessionFactory(
            user=None,
            course=self.course,
            participation=None)
        fs4 = factories.FlowSessionFactory(
            user=None,
            course=self.course,
            participation=None)

        self.assertNotEqual(str(fs3), str(fs4))

    def test_points_percentage(self):
        fs = factories.FlowSessionFactory(
            participation=self.participation, points=None)
        self.assertEqual(fs.points_percentage(), None)

        fs = factories.FlowSessionFactory(
            participation=self.participation, points=20, max_points=None)
        self.assertEqual(fs.points_percentage(), None)

        fs = factories.FlowSessionFactory(
            participation=self.participation, points=20, max_points=20)
        self.assertEqual(fs.points_percentage(), 100)

    def test_last_activity(self):
        fs = factories.FlowSessionFactory(participation=self.participation)
        fpdata = factories.FlowPageDataFactory(flow_session=fs)
        factories.FlowPageVisitFactory(
            page_data=fpdata, answer=None,
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC)
        )
        factories.FlowPageVisitFactory(
            page_data=fpdata, answer=None,
            visit_time=datetime(2019, 1, 2, tzinfo=pytz.UTC)
        )
        self.assertEqual(fs.last_activity(), None)

        fpv = factories.FlowPageVisitFactory(
            page_data=fpdata, answer={"answer": "hi"},
            visit_time=datetime(2018, 12, 31, tzinfo=pytz.UTC)
        )

        self.assertEqual(fs.last_activity(), fpv.visit_time)


class FlowPageDataTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(FlowPageDataTest, self).setUp()
        self.user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=self.user)

    def test_unicode(self):
        fs = factories.FlowSessionFactory(participation=self.participation)
        fpdata = factories.FlowPageDataFactory(flow_session=fs)
        self.assertIsNotNone(str(fpdata))


class FlowPageVisitTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(FlowPageVisitTest, self).setUp()
        self.user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=self.user)
        fs = factories.FlowSessionFactory(participation=self.participation)
        self.fpdata = factories.FlowPageDataFactory(flow_session=fs)

    def test_unicode(self):
        visit1 = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC)
        )
        visit2 = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 2, tzinfo=pytz.UTC)
        )

        self.assertNotEqual(str(visit1), str(visit2))
        self.assertNotIn("with answer", str(visit1))

    def test_unicode_with_answer(self):
        visit1 = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer={"answer": "hi"},
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC)
        )
        visit2 = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer={"answer": "hi"},
            visit_time=datetime(2019, 1, 2, tzinfo=pytz.UTC)
        )

        self.assertNotEqual(str(visit1), str(visit2))
        self.assertIn("with answer", str(visit1))


class FlowPageVisitGradeTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(FlowPageVisitGradeTest, self).setUp()
        self.user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=self.user)
        fs = factories.FlowSessionFactory(participation=self.participation)
        self.fpdata = factories.FlowPageDataFactory(flow_session=fs)

    def test_percentage_none(self):
        visit = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC),
        )
        fpvg = factories.FlowPageVisitGradeFactory(
            visit=visit, correctness=None
        )
        self.assertEqual(fpvg.percentage(), None)

    def test_percentage(self):
        visit = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer={"answer": "hi"},
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC),
        )
        fpvg = factories.FlowPageVisitGradeFactory(
            visit=visit, correctness=0.5
        )
        self.assertEqual(fpvg.percentage(), 50)

    def test_uniqueness(self):
        visit = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC),
        )

        factories.FlowPageVisitGradeFactory(
            visit=visit
        )
        from django.db import IntegrityError
        with self.assertRaises(IntegrityError):
            factories.FlowPageVisitGradeFactory(
                visit=visit
            )

    def test_unicode(self):
        visit = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 1, tzinfo=pytz.UTC),
        )
        visit2 = factories.FlowPageVisitFactory(
            page_data=self.fpdata, answer=None,
            visit_time=datetime(2019, 1, 2, tzinfo=pytz.UTC),
        )

        fpvg = factories.FlowPageVisitGradeFactory(
            visit=visit
        )
        fpvg2 = factories.FlowPageVisitGradeFactory(
            visit=visit2
        )
        self.assertEqual(fpvg.percentage(), None)

        self.assertNotEqual(str(fpvg), str(fpvg2))


class GetFeedbackForGradeTest(RelateModelTestMixin, unittest.TestCase):
    # test models.get_feedback_for_grade
    def test_grade_is_none(self):
        self.assertIsNone(models.get_feedback_for_grade(None))


class FlowRuleExceptionTest(RelateModelTestMixin, TestCase):
    def setUp(self):
        super(FlowRuleExceptionTest, self).setUp()
        user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=user)
        fake_get_course_repo = mock.patch("course.content.get_course_repo")
        self.mock_get_course_repo = fake_get_course_repo.start()
        self.mock_get_course_repo.return_value = mock.MagicMock()
        self.addCleanup(fake_get_course_repo.stop)

        fake_get_flow_desc = mock.patch("course.content.get_flow_desc")
        self.mock_get_flow_desc = fake_get_flow_desc.start()
        self.addCleanup(fake_get_flow_desc.stop)

        fake_validate_session_start_rule = mock.patch(
            "course.validation.validate_session_start_rule")
        self.mock_validate_session_start_rule = (
            fake_validate_session_start_rule.start())
        self.addCleanup(fake_validate_session_start_rule.stop)

        fake_validate_session_access_rule = mock.patch(
            "course.validation.validate_session_access_rule")
        self.mock_validate_session_access_rule = (
            fake_validate_session_access_rule.start())
        self.addCleanup(fake_validate_session_access_rule.stop)

        fake_validate_session_grading_rule = mock.patch(
            "course.validation.validate_session_grading_rule")
        self.mock_validate_session_grading_rule = (
            fake_validate_session_grading_rule.start())
        self.addCleanup(fake_validate_session_grading_rule.stop)

    def test_unicode(self):
        fre1 = factories.FlowRuleExceptionFactory(
            participation=self.participation)
        fre2 = factories.FlowRuleExceptionFactory(
            participation=self.participation)

        self.assertNotEqual(str(fre1), str(fre2))

    def test_clean_success_null_exception_rule(self):
        rule = dict()
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=constants.flow_rule_kind.start,
            rule=rule,
            expiration=None
        )

        fre.clean()
        self.assertEqual(self.mock_get_course_repo.call_count, 1)
        self.assertEqual(self.mock_get_flow_desc.call_count, 1)
        self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)

    def test_clean_failure_with_invalid_existing_session_rules(self):
        rule = dict()
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=constants.flow_rule_kind.start,
            rule=rule,
            expiration=None
        )
        from course import validation
        my_custom_error = "my custom error"
        self.mock_validate_session_start_rule.side_effect = (
            validation.ValidationError(my_custom_error))

        with self.assertRaises(ValidationError) as cm:
            fre.clean()

        expected_error_msg = "invalid existing_session_rules: %s" % my_custom_error
        self.assertIn(expected_error_msg, str(cm.exception))
        self.assertEqual(self.mock_get_course_repo.call_count, 1)
        self.assertEqual(self.mock_get_flow_desc.call_count, 1)
        self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)

    def test_clean_success_no_existing_rules(self):
        self.mock_get_flow_desc.return_value = dict_to_struct(
            {"id": "no_existing_flow"})
        rule = dict()
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=constants.flow_rule_kind.start,
            rule=rule,
            expiration=None
        )

        fre.clean()
        self.assertEqual(self.mock_get_course_repo.call_count, 1)
        self.assertEqual(self.mock_get_flow_desc.call_count, 1)
        self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)

    def test_clean_grading_success(self):
        rule = {
            "if_completed_before": now(),
            "credit_percent": 100
        }
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=constants.flow_rule_kind.grading,
            rule=rule,
            expiration=None
        )

        fre.clean()
        self.assertEqual(self.mock_get_course_repo.call_count, 1)
        self.assertEqual(self.mock_get_flow_desc.call_count, 1)
        self.assertEqual(self.mock_validate_session_grading_rule.call_count, 1)

    def test_clean_grading_no_expire_failure(self):
        rule = {
            "if_completed_before": now(),
            "credit_percent": 100
        }
        expected_error_msg = "grading rules may not expire"
        with self.assertRaises(ValidationError) as cm:
            fre = models.FlowRuleException(
                flow_id=factories.DEFAULT_FLOW_ID,
                participation=self.participation,
                kind=constants.flow_rule_kind.grading,
                rule=rule,
                expiration=now()
            )

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

        self.assertEqual(
            self.mock_get_course_repo.call_count, 0,
            "The expensive operation should be skipped in this case")
        self.assertEqual(
            self.mock_get_flow_desc.call_count, 0,
            "The expensive operation should be skipped in this case")
        self.assertEqual(self.mock_validate_session_grading_rule.call_count, 0,
                         "The expensive operation should be skipped in this case")

    def test_clean_access_success(self):
        rule = {
            "if_before": now()
        }
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=constants.flow_rule_kind.access,
            rule=rule,
            expiration=now()
        )

        fre.clean()
        self.assertEqual(self.mock_get_course_repo.call_count, 1)
        self.assertEqual(self.mock_get_flow_desc.call_count, 1)
        self.assertEqual(self.mock_validate_session_access_rule.call_count, 1)

    def test_clean_unknown_exception_rule(self):
        unknown_flow_rule_kind = "unknown_kind"
        rule = {
            "if_before": now()
        }
        fre = models.FlowRuleException(
            flow_id=factories.DEFAULT_FLOW_ID,
            participation=self.participation,
            kind=unknown_flow_rule_kind,
            rule=rule,
            expiration=now()
        )

        with self.assertRaises(ValidationError) as cm:
            fre.clean()
        expected_error_msg = "invalid exception rule kind"
        self.assertIn(expected_error_msg, str(cm.exception))

        for call in (self.mock_get_course_repo,
                     self.mock_get_flow_desc,
                     self.mock_validate_session_access_rule,
                     self.mock_validate_session_access_rule,
                     self.mock_validate_session_access_rule,
                     self.mock_validate_session_access_rule):
            self.assertEqual(
                call.call_count, 0,
                "The expensive operation should be skipped in this case")


class GradingChangeTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(GradingChangeTest, self).setUp()
        self.user = factories.UserFactory()
        self.participation = factories.ParticipationFactory(
            course=self.course,
            user=self.user)
        self.flow_session = factories.FlowSessionFactory(
            participation=self.participation)
        self.opportunity1 = factories.GradingOpportunityFactory(
            course=self.course, identifier="gopp1"
        )
        self.opportunity2 = factories.GradingOpportunityFactory(
            course=self.course, identifier="gopp2"
        )

    def test_unicode(self):
        gc1 = factories.GradeChangeFactory(
            opportunity=self.opportunity1, participation=self.participation)
        self.assertIsNotNone(str(gc1))

    def test_clean(self):
        gc = factories.GradeChangeFactory(
            opportunity=self.opportunity1, participation=self.participation)
        gc.clean()

    def test_clean_fail(self):
        course2 = factories.CourseFactory(identifier="another-course")
        opportunity3 = factories.GradingOpportunityFactory(
            course=course2, identifier="gopp3"
        )
        gc = factories.GradeChangeFactory(
            opportunity=opportunity3, participation=self.participation)

        with self.assertRaises(ValidationError) as cm:
            gc.clean()

        expected_error_msg = ("Participation and opportunity must live "
                              "in the same course")
        self.assertIn(expected_error_msg, str(cm.exception))


class InstantMessageTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        user = factories.UserFactory()
        participation = factories.ParticipationFactory(
            course=self.course,
            user=user)
        im1 = factories.InstantMessageFactory(
            participation=participation, text="my message")
        im2 = factories.InstantMessageFactory(
            participation=participation, text="my message2")

        self.assertNotEqual(str(im1), str(im2))


class ExamTest(RelateModelTestMixin, unittest.TestCase):
    def test_unicode(self):
        exam1 = factories.ExamFactory(
            course=self.course, description="hello", flow_id="exam-1")
        exam2 = factories.ExamFactory(
            course=self.course, description="exam", flow_id="exam-1")

        self.assertNotEqual(str(exam1), str(exam2))


class ExamTicketTest(RelateModelTestMixin, unittest.TestCase):
    def setUp(self):
        super(ExamTicketTest, self).setUp()
        self.exam = factories.ExamFactory(course=self.course)

        self.user1 = factories.UserFactory()
        self.participation1 = factories.ParticipationFactory(
            course=self.course,
            user=self.user1)
        self.user2 = factories.UserFactory()
        self.participation2 = factories.ParticipationFactory(
            course=self.course,
            user=self.user2)

    def test_unicode(self):
        et1 = factories.ExamTicketFactory(
            exam=self.exam, participation=self.participation1, code="abcd")
        et2 = factories.ExamTicketFactory(
            exam=self.exam, participation=self.participation2, code="cdef")

        self.assertNotEqual(str(et1), str(et2))

    def test_clean(self):
        et1 = models.ExamTicket(
            exam=self.exam, participation=self.participation1, code="abcd")
        et1.clean()

    def test_clean_failure(self):
        course2 = factories.CourseFactory(identifier="another-course")
        participation3 = factories.ParticipationFactory(
            course=course2,
            user=self.user2)

        et2 = models.ExamTicket(
            exam=self.exam, participation=participation3, code="cdef")

        with self.assertRaises(ValidationError) as cm:
            et2.clean()

        expected_error_msg = ("Participation and exam must live "
                              "in the same course")
        self.assertIn(expected_error_msg, str(cm.exception))

    def test_clean_no_participation(self):
        et1 = models.ExamTicket(
            exam=self.exam, participation=None, code="abcd")
        et1.clean()