Skip to content
test_exam.py 24.2 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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
# -*- coding: utf-8 -*-

from __future__ import division

__copyright__ = "Copyright (C) 2014 Andreas Kloeckner, Zesheng Wang, 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 datetime
import pytz

import unittest
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils.timezone import now, timedelta

from course.models import ExamTicket
from course import constants, exam

from tests.constants import (
    DATE_TIME_PICKER_TIME_FORMAT)

from tests.base_test_mixins import (
    SingleCourseTestMixin, MockAddMessageMixing)
from tests.utils import mock
from tests import factories


class GenTicketCodeTest(unittest.TestCase):
    """test exam.gen_ticket_code"""

    def test_unique(self):
        code = set()
        for i in range(10):
            code.add(exam.gen_ticket_code())

        self.assertEqual(len(code), 10)


class ExamTestMixin(SingleCourseTestMixin, MockAddMessageMixing):
    force_login_student_for_each_test = False

    default_faked_now = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)

    default_valid_start_time = default_faked_now
    default_valid_end_time = default_valid_start_time + timedelta(hours=3)

    @classmethod
    def setUpTestData(cls):  # noqa
        super(ExamTestMixin, cls).setUpTestData()
        cls.add_user_permission(
            cls.instructor_participation.user, "can_issue_exam_tickets",
            model=ExamTicket)
        cls.exam = factories.ExamFactory(course=cls.course)

    def setUp(self):
        super(ExamTestMixin, self).setUp()
        self.c.force_login(self.instructor_participation.user)

    def get_post_data(self, **kwargs):
        data = {
            "user": self.student_participation.user.pk,
            "exam": self.exam.pk,
            "valid_start_time": (
                self.default_valid_start_time.strftime(
                    DATE_TIME_PICKER_TIME_FORMAT)),
            "valid_end_time": (
                self.default_valid_end_time.strftime(
                    DATE_TIME_PICKER_TIME_FORMAT))}
        data.update(kwargs)
        return data


class IssueExamTicketTest(ExamTestMixin, TestCase):
    """test exam.issue_exam_ticket
    """

    def get_issue_exam_ticket_url(self):
        from django.urls import reverse
        return reverse("relate-issue_exam_ticket")

    def get_issue_exam_ticket_view(self):
        return self.c.get(self.get_issue_exam_ticket_url())

    def post_issue_exam_ticket_view(self, data):
        return self.c.post(self.get_issue_exam_ticket_url(), data)

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_issue_exam_ticket_view()
            self.assertEqual(resp.status_code, 403)

            resp = self.post_issue_exam_ticket_view(data={})
            self.assertEqual(resp.status_code, 403)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_issue_exam_ticket_view()
            self.assertEqual(resp.status_code, 403)

            resp = self.post_issue_exam_ticket_view(data={})
            self.assertEqual(resp.status_code, 403)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_get_success(self):
        resp = self.get_issue_exam_ticket_view()
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 0)

    def test_post_success(self):
        resp = self.post_issue_exam_ticket_view(data=self.get_post_data())
        self.assertFormErrorLoose(resp, None)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 1)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Ticket issued for", reset=False)
        self.assertAddMessageCalledWith("The ticket code is")

    def test_form_invalid(self):
        with mock.patch("course.exam.IssueTicketForm.is_valid") as mock_is_valid:
            mock_is_valid.return_value = False
            resp = self.post_issue_exam_ticket_view(data=self.get_post_data())
            self.assertFormErrorLoose(resp, None)
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_participation_not_match(self):
        another_exam = factories.ExamFactory(
            course=factories.CourseFactory(identifier="another-course"))
        resp = self.post_issue_exam_ticket_view(
            data=self.get_post_data(exam=another_exam.pk))
        self.assertFormErrorLoose(resp, None)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 0)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("User is not enrolled in course.")

    def test_revoke_revoke_prior_ticket(self):
        prior_ticket = factories.ExamTicketFactory(
            exam=self.exam,
            participation=self.student_participation,
            state=constants.exam_ticket_states.valid)

        resp = self.post_issue_exam_ticket_view(
            data=self.get_post_data(revoke_prior=True))
        self.assertFormErrorLoose(resp, None)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 2)
        prior_ticket.refresh_from_db()
        self.assertEqual(prior_ticket.state, constants.exam_ticket_states.revoked)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Ticket issued for", reset=False)
        self.assertAddMessageCalledWith("The ticket code is")


class BatchIssueExamTicketsTest(ExamTestMixin, TestCase):
    def get_batch_issue_exam_ticket_url(self, course_identifier=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        return self.get_course_view_url(
            "relate-batch_issue_exam_tickets",
            course_identifier=course_identifier)

    def get_batch_issue_exam_ticket_view(self):
        return self.c.get(self.get_batch_issue_exam_ticket_url())

    def post_batch_issue_exam_ticket_view(self, data):
        return self.c.post(self.get_batch_issue_exam_ticket_url(), data)

    def get_post_data(self, **kwargs):
        data = super(BatchIssueExamTicketsTest, self).get_post_data()
        del data["user"]
        data["format"] = "{{ tickets }}{{checkin_uri}}"
        data.update(kwargs)
        return data

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_batch_issue_exam_ticket_view()
            self.assertEqual(resp.status_code, 403)

            resp = self.post_batch_issue_exam_ticket_view(data={})
            self.assertEqual(resp.status_code, 403)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_batch_issue_exam_ticket_view()
            self.assertEqual(resp.status_code, 403)

            resp = self.post_batch_issue_exam_ticket_view(data={})
            self.assertEqual(resp.status_code, 403)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_get_success(self):
        resp = self.get_batch_issue_exam_ticket_view()
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 0)

    def test_form_invalid(self):
        with mock.patch("course.exam.BatchIssueTicketsForm.is_valid"
                        ) as mock_is_valid:
            mock_is_valid.return_value = False
            resp = self.post_batch_issue_exam_ticket_view(data=self.get_post_data())
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(ExamTicket.objects.count(), 0)

    def test_template_syntax_error(self):
        with mock.patch("course.content.markup_to_html") as mock_mth:
            from jinja2 import TemplateSyntaxError
            mock_mth.side_effect = TemplateSyntaxError(
                lineno=10, message=b"my error")
            resp = self.post_batch_issue_exam_ticket_view(data=self.get_post_data())
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(ExamTicket.objects.count(), 0)
            self.assertAddMessageCallCount(1)
            self.assertAddMessageCalledWith("Template rendering failed")

    def test_unknown_error(self):
        with mock.patch("course.content.markup_to_html") as mock_mth:
            mock_mth.side_effect = RuntimeError("my error")
            resp = self.post_batch_issue_exam_ticket_view(data=self.get_post_data())
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(ExamTicket.objects.count(), 0)
            self.assertAddMessageCallCount(1)
            self.assertAddMessageCalledWith("Template rendering failed")

    def test_post_success(self):
        factories.ParticipationFactory(course=self.course)
        factories.ParticipationFactory(
            course=self.course, status=constants.participation_status.dropped)
        resp = self.post_batch_issue_exam_ticket_view(
            data=self.get_post_data())
        self.assertEqual(resp.status_code, 200)
        self.assertFormErrorLoose(resp, None)
        self.assertEqual(ExamTicket.objects.count(), 4)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("4 tickets issued.")

    def test_revoke_revoke_prior_ticket(self):
        prior_ticket = factories.ExamTicketFactory(
            exam=self.exam,
            participation=self.student_participation,
            state=constants.exam_ticket_states.valid)

        resp = self.post_batch_issue_exam_ticket_view(
            data=self.get_post_data(revoke_prior=True))
        self.assertFormErrorLoose(resp, None)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(ExamTicket.objects.count(), 4)
        prior_ticket.refresh_from_db()
        self.assertEqual(prior_ticket.state, constants.exam_ticket_states.revoked)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("3 tickets issued.")


@override_settings(RELATE_TICKET_MINUTES_VALID_AFTER_USE=120)
class CheckExamTicketTest(ExamTestMixin, TestCase):
    def setUp(self):
        super(CheckExamTicketTest, self).setUp()
        self.exam.refresh_from_db()
        self.now = now()
        self.facilities = frozenset([])
        self.exam.no_exams_after = self.now + timedelta(days=1)
        self.exam.no_exams_before = self.now - timedelta(days=2)
        self.exam.save()
        self.ticket = factories.ExamTicketFactory(
            exam=self.exam, participation=self.student_participation,
            state=constants.exam_ticket_states.valid,
            usage_time=self.now + timedelta(minutes=100),
            valid_start_time=self.now - timedelta(minutes=20),
            valid_end_time=self.now + timedelta(minutes=100),
        )

    def test_object_not_found(self):
        result, msg = exam.check_exam_ticket(
            username="abcd",
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)
        self.assertFalse(result)
        self.assertEqual(msg, "User name or ticket code not recognized.")

    def test_ticket_not_usable(self):
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)

        self.assertTrue(result, msg=msg)

        self.ticket.state = constants.exam_ticket_states.revoked
        self.ticket.save()

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Ticket is not in usable state. (Has it been revoked?)")

    def test_ticket_expired(self):
        self.ticket.usage_time = self.now - timedelta(days=1)
        self.ticket.state = constants.exam_ticket_states.used
        self.ticket.save()
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)

        self.assertFalse(result, msg=msg)
        self.assertEqual(msg, "Ticket has exceeded its validity period.")

    def test_ticket_not_active(self):
        self.exam.active = False
        self.exam.save()

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)

        self.assertFalse(result, msg=msg)
        self.assertEqual(msg, "Exam is not active.")

    def test_not_started(self):
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now - timedelta(days=5),
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(msg, "Exam has not started yet.")

    def test_ended(self):
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now + timedelta(days=5),
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(msg, "Exam has ended.")

    def test_restrict_to_facility(self):
        self.ticket.restrict_to_facility = "my_fa1"
        self.ticket.save()

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=frozenset(["my_fa2"]))
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Exam ticket requires presence in facility 'my_fa1'.")

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=None)
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Exam ticket requires presence in facility 'my_fa1'.")

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Exam ticket requires presence in facility 'my_fa1'.")

        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.now,
            facilities=frozenset(["my_fa1", "my_fa2"]))
        self.assertTrue(result, msg=msg)

    def test_not_yet_valid(self):
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.ticket.valid_start_time - timedelta(minutes=1),
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Exam ticket is not yet valid.")

    def test_expired(self):
        result, msg = exam.check_exam_ticket(
            username=self.student_participation.user.username,
            code=self.ticket.code,
            now_datetime=self.ticket.valid_end_time + timedelta(minutes=1),
            facilities=self.facilities)
        self.assertFalse(result, msg=msg)
        self.assertEqual(
            msg, "Exam ticket has expired.")


class ExamTicketBackendTest(ExamTestMixin, TestCase):
    def setUp(self):
        super(ExamTicketBackendTest, self).setUp()
        self.backend = exam.ExamTicketBackend()

    def test_not_authenticate(self):
        with mock.patch("course.exam.check_exam_ticket") as mock_check_ticket:
            mock_check_ticket.return_value = False, "msg"
            self.assertIsNone(self.backend.authenticate("foo", "bar"))

    def test_get_user(self):
        self.assertEqual(
            self.backend.get_user(self.instructor_participation.user.pk),
            self.instructor_participation.user)

        self.assertIsNone(self.backend.get_user(100))


class IsFromExamsOnlyFacilityTest(unittest.TestCase):
    # test exam.is_from_exams_only_facility
    def setUp(self):
        self.requset = mock.MagicMock()
        self.requset.relate_facilities = ["fa1", "fa2"]
        fake_get_facilities_config = mock.patch(
            "course.utils.get_facilities_config")
        self.mock_get_facilities_config = fake_get_facilities_config.start()
        self.addCleanup(fake_get_facilities_config.stop)

    def test_true(self):
        self.mock_get_facilities_config.return_value = {
            "fa1": {"exams_only": False, "ip_range": "foo"},
            "fa2": {"exams_only": True, "ip_range": "bar"}}
        self.assertTrue(exam.is_from_exams_only_facility(self.requset))

    def test_false(self):
        self.mock_get_facilities_config.return_value = dict()
        self.assertFalse(exam.is_from_exams_only_facility(self.requset))

    def test_false_2(self):
        self.mock_get_facilities_config.return_value = {
            "fa1": {"exams_only": False, "ip_range": "foo"},
            "fa3": {"exams_only": True, "ip_range": "bar"}}
        self.assertFalse(exam.is_from_exams_only_facility(self.requset))


class CheckInForExamTest(ExamTestMixin, TestCase):
    force_login_student_for_each_test = True

    def setUp(self):
        super(CheckInForExamTest, self).setUp()
        self.ticket = factories.ExamTicketFactory(
            exam=self.exam, participation=self.student_participation,
            state=constants.exam_ticket_states.valid)
        self.instructor_ticket = factories.ExamTicketFactory(
            exam=self.exam, participation=self.instructor_participation,
            state=constants.exam_ticket_states.valid)

        fake_check_exam_ticket = mock.patch("course.exam.check_exam_ticket")
        self.mock_check_exam_ticket = fake_check_exam_ticket.start()
        self.addCleanup(fake_check_exam_ticket.stop)

    def get_check_in_for_exam_url(self):
        return reverse("relate-check_in_for_exam")

    def get_check_in_for_exam_view(self):
        return self.c.get(self.get_check_in_for_exam_url())

    def post_check_in_for_exam_view(self, data):
        return self.c.post(self.get_check_in_for_exam_url(), data)

    def get_post_data(self, **kwargs):
        data = {
            "username": self.student_participation.user.username,
            "code": self.ticket.code
        }
        data.update(kwargs)
        return data

    def test_get(self):
        resp = self.get_check_in_for_exam_view()
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.ticket.state, constants.exam_ticket_states.valid)

    def test_login_success(self):
        self.mock_check_exam_ticket.return_value = True, "hello"
        resp = self.post_check_in_for_exam_view(
            self.get_post_data())

        self.assertEqual(resp.status_code, 302)
        self.ticket.refresh_from_db()
        self.assertEqual(self.ticket.state, constants.exam_ticket_states.used)

    def test_login_failure(self):
        self.mock_check_exam_ticket.return_value = False, "what's wrong?"
        resp = self.post_check_in_for_exam_view(
            self.get_post_data())

        self.assertEqual(resp.status_code, 200)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("what's wrong?")
        self.assertEqual(self.ticket.state, constants.exam_ticket_states.valid)

    def test_form_invalid(self):
        with mock.patch("course.exam.ExamCheckInForm.is_valid"
                        ) as mock_is_valid:
            mock_is_valid.return_value = False
            resp = self.post_check_in_for_exam_view(data=self.get_post_data())
            self.assertEqual(resp.status_code, 200)
        self.assertEqual(self.ticket.state, constants.exam_ticket_states.valid)

    def test_login_though_ticket_not_valid(self):
        self.ticket.state = constants.exam_ticket_states.used
        self.ticket.save()
        self.mock_check_exam_ticket.return_value = True, "hello"
        resp = self.post_check_in_for_exam_view(
            self.get_post_data())

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(self.ticket.state, constants.exam_ticket_states.used)

    def test_pretend_facility(self):
        with self.temporarily_switch_to_user(self.instructor_participation.user):
            session = self.c.session
            fa = ["my_falicity_1"]
            session["relate_pretend_facilities"] = fa
            session.save()

            self.mock_check_exam_ticket.return_value = True, "hello"
            resp = self.post_check_in_for_exam_view(
                self.get_post_data(
                    username=self.instructor_participation.user.username,
                    code=self.instructor_ticket.code
                ))
            self.assertEqual(resp.status_code, 302)

            self.assertIn(frozenset(fa), self.mock_check_exam_ticket.call_args[0])
            self.assertEqual(
                self.c.session["relate_pretend_facilities"], fa)
            self.assertEqual(
                self.c.session["relate_exam_ticket_pk_used_for_login"],
                self.instructor_ticket.pk)

            self.instructor_ticket.refresh_from_db()
            self.assertEqual(self.instructor_ticket.state,
                             constants.exam_ticket_states.used)


class ListAvailableExamsTest(ExamTestMixin, TestCase):

    def setUp(self):
        super(ListAvailableExamsTest, self).setUp()
        self.ticket = factories.ExamTicketFactory(
            exam=self.exam, participation=self.student_participation,
            state=constants.exam_ticket_states.valid)
        self.instructor_ticket = factories.ExamTicketFactory(
            exam=self.exam, participation=self.instructor_participation,
            state=constants.exam_ticket_states.valid)

    def get_list_available_exams_url(self):
        return reverse("relate-list_available_exams")

    def get_list_available_view(self):
        return self.c.get(self.get_list_available_exams_url())

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_list_available_view()
            exams = resp.context["exams"]
            self.assertEqual(exams.count(), 0)

    def test_not_found(self):
        self.exam.no_exams_after = now() - timedelta(days=1)
        self.exam.no_exams_before = now() - timedelta(days=2)
        self.exam.save()
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_list_available_view()
            exams = resp.context["exams"]
            self.assertEqual(exams.count(), 0)

    def test_found(self):
        self.exam.no_exams_after = now() + timedelta(days=1)
        self.exam.no_exams_before = now() - timedelta(days=2)
        self.exam.save()
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_list_available_view()
            exams = resp.context["exams"]
            self.assertEqual(exams.count(), 1)

# vim: fdm=marker