def test_list_with_certificate_type(self): """ Verify the list endpoint supports filtering by certificate_type.""" course_cert = factories.CourseCertificateFactory( certificate_type="verified") factories.UserCredentialFactory.create(credential=course_cert) # course_id is mandatory response = self.client.get(self.list_path, data={ 'course_id': self.course_id, 'certificate_type': self.course_certificate.certificate_type }) # after filtering it is only one related record expected = UserCredentialSerializer(self.user_credential, context={ 'request': self.request }).data self.assertEqual(json.loads(response.content), { 'count': 1, 'next': None, 'previous': None, 'results': [expected] })
def test_list_with_status_filter(self): """ Verify the list endpoint supports filtering by status.""" factories.UserCredentialFactory.create_batch( 2, status="revoked", username=self.user_credential.username) response = self.client.get( self.list_path, data={'status': self.user_credential.status}) self.assertEqual(response.status_code, 400) # username and status will return the data. response = self.client.get(self.list_path, data={ 'username': self.user_credential.username, 'status': UserCredential.AWARDED }) # after filtering it is only one related record expected = UserCredentialSerializer(self.user_credential, context={ 'request': self.request }).data self.assertEqual(response.data, { 'count': 1, 'next': None, 'previous': None, 'results': [expected] })
def test_create_with_programcertificate(self): """ Verify the endpoint supports issuing a new ProgramCertificate credential. """ program_certificate = factories.ProgramCertificateFactory() data = { "username": self.username, "credential": { "program_id": program_certificate.program_id }, "attributes": [ { "name": self.user_credential_attribute.name, "value": self.user_credential_attribute.value }, ] } response = self._attempt_create_user_credentials(data) self.assertEqual(response.status_code, 201) user_credential = UserCredential.objects.get(username=self.username) self.assertEqual( dict(response.data), dict( UserCredentialSerializer(user_credential, context={ 'request': self.request }).data))
def test_get(self): """ Verify a single user credential is returned. """ path = reverse("api:v1:usercredential-detail", args=[self.user_credential.id]) response = self.client.get(path) self.assertEqual(response.status_code, 200) self.assertEqual( response.data, UserCredentialSerializer(self.user_credential, context={ 'request': self.request }).data)
def assert_list_with_status_filter(self, data): """Helper method for making request and assertions. """ expected = { 'count': 1, 'next': None, 'previous': None, 'results': [ UserCredentialSerializer(self.user_credential, context={ 'request': self.request }).data ] } response = self.client.get(self.list_path, data, expected) self.assertEqual(json.loads(response.content), expected)
def _assert_usercredential_fields(self, response, username, expected_attrs): """ Verify the fields on a UserCredential object match expectations. """ user_credential = UserCredential.objects.filter(username=username) self.assertEqual(user_credential.count(), 1) self.assertEqual( dict(response.data), dict( UserCredentialSerializer(user_credential[0], context={ 'request': self.request }).data)) actual_attributes = [{ "name": attr.name, "value": attr.value } for attr in user_credential[0].attributes.all()] self.assertEqual(actual_attributes, expected_attrs)
def test_list_with_username_filter(self): """ Verify the list endpoint supports filter data by username.""" factories.UserCredentialFactory(username="******") response = self.client.get( self.list_path, data={'username': self.user_credential.username}) self.assertEqual(response.status_code, 200) # after filtering it is only one related record expected = UserCredentialSerializer(self.user_credential, context={ 'request': self.request }).data self.assertEqual(response.data, { 'count': 1, 'next': None, 'previous': None, 'results': [expected] })