def test_program_details(self, use_org_name, credential_title): """Verify the method returns the details of program associated with the ProgramCertificate.""" program_certificate = ProgramCertificateFactory( site=self.site, use_org_name=use_org_name, title=credential_title) program_uuid = program_certificate.program_uuid.hex courses = [ { "key": "ACMEx/101x" }, { "key": "FakeX/101x" }, ] expected = ProgramDetails( uuid=program_uuid, title="Test Program", type="MicroFakers", type_slug=slugify("MicroFakers"), credential_title=credential_title, course_count=len(courses), organizations=[ OrganizationDetails( uuid=uuid.uuid4().hex, key="ACMEx", name="ACME University", display_name="ACME University" if use_org_name else "ACMEx", certificate_logo_image_url="http://example.com/acme.jpg", ), OrganizationDetails( uuid=uuid.uuid4().hex, key="FakeX", name="Fake University", display_name="Fake University" if use_org_name else "FakeX", certificate_logo_image_url="http://example.com/fakex.jpg", ), ], hours_of_effort=None, status="active", ) # Mocked at apps.credentials instead of apps.catalog because that's where it's being referenced with mock.patch( "credentials.apps.credentials.models.get_program_details_by_uuid" ) as mock_program_get: mock_program_get.return_value = expected self.assertEqual(program_certificate.program_details, expected) mock_program_get.assert_called_with( uuid=program_certificate.program_uuid, site=program_certificate.site)
def test_program_uuid(self): """Verify a ValidationError is raised if the program's authoring organizations have no certificate images.""" sc = SiteConfigurationFactory() data = factory.build(dict, FACTORY_CLASS=ProgramCertificateFactory) data["site"] = sc.site.id form = ProgramCertificateAdminForm(data) fake = faker.Faker() bad_organization = OrganizationDetails( uuid=fake.uuid4(), key=fake.word(), name=fake.word(), display_name=fake.word(), certificate_logo_image_url=None, ) good_organization = dataclasses.replace(bad_organization, certificate_logo_image_url=fake.word()) bad_program = ProgramDetails( uuid=fake.uuid4(), title=fake.word(), type=fake.word(), type_slug=fake.word(), credential_title=fake.word(), course_count=fake.random_digit(), organizations=[bad_organization], hours_of_effort=fake.random_digit(), status=fake.word(), ) good_program = dataclasses.replace(bad_program, organizations=[good_organization]) with mock.patch( "credentials.apps.credentials.forms.get_program_details_by_uuid", return_value=bad_program ) as mock_method: self.assertFalse(form.is_valid()) mock_method.assert_called_with(data["program_uuid"], sc.site) self.assertEqual( form.errors["program_uuid"][0], "All authoring organizations of the program MUST have a certificate image defined!", ) form = ProgramCertificateAdminForm(data) with mock.patch( "credentials.apps.credentials.forms.get_program_details_by_uuid", return_value=good_program ) as mock_method: self.assertFalse(form.is_valid()) mock_method.assert_called_with(data["program_uuid"], sc.site) self.assertNotIn("program_uuid", form.errors)
def _render_user_credential(self, use_proper_logo_url=True, user_credential=None, program_certificate=None): """ Helper method to render a user certificate.""" user_credential = user_credential or self.user_credential program_certificate = program_certificate or self.program_certificate if use_proper_logo_url: certificate_logo_image_url = self.faker.url() else: certificate_logo_image_url = None program_uuid = program_certificate.program_uuid credential_title = program_certificate.title or self.PROGRAM_NAME mocked_program_data = ProgramDetails( uuid=str(program_uuid), title=self.PROGRAM_NAME, type=self.PROGRAM_TYPE, type_slug=slugify(self.PROGRAM_TYPE), credential_title=credential_title, course_count=2, organizations=[ OrganizationDetails( uuid=str(uuid.uuid4()), key=self.faker.word(), name=self.faker.word(), display_name=self.faker.word(), certificate_logo_image_url=certificate_logo_image_url, ), OrganizationDetails( uuid=str(uuid.uuid4()), key=self.faker.word(), name=self.faker.word(), display_name=self.faker.word(), certificate_logo_image_url=certificate_logo_image_url, ), ], hours_of_effort=self.faker.pyint(), status="active", ) with patch("credentials.apps.core.models.SiteConfiguration.get_user_api_data") as user_data, patch( "credentials.apps.credentials.models.ProgramCertificate.program_details", new_callable=PropertyMock ) as mock_program_details: user_data.return_value = self.MOCK_USER_DATA mock_program_details.return_value = mocked_program_data response = self.client.get(user_credential.get_absolute_url()) self.assertEqual(response.status_code, 200) return response
def get_context_data(self, **kwargs): program_type = self.request.GET.get("program_type", "Professional Certificate") context = super().get_context_data(**kwargs) program_details = ProgramDetails( uuid=uuid.uuid4(), title="Completely Example Program", type=program_type, type_slug=slugify(program_type), credential_title=None, course_count=3, organizations=[ OrganizationDetails( uuid=uuid.uuid4(), key="ExampleX", name="Example University", display_name="Absolutely Fake University", certificate_logo_image_url="https://placehold.it/204x204", ) ], hours_of_effort=None, status="active", ) context.update({ "user_credential": { "modified": timezone.now(), "uuid": uuid.uuid4(), "credential": { "program_uuid": uuid.uuid4(), "signatories": { # NOTE (CCB): This is a small hack to workaround the fact that the template expects a # QuerySet. "all": [{ "name": "Pseudo McFakerson", "title": "Professor...really just Some Guy", "organization_name_override": "Parts Unknown", "image": { "url": "https://placehold.it/720x280" }, }] }, "program_details": program_details, }, }, "user_data": { "name": "John Doe", "username": self.request.user.username, }, "child_templates": { "credential": self.select_theme_template([ "credentials/programs/{type}/certificate.html".format( type=slugify(program_type)) ]), "footer": self.select_theme_template(["_footer.html"]), "header": self.select_theme_template(["_header.html"]), }, "page_title": program_details.type, "program_name": program_details.title, "credential_title": program_details.credential_title, "render_language": settings.LANGUAGE_CODE, "org_name_string": "Example University", }) return context