def test_manual_migration_makes_submissions_out_of_only_parents_not_children( self): self.client.login(username='******', password='******') # make 1 submission with 4 children parent = SubmissionFactory(owner=self.creator, phase=self.phase_1, has_children=True, status=Submission.FINISHED) for _ in range(4): # Make a submission _and_ new Task for phase 2 self.phase_2.tasks.add(TaskFactory()) SubmissionFactory(owner=self.creator, phase=self.phase_1, parent=parent, status=Submission.FINISHED) assert self.phase_1.submissions.count() == 5 assert self.phase_2.submissions.count() == 0 # call "migrate" from phase 1 -> 2 with mock.patch( "competitions.tasks.run_submission") as run_submission_mock: url = reverse('phases-manually_migrate', kwargs={"pk": self.phase_1.pk}) resp = self.client.post(url) assert resp.status_code == 200 # Only 1 run here because parent has to create children assert run_submission_mock.call_count == 1 # check phase 2 has the 1 parent submission assert self.phase_1.submissions.count() == 5 assert self.phase_2.submissions.count() == 1
def make_submission(self, **kwargs): kwargs.setdefault('owner', self.owner) kwargs.setdefault('participant', self.competition_participant) kwargs.setdefault('phase', self.phase1) kwargs.setdefault('status', Submission.FINISHED) sub = SubmissionFactory(**kwargs) return sub
def make_submission(self, create_score=True, **kwargs): kwargs.setdefault('owner', self.user) kwargs.setdefault('phase', self.phase) parent_sub = kwargs.pop('parent_submission', None) sub = SubmissionFactory(**kwargs) subs = [sub] if parent_sub: subs.append(parent_sub) if create_score: SubmissionScoreFactory(submissions=subs, column=self.column) return sub
def setUp(self): self.user = UserFactory() self.comp = CompetitionFactory(published=True) self.participant = CompetitionParticipantFactory(user=self.user, competition=self.comp) self.phase = PhaseFactory(competition=self.comp) self.data = DataFactory() # Calling this after initial setup so we don't turn on FORCE_CHAHUB and try and send all our setup objects super().setUp() self.submission = SubmissionFactory.build( owner=self.user, phase=self.phase, data=self.data, participant=self.participant, status='Finished', is_public=True, )
def setUp(self): for _ in range(5): user = UserFactory(chahub_needs_retry=True) comp = CompetitionFactory(chahub_needs_retry=True, published=True) participant = CompetitionParticipantFactory(competition=comp, user=user, status='approved') phase = PhaseFactory(competition=comp) DataFactory(chahub_needs_retry=True, is_public=True, upload_completed_successfully=True) SubmissionFactory(chahub_needs_retry=True, status="Finished", phase=phase, is_public=True, participant=participant) super().setUp()
def test_bots_can_exceed_max_submissions_per_person(self): self.client.login(username='******', password='******') resp = self.client.get( reverse("can_make_submission", args=(self.bot_phase_person_limited.pk, ))) assert resp.status_code == 200 assert resp.data['can'] for _ in range(2): SubmissionFactory(phase=self.bot_phase_person_limited, owner=self.bot_user, status=Submission.SUBMITTED, secret='7df3600c-1234-5678-bbc8-bbe91f42d875') assert Submission.objects.filter( owner=self.bot_user, phase=self.bot_phase_person_limited).count( ) > self.bot_phase_person_limited.max_submissions_per_person
def test_manual_migration_makes_submissions_from_one_phase_in_another(self): self.client.login(username='******', password='******') # make 5 submissions in phase 1 for _ in range(5): SubmissionFactory(owner=self.creator, phase=self.phase_1, status=Submission.FINISHED) assert self.phase_1.submissions.count() == 5 assert self.phase_2.submissions.count() == 0 # call "migrate" from phase 1 -> 2 with mock.patch("competitions.tasks.run_submission") as run_submission_mock: url = reverse('phases-manually_migrate', kwargs={"pk": self.phase_1.pk}) resp = self.client.post(url) assert resp.status_code == 200 assert run_submission_mock.call_count == 5 # check phase 2 has the 5 submissions assert self.phase_1.submissions.count() == 5 assert self.phase_2.submissions.count() == 5
def test_non_bot_users_cannot_exceed_max_submissions_per_person(self): self.client.login(username='******', password='******') resp = self.client.get( reverse("can_make_submission", args=(self.bot_phase_person_limited.pk, ))) assert resp.status_code == 200 assert resp.data['can'] SubmissionFactory(phase=self.bot_phase_person_limited, owner=self.non_bot_user, status=Submission.SUBMITTED, secret='7df3600c-1234-5678-bbc8-bbe91f42d875') resp = self.client.get( reverse("can_make_submission", args=(self.bot_phase_person_limited.pk, ))) assert resp.status_code == 200 assert not resp.data['can']
def handle(self, *args, **kwargs): size = kwargs.get('size') or 3 no_admin = kwargs.get('no_admin') print( f'Creating data of size {size} {"without an admin account." if no_admin else "with an admin account." }' ) users = [] for i in range(size): if i == 0 and not no_admin: try: user = UserFactory(username='******', password='******', super_user=True) except IntegrityError: # admin user already exists user = User.objects.get(username='******') else: user = UserFactory() users.append(user) for user in users: for _ in range(size): comp = CompetitionFactory(created_by=user) for u in users: try: CompetitionParticipantFactory(competition=comp, user=u, status='approved') except IntegrityError: # User already a participant in the competition pass for i in range(size): phase = PhaseFactory(competition=comp, index=i, tasks=[TaskFactory(created_by=user)]) for _ in range(size): SubmissionFactory(phase=phase, owner=random.choice(users))
def test_cannot_delete_submission_you_didnt_create(self): url = reverse('submission-detail', args=(self.existing_submission.pk, )) # As anonymous user resp = self.client.delete(url) assert resp.status_code == 403 assert resp.data[ "detail"] == "Cannot interact with submission you did not make" # As regular user self.client.force_login(self.other_user) resp = self.client.delete(url) assert resp.status_code == 403 assert resp.data[ "detail"] == "Cannot interact with submission you did not make" # As user who made submission self.client.force_login(self.participant) resp = self.client.delete(url) assert resp.status_code == 204 assert not Submission.objects.filter( pk=self.existing_submission.pk).exists() # As superuser (re-making submission since it has been destroyed) self.existing_submission = SubmissionFactory( phase=self.phase, owner=self.participant, status=Submission.SUBMITTED, secret='7df3600c-1234-5678-90c8-bbe91f42d875') url = reverse('submission-detail', args=(self.existing_submission.pk, )) self.client.force_login(self.superuser) resp = self.client.delete(url) assert resp.status_code == 204 assert not Submission.objects.filter( pk=self.existing_submission.pk).exists()
def setUp(self): self.superuser = UserFactory(is_superuser=True, is_staff=True) # Competition and creator self.creator = UserFactory(username='******', password='******') self.collaborator = UserFactory(username='******', password='******') self.comp = CompetitionFactory(created_by=self.creator, collaborators=[self.collaborator]) self.phase = PhaseFactory(competition=self.comp) # Extra dummy user to test permissions, they shouldn't have access to many things self.other_user = UserFactory(username='******', password='******') # Make a participant and submission into competition self.participant = UserFactory(username='******', password='******') CompetitionParticipantFactory(user=self.participant, competition=self.comp) self.existing_submission = SubmissionFactory( phase=self.phase, owner=self.participant, status=Submission.SUBMITTED, secret='7df3600c-1234-5678-bbc8-bbe91f42d875')
def make_submission(self, **kwargs): kwargs.setdefault('owner', self.user) kwargs.setdefault('phase', self.phase) SubmissionFactory(**kwargs)
def make_submission(self, **kwargs): kwargs.setdefault('owner', self.user) kwargs.setdefault('phase', self.phase) kwargs.setdefault('created_when', timezone.now()) return SubmissionFactory(**kwargs)
def test_task_shown_as_validated_properly(self): user = UserFactory(username='******') solution = SolutionFactory(md5="12345") task = TaskFactory(created_by=user, solutions=[solution]) competition = CompetitionFactory(created_by=user) phase = PhaseFactory(competition=competition, tasks=[task]) submission = SubmissionFactory(md5="12345", phase=phase, status=Submission.FINISHED) url = reverse('task-detail', kwargs={'pk': task.id}) self.client.login(username=user.username, password='******') # task should be validated because we have a successful submission matching # our solution resp = self.client.get(url) assert resp.status_code == 200 assert resp.data["validated"] # make submission anything but Submission.FINISHED, task -> invalidated submission.status = Submission.FAILED submission.save() resp = self.client.get(url) assert resp.status_code == 200 assert not resp.data["validated"] # make submission Submission.Finished, task -> re-validated submission.status = Submission.FINISHED submission.save() resp = self.client.get(url) assert resp.status_code == 200 assert resp.data["validated"] # delete submission, task -> re-invalidated submission.delete() resp = self.client.get(url) assert resp.status_code == 200 assert not resp.data["validated"] # make submission with different Sha -> still invalid SubmissionFactory(md5="different", phase=phase, status=Submission.FINISHED) resp = self.client.get(url) assert resp.status_code == 200 assert not resp.data["validated"]