def test_user_email(self, email): """ test invalid email """ self.profile.user.email = email self.profile.user.save() assert validate_profile(self.profile) is False
def test_when_field_is_blank(self, field): """ test validate_profile when a field is empty """ setattr(self.profile, field, '') self.profile.save() assert validate_profile(self.profile) is False
def test_when_field_is_invalid(self, field): """ test validate_profile when a field is invalid """ setattr(self.profile, field, '汉字') self.profile.save() assert validate_profile(self.profile) is False
def test_romanized_name(self, name, romanized_name, result): """ test romanized name optional/required """ self.profile.first_name = name self.profile.romanized_first_name = romanized_name self.profile.save() assert validate_profile(self.profile) is result
def test_postal_code(self, country, postal_code, result): """ when postal_code is (not) required and valid/invalid """ self.profile.country = country self.profile.postal_code = postal_code self.profile.save() assert validate_profile(self.profile) is result
def export_exam_profiles(self): """ Sync any outstanding profiles """ if not settings.FEATURES.get("PEARSON_EXAMS_SYNC", False): return exam_profiles = ExamProfile.objects.filter( status=ExamProfile.PROFILE_PENDING).select_related('profile') file_prefix = now_in_utc().strftime(PEARSON_CDD_FILE_PREFIX) valid_exam_profiles = [] for exam_profile in exam_profiles: validated = validate_profile(exam_profile.profile) if validated: valid_exam_profiles.append(exam_profile) else: exam_profile.status = ExamProfile.PROFILE_INVALID exam_profile.save() # write the file out locally # this will be written out to a file like: /tmp/cdd-20160405_kjfiamdf.dat with tempfile.NamedTemporaryFile( prefix=file_prefix, encoding=PEARSON_FILE_ENCODING, suffix=PEARSON_FILE_EXTENSION, mode='w', ) as tsv: cdd_writer = writers.CDDWriter() valid_profiles, invalid_profiles = cdd_writer.write( tsv, valid_exam_profiles) # flush data to disk before upload tsv.flush() try: audit.ExamDataAuditor().audit_request_file(tsv.name) except ImproperlyConfigured: log.exception('Exam auditing improperly configured') return except: # pylint: disable=bare-except log.exception('Unexpected error auditing CDD file') return try: # upload to SFTP server upload.upload_tsv(tsv.name) except ImproperlyConfigured: log.exception( 'export_exam_profiles is improperly configured, please review require settings.' ) except RetryableSFTPException as exc: log.exception( 'Retryable error during upload of CDD file to Pearson SFTP') # retry up to 3 times w/ exponential backoff if this was a connection error self.retry(exc=exc, countdown=exponential_backoff(self.request.retries)) except: # pylint: disable=bare-except log.exception('Unexpected exception uploading CDD file') return valid_profile_ids = [exam_profile.id for exam_profile in valid_profiles] invalid_profile_ids = [ exam_profile.id for exam_profile in invalid_profiles ] # if this transaction fails, we log it but allow the task to complete # since the records never got updated, the next run of this task will attempt to reconile those again # worst-case this means we send duplicate requests to Pearson, but they are idempotent so that's ok try: with transaction.atomic(): # update records to reflect the successful upload if valid_profile_ids: ExamProfile.objects.filter(id__in=valid_profile_ids).update( status=ExamProfile.PROFILE_IN_PROGRESS) # update records to reflect invalid profile if invalid_profile_ids: ExamProfile.objects.filter(id__in=invalid_profile_ids).update( status=ExamProfile.PROFILE_INVALID) except: # pylint: disable=bare-except log.exception('Unexpected exception updating ExamProfile.status')
def test_exam_profile_validated(self): """ test validate_profile when a field is empty """ assert validate_profile(self.profile) is True