def test_list_by_coordinator(self): study = StudyFactory.create(author=self.coordinator) StudyFactory.create() self.authenticate_as_doctor() resp = self.client.get('/api/v1/study/', format='json') self.assertSuccessResponse(resp) self.assertSetEqual(set(item['pk'] for item in resp.data), {study.pk})
def test_list_by_participant_empty(self): StudyFactory.create() StudyFactory.create() ParticipantFactory.create(doctor_ptr=self.other_doctor) self.authenticate_as_doctor(self.other_doctor) resp = self.client.get('/api/v1/study/', format='json') self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 0)
def test_list_by_participant(self): study = StudyFactory.create() study2 = StudyFactory.create() ParticipantFactory.create(doctor_ptr=self.other_doctor) DoctorToPatient.objects.create(patient=self.patient, doctor=self.other_doctor, encrypted_key='123') StudyToPatient.objects.create(study=study, patient=self.patient) self.authenticate_as_doctor(self.other_doctor) resp = self.client.get('/api/v1/study/', format='json') self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 1) self.assertEqual(resp.data[0]['pk'], study.pk)
def test_add_doctor_bad_request(self): study = StudyFactory.create(author=self.coordinator) self.authenticate_as_doctor() resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), {}, format='json') self.assertBadRequest(resp)
def test_list_as_doctor_with_consent(self): doctor = DoctorFactory.create() study = StudyFactory.create() study.doctors.add(doctor) study.save() DoctorToPatient.objects.create(doctor=doctor, patient=self.patient) now = timezone.now() consent = PatientConsentFactory.create(patient=self.patient, date_expired=now) StudyToPatient.objects.create(study=study, patient=self.patient, patient_consent=consent) self.authenticate_as_doctor(doctor) resp = self.client.get('/api/v1/study/', format='json') self.assertEqual(len(resp.data), 1) self.assertListEqual(list(resp.data[0]['patients_consents'].keys()), [self.patient.pk]) self.assertEqual( resp.data[0]['patients_consents'][self.patient.pk]['pk'], consent.pk)
def test_create_success_with_study(self, mock_requests): study = StudyFactory.create() self.authenticate_as_doctor() position_info = {'x': 10, 'y': 10} mole_data = { 'anatomical_site': self.anatomical_site.pk, 'position_info': json.dumps(position_info), 'photo': self.get_sample_image_file(), 'study': study.pk } with self.fake_media(): resp = self.client.post(self.get_url(self.first_patient.pk), mole_data) self.assertSuccessResponse(resp) data = resp.data self.assertIsNotNone(data['pk']) mole = Mole.objects.get(pk=data['pk']) mole_image = mole.images.first() self.assertIsNotNone(mole_image.study) self.assertEqual(mole_image.study.pk, study.pk)
def test_add_consent(self): doctor = DoctorFactory.create() study = StudyFactory.create() study.doctors.add(doctor) study.save() DoctorToPatient.objects.create(doctor=doctor, patient=self.patient) expired = timezone.now() - timedelta(days=1) consent = PatientConsentFactory.create(patient=self.patient, date_expired=expired) study_to_patient = StudyToPatient.objects.create( study=study, patient=self.patient, patient_consent=consent) self.authenticate_as_doctor(doctor) count_before_post = PatientConsent.objects.all().count() resp = self.client.post('/api/v1/study/{0}/add_consent/'.format( study.pk), { 'patient_pk': self.patient.pk, 'signature': 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD' '0lEQVQIHQEEAPv/AP///wX+Av4DfRnGAAAAAElFTkSuQmCC', }, format='json') self.assertNotEqual( study_to_patient.pk, resp.data['patients_consents'][self.patient.pk]['pk']) self.assertEqual(count_before_post + 1, PatientConsent.objects.all().count())
def test_update_and_check_changes(self): study = StudyFactory.create(author=self.coordinator) initial_title = study.title self.authenticate_as_doctor() self.client.put(self.target_path(study.pk), {'title': 'test'}, format='json') study.refresh_from_db() self.assertNotEqual(initial_title, study.title)
def test_invitations(self): self.authenticate_as_doctor() study = StudyFactory.create(author=self.coordinator) invitation = StudyInvitationFactory.create(study=study) StudyInvitationFactory.create() resp = self.client.get('/api/v1/study/{0}/invites/'.format(study.pk), format='json') self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 1) self.assertEqual(resp.data[0]['pk'], invitation.pk)
def test_list_as_doctor(self): doctor = DoctorFactory.create() self.authenticate_as_doctor(doctor) study = StudyFactory.create() resp = self.client.get('/api/v1/study/', format='json') self.assertEqual(len(resp.data), 0) study.doctors.add(doctor) study.save() self.authenticate_as_doctor(doctor) resp = self.client.get('/api/v1/study/', format='json') self.assertEqual(len(resp.data), 1)
def test_add_doctor_with_myself(self): study = StudyFactory.create(author=self.coordinator) self.authenticate_as_doctor() resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), { 'doctor_pk': self.doctor.pk, 'emails': ['*****@*****.**'] }, format='json') self.assertSuccessResponse(resp) self.assertTrue(resp.data['all_success'])
def test_get_by_email_for_exists_patient(self): self.authenticate_as_doctor() study = StudyFactory.create() patient = PatientFactory.create(doctor=self.doctor) StudyInvitationFactory.create(email='*****@*****.**', doctor=self.doctor, study=study, patient=patient) resp = self.client.get( '/api/v1/doctor/get_by_email/[email protected]') self.assertBadRequest(resp)
def test_add_doctor_not_from_my_site_study(self): study = StudyFactory.create() doctor = DoctorFactory.create(my_coordinator=self.coordinator) self.authenticate_as_doctor() resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), { 'doctor_pk': doctor.pk, 'emails': ['*****@*****.**'] }, format='json') self.assertNotFound(resp)
def test_update_consent_docs(self, mock_invalidate_consents): study = StudyFactory.create(author=self.coordinator) new_consent_docs = ConsentDocFactory.create() self.authenticate_as_doctor() self.client.put(self.target_path(study.pk), { 'title': study.title, 'consent_docs': [new_consent_docs.pk] }, format='json') mock_invalidate_consents.assert_called_once()
def test_validate_study_consent(self): study = StudyFactory.create() patient = PatientFactory.create() expired = timezone.now() - timedelta(days=1) consent = PatientConsentFactory.create(patient=patient, date_expired=expired) StudyToPatient.objects.create(study=study, patient=patient, patient_consent=consent) validate_study_consent_for_patient(study, patient)
def test_list_with_study(self): self.authenticate_as_doctor() study = StudyFactory.create() StudyToPatient.objects.create( study=study, patient=self.first_patient ) resp = self.client.get('/api/v1/patient/') self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 1) self.assertListEqual(resp.data[0]['studies'], [])
def test_mole_studies_detail(self): study = StudyFactory.create() MoleImageFactory.create(study=study, mole=self.first_patient_mole) MoleImageFactory.create(study=study, mole=self.first_patient_mole) self.authenticate_as_doctor() resp = self.client.get( self.get_url(self.first_patient.pk, self.first_patient_mole.pk), {'study': study.pk}) self.assertSuccessResponse(resp) self.assertListEqual(resp.data['studies'], [study.pk])
def test_get_patient_moles_with_study(self): self.authenticate_as_doctor() study = StudyFactory.create() MoleImageFactory.create(study=study, mole=self.first_patient_mole) MoleImageFactory.create(study=study, mole=self.first_patient_mole) resp = self.client.get(self.get_url(self.first_patient.pk), {'study': study.pk}) self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 1) data = resp.data[0] self.assertListEqual(data['studies'], [study.pk]) self.assertEqual(data['images_count'], 2)
def test_add_doctor_forbidden(self): study = StudyFactory.create(author=self.coordinator) doctor = DoctorFactory.create(my_coordinator=self.coordinator) patient = DoctorFactory.create() ParticipantFactory.create(doctor_ptr=patient) emails = [ doctor.email, self.doctor.email, patient.email, '*****@*****.**' ] resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), { 'doctor_pk': doctor.pk, 'emails': emails }, format='json') self.assertUnauthorized(resp)
def test_validate_study_consent_with_error(self): study = StudyFactory.create() patient = PatientFactory.create() expired = timezone.now() - timedelta(days=1) consent = PatientConsentFactory.create(patient=patient, date_expired=expired) consent.date_expired = expired consent.save() StudyToPatient.objects.create(study=study, patient=patient, patient_consent=consent) with self.assertRaises(serializers.ValidationError): validate_study_consent_for_patient(study, patient)
def test_register_as_doctor_with_patient_email(self): study = StudyFactory.create() patient = PatientFactory.create(doctor=self.doctor) StudyInvitationFactory.create(email='*****@*****.**', doctor=self.doctor, study=study, patient=patient) data = { 'first_name': Faker('first_name').generate({}), 'last_name': Faker('last_name').generate({}), 'email': '*****@*****.**', 'password': Faker('password').generate({}), 'site': self.site.id, } resp = self.client.post('/api/v1/auth/register/', data) self.assertBadRequest(resp)
def test_approve_invalid_consent(self): doctor = DoctorFactory.create() participant = DoctorFactory.create() ParticipantFactory.create(doctor_ptr=participant) PatientFactory.create(doctor=participant) study = StudyFactory.create() study_invitation = StudyInvitationFactory.create( study=study, email=participant.email, doctor=doctor) self.authenticate_as_doctor(doctor=participant) consent = PatientConsentFactory.create() resp = self.client.post('/api/v1/study/invites/{0}/approve/'.format( study_invitation.pk), { 'doctor_encryption_key': 'qwertyuiop', 'consent_pk': consent.pk }, format='json') self.assertBadRequest(resp)
def test_add_doctor_invited_email(self): study = StudyFactory.create(author=self.coordinator) doctor = DoctorFactory.create(my_coordinator=self.coordinator) patient = DoctorFactory.create() ParticipantFactory.create(doctor_ptr=patient) self.authenticate_as_doctor() StudyInvitationFactory.create(email='*****@*****.**', study=study) emails = ['*****@*****.**'] resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), { 'doctor_pk': doctor.pk, 'emails': emails }, format='json') self.assertSuccessResponse(resp) self.assertSetEqual(set(resp.data['fail_emails']), {'*****@*****.**'})
def test_create_patient_with_study_and_email(self): self.authenticate_as_doctor() study = StudyFactory.create() patient_data = { 'first_name': 'first name', 'last_name': 'first name', 'sex': SexEnum.MALE, 'race': RaceEnum.ASIAN, 'date_of_birth': '1990-01-01', 'photo': self.get_sample_image_file(), 'signature': 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0l' 'EQVQIHQEEAPv/AP///wX+Av4DfRnGAAAAAElFTkSuQmCC', 'encryption_keys': json.dumps({self.doctor.pk: 'qwertyuiop'}), 'email': '*****@*****.**', 'study': study.pk, } with self.fake_media(): resp = self.client.post('/api/v1/patient/', patient_data) self.assertSuccessResponse(resp) data = resp.data self.assertIsNotNone(data['pk']) patient = Patient.objects.get(pk=data['pk']) self.assertEqual(patient.consents.count(), 1) self.assertTrue(DoctorToPatient.objects.filter( doctor=self.doctor, patient=patient).exists()) self.assertEqual(patient.first_name, patient_data['first_name']) self.assertEqual(patient.last_name, patient_data['last_name']) self.assertTrue(StudyInvitation.objects.filter( email='*****@*****.**', study=study, doctor=self.doctor, patient=patient ).exists()) study_to_patient = StudyToPatient.objects.get( study=study, patient=patient) self.assertEqual(study_to_patient.patient_consent, patient.consents.first())
def setUp(self): self.doctor = DoctorFactory.create() self.patient = PatientFactory.create() self.study = StudyFactory.create( author=CoordinatorFactory.create() ) self.study.doctors.add(self.doctor) self.study.save() DoctorToPatient.objects.create( doctor=self.doctor, patient=self.patient) consent = PatientConsentFactory.create( patient=self.patient) self.study_to_patient = StudyToPatient.objects.create( study=self.study, patient=self.patient, patient_consent=consent)
def test_create_with_study(self, mock_requests): self.authenticate_as_doctor() study = StudyFactory.create() mole_image_data = { 'photo': self.get_sample_image_file(), 'study': study.pk, } with self.fake_media(): resp = self.client.post( self.get_url(self.first_patient.pk, self.first_patient_mole.pk), mole_image_data) self.assertSuccessResponse(resp) data = resp.data mole_image = MoleImage.objects.get(pk=data['pk']) self.assertEqual(mole_image.study.pk, study.pk)
def test_create_with_study_outdated_consent(self, mock_requests): self.authenticate_as_doctor() study = StudyFactory.create() mole_image_data = { 'photo': self.get_sample_image_file(), 'study': study.pk, } self.first_patient_consent.date_expired = \ timezone.now() - timedelta(days=1) self.first_patient_consent.save() with self.fake_media(): resp = self.client.post( self.get_url(self.first_patient.pk, self.first_patient_mole.pk), mole_image_data) self.assertForbidden(resp)
def setUp(self): super(StudyInvitationForDoctorViewSetTest, self).setUp() doctor = DoctorFactory.create() self.study = StudyFactory.create() self.patient = PatientFactory.create(doctor=self.doctor) self.participant = DoctorFactory.create(email='*****@*****.**', public_key='public_key_123') ParticipantFactory.create(doctor_ptr=self.participant) self.invitation = StudyInvitationFactory.create(email='*****@*****.**', doctor=self.doctor, study=self.study, patient=self.patient) StudyInvitationFactory.create(email='*****@*****.**', doctor=self.doctor, study=self.study) StudyInvitationFactory.create(email='*****@*****.**', doctor=doctor, study=self.study)
def test_add_doctor_bad_doctor_pk(self): study = StudyFactory.create(author=self.coordinator) doctor = DoctorFactory.create(my_coordinator=self.coordinator) patient = DoctorFactory.create() ParticipantFactory.create(doctor_ptr=patient) old_doc_count = study.doctors.count() self.authenticate_as_doctor() emails = [ doctor.email, self.doctor.email, patient.email, '*****@*****.**' ] resp = self.client.post('/api/v1/study/{0}/add_doctor/'.format( study.pk), { 'doctor_pk': 999999, 'emails': emails }, format='json') self.assertNotFound(resp) study.refresh_from_db() self.assertEqual(old_doc_count, study.doctors.count())
def test_list_with_study_in_request(self): from apps.moles.factories import MoleFactory, MoleImageFactory self.authenticate_as_doctor() study = StudyFactory.create() StudyToPatient.objects.create( study=study, patient=self.first_patient ) mole = MoleFactory.create(patient=self.first_patient) MoleImageFactory(mole=mole, study=study) resp = self.client.get('/api/v1/patient/', { 'study': study.pk }) self.assertSuccessResponse(resp) self.assertEqual(len(resp.data), 1) data = resp.data[0] self.assertEqual(data['moles_count'], 1) self.assertEqual(data['moles_images_count'], 1)