Example #1
0
    def test_create_with_existing_user_credential(self):
        """ Verify that, if a user has already been issued a credential, further attempts to issue the same credential
        will NOT create a new credential, but update the attributes of the existing credential.
        """
        user_credential = UserCredentialFactory(credential__site=self.site)
        self.authenticate_user(self.user)
        self.add_user_permission(self.user, 'add_usercredential')

        # POSTing the exact data that exists in the database should not change the UserCredential
        data = self.serialize_user_credential(user_credential)
        response = self.client.post(self.list_path,
                                    data=JSONRenderer().render(data),
                                    content_type=JSON_CONTENT_TYPE)
        self.assertEqual(response.status_code, 201)

        # POSTing with modified status/attributes should update the existing UserCredential
        data = self.serialize_user_credential(user_credential)
        expected_attribute = UserCredentialAttributeFactory.build()
        data['status'] = 'revoked'
        data['attributes'] = [
            UserCredentialAttributeSerializer(expected_attribute).data
        ]
        response = self.client.post(self.list_path,
                                    data=JSONRenderer().render(data),
                                    content_type=JSON_CONTENT_TYPE)
        self.assertEqual(response.status_code, 201)

        user_credential.refresh_from_db()
        self.assertEqual(response.data,
                         self.serialize_user_credential(user_credential))
        self.assertEqual(user_credential.attributes.count(), 1)

        actual_attribute = user_credential.attributes.first()
        self.assertEqual(actual_attribute.name, expected_attribute.name)
        self.assertEqual(actual_attribute.value, expected_attribute.value)
Example #2
0
    def test_data(self):
        program_certificate = ProgramCertificateFactory()
        user_credential = UserCredentialFactory(credential=program_certificate)
        program_certificate_attr = UserCredentialAttributeFactory(user_credential=user_credential)

        expected = {"name": program_certificate_attr.name, "value": program_certificate_attr.value}
        actual = UserCredentialAttributeSerializer(program_certificate_attr).data
        self.assertEqual(actual, expected)