def setUp(self): self.user = create_user("user1", "password", "*****@*****.**") category_1 = create_category(name='python') category_2 = create_category(name='javascript') self.quiz_published_1 = create_quiz(name='quiz 1', category=category_1, date_published=timezone.now()) self.quiz_published_2 = create_quiz(name='quiz 2', category=category_2, date_published=timezone.now()) self.quiz_not_published = create_quiz(name="quiz 3")
def test_removing_image_by_non_creator(self, mock_storage): """Make sure removing quiz image by non creator is not possible.""" mock_storage.return_value = 'quiz-1.png' image = self.get_image_file("image.png") quiz = create_quiz(name='Quiz 1', creator=self.user_2, image=image) response = self.client.delete(reverse('quiz_image', args=(quiz.id, ))) quiz_obj = Quiz.objects.get(name='Quiz 1') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(quiz_obj.image)
def test_removing_image_by_creator(self, mock_storage): """Assert quiz image is removed if author requested it.""" mock_storage.return_value = 'quiz-1.png' image = self.get_image_file("image.png") quiz = create_quiz(name='Quiz 1', creator=self.user, image=image) response = self.client.delete(reverse('quiz_image', args=(quiz.id, ))) quiz_obj = Quiz.objects.get(name='Quiz 1') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(quiz_obj.image)
def test_get_image_by_authenticated(self, mock_storage): """Assert any authenticated user can access quiz image.""" mock_storage.return_value = 'image.png' image = self.get_image_file("image.png") quiz = create_quiz(name='Quiz 1', creator=self.user, image=image) response = self.client.get(reverse('quiz_image', args=(quiz.id, )), format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['url'], quiz.image.url)
def test_changing_image_by_not_creator(self): """Assert quiz image is not changed if not creator requested it.""" quiz = create_quiz(name='test', creator=self.user_2) image_to_upload = self.get_image_file("sample.png") response = self.client.put(reverse('quiz_image', args=(quiz.id, )), data={'image': image_to_upload}, format='multipart') quiz_obj = Quiz.objects.get(name='test') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(quiz_obj.image.name, 'default_quiz.jpeg')
def setUp(self): self.user = create_user("user", "password", "*****@*****.**") self.client.force_authenticate(user=self.user) self.quiz_unpublished = create_quiz(name="quiz 2") self.quiz_published = create_quiz(name="quiz 1", date_published=timezone.now()) self.question_1 = create_question( quiz=self.quiz_published, question="What is your favourite color?", explanation="Some explanation") self.question_2 = create_question(quiz=self.quiz_published, question="What is your name?", explanation="Some explanation") self.answer_1 = create_question_answer(question=self.question_1, content='Answer 1', is_correct=False) self.answer_2 = create_question_answer(question=self.question_1, content='Answer 2', is_correct=True)
def test_uploading_image_by_creator(self, mock_storage): """Assert quiz image is uploaded by creator.""" mock_storage.return_value = 'test.png' quiz = create_quiz(name='test', creator=self.user) image_to_upload = self.get_image_file("test.png") response = self.client.put(reverse('quiz_image', args=(quiz.id, )), data={'image': image_to_upload}, format='multipart') quiz_obj = Quiz.objects.get(name='test') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertTrue(quiz_obj.image)
def setUp(self): self.user_1 = create_user(username='******', password='******', email='*****@*****.**') self.user_2 = create_user(username='******', password='******', email='*****@*****.**') self.client.force_authenticate(user=self.user_1) category_python = create_category(name="python") category_javascript = create_category(name="javascript") self.quiz_unpublished_user_1 = create_quiz(name='quiz1', creator=self.user_1) self.quiz_unpublished_user_2 = create_quiz(name='quiz2', creator=self.user_2) self.quiz_published_user_1 = create_quiz(name='quiz1published', creator=self.user_1, date_published=timezone.now(), category=category_python) self.quiz_published_user_2 = create_quiz(name='quiz2published', creator=self.user_2, date_published=timezone.now(), category=category_javascript)
def test_get_question_detail_for_not_author(self): """Assure questions detail is returned for not author.""" quiz = create_quiz(name='name', creator=self.user_2, date_published=timezone.now()) question = create_question(quiz=quiz, question='...', explanation='...') response = self.client.get( reverse('question_detail', args=(quiz.id, question.id))) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['question'], '...')
def setUp(self): self.user = create_user(username='******', password='******', email='*****@*****.**') self.user_2 = create_user(username='******', password='******', email='*****@*****.**') self.client.force_authenticate(user=self.user) self.quiz = create_quiz(name='Quiz 1', creator=self.user) self.question = create_question(quiz=self.quiz, question='...', explanation='...') self.answer = create_question_answer(question=self.question, content='...', is_correct=True)
def test_update_question_not_author(self): """Assure only author can update question.""" quiz = create_quiz(name='name', creator=self.user_2) question = create_question(quiz=quiz, question='...', explanation='...') payload_data = { 'question': 'New one ...', 'explanation': 'changed explanation' } response = self.client.put(reverse('question_detail', args=(quiz.id, question.id)), data=payload_data) question_obj = Question.objects.get(pk=question.id) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(question_obj.question, "...")
def test_update_question_by_author(self): """Assure author of quiz can update one of its questions.""" quiz = create_quiz(name='name', creator=self.user) question = create_question(quiz=quiz, question='...', explanation='...') payload_data = { 'question': 'New one ...', 'explanation': 'changed explanation' } response = self.client.put(reverse('question_detail', args=(quiz.id, question.id)), data=payload_data) question_obj = Question.objects.get(pk=question.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(question_obj.question, "New one ...") self.assertEqual(response.data['question'], "New one ...")