def test_serializer_cant_apply_for_same_cohort_twice(self): data = {'isMentor': True} serializer = ParticipantSerializer(data=data) serializer.is_valid() participant = serializer.save(user=self.user, cohort=self.cohort) data = {'isMentor': False} serializer = ParticipantSerializer(data=data) serializer.is_valid() with self.assertRaises(IntegrityError): participant = serializer.save(user=self.user, cohort=self.cohort)
def test_serializer_valid_participant(self): data = {'isMentor': False} serializer = ParticipantSerializer(data=data) self.assertTrue(serializer.is_valid()) participant = serializer.save(user=self.user, cohort=self.cohort) self.assertFalse(participant.isMatched) self.assertTrue(participant.signUpDate <= timezone.now())
def test_serializer_ignore_duplicate_tags(self): data = {'isMentor': True, 'tags': ['node.js', 'sports', 'sports']} Tag.objects.create(name="node.js") Tag.objects.create(name="sports") serializer = ParticipantSerializer(data=data) if not serializer.is_valid(): self.fail(serializer.errors) participant = serializer.save(user=self.user, cohort=self.cohort) self.assertEqual(len(participant.tags.all()), 2)
def test_serializer_cant_apply_for_full_cohort(self): smol_programme = Programme.objects.create( name='Smaller Programme', description='This is a test programme.', defaultCohortSize=1, createdBy=self.user) smol_cohort = Cohort.objects.create( programme=smol_programme, createdBy=self.user, cohortSize=smol_programme.defaultCohortSize) data = {'isMentor': True} serializer = ParticipantSerializer(data=data) serializer.is_valid() participant = serializer.save(user=self.user, cohort=smol_cohort) data = {'isMentor': False} serializer = ParticipantSerializer(data=data) serializer.is_valid() with self.assertRaises(ValidationError): participant = serializer.save(user=self.other_user, cohort=smol_cohort)
def register(self, request, **kwargs): c = Cohort.objects.get(cohortId=self.kwargs['cohortId']) s = ParticipantSerializer(data=request.data) if not s.is_valid(): return JSONResponse({'detail': s.errors}, status=status.HTTP_400_BAD_REQUEST) try: s.save(user=request.user, cohort=c) except IntegrityError: return JSONResponse( {'detail': 'You have already applied for this cohort.'}, status=status.HTTP_403_FORBIDDEN) except ValidationError as e: return JSONResponse({'detail': e.args[0]}, status=status.HTTP_403_FORBIDDEN) return JSONResponse(s.data, status=status.HTTP_200_OK)
def test_serializer_is_mentor_not_set(self): data = {} serializer = ParticipantSerializer(data=data) self.assertFalse(serializer.is_valid())