def test_transmit_create_failure(self, update_content_metadata_mock): """ Test unsuccessful creation of content metadata during transmission. """ content_id = 'course:DemoX' content_id_2 = 'course:DemoX2' # Set the chunk size to 1 to simulate 2 network calls self.enterprise_config.transmission_chunk_size = 1 self.enterprise_config.save() update_content_metadata_mock.side_effect = ClientError( 'error occurred') responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) with LogCapture(level=logging.ERROR) as log_capture: transmitter = SapSuccessFactorsContentMetadataTransmitter( self.enterprise_config) transmitter.transmit({ content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id, 'update': True }), content_id_2: ContentMetadataItemExport( { 'key': content_id_2, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id_2, 'update': True }) }) assert len(log_capture.records) == 2 assert 'Failed to update [1] content metadata items' in log_capture.records[ 0].getMessage() assert not ContentMetadataItemTransmission.objects.filter( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, ).exists() assert not ContentMetadataItemTransmission.objects.filter( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id_2, ).exists()
def test_transmit_create_failure(self): """ Test unsuccessful creation of content metadata during transmission. """ content_id = 'course:DemoX' channel_metadata = {'update': True} payload = { content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE, }, channel_metadata, ) } self.create_content_metadata_mock.side_effect = ClientError( 'error occurred') transmitter = ContentMetadataTransmitter(self.enterprise_config) transmitter.transmit(payload) self.create_content_metadata_mock.assert_called() self.update_content_metadata_mock.assert_not_called() self.delete_content_metadata_mock.assert_not_called() assert not ContentMetadataItemTransmission.objects.filter( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, )
def test_transmit_create_success(self): """ Test successful creation of content metadata during transmission. """ content_id = 'course:DemoX' channel_metadata = {'update': True} payload = { content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE }, channel_metadata, ) } self.create_content_metadata_mock.return_value = (200, '{"success":"true"}') transmitter = ContentMetadataTransmitter(self.enterprise_config) transmitter.transmit(payload) self.create_content_metadata_mock.assert_called() self.update_content_metadata_mock.assert_not_called() self.delete_content_metadata_mock.assert_not_called() created_transmission = ContentMetadataItemTransmission.objects.get( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, ) assert created_transmission.channel_metadata == channel_metadata
def test_transmit_update_not_needed(self): """ Test successful update of content metadata during transmission. """ content_id = 'course:DemoX' channel_metadata = {'update': True} ContentMetadataItemTransmission( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, channel_metadata=channel_metadata).save() payload = { content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE }, channel_metadata, ) } transmitter = ContentMetadataTransmitter(self.enterprise_config) transmitter.transmit(payload) self.create_content_metadata_mock.assert_not_called() self.update_content_metadata_mock.assert_not_called() self.delete_content_metadata_mock.assert_not_called()
def test_transmit_update_failure(self): """ Test unsuccessful update of content metadata during transmission. """ content_id = 'course:DemoX' channel_metadata = {'update': True} ContentMetadataItemTransmission( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, channel_metadata={}).save() payload = { content_id: ContentMetadataItemExport( { 'key': content_id, 'content_type': 'course' }, channel_metadata) } self.update_content_metadata_mock.side_effect = ClientError( 'error occurred') transmitter = ContentMetadataTransmitter(self.enterprise_config) transmitter.transmit(payload) self.create_content_metadata_mock.assert_not_called() self.update_content_metadata_mock.assert_called() self.delete_content_metadata_mock.assert_not_called() updated_transmission = ContentMetadataItemTransmission.objects.get( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), content_id=content_id, ) assert updated_transmission.channel_metadata == {}
def test_transmit_api_usage_limit_disabled(self, update_content_metadata_mock): """ Test that API usage limit is not applied if setting is not present. """ content_id = 'course:DemoX' content_id_2 = 'course:DemoX2' # Set the chunk size to 1 to simulate 2 network calls self.enterprise_config.transmission_chunk_size = 1 self.enterprise_config.save() responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) transmitter = SapSuccessFactorsContentMetadataTransmitter( self.enterprise_config) with override_settings( INTEGRATED_CHANNELS_API_CHUNK_TRANSMISSION_LIMIT={}): transmitter.transmit({ content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id, 'update': True }), content_id_2: ContentMetadataItemExport( { 'key': content_id_2, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id_2, 'update': True }), }) assert update_content_metadata_mock.call_count == 2 assert ContentMetadataItemTransmission.objects.filter( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), ).count() == 2
def test_transmit_api_usage_limit(self, update_content_metadata_mock): """ Test that API usage limit is being observed while transmitting content metadata. """ content_id = 'course:DemoX' content_id_2 = 'course:DemoX2' # Set the chunk size to 1 to simulate 2 network calls self.enterprise_config.transmission_chunk_size = 1 self.enterprise_config.save() responses.add(responses.POST, self.url_base + self.oauth_api_path, json=self.expected_token_response_body, status=200) transmitter = SapSuccessFactorsContentMetadataTransmitter( self.enterprise_config) transmitter.transmit({ content_id: ContentMetadataItemExport( { 'key': content_id, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id, 'update': True }), content_id_2: ContentMetadataItemExport( { 'key': content_id_2, ContentType.METADATA_KEY: ContentType.COURSE }, { 'courseID': content_id_2, 'update': True }), }) assert update_content_metadata_mock.call_count == 1 assert ContentMetadataItemTransmission.objects.filter( enterprise_customer=self.enterprise_config.enterprise_customer, integrated_channel_code=self.enterprise_config.channel_code(), ).count() == 1