def problem_copy(user, original_problem, new_problem_set): """ Copies the given problem to the given problem set. Verifies if: a) user can edit the problem. b) user can edit the problem set. Returns the copy of the given problem. """ verify(user.can_view_problem(original_problem)) verify(request.user.can_edit_problem_set(new_problem_set)) new_problem = Problem() new_problem.title = original_problem.title new_problem.description = original_problem.description new_problem.problem_set = new_problem_set new_problem.tags = original_problem.tags new_problem.save() original_parts = original_problem.parts.all() for original_part in original_parts: new_part = Part() new_part.problem = new_problem new_part.description = original_part.description new_part.solution = original_part.solution new_part.validation = original_part.validation new_part.secret = original_part.secret new_part.save() return new_problem
def setUp(self): cour = Course.objects.create() prob_set = ProblemSet.objects.create(course=cour) self.problem = Problem.objects.create(problem_set=prob_set) self.part1 = Part(problem=self.problem, secret='["1"]') self.part2 = Part(problem=self.problem, secret='["1", "2"]') self.part3 = Part(problem=self.problem, secret='["1", "2", "3"]') self.part1.save() self.part2.save() self.part3.save() self.attempts_data = [ { "solution": "s1", "valid": False, "feedback": ["f1", "f2"], "secret": [], "part": self.part1.pk }, { "solution": "s2", "valid": True, "feedback": ["Error"], "secret": ["1"], "part": self.part2.pk }, { "solution": "", "valid": True, "feedback": [], "secret": ["1", "2", "3"], "part": self.part3.pk }, { "solution": "", "valid": True, "feedback": [], "secret": ["1", "4", "3"], "part": self.part3.pk } ] self.user = User.objects.get(username='******') self.client = APIClient() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.user.auth_token.key)
def setUp(self): course = Course.objects.create() problem_set = ProblemSet.objects.create(course=course) problem = Problem.objects.create(problem_set=problem_set) self.part1 = Part(problem=problem, secret='["1"]') self.part1.save() self.part2 = Part(problem=problem, secret='["1", "2"]') self.part2.save() self.part3 = Part(problem=problem, secret='["1", "2", "3"]') self.part3.save() user = User.objects.create_user(username='******') self.client = APIClient() self.client.credentials(HTTP_AUTHORIZATION='Token ' + user.auth_token.key) self.attempts_data = [{ 'solution': 's1', 'valid': False, 'feedback': ['f1', 'f2'], 'secret': [], 'part': self.part1.pk }, { 'solution': 's2', 'valid': True, 'feedback': ['Error'], 'secret': ['1'], 'part': self.part2.pk }, { 'solution': '', 'valid': True, 'feedback': [], 'secret': ['1', '2', '3'], 'part': self.part3.pk }, { 'solution': '', 'valid': True, 'feedback': [], 'secret': ['1', '4', '3'], 'part': self.part3.pk }]
class AttemptSubmitTestCase(TestCase): fixtures = ['users.json'] def setUp(self): cour = Course.objects.create() prob_set = ProblemSet.objects.create(course=cour) self.problem = Problem.objects.create(problem_set=prob_set) self.part1 = Part(problem=self.problem, secret='["1"]') self.part2 = Part(problem=self.problem, secret='["1", "2"]') self.part3 = Part(problem=self.problem, secret='["1", "2", "3"]') self.part1.save() self.part2.save() self.part3.save() self.attempts_data = [ { "solution": "s1", "valid": False, "feedback": ["f1", "f2"], "secret": [], "part": self.part1.pk }, { "solution": "s2", "valid": True, "feedback": ["Error"], "secret": ["1"], "part": self.part2.pk }, { "solution": "", "valid": True, "feedback": [], "secret": ["1", "2", "3"], "part": self.part3.pk }, { "solution": "", "valid": True, "feedback": [], "secret": ["1", "4", "3"], "part": self.part3.pk } ] self.user = User.objects.get(username='******') self.client = APIClient() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.user.auth_token.key) def testSimpleSubmit(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', self.attempts_data, format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 3, "There should be exactly two attempts" " in the database") def testSubmitData(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[0]], format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertEqual(attempt.solution, self.attempts_data[0]["solution"], 'Saved and received data must be equal') self.assertSequenceEqual(json.loads(attempt.feedback), self.attempts_data[0]["feedback"], 'Saved and received data must be equal') self.assertEqual(attempt.valid, self.attempts_data[0]["valid"], 'Saved and received data must be equal') self.assertEqual(attempt.part.id, self.attempts_data[0]["part"], 'Saved and received data must be equal') def testInvalidSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[1]], format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertFalse(attempt.valid, 'Secret is invalid, attempt must be marked as invalid') def testValidSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[2]], format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertTrue(attempt.valid, 'Attempt must be marked as valid') def testChangedSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[2]], format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertTrue(attempt.valid, 'Attempt must be marked as valid') response = self.client.post('/api/attempts/submit/', [self.attempts_data[3]], format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertFalse(attempt.valid, 'Attempt must be marked as valid')
class AttemptSubmitTestCase(TestCase): def setUp(self): course = Course.objects.create() problem_set = ProblemSet.objects.create(course=course) problem = Problem.objects.create(problem_set=problem_set) self.part1 = Part(problem=problem, secret='["1"]') self.part1.save() self.part2 = Part(problem=problem, secret='["1", "2"]') self.part2.save() self.part3 = Part(problem=problem, secret='["1", "2", "3"]') self.part3.save() user = User.objects.create_user(username='******') self.client = APIClient() self.client.credentials(HTTP_AUTHORIZATION='Token ' + user.auth_token.key) self.attempts_data = [{ 'solution': 's1', 'valid': False, 'feedback': ['f1', 'f2'], 'secret': [], 'part': self.part1.pk }, { 'solution': 's2', 'valid': True, 'feedback': ['Error'], 'secret': ['1'], 'part': self.part2.pk }, { 'solution': '', 'valid': True, 'feedback': [], 'secret': ['1', '2', '3'], 'part': self.part3.pk }, { 'solution': '', 'valid': True, 'feedback': [], 'secret': ['1', '4', '3'], 'part': self.part3.pk }] def testSimpleSubmit(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', self.attempts_data, format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 3, "There should be exactly three attempts" " in the database") def testSubmitData(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[0]], format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertEqual(attempt.solution, self.attempts_data[0]["solution"], 'Saved and received data must be equal') self.assertSequenceEqual(json.loads(attempt.feedback), self.attempts_data[0]["feedback"], 'Saved and received data must be equal') self.assertEqual(attempt.valid, self.attempts_data[0]["valid"], 'Saved and received data must be equal') self.assertEqual(attempt.part.id, self.attempts_data[0]["part"], 'Saved and received data must be equal') def testInvalidSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[1]], format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertFalse( attempt.valid, 'Secret is invalid, attempt must be marked as invalid') def testValidSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[2]], format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertTrue(attempt.valid, 'Attempt must be marked as valid') def testChangedSecret(self): self.assertEqual(Attempt.objects.count(), 0, "There should be no attempts in the database") response = self.client.post('/api/attempts/submit/', [self.attempts_data[2]], format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertTrue(attempt.valid, 'Attempt must be marked as valid') response = self.client.post('/api/attempts/submit/', [self.attempts_data[3]], format='json') self.assertEqual(response.status_code, 200) self.assertEqual( Attempt.objects.count(), 1, "There should be exactly one attempt" " in the database") attempt = Attempt.objects.get() self.assertTrue(self.attempts_data[1]["valid"], "Attempt data should be true") self.assertFalse(attempt.valid, 'Attempt must be marked as invalid') def testHistory(self): self.client.post('/api/attempts/submit/', [self.attempts_data[2]], format='json') self.client.post('/api/attempts/submit/', [self.attempts_data[3]], format='json') self.assertEqual(Attempt.objects.count(), 1) attempt = Attempt.objects.get() self.assertEqual(attempt.history.count(), 2) self.client.post('/api/attempts/submit/', [self.attempts_data[3]], format='json') self.assertEqual(attempt.history.count(), 2)