def test_issue_credential_with_invalid_type(self): """ Verify the method raises an error for attempts to issue an unsupported credential type. """ accreditor = Accreditor(issuers=[ProgramCertificateIssuer()]) course_cert = CourseCertificateFactory() with self.assertRaises(UnsupportedCredentialTypeError): accreditor.issue_credential(course_cert, 'tester', attributes=self.attributes)
class AccreditorTests(TestCase): """ Tests for Accreditor class. This class is responsible to call the right credential issuer class to generate the credential. """ def setUp(self): super(AccreditorTests, self).setUp() self.accreditor = Accreditor() self.program_cert = ProgramCertificateFactory() self.program_credential = ProgramCertificate self.attributes = [{ "name": "whitelist_reason", "value": "Reason for whitelisting." }] def test_create_credential_type_issuer_map(self): """ Verify the Accreditor supports only one issuer per credential type. """ accreditor = Accreditor( issuers=[ProgramCertificateIssuer(), ProgramCertificateIssuer()]) expected = {self.program_credential: accreditor.issuers[0]} self.assertEqual(accreditor.credential_type_issuer_map, expected) def test_issue_credential_with_invalid_type(self): """ Verify the method raises an error for attempts to issue an unsupported credential type. """ course_cert = CourseCertificateFactory() with self.assertRaises(UnsupportedCredentialTypeError): self.accreditor.issue_credential(course_cert, 'tester', self.attributes) def test_issue_credential(self): """ Verify the method calls the Issuer's issue_credential method. """ with patch.object(ProgramCertificateIssuer, 'issue_credential') as mock_method: self.accreditor.issue_credential(self.program_cert, 'tester', self.attributes) mock_method.assert_called_with(self.program_cert, 'tester', self.attributes) def test_constructor_with_multiple_issuers_for_same_credential_type(self): """ Verify the Accreditor supports a single Issuer per credential type. Attempts to register additional issuers for a credential type should result in a warning being logged. """ # pylint: disable=no-member msg = "The issuer [{}] is already registered to issue credentials of type [{}]. [{}] will NOT be used.".format( ProgramCertificateIssuer, self.program_credential, ProgramCertificateIssuer) with LogCapture(LOGGER_NAME) as l: accreditor = Accreditor(issuers=[ ProgramCertificateIssuer(), ProgramCertificateIssuer() ]) l.check((LOGGER_NAME, 'WARNING', msg)) expected = {self.program_credential: accreditor.issuers[0]} self.assertEqual(accreditor.credential_type_issuer_map, expected)
def test_issue_credential(self): """ Verify the method calls the Issuer's issue_credential method. """ accreditor = Accreditor(issuers=[ProgramCertificateIssuer()]) with patch.object(ProgramCertificateIssuer, 'issue_credential') as mock_method: accreditor.issue_credential(self.program_cert, 'tester', attributes=self.attributes) mock_method.assert_called_with(self.program_cert, 'tester', 'awarded', self.attributes, None)
class AccreditorTests(TestCase): """ Tests for Accreditor class. This class is responsible to call the right credential issuer class to generate the credential. """ def setUp(self): super(AccreditorTests, self).setUp() self.accreditor = Accreditor() self.program_cert = ProgramCertificateFactory() self.program_credential = ProgramCertificate self.attributes = [{"name": "whitelist_reason", "value": "Reason for whitelisting."}] def test_create_credential_type_issuer_map(self): """ Verify the Accreditor supports only one issuer per credential type. """ accreditor = Accreditor(issuers=[ProgramCertificateIssuer(), ProgramCertificateIssuer()]) expected = { self.program_credential: accreditor.issuers[0] } self.assertEqual(accreditor.credential_type_issuer_map, expected) def test_issue_credential_with_invalid_type(self): """ Verify the method raises an error for attempts to issue an unsupported credential type. """ course_cert = CourseCertificateFactory() with self.assertRaises(UnsupportedCredentialTypeError): self.accreditor.issue_credential(course_cert, 'tester', self.attributes) def test_issue_credential(self): """ Verify the method calls the Issuer's issue_credential method. """ with patch.object(ProgramCertificateIssuer, 'issue_credential') as mock_method: self.accreditor.issue_credential(self.program_cert, 'tester', self.attributes) mock_method.assert_called_with(self.program_cert, 'tester', self.attributes) def test_constructor_with_multiple_issuers_for_same_credential_type(self): """ Verify the Accreditor supports a single Issuer per credential type. Attempts to register additional issuers for a credential type should result in a warning being logged. """ # pylint: disable=no-member msg = "The issuer [{}] is already registered to issue credentials of type [{}]. [{}] will NOT be used.".format( ProgramCertificateIssuer, self.program_credential, ProgramCertificateIssuer ) with LogCapture(LOGGER_NAME) as l: accreditor = Accreditor(issuers=[ProgramCertificateIssuer(), ProgramCertificateIssuer()]) l.check((LOGGER_NAME, 'WARNING', msg)) expected = {self.program_credential: accreditor.issuers[0]} self.assertEqual(accreditor.credential_type_issuer_map, expected)
def issue_credential(self, validated_data): """ Issue a new credential. Args: validated_data (dict): Input data specifying the credential type, recipient, and attributes. Returns: AbstractCredential """ accreditor = Accreditor() credential = validated_data['credential'] username = validated_data['username'] attributes = validated_data.pop('attributes', None) return accreditor.issue_credential(credential, username, attributes)
def issue_credential(self, validated_data): """ Issue a new credential. Args: validated_data (dict): Input data specifying the credential type, recipient, and attributes. Returns: AbstractCredential """ accreditor = Accreditor() credential = validated_data["credential"] username = validated_data["username"] status = validated_data.get("status", UserCredentialStatus.AWARDED) attributes = validated_data.pop("attributes", None) request = self.context.get("request") return accreditor.issue_credential(credential, username, status=status, attributes=attributes, request=request)