Skip to content
test_pages.py 23.6 KiB
Newer Older
Andreas Klöckner's avatar
Andreas Klöckner committed
from __future__ import division

Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner, Zesheng Wang, Dong Zhuang"
Andreas Klöckner's avatar
Andreas Klöckner committed

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

Dong Zhuang's avatar
Dong Zhuang committed
import os
from base64 import b64encode
Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase
Dong Zhuang's avatar
Dong Zhuang committed
from django.urls import reverse
Dong Zhuang's avatar
Dong Zhuang committed
from django.core import mail
Dong Zhuang's avatar
Dong Zhuang committed
from django.contrib.auth import get_user_model
Dong Zhuang's avatar
Dong Zhuang committed
from course.models import FlowPageVisit, Course, FlowSession
from .base_test_mixins import (
    SingleCoursePageTestMixin, FallBackStorageMessageTestMixin)
from .utils import LocmemBackendTestsMixin
Dong Zhuang's avatar
Dong Zhuang committed
QUIZ_FLOW_ID = "quiz-test"
Dong Zhuang's avatar
Dong Zhuang committed
MESSAGE_ANSWER_SAVED_TEXT = "Answer saved."
MESSAGE_ANSWER_FAILED_SAVE_TEXT = "Failed to submit answer."
Dong Zhuang's avatar
Dong Zhuang committed

class SingleCourseQuizPageTest(SingleCoursePageTestMixin,
                               FallBackStorageMessageTestMixin, TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    flow_id = QUIZ_FLOW_ID
zwang180's avatar
zwang180 committed

Dong Zhuang's avatar
Dong Zhuang committed
    def setUp(self):  # noqa
        super(SingleCourseQuizPageTest, self).setUp()
        self.c.force_login(self.student_participation.user)
        self.start_quiz(self.flow_id)

Dong Zhuang's avatar
Dong Zhuang committed
    # TODO: This should be moved to tests for auth module
zwang180's avatar
zwang180 committed
    def test_user_creation(self):
Dong Zhuang's avatar
Dong Zhuang committed
        # Should have 4 users
        self.assertEqual(get_user_model().objects.all().count(), 4)
        self.c.logout()

        self.assertTrue(
            self.c.login(
                username=self.instructor_participation.user.username,
                password=(
                    self.courses_setup_list[0]
                    ["participations"][0]
                    ["user"]["password"])))

    # TODO: This should move to tests for course.view module
zwang180's avatar
zwang180 committed
    def test_course_creation(self):
        # Should only have one course
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(Course.objects.all().count(), 1)
        resp = self.c.get(reverse("relate-course_page",
                                  args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        # 200 != 302 is better than False is not True
        self.assertEqual(resp.status_code, 200)

Dong Zhuang's avatar
Dong Zhuang committed
    # {{{ auto graded questions
    def test_quiz_no_answer(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(5)
    def test_quiz_choice(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(2, {"choice": ['0']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(2)
Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_choice_failed_no_answer(self):
        self.assertSubmitHistoryItemsCount(page_ordinal=2, expected_count=0)
        resp = self.client_post_answer_by_ordinal(2, {"choice": []})
        self.assertEqual(resp.status_code, 200)
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_FAILED_SAVE_TEXT)

        # There should be no submission history
        # https://github.com/inducer/relate/issues/351
        self.assertSubmitHistoryItemsCount(page_ordinal=2, expected_count=0)
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)

zwang180's avatar
zwang180 committed
    def test_quiz_multi_choice_exact_correct(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=0)
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1', '4']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=1)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(1)
zwang180's avatar
zwang180 committed
    def test_quiz_multi_choice_exact_wrong(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=0)
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=1)
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)

    def test_quiz_multi_choice_failed_change_answer(self):
        # Note: this page doesn't have permission to change_answer
        # submit a wrong answer
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=0)
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1']})
        self.assertEqual(resp.status_code, 200)
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=1)

        # try to change answer to a correct one
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1', '4']})
        self.assertSubmitHistoryItemsCount(page_ordinal=3, expected_count=1)
        self.assertEqual(resp.status_code, 200)
        self.assertResponseMessagesContains(
                    resp, ["Already have final answer.",
                           "Failed to submit answer."])
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)
Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_multi_choice_proportion_partial(self):
        resp = self.client_post_answer_by_ordinal(4, {"choice": ['0']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0.8)
Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_multi_choice_proportion_correct(self):
        resp = self.client_post_answer_by_ordinal(4, {"choice": ['0', '3']})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(1)
zwang180's avatar
zwang180 committed
    def test_quiz_inline(self):
Dong Zhuang's avatar
Dong Zhuang committed
        answer_data = {
            'blank1': ['Bar'], 'blank_2': ['0.2'], 'blank3': ['1'],
            'blank4': ['5'], 'blank5': ['Bar'], 'choice2': ['0'],
            'choice_a': ['0']}
        resp = self.client_post_answer_by_ordinal(5, answer_data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(10)
Dong Zhuang's avatar
Dong Zhuang committed
    # }}}

    # {{{ survey questions

    def test_quiz_survey_text(self):
        self.assertSubmitHistoryItemsCount(page_ordinal=6, expected_count=0)
        resp = self.client_post_answer_by_ordinal(
                            6, {"answer": ["NOTHING!!!"]})
        self.assertSubmitHistoryItemsCount(page_ordinal=6, expected_count=1)
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed

        # Survey question won't be counted into final score
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertSessionScoreEqual(0)

        query = FlowPageVisit.objects.filter(
Dong Zhuang's avatar
Dong Zhuang committed
            flow_session__exact=self.page_params["flow_session_id"],
            answer__isnull=False)
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["answer"], "NOTHING!!!")

Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_survey_choice(self):
        self.assertSubmitHistoryItemsCount(page_ordinal=7, expected_count=0)

        # no answer thus no history
        self.client_post_answer_by_ordinal(7, {"choice": []})
        self.assertSubmitHistoryItemsCount(page_ordinal=7, expected_count=0)

Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(7, {"choice": ['8']})
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertSubmitHistoryItemsCount(page_ordinal=7, expected_count=1)
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed

        # Survey question won't be counted into final score
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertSessionScoreEqual(0)

        query = FlowPageVisit.objects.filter(
Dong Zhuang's avatar
Dong Zhuang committed
                            flow_session__exact=self.page_params["flow_session_id"],
zwang180's avatar
zwang180 committed
                            answer__isnull=False)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["choice"], 8)
Dong Zhuang's avatar
Dong Zhuang committed
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

    def test_fileupload_any(self):
        page_id = "anyup"
        page_ordinal = self.get_ordinal_via_page_id(page_id)
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=0)
        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.txt'), 'rb') as fp:
            resp = self.client_post_answer_by_page_id(
                page_id, {"uploaded_file": fp})
            fp.seek(0)
            expected_result = b64encode(fp.read()).decode()
            self.assertEqual(resp.status_code, 200)

        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=1)
        query = FlowPageVisit.objects.filter(
                            flow_session__exact=self.page_params["flow_session_id"],
                            answer__isnull=False)
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["base64_data"], expected_result)
        self.assertSessionScoreEqual(None)

    def test_fileupload_any_change_answer(self):
        page_id = "anyup"
        page_ordinal = self.get_ordinal_via_page_id(page_id)
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=0)
        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.txt'), 'rb') as fp:
            resp = self.client_post_answer_by_page_id(
                page_id, {"uploaded_file": fp})
            fp.seek(0)
            expected_result1 = b64encode(fp.read()).decode()
            self.assertEqual(resp.status_code, 200)

        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=1)

        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.pdf'), 'rb') as fp:
            resp = self.client_post_answer_by_page_id(
                page_id, {"uploaded_file": fp})
            self.assertEqual(resp.status_code, 200)
            fp.seek(0)
            expected_result2 = b64encode(fp.read()).decode()

        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=2)

        query = FlowPageVisit.objects.filter(
            flow_session__exact=self.page_params["flow_session_id"],
            answer__isnull=False)
        self.assertEqual(query.count(), 2)
        self.assertEqual(query[1].answer["base64_data"], expected_result2)
        self.assertEqual(query[0].answer["base64_data"], expected_result1)
        self.assertSessionScoreEqual(None)

    def test_fileupload_pdf(self):
        page_id = "proof"
        page_ordinal = self.get_ordinal_via_page_id(page_id)
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=0)
        # wrong MIME type
        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.txt'), 'rb') as fp:
            resp = self.client_post_answer_by_page_id(
                page_id, {"uploaded_file": fp})
            self.assertEqual(resp.status_code, 200)

        self.assertResponseMessagesContains(resp, [MESSAGE_ANSWER_FAILED_SAVE_TEXT])

        # There should be no submission history
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=0)
        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.pdf'), 'rb') as fp:
            resp = self.client_post_answer_by_page_id(
                page_id, {"uploaded_file": fp})
            self.assertEqual(resp.status_code, 200)
            fp.seek(0)
            expected_result = b64encode(fp.read()).decode()

        self.assertResponseMessagesContains(resp, MESSAGE_ANSWER_SAVED_TEXT)
        self.assertSubmitHistoryItemsCount(page_ordinal=page_ordinal,
                                           expected_count=1)
        query = FlowPageVisit.objects.filter(
            flow_session__exact=self.page_params["flow_session_id"],
            answer__isnull=False)
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["base64_data"], expected_result)
        self.assertSessionScoreEqual(None)

    # {{{ tests on submission history dropdown
    def test_submit_history_failure_not_ajax(self):
        self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        resp = self.c.get(
            self.page_submit_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_not_get(self):
        self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        resp = self.c.post(
            self.page_submit_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_not_authenticated(self):
        self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        self.c.logout()
        resp = self.c.post(
            self.page_submit_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_no_perm(self):
        self.c.force_login(self.ta_participation.user)
        self.start_quiz(self.flow_id)
        self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        self.c.logout()
        self.c.force_login(self.student_participation.user)
        resp = self.c.post(
            self.page_submit_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    # }}}


class SingleCourseQuizPageGradeInterfaceTest(LocmemBackendTestsMixin,
                                SingleCoursePageTestMixin,
                                FallBackStorageMessageTestMixin, TestCase):
    flow_id = QUIZ_FLOW_ID

    def setUp(self):  # noqa
        super(SingleCourseQuizPageGradeInterfaceTest, self).setUp()
        self.c.force_login(self.student_participation.user)
        self.start_quiz(self.flow_id)
        self.submit_any_upload_question()

    def get_grading_page_url_by_page_id(self, flow_session_id, page_id):
        return reverse(
            "relate-grade_flow_page",
            kwargs={"course_identifier": self.course.identifier,
                    "flow_session_id": flow_session_id,
                    "page_ordinal": self.get_ordinal_via_page_id(page_id)})

    def submit_any_upload_question_null_failure(self):
        self.client_post_answer_by_page_id(
            "anyup", {"uploaded_file": []})

    def submit_any_upload_question(self):
        with open(
                os.path.join(os.path.dirname(__file__),
                             'fixtures', 'test_file.txt'), 'rb') as fp:
            self.client_post_answer_by_page_id(
                "anyup", {"uploaded_file": fp})

    def post_grade(self, flow_session_id, page_id, grade_data):
        post_data = {"submit": [""]}
        post_data.update(grade_data)
        resp = self.c.post(
            self.get_grading_page_url_by_page_id(flow_session_id, page_id),
            data=post_data,
            follow=True)
        return resp

    def test_post_grades(self):
        self.end_quiz()
        last_session = FlowSession.objects.all().last()
        grade_data = {
            "grade_percent": ["100"],
            "released": ["on"]
        }
        self.c.force_login(self.ta_participation.user)
        resp = self.post_grade(last_session.pk, "anyup", grade_data)
        self.assertTrue(resp.status_code, 200)
        self.assertSessionScoreEqual(5)

        grade_data = {
            "grade_points": ["4"],
            "released": []
        }
        resp = self.post_grade(last_session.pk, "anyup", grade_data)
        self.assertTrue(resp.status_code, 200)

        self.assertSessionScoreEqual(None)

        grade_data = {
            "grade_points": ["4"],
            "released": ["on"]
        }
        resp = self.post_grade(last_session.pk, "anyup", grade_data)
        self.assertTrue(resp.status_code, 200)

        self.assertSessionScoreEqual(4)

    def test_post_grades_history(self):
        self.c.force_login(self.student_participation.user)
        page_id = "anyup"
        page_ordinal = self.get_ordinal_via_page_id(page_id)

        # failure
        self.submit_any_upload_question_null_failure()

        # 2nd success
        self.submit_any_upload_question()
        self.end_quiz()

        last_session = FlowSession.objects.all().last()
        grade_data = {
            "grade_percent": ["100"],
            "released": ["on"]
        }
        self.c.force_login(self.ta_participation.user)
        resp = self.post_grade(last_session.pk, page_id, grade_data)
        self.assertTrue(resp.status_code, 200)
        self.assertSessionScoreEqual(5)
        self.assertGradeHistoryItemsCount(page_ordinal=page_ordinal,
                                          expected_count=3)

        grade_data = {
            "grade_points": ["4"],
            "released": []
        }
        resp = self.post_grade(last_session.pk, page_id, grade_data)
        self.assertTrue(resp.status_code, 200)
        self.assertSessionScoreEqual(None)
        self.assertGradeHistoryItemsCount(page_ordinal=page_ordinal,
                                          expected_count=4)

        grade_data = {
            "grade_points": ["4"],
            "released": ["on"]
        }
        resp = self.post_grade(last_session.pk, page_id, grade_data)
        self.assertTrue(resp.status_code, 200)
        self.assertSessionScoreEqual(4)
        self.assertGradeHistoryItemsCount(page_ordinal=page_ordinal,
                                          expected_count=5)

    def test_post_grades_success(self):
        self.end_quiz()

        last_session = FlowSession.objects.all().last()

        grade_data = {
            "grade_percent": ["100"],
            "released": ['on']
        }

        self.c.force_login(self.ta_participation.user)

        resp = self.post_grade(last_session.pk, "anyup", grade_data)
        self.assertTrue(resp.status_code, 200)

        self.assertSessionScoreEqual(5)

    def test_post_grades_forbidden(self):
        page_id = "anyup"
        self.end_quiz()
        last_session = FlowSession.objects.all().last()

        grade_data = {
            "grade_percent": ["100"],
            "released": ['on']
        }

        # with self.student_participation.user logged in
        resp = self.post_grade(last_session.pk, page_id, grade_data)
        self.assertTrue(resp.status_code, 403)

        self.assertSessionScoreEqual(None)

    def test_feedback_and_notify(self):
        page_id = "anyup"
        self.end_quiz()
        last_session = FlowSession.objects.all().last()

        grade_data = {
            "grade_percent": ["100"],
            "released": ['on'],
            "feedback_text": ['test feedback']
        }

        self.c.force_login(self.ta_participation.user)
        self.post_grade(last_session.pk, page_id, grade_data)
        self.assertEqual(len(mail.outbox), 0)

        grade_data["notify"] = ["on"]
        self.post_grade(last_session.pk, page_id, grade_data)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].reply_to, [])

    def test_feedback_email_may_reply(self):
        page_id = "anyup"
        self.end_quiz()

        last_session = FlowSession.objects.all().last()

        grade_data = {
            "grade_percent": ["100"],
            "released": ['on'],
            "feedback_text": ['test feedback'],
            "notify": ["on"],
            "may_reply": ["on"]
        }

        self.c.force_login(self.ta_participation.user)
        self.post_grade(last_session.pk, page_id, grade_data)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].reply_to, [self.ta_participation.user.email])

    def test_notes_and_notify(self):
        page_id = "anyup"
        self.end_quiz()

        last_session = FlowSession.objects.all().last()

        grade_data = {
            "grade_percent": ["100"],
            "released": ['on'],
            "notes": ['test notes']
        }

        self.c.force_login(self.ta_participation.user)
        self.post_grade(last_session.pk, page_id, grade_data)
        self.assertEqual(len(mail.outbox), 0)

        grade_data["notify_instructor"] = ["on"]
        self.post_grade(last_session.pk, page_id, grade_data)
        self.assertEqual(len(mail.outbox), 1)

    # {{{ tests on grading history dropdown
    def test_grade_history_failure_not_ajax(self):
        self.end_quiz()

        self.c.force_login(self.ta_participation.user)
        resp = self.c.get(
            self.page_grade_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_not_get(self):
        self.end_quiz()

        self.c.force_login(self.ta_participation.user)
        resp = self.c.post(
            self.page_grade_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_not_authenticated(self):
        self.end_quiz()

        self.c.logout()
        resp = self.c.post(
            self.page_grade_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    def test_submit_history_failure_no_perm(self):
        self.c.force_login(self.ta_participation.user)
        self.start_quiz(self.flow_id)
        self.end_quiz()

        self.c.force_login(self.student_participation.user)
        resp = self.c.post(
            self.page_grade_history_url(
                flow_session_id=FlowSession.objects.all().last().pk,
                page_ordinal=1))
        self.assertEqual(resp.status_code, 403)

    # }}}

# vim: fdm=marker