def test_shouldnt_allow_consulting_a_problem_which_isnt_unlocked(self): self.setupProblemsWithAGoodAnswer() url = reverse('problem-by-id', kwargs={'problem_id': ID_PROBLEME_3}) auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_should_indicate_accessible_problems_and_those_who_arent(self): self.setupClassicProblems() url = reverse('problems-list') auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertTrue(response.data[0]["accessible"]) self.assertFalse(response.data[1]["accessible"]) self.assertFalse(response.data[2]["accessible"])
def test_should_take_in_order_when_good_answers_are_given(self): self.setupProblemsWithAGoodAnswer() url = reverse('problems-list') auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertTrue(response.data[0]["accessible"]) self.assertTrue(response.data[1]["accessible"]) self.assertFalse(response.data[2]["accessible"])
def test_should_get_problem_details(self): self.setupClassicProblems() url = reverse('problem-by-id', kwargs={'problem_id': ID_PROBLEME_1}) auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertContains(response=response, text=ID_PROBLEME_1) self.assertContains(response=response, text=TITRE_PROBLEME_1) self.assertContains(response=response, text=CONTENU_PROBLEME_1)
def test_should_return_correct_info_for_whoami(self): auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) url_whoami = reverse("whoami") response_whoami = self.client.get(url_whoami, **auth_headers) print(response_whoami) self.assertEqual(response_whoami.status_code, status.HTTP_200_OK) self.assertEqual(response_whoami.data["id_utilisateur"], ID_UTILISATEUR_1) self.assertEqual(response_whoami.data["prenom"], PRENOM_UTILISATEUR_1) self.assertEqual(response_whoami.data["nom"], NOM_UTILISATEUR_1)
def test_should_fetch_problems_summaries(self): self.setupClassicProblems() url = reverse('problems-list') auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertContains(response=response, text=TITRE_PROBLEME_1) self.assertContains(response=response, text=ID_PROBLEME_1) self.assertContains(response=response, text=ID_PROBLEME_2) self.assertEqual(response.data[0]["image_url"], self.probleme1.image.url)
def test_should_allow_good_answer_with_different_case(self): self.setupClassicProblems() url = reverse('submit-answer', kwargs={'problem_id': ID_PROBLEME_1}) auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) good_answer = self.probleme1.get_question(self.utilisateur1.id).reponse response = self.client.post(url, format='json', data={"reponse": good_answer.upper()}, **auth_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data["correct"])
def test_should_respect_questions_order(self): self.setupClassicProblems() url = reverse('problems-list') auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertEqual(len(response.data), 3) self.assertEqual(response.data[0]["id"], ID_PROBLEME_1) self.assertEqual(response.data[1]["id"], ID_PROBLEME_2) self.assertEqual(response.data[2]["id"], ID_PROBLEME_3) self.assertEqual(response.data[0]["index"], 1) self.assertEqual(response.data[1]["index"], 2) self.assertEqual(response.data[2]["index"], 3)
def test_should_create_a_good_answer_when_given(self): self.setupClassicProblems() url = reverse('submit-answer', kwargs={'problem_id': ID_PROBLEME_1}) auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) good_answer = self.probleme1.get_question(self.utilisateur1.id).reponse response = self.client.post(url, format='json', data={"reponse": good_answer}, **auth_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data["correct"]) self.assertEqual(BonneReponse.objects.count(), 1) bonne_reponse: BonneReponse = BonneReponse.objects.first() self.assertEqual(bonne_reponse.probleme, self.probleme1) self.assertEqual(bonne_reponse.utilisateur, self.utilisateur1)
def test_should_not_fetch_infos_on_not_unlocked_problems(self): self.setupProblemsWithAGoodAnswer() url = reverse('problems-list') auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) response = self.client.get(url, format='json', **auth_headers) self.assertTrue(response.data[0]["accessible"]) self.assertEqual(TITRE_PROBLEME_1, response.data[0]["titre"]) self.assertEqual(self.probleme1.image.url, response.data[0]["image_url"]) self.assertTrue(response.data[1]["accessible"]) self.assertEqual(TITRE_PROBLEME_2, response.data[1]["titre"]) self.assertEqual(self.probleme2.image.url, response.data[1]["image_url"]) self.assertFalse(response.data[2]["accessible"]) self.assertEqual("", response.data[2]["titre"]) self.assertEqual("", response.data[2]["image_url"])
def test_should_store_when_a_bad_answer_is_given(self): self.setupClassicProblems() url = reverse('submit-answer', kwargs={'problem_id': ID_PROBLEME_1}) auth_headers = compute_auth_header(self.client, MAIL_UTILISATEUR_1, PASS_UTILISATEUR_1) bad_answer = "la mer noire" response = self.client.post(url, format='json', data={"reponse": bad_answer}, **auth_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(response.data["correct"]) self.assertEqual(MauvaiseReponse.objects.count(), 1) mauvaise_reponse: MauvaiseReponse = MauvaiseReponse.objects.first() self.assertEqual(mauvaise_reponse.utilisateur, self.utilisateur1) self.assertEqual(mauvaise_reponse.probleme, self.probleme1) self.assertEqual(mauvaise_reponse.reponse_donnee, bad_answer)