Created
April 4, 2016 22:38
-
-
Save cnk/177cfec9261583d3ce6f71756ad902de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
---------------------------------------------------------------------- | |
Ran 1 test in 0.412s | |
OK | |
BUT if I change from force_authentication to the login line, | |
403 | |
{'detail': 'CSRF Failed: CSRF cookie not set.'} | |
F | |
====================================================================== | |
FAIL: test_lead_can_access_submit_multiple_choice_answer (course_builder.multiple_choice_question.tests2.MCTests) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "/Users/cnk/Code/mereokratos/mk_web_core/course_builder/multiple_choice_question/tests2.py", line 51, in test_le\ | |
ad_can_access_submit_multiple_choice_answer | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | |
AssertionError: 403 != 200 | |
---------------------------------------------------------------------- | |
Ran 1 test in 0.628s | |
FAILED (failures=1) | |
Preserving test database for alias 'default'... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import status | |
from rest_framework.test import APITestCase, APIClient | |
from django.core.urlresolvers import reverse | |
from course_materials.tests.factories import CourseFactory, MaterialVersionFactory, MaterialVersionBlockFactory, \ | |
MultipleChoiceQuestionFactory, MultipleChoiceAnswerFactory, UserWithRoleFactory, TextElementFactory, HintFactory | |
class MCTests(APITestCase): | |
''' | |
This class tests permissions for the API endpoints for course_builder/multiple_choice_question. | |
It also serves as the tests for the most general happy path through our API. | |
''' | |
@classmethod | |
def setUpClass(cls): | |
super(MCTests, cls).setUpClass() | |
cls.course = CourseFactory.create() | |
cls.question = MultipleChoiceQuestionFactory.create(course=cls.course) | |
cls.answers = [] | |
for i in range(4): | |
cls.answers.append(MultipleChoiceAnswerFactory.create(question=cls.question, answer_text=i)) | |
cls.question.correct_answer = cls.answers[1] | |
cls.question.save() | |
cls.mv = MaterialVersionFactory(course=cls.course) | |
cls.mvb = MaterialVersionBlockFactory.create(course=cls.course, material_version=cls.mv, | |
block_id=cls.question.id, block_type=cls.question.model_name) | |
# People we need to test the various roles: | |
cls.tester = UserWithRoleFactory.create(username='tester', is_superuser=False, role='tester', course=cls.course\ | |
) | |
cls.team = UserWithRoleFactory.create(username='team', is_superuser=False, role='course_team', course=cls.cours\ | |
e) # noqa | |
cls.lead = UserWithRoleFactory.create(username='leader', is_superuser=False, role='course_lead', course=cls.cou\ | |
rse) # noqa | |
@classmethod | |
def tearDownClass(cls): | |
cls.course.delete() | |
cls.lead.delete() | |
cls.tester.delete() | |
cls.team.delete() | |
super(MCTests, cls).tearDownClass() | |
def test_lead_can_access_submit_multiple_choice_answer(self): | |
client = APIClient(enforce_csrf_checks=True) | |
# client.login(username=self.lead.username, password='password') | |
client.force_authenticate(user=self.lead) | |
url = reverse('course_builder:multiple_choice_question:submit_multiple_choice_answer', | |
kwargs={'course_key': self.course.course_key, 'pk': self.question.id}) | |
response = client.post(url, {'answer': self.question.correct_answer.get_absolute_url()}) | |
print(response.status_code) | |
print(response.data) | |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But if I declare the client in the class setup, login works fine.