def test_post_text_correct_answers(self): test_data = self.get_test_data() for answer in test_data['text_sections'][0]['questions'][0]['answers']: answer['correct'] = False resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 400, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('errors', resp_content) self.assertIn('textsection_0_question_0_answers', resp_content['errors']) self.assertEquals( 'You must choose a correct answer for this question.', resp_content['errors']['textsection_0_question_0_answers']) # set one correct answer test_data['text_sections'][0]['questions'][0]['answers'][0][ 'correct'] = True resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4))
def test_put_new_section(self): test_data = self.get_test_data() resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(Text.objects.count(), 1) resp_content = json.loads(resp.content.decode('utf8')) text_id = resp_content['id'] test_data['text_sections'].append(self.gen_text_section_params(2)) resp = self.instructor.put(reverse_lazy('text-item-api', kwargs={'pk': text_id}), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) text = Text.objects.get(pk=text_id) self.assertEquals(3, text.sections.count())
def test_post_text_max_char_limits(self): test_data = self.get_test_data() test_text_section_body_size = 4096 answer_feedback_limit = Answer._meta.get_field('feedback').max_length # answer feedback limited test_data['text_sections'][0]['questions'][0]['answers'][0][ 'feedback'] = 'a' * (answer_feedback_limit + 1) # no limit for text section bodies test_data['text_sections'][0][ 'body'] = 'a' * test_text_section_body_size resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 400, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('errors', resp_content) self.assertIn('textsection_0_question_0_answer_0_feedback', resp_content['errors']) self.assertEquals( resp_content['errors'] ['textsection_0_question_0_answer_0_feedback'], 'Ensure this value has at most ' '{0} characters (it has {1}).'.format(answer_feedback_limit, (answer_feedback_limit + 1))) self.assertNotIn('textsection_0_body', resp_content['errors']) test_data['text_sections'][0]['questions'][0]['answers'][0][ 'feedback'] = 'a' resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertNotIn('errors', resp_content) self.assertIn('id', resp_content) # ensure db doesn't truncate text_section = TextSection.objects.get(pk=resp_content['id']) self.assertEquals(len(text_section.body), test_text_section_body_size)
def test_post_text(self, test_data: Optional[Dict] = None) -> Text: test_data = test_data or self.get_test_data() num_of_sections = len(test_data['text_sections']) resp = self.instructor.post(reverse_lazy('text-api'), json.dumps({"malformed": "json"}), content_type='application/json') self.assertEquals(resp.status_code, 400) resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(Text.objects.count(), 1) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('id', resp_content) self.assertIn('redirect', resp_content) text = Text.objects.get(pk=resp_content['id']) self.assertEquals(TextSection.objects.count(), num_of_sections) resp = self.instructor.get(reverse_lazy('text-item-api', kwargs={'pk': text.id}), content_type='application/json') self.assertEquals( resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertTrue(resp_content) self.assertEquals(resp_content['title'], test_data['title']) self.assertEquals(resp_content['introduction'], test_data['introduction']) self.assertEquals(resp_content['tags'], test_data['tags']) return text
def student_login(self, client: Client, user: Optional[ReaderUser] = None, username: Optional[AnyStr] = None, password: Optional[AnyStr] = None) -> Client: return self.login(client, reverse_lazy('api-student-login'), user, username, password)
def test_text_tags(self): test_data = self.get_test_data() text_one = self.create_text() text_two = self.create_text(diff_data={'tags': ['Society and Societal Trends']}) resp = self.instructor.put(reverse_lazy('text-tag-api', kwargs={'pk': text_one.pk}), json.dumps('Society and Societal Trends'), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(text_one.tags.count(), len(test_data['tags'])+1) self.assertIn('Society and Societal Trends', [tag.name for tag in text_one.tags.all()]) resp = self.instructor.put(reverse_lazy('text-tag-api', kwargs={'pk': text_one.pk}), json.dumps(['Sports', 'Science/Technology', 'Other']), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(text_one.tags.count(), 4) resp = self.instructor.delete(reverse_lazy('text-tag-api', kwargs={'pk': text_one.pk}), json.dumps('Other'), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(text_one.tags.count(), 3) self.assertEquals(text_two.tags.count(), 1) resp = self.instructor.delete(reverse_lazy('text-tag-api', kwargs={'pk': text_two.pk}), json.dumps('Society and Societal Trends'), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(text_one.tags.count(), 3) self.assertEquals(text_two.tags.count(), 0) text_three = self.create_text(diff_data={'tags': ['Science/Technology']}) science_tech_tag = Tag.objects.get(name='Science/Technology') self.assertEquals(science_tech_tag.texts.count(), 2)
def test_text_lock(self): other_instructor_client = self.new_instructor_client(Client()) resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(self.get_test_data()), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('id', resp_content) text = Text.objects.get(pk=resp_content['id']) lock_api_endpoint_for_text = reverse_lazy('text-lock-api', kwargs={'pk': text.pk}) resp = self.instructor.post(reverse_lazy('text-lock-api', kwargs={'pk': text.pk}), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp = other_instructor_client.post(lock_api_endpoint_for_text, content_type='application/json') self.assertEquals(resp.status_code, 500, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp = other_instructor_client.delete(lock_api_endpoint_for_text, content_type='application/json') self.assertEquals(resp.status_code, 500, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp = self.instructor.delete(lock_api_endpoint_for_text, content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp = other_instructor_client.post(lock_api_endpoint_for_text, content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp = self.instructor.post(lock_api_endpoint_for_text, content_type='application/json') self.assertEquals(resp.status_code, 500, json.dumps(json.loads(resp.content.decode('utf8')), indent=4))
def test_delete_text(self, text: Optional[Text] = None): if text is None: text = self.create_text() resp = self.instructor.delete(reverse_lazy('text-item-api', kwargs={'pk': text.pk}), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertTrue(resp_content) self.assertTrue('deleted' in resp_content)
def setup_text(self, test_data) -> Text: resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(Text.objects.count(), 1) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('id', resp_content) text = Text.objects.get(pk=resp_content['id']) return text
def create_text(self, test_data: Dict = None, diff_data: Dict = None) -> Text: text_data = test_data or self.get_test_data() if diff_data: text_data.update(diff_data) resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(text_data), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) text = Text.objects.get(pk=resp_content['id']) return text
def setUp(self): super(TestTextWordTranslations, self).setUp() Tag.setup_default() TextDifficulty.setup_default() self.instructor = self.new_instructor_client(Client()) self.student = self.new_student_client(Client()) resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(self.test_text_data), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.text = Text.objects.get(pk=resp_content['id'])
def test_translations_merge(self): # mock text word since we don't necessarily want to get into running translations code in this particular test test_text_word = TextWord.objects.create( phrase='something', text_section=self.text.sections.all()[0] ) resp = self.instructor.put( reverse_lazy('text-translation-match-method'), json.dumps({ 'words': [{'id': test_text_word.pk}], 'translations': [ {'correct_for_context': True, 'phrase': 'stuff'}, {'correct_for_context': False, 'phrase': 'stuff 2'}, {'correct_for_context': False, 'phrase': 'stuff 3'} ]} ) ) self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) translations = TextPhraseTranslation.objects.filter(text_phrase=test_text_word) self.assertEquals(len(translations), 3)
def test_put_text(self): test_data = self.get_test_data() resp = self.instructor.post(reverse_lazy('text-api'), json.dumps(test_data), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) self.assertEquals(Text.objects.count(), 1) resp_content = json.loads(resp.content.decode('utf8')) text = Text.objects.get(pk=resp_content['id']) test_data['title'] = 'a new text title' test_data['introduction'] = 'a new introduction' test_data['author'] = 'J. K. Idding' resp = self.instructor.put(reverse_lazy('text-item-api', kwargs={'pk': text.pk}), json.dumps(test_data), content_type='application/json') self.assertEquals(resp.status_code, 200) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('id', resp_content) resp = self.instructor.get(reverse_lazy('text-item-api', kwargs={'pk': text.id}), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertTrue(resp_content) self.assertIn('difficulty', resp_content) self.assertEquals(resp_content['title'], 'a new text title') self.assertEquals(resp_content['introduction'], 'a new introduction') self.assertEquals(resp_content['author'], 'J. K. Idding') test_data['text_sections'][1]['questions'][0]['body'] = 'A new question?' test_data['text_sections'][1]['questions'][0]['answers'][1]['text'] = 'A new answer.' resp = self.instructor.put(reverse_lazy('text-item-api', kwargs={'pk': text.pk}), json.dumps(test_data), content_type='application/json') self.assertEquals(resp.status_code, 200) resp_content = json.loads(resp.content.decode('utf8')) self.assertIn('id', resp_content) resp = self.instructor.get(reverse_lazy('text-item-api', kwargs={'pk': text.id}), content_type='application/json') self.assertEquals(resp.status_code, 200, json.dumps(json.loads(resp.content.decode('utf8')), indent=4)) resp_content = json.loads(resp.content.decode('utf8')) self.assertTrue(resp_content) self.assertEquals(resp_content['text_sections'][1]['questions'][0]['body'], 'A new question?') self.assertEquals(resp_content['text_sections'][1]['questions'][0]['answers'][1]['text'], 'A new answer.')
def __init__(self, *args, **kwargs): super(TestText, self).__init__(*args, **kwargs) self.text_endpoint = reverse_lazy('text-api')