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_course_completion_statement(lrs_configuration, user, course_overview, course_grade): """ Send xAPI statement for course completion. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. user (User): Django User object. course_overview (CourseOverview): Course over view object containing course details. course_grade (CourseGrade): course grade object. """ user_details = LearnerInfoSerializer(user) course_details = CourseInfoSerializer( course_overview, context={'site': lrs_configuration.enterprise_customer.site}) LOGGER.info( 'Sending course completion to xAPI for user: {username}, course: {course_key} with {percentage}%' .format(username=user.username if user else '', course_key=six.text_type(course_overview.id), percentage=course_grade.percent * 100)) statement = LearnerCourseCompletionStatement( user, course_overview, user_details.data, course_details.data, course_grade, ) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
def setUp(self): super(TestUtils, self).setUp() self.faker = FakerFactory.create() self.user = factories.UserFactory() self.user.profile = mock.Mock(country=mock.Mock(code='PK')) self.mock_social_auth = mock.Mock(provider='tpa-saml', uid='default:edxsso') self.mock_save_statement_success = mock.Mock(name='response', response=mock.Mock( name='response', status=200)) now = datetime.now() # pylint: disable=no-member self.course_overview_mock_data = dict( id=self.faker.text(max_nb_chars=25), # pylint: disable=no-member display_name=self.faker.text(max_nb_chars=25), # pylint: disable=no-member short_description=self.faker.text(), # pylint: disable=no-member marketing_url=self.faker.url(), # pylint: disable=no-member effort=self.faker.text(max_nb_chars=10), # pylint: disable=no-member start=now, end=now + timedelta(weeks=3, days=4), course_key='OrgX+Course101', course_uuid='b1e7c719af3c42288c6f50e2124bb913', ) self.course_overview = mock.Mock(**self.course_overview_mock_data) self.course_enrollment = mock.Mock(user=self.user, course=self.course_overview) self.course_grade = mock.Mock(percent_grade=0.80, passed_timestamp='2020-04-01') self.x_api_lrs_config = factories.XAPILRSConfigurationFactory() self.x_api_client = EnterpriseXAPIClient(self.x_api_lrs_config)
def setUp(self): super(TestUtils, self).setUp() self.faker = FakerFactory.create() self.user = factories.UserFactory() self.user.profile = mock.Mock(country=mock.Mock(code='PK')) now = datetime.now() # pylint: disable=no-member self.course_overview_mock_data = dict( id=self.faker.text(max_nb_chars=25), # pylint: disable=no-member display_name=self.faker.text(max_nb_chars=25), # pylint: disable=no-member short_description=self.faker.text(), # pylint: disable=no-member marketing_url=self.faker.url(), # pylint: disable=no-member effort=self.faker.text(max_nb_chars=10), # pylint: disable=no-member start=now, end=now + timedelta(weeks=3, days=4), ) self.course_overview = mock.Mock(**self.course_overview_mock_data) self.course_enrollment = mock.Mock(user=self.user, course=self.course_overview) self.course_grade = mock.Mock(percent=0.80, passed=True) self.x_api_lrs_config = factories.XAPILRSConfigurationFactory() self.x_api_client = EnterpriseXAPIClient(self.x_api_lrs_config)
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
def setUp(self): super(TestUtils, self).setUp() faker = FakerFactory.create() self.user = factories.UserFactory() # pylint: disable=no-member self.course_overview = mock.Mock(display_name=faker.text(max_nb_chars=25), short_description=faker.text()) self.course_enrollment = mock.Mock(user=self.user, course=self.course_overview) self.x_api_lrs_config = factories.XAPILRSConfigurationFactory() self.x_api_client = EnterpriseXAPIClient(self.x_api_lrs_config)
def send_course_enrollment_statement(lrs_configuration, course_enrollment): """ Send xAPI statement for course enrollment. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. course_enrollment (CourseEnrollment): Course enrollment object. """ statement = LearnerCourseEnrollmentStatement(course_enrollment.user, course_enrollment.course, {}, {}) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
def send_course_enrollment_statement(lrs_configuration, course_enrollment): """ Send xAPI statement for course enrollment. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. course_enrollment (CourseEnrollment): Course enrollment object. """ user_details = LearnerInfoSerializer(course_enrollment.user) course_details = CourseInfoSerializer(course_enrollment.course) statement = LearnerCourseEnrollmentStatement( course_enrollment.user, course_enrollment.course, user_details.data, course_details.data, ) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
def send_course_enrollment_statement(lrs_configuration, course_enrollment): """ Send xAPI statement for course enrollment. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. course_enrollment (CourseEnrollment): Course enrollment object. """ user = course_enrollment.user LOGGER.info( 'Sending course enrollment to xAPI for user: {username} for course: {course_key}' .format(username=user.username, course_key=six.text_type(course_enrollment.course.id))) user_social_auth = get_user_social_auth( user, lrs_configuration.enterprise_customer) statement = LearnerCourseEnrollmentStatement( user, user_social_auth, course_enrollment.course, ) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
def send_course_completion_statement(lrs_configuration, user, course_overview, course_grade): """ Send xAPI statement for course completion. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. user (User): Django User object. course_overview (CourseOverview): Course over view object containing course details. course_grade (CourseGrade): course grade object. """ user_details = LearnerInfoSerializer(user) course_details = CourseInfoSerializer(course_overview) statement = LearnerCourseCompletionStatement( user, course_overview, user_details.data, course_details.data, course_grade, ) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
def send_course_enrollment_statement(lrs_configuration, course_enrollment): """ Send xAPI statement for course enrollment. Arguments: lrs_configuration (XAPILRSConfiguration): XAPILRSConfiguration instance where to send statements. course_enrollment (CourseEnrollment): Course enrollment object. """ user_details = LearnerInfoSerializer(course_enrollment.user) course_details = CourseInfoSerializer( course_enrollment.course, context={'site': lrs_configuration.enterprise_customer.site}) LOGGER.info( 'Sending course enrollment to xAPI for user: {username} for course: {course_key}' .format(username=course_enrollment.user.username, course_key=six.text_type(course_enrollment.course.id))) statement = LearnerCourseEnrollmentStatement( course_enrollment.user, course_enrollment.course, user_details.data, course_details.data, ) EnterpriseXAPIClient(lrs_configuration).save_statement(statement)
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()