class TestXAPILRSConfiguration(unittest.TestCase): """ Tests for the ``XAPILRSConfiguration`` model. """ def setUp(self): super(TestXAPILRSConfiguration, self).setUp() self.x_api_lrs_config = factories.XAPILRSConfigurationFactory() self.x_api_client = EnterpriseXAPIClient(self.x_api_lrs_config) self.statement = EnterpriseStatement() @mock.patch('integrated_channels.xapi.client.RemoteLRS', mock.MagicMock()) def test_save_statement(self): """ Verify that save_statement sends xAPI statement to LRS. """ # verify that request completes without an error. self.x_api_client.save_statement(self.statement) self.x_api_client.lrs.save_statement.assert_called_once_with(self.statement) @mock.patch('integrated_channels.xapi.client.RemoteLRS', mock.MagicMock()) def test_save_statement_raises_client_error(self): """ Verify that save_statement raises ClientError if it could not complete request successfully. """ self.x_api_client.lrs.save_statement = mock.Mock(return_value=None) with raises(ClientError): self.x_api_client.save_statement(self.statement)
def _send_statement(statement, object_type, event_type, lrs_configuration, customer_name, username, course_id, response_fields): """ Transmit the specified xAPI Event information to the specified xAPI Learning Record Store service. """ LOGGER.info( '[Integrated Channel][xAPI] Sending {object_type} enrollment to xAPI LRS for user: {username} ' 'for {object_type}: {course_id}'.format( object_type=object_type, username=username, course_id=course_id, )) lrs_client = EnterpriseXAPIClient(lrs_configuration) try: lrs_response = lrs_client.save_statement(statement) response_fields.update({ 'status': lrs_response.response.status, 'error_message': lrs_response.data }) except ClientError: error_message = 'EnterpriseXAPIClient request failed.' response_fields.update({'error_message': error_message}) LOGGER.exception(error_message) status_string = 'Error transmitting' if response_fields['status'] == 200: status_string = 'Successfully transmitted' LOGGER.info( '[Integrated Channel][xAPI] {status_string} {object_type} {event_type} event to {lrs_hostname} for ' 'Enterprise Customer: {enterprise_customer}, User: {username} ' 'and {object_type}: {course_id}'.format( status_string=status_string, object_type=object_type, event_type=event_type, lrs_hostname=lrs_configuration.endpoint, enterprise_customer=customer_name, username=username, course_id=course_id, )) return response_fields