def test_submissions_permissions(self): contest = Contest.objects.get() submission = Submission.objects.get() check_not_accessible(self, 'submission', kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id }) contest.controller_name = \ 'oioioi.contests.tests.PrivateContestController' contest.save() problem_instance = ProblemInstance.objects.get() problem = problem_instance.problem self.client.login(username='******') check_not_accessible(self, 'problems_list', kwargs={'contest_id': contest.id}) check_not_accessible(self, 'problem_statement', kwargs={ 'contest_id': contest.id, 'problem_instance': problem_instance.short_name }) check_not_accessible(self, 'my_submissions', kwargs={'contest_id': contest.id}) check_not_accessible(self, 'contest_files', kwargs={'contest_id': contest.id})
def test_ranking_csv_view(self): contest = Contest.objects.get() url = reverse('ranking_csv', kwargs={'contest_id': contest.id, 'key': 'c'}) self.client.login(username='******') with fake_time(datetime(2015, 8, 5, tzinfo=utc)): check_not_accessible(self, url) self.client.login(username='******') with fake_time(datetime(2012, 8, 5, tzinfo=utc)): response = self.client.get(url) self.assertContains(response, 'User,') # Check that Admin is filtered out. self.assertNotContains(response, 'Admin') expected_order = ['Test,User', 'Test,User 2'] prev_pos = 0 for user in expected_order: pattern = '%s,' % (user,) self.assertIn(user, response.content) pos = response.content.find(pattern) self.assertGreater(pos, prev_pos, msg=('User %s has incorrect ' 'position' % (user,))) prev_pos = pos for task in ['zad1', 'zad2', 'zad3', 'zad3']: self.assertContains(response, task) response = self.client.get(reverse('ranking', kwargs={'contest_id': contest.id, 'key': '1'})) self.assertContains(response, 'zad1') for task in ['zad2', 'zad3', 'zad3']: self.assertNotContains(response, task)
def test_submissions_permissions(self): submission = TestRunProgramSubmission.objects.get(pk=1) kwargs = { 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id, } self.assertTrue(self.client.login(username='******')) for view in [ 'get_testrun_output', 'get_testrun_input', 'download_testrun_output', 'download_testrun_input', ]: check_not_accessible(self, view, kwargs=kwargs) for view in ['get_testrun_output', 'get_testrun_input']: check_ajax_not_accessible(self, view, kwargs=kwargs) contest = Contest.objects.get(pk='c') contest.controller_name = 'oioioi.contests.tests.PrivateContestController' contest.save() self.client.logout() for view in ['get_testrun_output', 'get_testrun_input']: check_ajax_not_accessible(self, view, kwargs=kwargs)
def test_ranking_csv_view(self): contest = Contest.objects.get() url = reverse("ranking_csv", kwargs={"contest_id": contest.id, "key": "c"}) self.client.login(username="******") with fake_time(datetime(2015, 8, 5, tzinfo=utc)): check_not_accessible(self, url) self.client.login(username="******") with fake_time(datetime(2012, 8, 5, tzinfo=utc)): response = self.client.get(url) self.assertContains(response, "User,") # Check that Admin is filtered out. self.assertNotContains(response, "Admin") expected_order = ["Test,User", "Test,User 2"] prev_pos = 0 for user in expected_order: pattern = "%s," % (user,) self.assertIn(user, response.content) pos = response.content.find(pattern) self.assertGreater(pos, prev_pos, msg=("User %s has incorrect " "position" % (user,))) prev_pos = pos for task in ["zad1", "zad2", "zad3", "zad3"]: self.assertContains(response, task) response = self.client.get(reverse("ranking", kwargs={"contest_id": contest.id, "key": "1"})) self.assertContains(response, "zad1") for task in ["zad2", "zad3", "zad3"]: self.assertNotContains(response, task)
def test_change_denied(self): self.assertTrue(self.client.login(username='******')) self.client.get('/c/c/') # 'c' becomes the current contest msg = Message.objects.filter(author__username='******')[0] url = reverse('oioioiadmin:questions_message_change', args=(msg.id, )) check_not_accessible(self, url)
def test_invalidate_view(self): contest = Contest.objects.get() url = reverse('ranking_invalidate', kwargs={ 'contest_id': contest.id, 'key': 'key' }) self.assertTrue(self.client.login(username='******')) with fake_time(datetime(2019, 1, 27, tzinfo=utc)): check_not_accessible(self, url) self.assertTrue(self.client.login(username='******')) with fake_time(datetime(2019, 1, 27, tzinfo=utc)): ranking, _ = Ranking.objects.get_or_create( contest=contest, key='admin#key', needs_recalculation=False) ranking.save() self.assertTrue(ranking.is_up_to_date()) recalc = choose_for_recalculation() self.assertIsNone(recalc) response = self.client.post(url, key='key') ranking.refresh_from_db() self.assertFalse(ranking.is_up_to_date()) recalc = choose_for_recalculation() self.assertIsNotNone(recalc)
def test_ranking_csv_view(self): contest = Contest.objects.get() url = reverse('ranking_csv', kwargs={'contest_id': contest.id, 'key': 'c'}) self.assertTrue(self.client.login(username='******')) with fake_time(datetime(2015, 8, 5, tzinfo=utc)): check_not_accessible(self, url) self.assertTrue(self.client.login(username='******')) with fake_time(datetime(2012, 8, 5, tzinfo=utc)): response = self.client.get(url) self.assertContains(response, 'User,') # Check that Admin is filtered out. self.assertNotContains(response, 'Admin') expected_order = ['Test,User', 'Test,User 2'] prev_pos = 0 content = response.content.decode('utf-8') for user in expected_order: pattern = '%s,' % (user,) self.assertContains(response, user) pos = content.find(pattern) self.assertGreater(pos, prev_pos, msg=('User %s has incorrect ' 'position' % (user,))) prev_pos = pos for task in ['zad1', 'zad2', 'zad3', 'zad3']: self.assertContains(response, task) response = self.client.get(reverse('ranking', kwargs={'contest_id': contest.id, 'key': '1'})) self.assertContains(response, 'zad1') for task in ['zad2', 'zad3', 'zad3']: self.assertNotContains(response, task)
def test_reply_templates(self): contest = Contest.objects.get() self.assertTrue(self.client.login(username='******')) url1 = reverse('get_reply_templates', kwargs={'contest_id': contest.id}) response = self.client.get(url1) templates = response.json() self.assertEqual(templates[0]['name'], "N/A") self.assertEqual(templates[0]['content'], "No answer.") self.assertEqual(templates[3]['name'], "What contest is this?") self.assertEqual(len(templates), 4) url_inc = reverse( 'increment_template_usage', kwargs={ 'contest_id': contest.id, 'template_id': 4 }, ) for _i in range(12): response = self.client.get(url_inc) response = self.client.get(url1) templates = response.json() self.assertEqual(templates[0]['name'], "What contest is this?") self.assertEqual(len(templates), 4) self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url1)
def test_change_denied(self): self.client.login(username='******') self.client.get('/c/c/') # 'c' becomes the current contest msg = Message.objects.filter(author__username='******')[0] url = reverse('oioioiadmin:questions_message_change', args=(msg.id,)) check_not_accessible(self, url)
def test_permissions(self): url = reverse('bulk_add_similarities', kwargs={'contest_id': 'c'}) # test anonymous self.client.logout() check_not_accessible(self, url) # normal user self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url)
def test_attachments(self): contest = Contest.objects.get() problem = Problem.objects.get() ca = ContestAttachment(contest=contest, description='contest-attachment', content=ContentFile('content-of-conatt', name='conatt.txt')) ca.save() pa = ProblemAttachment(problem=problem, description='problem-attachment', content=ContentFile('content-of-probatt', name='probatt.txt')) pa.save() round = Round.objects.get(pk=1) ra = ContestAttachment(contest=contest, description='round-attachment', content=ContentFile('content-of-roundatt', name='roundatt.txt'), round=round) ra.save() self.client.login(username='******') response = self.client.get(reverse('contest_files', kwargs={'contest_id': contest.id})) self.assertEqual(response.status_code, 200) for part in ['contest-attachment', 'conatt.txt', 'problem-attachment', 'probatt.txt', 'round-attachment', 'roundatt.txt']: self.assertIn(part, response.content) response = self.client.get(reverse('contest_attachment', kwargs={'contest_id': contest.id, 'attachment_id': ca.id})) self.assertStreamingEqual(response, 'content-of-conatt') response = self.client.get(reverse('problem_attachment', kwargs={'contest_id': contest.id, 'attachment_id': pa.id})) self.assertStreamingEqual(response, 'content-of-probatt') response = self.client.get(reverse('contest_attachment', kwargs={'contest_id': contest.id, 'attachment_id': ra.id})) self.assertStreamingEqual(response, 'content-of-roundatt') with fake_time(datetime(2011, 7, 10, tzinfo=utc)): response = self.client.get(reverse('contest_files', kwargs={'contest_id': contest.id})) self.assertEqual(response.status_code, 200) for part in ['contest-attachment', 'conatt.txt']: self.assertIn(part, response.content) for part in ['problem-attachment', 'probatt.txt', 'round-attachment', 'roundatt.txt']: self.assertNotIn(part, response.content) response = self.client.get(reverse('contest_attachment', kwargs={'contest_id': contest.id, 'attachment_id': ca.id})) self.assertStreamingEqual(response, 'content-of-conatt') check_not_accessible(self, 'problem_attachment', kwargs={'contest_id': contest.id, 'attachment_id': pa.id}) check_not_accessible(self, 'contest_attachment', kwargs={'contest_id': contest.id, 'attachment_id': ra.id})
def test_submissions_permissions(self): submission = Submission.objects.get(pk=1) test = Test.objects.get(name='0') for view in ['show_submission_source', 'download_submission_source']: check_not_accessible(self, view, kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id }) check_not_accessible(self, 'source_diff', kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission1_id': submission.id, 'submission2_id': submission.id }) for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id}) self.client.login(user='******') for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id})
def test_admin_changelist_view(self): self.client.login(username='******') url = reverse('oioioiadmin:problems_problem_changelist') response = self.client.get(url) self.assertContains(response, 'Sum') self.client.login(username='******') check_not_accessible(self, url) user = User.objects.get(username='******') content_type = ContentType.objects.get_for_model(Problem) permission = Permission.objects.get(content_type=content_type, codename='problems_db_admin') user.user_permissions.add(permission) response = self.client.get(url) self.assertContains(response, 'Sum')
def _test_package_permissions(self, is_admin=False): models = ['problempackage', 'contestproblempackage'] view_prefix = 'oioioiadmin:problems_' package = ProblemPackage.objects.get(pk=2) for m in models: prefix = view_prefix + m + '_' check_not_accessible(self, prefix + 'add') check_not_accessible(self, prefix + 'change', args=(package.id,)) if not is_admin: check_not_accessible(self, prefix + 'delete', args=(package.id,)) if not is_admin: check_not_accessible(self, 'oioioi.problems.views.download_problem_package_view', args=(package.id,)) check_not_accessible(self, 'oioioi.problems.views.download_package_traceback_view', kwargs={'package_id': str(package.id)})
def test_response(self): contest = Contest.objects.get() url = reverse('timeline_view', kwargs={'contest_id': contest.id}) self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url) self.assertTrue(self.client.login(username='******')) response = self.client.get(url, {'contest': contest}) self.assertEqual(response.status_code, 200) for item in date_registry.tolist(contest.id): obj = item['model'].objects.get(pk=item['id']) date = getattr(obj, item['date_field']) if date is not None: self.assertContains(response, date.strftime('%Y-%m-%d %H:%M')) for round in Round.objects.filter(contest=contest.id).values(): self.assertContains(response, round['start_date'].strftime('%Y-%m-%d %H:%M'))
def test_submissions_permissions(self): submission = TestRunProgramSubmission.objects.get(pk=1) kwargs = {'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id} self.assertTrue(self.client.login(username='******')) for view in ['get_testrun_output', 'get_testrun_input', 'download_testrun_output', 'download_testrun_input']: check_not_accessible(self, view, kwargs=kwargs) for view in ['get_testrun_output', 'get_testrun_input']: check_ajax_not_accessible(self, view, kwargs=kwargs) contest = Contest.objects.get(pk='c') contest.controller_name = \ 'oioioi.contests.tests.PrivateContestController' contest.save() self.client.logout() for view in ['get_testrun_output', 'get_testrun_input']: check_ajax_not_accessible(self, view, kwargs=kwargs)
def test_authors_list(self): self.client.login(username='******') contest = Contest.objects.get() url = reverse('get_messages_authors', kwargs={'contest_id': contest.id}) response = self.client.get(url, {'substr': ''}) self.assertEquals(404, response.status_code) response = self.client.get(url) self.assertEquals(404, response.status_code) response = self.client.get(url, {'substr': 't'}) response = json.loads(response.content) self.assertListEqual(['test_admin (Test Admin)', 'test_user (Test User)'], response) response = self.client.get(url, {'substr': 'test admin'}) response = json.loads(response.content) self.assertListEqual(['test_admin (Test Admin)'], response) self.client.login(username='******') check_not_accessible(self, url)
def test_participants_admin_visibility(self): contest = Contest.objects.get() contest.controller_name = \ 'oioioi.participants.tests.ParticipantsContestController' contest.save() user = User.objects.get(username='******') p = Participant(contest=contest, user=user) p.save() url = reverse('oioioiadmin:participants_participant_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') response = self.client.get(url) self.assertContains(response, 'test_user') self.client.login(username='******') response = self.client.get(url) self.assertContains(response, 'test_user')
def test_authors_list(self): self.client.login(username='******') contest = Contest.objects.get() url = reverse('get_messages_authors', kwargs={'contest_id': contest.id}) response = self.client.get(url, {'substr': ''}) self.assertEquals(404, response.status_code) response = self.client.get(url) self.assertEquals(404, response.status_code) response = self.client.get(url, {'substr': 't'}) response = json.loads(response.content) self.assertListEqual( ['test_admin (Test Admin)', 'test_user (Test User)'], response) response = self.client.get(url, {'substr': 'test admin'}) response = json.loads(response.content) self.assertListEqual(['test_admin (Test Admin)'], response) self.client.login(username='******') check_not_accessible(self, url)
def test_reply_templates(self): contest = Contest.objects.get() self.client.login(username='******') url1 = reverse('get_reply_templates', kwargs={'contest_id': contest.id}) response = self.client.get(url1) templates = json.loads(response.content) self.assertEqual(templates[0]['name'], "N/A") self.assertEqual(templates[0]['content'], "No answer.") self.assertEqual(templates[3]['name'], "What contest is this?") self.assertEqual(len(templates), 4) url_inc = reverse('increment_template_usage', kwargs={'contest_id': contest.id, 'template_id': 4}) for _i in xrange(12): response = self.client.get(url_inc) response = self.client.get(url1) templates = json.loads(response.content) self.assertEqual(templates[0]['name'], "What contest is this?") self.assertEqual(len(templates), 4) self.client.login(username='******') check_not_accessible(self, url1)
def test_submissions_permissions(self): contest = Contest.objects.get() submission = Submission.objects.get(pk=1) check_not_accessible(self, 'submission', kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id}) contest.controller_name = \ 'oioioi.contests.tests.PrivateContestController' contest.save() problem_instance = ProblemInstance.objects.get() self.client.login(username='******') check_not_accessible(self, 'problems_list', kwargs={'contest_id': contest.id}) check_not_accessible(self, 'problem_statement', kwargs={'contest_id': contest.id, 'problem_instance': problem_instance.short_name}) check_not_accessible(self, 'my_submissions', kwargs={'contest_id': contest.id}) check_not_accessible(self, 'contest_files', kwargs={'contest_id': contest.id})
def test_submissions_permissions(self): submission = Submission.objects.get(pk=1) test = Test.objects.get(name='0') for view in ['show_submission_source', 'download_submission_source']: check_not_accessible(self, view, kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id}) check_not_accessible(self, 'source_diff', kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission1_id': submission.id, 'submission2_id': submission.id}) for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id}) self.client.login(user='******') for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id})
def test_submissions_permissions(self): submission = Submission.objects.get(pk=1) test = Test.objects.get(name='0') for view in ['show_submission_source', 'download_submission_source']: check_not_accessible(self, view, kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission_id': submission.id}) check_not_accessible(self, 'source_diff', kwargs={ 'contest_id': submission.problem_instance.contest.id, 'submission1_id': submission.id, 'submission2_id': submission.id}) for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id}) self.client.login(username='******') for view in ['download_input_file', 'download_output_file']: check_not_accessible(self, view, kwargs={'test_id': test.id}) self.client.login(username='******') for view in ['download_input_file', 'download_output_file']: url = reverse(view, kwargs={'test_id': test.id}) response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_noadmin_admin_visibility(self): contest = Contest.objects.get() contest.controller_name = ('oioioi.participants.tests.' 'NoAdminParticipantsContestController') contest.save() user = User.objects.get(username='******') p = Participant(contest=contest, user=user) p.save() self.client.get('/c/c/') # 'c' becomes the current contest url = reverse('oioioiadmin:participants_participant_changelist') self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url) self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url) self.assertTrue(self.client.login(username='******')) check_not_accessible(self, url)
def test_admin_permissions(self): url = reverse('oioioiadmin:contests_contest_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 200) # without set request.contest url = reverse('oioioiadmin:contests_probleminstance_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 403) c = Contest.objects.create( id='test_contest', controller_name='oioioi.programs.controllers.' 'ProgrammingContestController', name='Test contest') # with request.contest url = reverse('oioioiadmin:contests_probleminstance_changelist') self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.client.login(username='******') check_not_accessible(self, url) user = User.objects.get(username='******') ContestPermission(user=user, contest=c, permission='contests.contest_admin').save() response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_noadmin_admin_visibility(self): contest = Contest.objects.get() contest.controller_name = \ 'oioioi.participants.tests.' \ 'NoAdminParticipantsContestController' contest.save() user = User.objects.get(username='******') p = Participant(contest=contest, user=user) p.save() self.client.get('/c/c/') # 'c' becomes the current contest url = reverse('oioioiadmin:participants_participant_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') check_not_accessible(self, url)
def test_admin_permissions(self): url = reverse('oioioiadmin:contests_contest_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 200) # without set request.contest url = reverse('oioioiadmin:contests_probleminstance_changelist') self.client.login(username='******') check_not_accessible(self, url) self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 403) c = Contest.objects.create(id='test_contest', controller_name='oioioi.programs.controllers.' 'ProgrammingContestController', name='Test contest') # with request.contest url = reverse('oioioiadmin:contests_probleminstance_changelist') self.client.login(username='******') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.client.login(username='******') check_not_accessible(self, url) user = User.objects.get(username='******') ContestPermission(user=user, contest=c, permission='contests.contest_admin').save() response = self.client.get(url) self.assertEqual(response.status_code, 200)
def _test_problem_permissions(self): problem = Problem.objects.get() contest = Contest.objects.get() statement = ProblemStatement.objects.get() check_not_accessible(self, 'oioioiadmin:problems_problem_add', data={ 'package_file': open(__file__, 'rb'), 'contest_id': contest.id }) check_not_accessible(self, 'add_or_update_problem', kwargs={'contest_id': contest.id}, qs={'problem': problem.id}) check_not_accessible(self, 'oioioiadmin:problems_problem_download', args=(problem.id, )) check_not_accessible(self, 'oioioiadmin:problems_problem_change', args=(problem.id, )) check_not_accessible(self, 'oioioiadmin:problems_problem_delete', args=(problem.id, )) check_not_accessible(self, 'show_statement', kwargs={'statement_id': statement.id})
def _test_problem_permissions(self): problem = Problem.objects.get() contest = Contest.objects.get() statement = ProblemStatement.objects.get() check_not_accessible(self, 'oioioiadmin:problems_problem_add', data={'package_file': open(__file__, 'rb'), 'contest_id': contest.id}) check_not_accessible(self, 'add_or_update_contest_problem', kwargs={'contest_id': contest.id}, qs={'problem': problem.id}) check_not_accessible(self, 'oioioiadmin:problems_problem_download', args=(problem.id,)) check_not_accessible(self, 'oioioiadmin:problems_problem_change', args=(problem.id,)) check_not_accessible(self, 'oioioiadmin:problems_problem_delete', args=(problem.id,)) check_not_accessible(self, 'show_statement', kwargs={'statement_id': statement.id})
def test_ask_and_reply(self): self.client.login(username='******') contest = Contest.objects.get() pi = ProblemInstance.objects.get() url = reverse('add_contest_message', kwargs={'contest_id': contest.id}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertIn('form', response.context) form = response.context['form'] self.assertEqual(len(form.fields['category'].choices) - 1, 2) post_data = { 'category': 'p_%d' % (pi.id, ), 'topic': 'the-new-question', 'content': 'the-new-body', } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 302) new_question = Message.objects.get(topic='the-new-question') self.assertEqual(new_question.content, 'the-new-body') self.assertEqual(new_question.kind, 'QUESTION') self.assertIsNone(new_question.top_reference) self.assertEqual(new_question.contest, contest) self.assertEqual(new_question.problem_instance, pi) self.assertEqual(new_question.author.username, 'test_user2') self.client.login(username='******') list_url = reverse('contest_messages', kwargs={'contest_id': contest.id}) response = self.client.get(list_url) self.assertIn('the-new-question', response.content) url = reverse('message', kwargs={ 'contest_id': contest.id, 'message_id': new_question.id }) response = self.client.get(url) self.assertIn('form', response.context) post_data = { 'kind': 'PUBLIC', 'topic': 're-new-question', 'content': 're-new-body', 'save_template': True, } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 302) post_data = { 'kind': 'PUBLIC', 'topic': 'another-re-new-question', 'content': 'another-re-new-body', 'save_template': False, } response = self.client.post(url, post_data) self.assertRaises( ReplyTemplate.DoesNotExist, lambda: ReplyTemplate.objects.get(content='another-re-new-body')) self.assertEqual(response.status_code, 302) new_reply = Message.objects.get(topic='re-new-question') self.assertEqual(new_reply.content, 're-new-body') self.assertEqual(new_reply.kind, 'PUBLIC') self.assertEqual(new_reply.top_reference, new_question) self.assertEqual(new_reply.contest, contest) self.assertEqual(new_reply.problem_instance, pi) self.assertEqual(new_reply.author.username, 'test_admin') self.client.login(username='******') q_url = reverse('message', kwargs={ 'contest_id': contest.id, 'message_id': new_question.id }) check_not_accessible(self, q_url) repl_url = reverse('message', kwargs={ 'contest_id': contest.id, 'message_id': new_reply.id }) response = self.client.get(repl_url) self.assertEqual(response.status_code, 200) self.assertIn('re-new-question', response.content) self.assertIn('re-new-body', response.content) self.assertNotIn('the-new-question', response.content) self.assertNotIn('the-new-body', response.content) response = self.client.get(list_url) self.assertIn(repl_url, response.content) self.assertIn('re-new-question', response.content) self.assertNotIn('the-new-question', response.content) self.client.login(username='******') response = self.client.get(q_url) self.assertEqual(response.status_code, 200) self.assertIn('the-new-question', response.content) self.assertIn('the-new-body', response.content) self.assertIn('re-new-body', response.content) response = self.client.get(list_url) self.assertIn(q_url, response.content) self.assertIn('re-new-question', response.content) self.assertNotIn('the-new-question', response.content)
def test_change_denied(self): self.client.login(username='******') msg = Message.objects.filter(author__username='******')[0] url = reverse('oioioiadmin:questions_message_change', args=(msg.id, )) check_not_accessible(self, url)
def test_ask_and_reply(self): self.client.login(username='******') contest = Contest.objects.get() pi = ProblemInstance.objects.get() url = reverse('add_contest_message', kwargs={'contest_id': contest.id}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertIn('form', response.context) form = response.context['form'] self.assertEqual(len(form.fields['category'].choices) - 1, 2) post_data = { 'category': 'p_%d' % (pi.id,), 'topic': 'the-new-question', 'content': 'the-new-body', } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 302) new_question = Message.objects.get(topic='the-new-question') self.assertEqual(new_question.content, 'the-new-body') self.assertEqual(new_question.kind, 'QUESTION') self.assertIsNone(new_question.top_reference) self.assertEqual(new_question.contest, contest) self.assertEqual(new_question.problem_instance, pi) self.assertEqual(new_question.author.username, 'test_user2') self.client.login(username='******') list_url = reverse('contest_messages', kwargs={'contest_id': contest.id}) response = self.client.get(list_url) self.assertIn('the-new-question', response.content) url = reverse('message', kwargs={'contest_id': contest.id, 'message_id': new_question.id}) response = self.client.get(url) self.assertIn('form', response.context) post_data = { 'kind': 'PUBLIC', 'topic': 're-new-question', 'content': 're-new-body', 'save_template': True, } response = self.client.post(url, post_data) self.assertEqual(response.status_code, 302) post_data = { 'kind': 'PUBLIC', 'topic': 'another-re-new-question', 'content': 'another-re-new-body', 'save_template': False, } response = self.client.post(url, post_data) self.assertRaises(ReplyTemplate.DoesNotExist, lambda: ReplyTemplate.objects .get(content='another-re-new-body')) self.assertEqual(response.status_code, 302) new_reply = Message.objects.get(topic='re-new-question') self.assertEqual(new_reply.content, 're-new-body') self.assertEqual(new_reply.kind, 'PUBLIC') self.assertEqual(new_reply.top_reference, new_question) self.assertEqual(new_reply.contest, contest) self.assertEqual(new_reply.problem_instance, pi) self.assertEqual(new_reply.author.username, 'test_admin') self.client.login(username='******') q_url = reverse('message', kwargs={'contest_id': contest.id, 'message_id': new_question.id}) check_not_accessible(self, q_url) repl_url = reverse('message', kwargs={'contest_id': contest.id, 'message_id': new_reply.id}) response = self.client.get(repl_url) self.assertEqual(response.status_code, 200) self.assertIn('re-new-question', response.content) self.assertIn('re-new-body', response.content) self.assertNotIn('the-new-question', response.content) self.assertNotIn('the-new-body', response.content) response = self.client.get(list_url) self.assertIn(repl_url, response.content) self.assertIn('re-new-question', response.content) self.assertNotIn('the-new-question', response.content) self.client.login(username='******') response = self.client.get(q_url) self.assertEqual(response.status_code, 200) self.assertIn('the-new-question', response.content) self.assertIn('the-new-body', response.content) self.assertIn('re-new-body', response.content) response = self.client.get(list_url) self.assertIn(q_url, response.content) self.assertIn('re-new-question', response.content) self.assertNotIn('the-new-question', response.content)
def test_attachments(self): contest = Contest.objects.get() problem = Problem.objects.get() ca = ContestAttachment(contest=contest, description='contest-attachment', content=ContentFile('content-of-conatt', name='conatt.txt')) ca.save() pa = ProblemAttachment(problem=problem, description='problem-attachment', content=ContentFile('content-of-probatt', name='probatt.txt')) pa.save() round = Round.objects.get(pk=1) ra = ContestAttachment(contest=contest, description='round-attachment', content=ContentFile('content-of-roundatt', name='roundatt.txt'), round=round) ra.save() self.client.login(username='******') response = self.client.get( reverse('contest_files', kwargs={'contest_id': contest.id})) self.assertEqual(response.status_code, 200) for part in [ 'contest-attachment', 'conatt.txt', 'problem-attachment', 'probatt.txt', 'round-attachment', 'roundatt.txt' ]: self.assertIn(part, response.content) response = self.client.get( reverse('contest_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': ca.id })) self.assertStreamingEqual(response, 'content-of-conatt') response = self.client.get( reverse('problem_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': pa.id })) self.assertStreamingEqual(response, 'content-of-probatt') response = self.client.get( reverse('contest_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': ra.id })) self.assertStreamingEqual(response, 'content-of-roundatt') with fake_time(datetime(2011, 7, 10, tzinfo=utc)): response = self.client.get( reverse('contest_files', kwargs={'contest_id': contest.id})) self.assertEqual(response.status_code, 200) for part in ['contest-attachment', 'conatt.txt']: self.assertIn(part, response.content) for part in [ 'problem-attachment', 'probatt.txt', 'round-attachment', 'roundatt.txt' ]: self.assertNotIn(part, response.content) response = self.client.get( reverse('contest_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': ca.id })) self.assertStreamingEqual(response, 'content-of-conatt') check_not_accessible(self, 'problem_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': pa.id }) check_not_accessible(self, 'contest_attachment', kwargs={ 'contest_id': contest.id, 'attachment_id': ra.id })