def test_no_previous_email_sent(self): """ Test that no additional email is sent if the user hasn't previously sent one """ self.assertEqual(0, len(mail.outbox)) send_updated_emails_for_program(self.USERNAME, self.pc) # Check that no email was sent self.assertEqual(0, len(mail.outbox))
def issue_credential(self, credential, username, status=UserCredentialStatus.AWARDED, attributes=None, request=None): """ Issue a Program Certificate to the user. This function is being overriden to provide functionality for sending an updated email to pathway partners This action is idempotent. If the user has already earned the credential, a new one WILL NOT be issued. The existing credential WILL be modified. Arguments: credential (AbstractCredential): Type of credential to issue. username (str): username of user for which credential required status (str): status of credential attributes (List[dict]): optional list of attributes that should be associated with the issued credential. request (HttpRequest): request object to build program record absolute uris Returns: UserCredential """ user_credential, created = UserCredential.objects.update_or_create( username=username, credential_content_type=ContentType.objects.get_for_model( credential), credential_id=credential.id, defaults={ "status": status, }, ) # Send an updated email to a pathway org only if the user has previously sent one # This function call should be moved into some type of task queue # once credentials has that functionality site_config = getattr(credential.site, "siteconfiguration", None) # Add a check to see if records_enabled is True for the site associated with # the credentials. If records is not enabled, we should not send this email if created and site_config and site_config.records_enabled: send_updated_emails_for_program(request, username, credential) # If this is a new ProgramCertificate and the `SEND_EMAIL_ON_PROGRAM_COMPLETION` # feature is enabled then let's send a congratulatory message to the learner if created and getattr(settings, "SEND_EMAIL_ON_PROGRAM_COMPLETION", False): send_program_certificate_created_message(username, credential) self.set_credential_attributes(user_credential, attributes) return user_credential
def test_send_updated_email_when_program_finished(self): """ Test that an additional updated email will be sent """ # Mock sending an email to the partner UserCreditPathwayFactory( user=self.user, pathway=self.pathway, status=UserCreditPathwayStatus.SENT) self.assertEqual(0, len(mail.outbox)) send_updated_emails_for_program(self.USERNAME, self.pc) # Check that another email was sent self.assertEqual(1, len(mail.outbox)) email = mail.outbox[0] self.assertIn(self.program.title + ' Updated Credit Request for', email.subject)
def test_send_updated_email_when_program_finished(self): """ Test that an additional updated email will be sent """ # Mock sending an email to the partner UserCreditPathwayFactory(user=self.user, pathway=self.pathway, status=UserCreditPathwayStatus.SENT) self.assertEqual(0, len(mail.outbox)) send_updated_emails_for_program(self.request, self.USERNAME, self.pc) # Check that another email was sent self.assertEqual(1, len(mail.outbox)) email = mail.outbox[0] record_path = reverse("records:public_programs", kwargs={"uuid": self.pcr.uuid.hex}) expected_record_link = self.request.build_absolute_uri(record_path) expected_csv_link = urllib.parse.urljoin(expected_record_link, "csv") self.assertIn(self.program.title + " Updated Credit Request for", email.subject) self.assertIn(expected_record_link, email.body) self.assertIn(expected_csv_link, email.body)
def issue_credential(self, credential, username, status=UserCredentialStatus.AWARDED, attributes=None): """ Issue a Program Certificate to the user. This function is being overriden to provide functionality for sending an updated email to pathway partners This action is idempotent. If the user has already earned the credential, a new one WILL NOT be issued. The existing credential WILL be modified. Arguments: credential (AbstractCredential): Type of credential to issue. username (str): username of user for which credential required status (str): status of credential attributes (List[dict]): optional list of attributes that should be associated with the issued credential. Returns: UserCredential """ user_credential, created = UserCredential.objects.update_or_create( username=username, credential_content_type=ContentType.objects.get_for_model( credential), credential_id=credential.id, defaults={ 'status': status, }, ) # Send an updated email to a pathway org only if the user has previously sent one # This function call should be moved into some type of task queue # once credentials has that functionality if created: send_updated_emails_for_program(username, credential) self.set_credential_attributes(user_credential, attributes) return user_credential