def create_courseref(request): """ Create CourseRef and Course entry based on context_title. """ request_dict = request.session['LTI_POST'] if not request.session.get('is_valid'): return redirect(reverse('ct:home')) context_id = request_dict.get('context_id') roles_from_request = request_dict.get('roles', '').split(',') roles = list(set((ROLES_MAP.get(role, Role.ENROLLED) for role in roles_from_request))) # Make sure this context_id is not used course_ref = CourseRef.objects.filter(context_id=context_id).first() if course_ref: if Role.INSTRUCTOR in roles: return redirect(reverse('ct:course', args=(course_ref.course.id,))) else: return redirect(reverse('ct:home')) course = Course( title=request_dict.get('context_title', 'Course title for %s' % context_id), addedBy=request.user ) course.save() role = Role(role=Role.INSTRUCTOR, course=course, user=request.user) role.save() course_id = course.id course_ref = CourseRef( course=course, context_id=context_id, tc_guid=request_dict.get('tool_consumer_instance_guid', request.META.get('HTTP_HOST')) ) course_ref.save() course_ref.instructors.add(request.user) return redirect(reverse('ct:edit_course', args=(course_id,)))
class ConceptsTests(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson(title='New York Test Lesson', text='brr', addedBy=self.user) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson(unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id) self.unit_lesson.save() def test_unit_concepts_search(self): self.lesson.concept = self.concept self.lesson.save() response = self.client.get( reverse('ct:unit_concepts', kwargs={'course_id': self.course.id, 'unit_id': self.unit.id}), {'search': 'New York'}, follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'ct/concepts.html') self.assertIn('actionTarget', response.context) cset_dict = {i[0]: i[1] for i in response.context['cset']} self.assertIn('New York Test Lesson', cset_dict) self.assertIn('New York', cset_dict) self.assertIn('The New York Times Company', cset_dict)
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson(title='New York Test Lesson', text='brr', addedBy=self.user) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson(unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id) self.unit_lesson.save()
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy')
def test_not_enroll_second_time(self): """ Should not enroll second time if already enrolled. """ enroll_code = EnrollUnitCode.get_code(self.courseunit) self.client.login(username='******', password='******') role = Role(role=Role.ENROLLED, course=self.course, user=self.user) role.save() response = self.client.get(reverse('chat:chat_enroll', args=(enroll_code,)), follow=True) self.assertEquals( Role.objects.filter(role=Role.ENROLLED, user=self.user, course=self.course).count(), 1 )
def create_courseref(request): """ Create CourseRef and Course entry based on context_title. """ request_dict = request.session['LTI_POST'] if not request.session.get('is_valid'): return redirect(reverse('ct:home')) context_id = request_dict.get('context_id') roles_from_request = request_dict.get('roles', '').split(',') roles = list( set((ROLES_MAP.get(role, Role.ENROLLED) for role in roles_from_request))) # Make sure this context_id is not used course_ref = CourseRef.objects.filter(context_id=context_id).first() if course_ref: if Role.INSTRUCTOR in roles: return redirect(reverse('ct:course', args=(course_ref.course.id, ))) else: return redirect(reverse('ct:home')) course = Course(title=request_dict.get('context_title', 'Course title for %s' % context_id), addedBy=request.user) course.save() role = Role(role=Role.INSTRUCTOR, course=course, user=request.user) role.save() course_id = course.id course_ref = CourseRef(course=course, context_id=context_id, tc_guid=request_dict.get( 'tool_consumer_instance_guid', request.META.get('HTTP_HOST'))) course_ref.save() course_ref.instructors.add(request.user) return redirect(reverse('ct:edit_course', args=(course_id, )))
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.enroll = EnrollUnitCode.get_code_for_user_chat(self.course_unit, True, self.user) self.history_live_chat = Chat( user=self.user, is_live=True, enroll_code=self.enroll ) self.history_live_chat.save()
class TestCourseView(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.enroll = EnrollUnitCode.get_code_for_user_chat(self.course_unit, True, self.user) self.history_live_chat = Chat( user=self.user, is_live=True, enroll_code=self.enroll ) self.history_live_chat.save() @patch('chat.serializers.ChatProgressSerializer') @patch('lms.views.get_object_or_404') @patch('lms.views.EnrollUnitCode.get_code') @patch('fsm.models.FSMState.find_live_sessions') @patch('chat.models.Chat.objects.filter') def test_course_view(self, chatFilterMock, find_live_sessions, get_code, get_obj_or_404, ChatProgressSerializer): """ This test tests that: - query FSMState.find_live_sessions(request.user).filter(activity__course=course).first() return live session and this live session is present in page's context """ filter_mock = Mock() filter_mock.filter = Mock() find_live_sessions.return_value = filter_mock first_mock = Mock() filter_mock.filter.return_value = first_mock first_mock.first = Mock() first_mock.first.return_value = Mock() first_mock.first.return_value.id = 1 unit = Mock() unit.unit.get_exercises.return_value=[Mock()] course_mock = Mock() course_units = Mock() course_mock.get_course_units = course_units course_units.return_value = [unit] get_obj_or_404.return_value = course_mock chatFilterMock = Mock() chatFilterMock.return_value = [Mock()] ChatProgressSerializer.data.get.return_value = 0 response = self.client.get(reverse('lms:course_view', kwargs={'course_id': 1})) self.assertEqual(response.status_code, 200) self.assertEqual(filter_mock.filter.call_count, 1) self.assertEqual(first_mock.first.call_count, 1) self.assertEqual(get_obj_or_404.call_count, 1) self.assertTemplateUsed(response, 'lms/course_page.html') # context should contain these keys: course, liveSession, courslets self.assertIn('course', response.context) self.assertIn('liveSession', response.context) self.assertIn('courslets', response.context) def test_course_view_negative(self): """ This test tests case when teacher not yet (opened) joined live session and student opens course page. Student should not see 'Join Live Session' button on the top of the page. """ response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'lms/course_page.html') self.assertIn('course', response.context) self.assertIn('liveSession', response.context) self.assertIn('courslets', response.context) self.assertEqual(response.context['liveSession'], None) self.assertFalse(response.context['liveSession']) #TODO: write test when teacher really creates Course and Courslets inside of the course and student open page. #TODO: user should see 'Join' button. @patch('chat.models.Chat.get_spent_time') def test_live_chat_history_time_spent(self, get_spent_time): get_spent_time.return_value = datetime.timedelta(days=1, hours=1) response = self.client.get(reverse('lms:course_view', kwargs={'course_id': self.course.id})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'lms/course_page.html') self.assertIn('livesessions', response.context) self.assertNotEqual(response.context['livesessions'], []) self.assertEqual(response.context['livesessions'][0].get_formatted_time_spent(), '1 day, 1:00:00')
class TestCourseletViewHistoryTab(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy') def test_click_on_courslet_creates_new_chat(self): # test that there's no history yet response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertIsNotNone(list(response.context['courslets'])) self.assertEqual(response.status_code, 200) chats_count_1 = Chat.objects.all().count() # firstly call to chat:init_chat_api function with enroll_key and chat_id=0 response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) self.assertEqual(response.status_code, 200) chats_count_2 = Chat.objects.count() self.assertNotEqual(chats_count_2, chats_count_1) response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_3 = Chat.objects.count() response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_4 = Chat.objects.count() self.assertEqual(response.status_code, 200) self.assertEqual(chats_count_4, chats_count_2) self.assertEqual(chats_count_3, chats_count_2) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) chat = Chat.objects.all().first() # get chat and set state to None it means that courslet finished. chat.state = None chat.save() response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertEqual(Chat.objects.count(), chats_count_2) self.assertEqual(len(list(response.context['courslets'])), 1) def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 4)
def setUp(self): """ Preconditions. """ from chat.fsm_plugin.chat import get_specs from chat.fsm_plugin.additional import get_specs as get_specs_additional self.client = Client() self.user = User.objects.create_user('test', '*****@*****.**', 'test') get_specs()[0].save_graph(self.user.username) get_specs_additional()[0].save_graph(self.user.username) mocked_nonce = u'135685044251684026041377608307' mocked_timestamp = u'1234567890' mocked_decoded_signature = u'my_signature=' self.headers = { u'user_id': 1, u'lis_person_name_full': u'Test Username', u'lis_person_name_given': u'First', u'lis_person_name_family': u'Second', u'lis_person_contact_email_primary': u'*****@*****.**', u'lis_person_sourcedid': u'Test_Username', u'oauth_callback': u'about:blank', u'launch_presentation_return_url': '', u'lti_message_type': u'basic-lti-launch-request', u'lti_version': 'LTI-1p0', u'roles': u'Student', u'context_id': 1, u'tool_consumer_info_product_family_code': u'moodle', u'context_title': u'Test title', u'tool_consumer_instance_guid': u'test.dot.com', u'resource_link_id': 'dfgsfhrybvrth', u'lis_result_sourcedid': 'wesgaegagrreg', u'oauth_nonce': mocked_nonce, u'oauth_timestamp': mocked_timestamp, u'oauth_consumer_key': u'consumer_key', u'oauth_signature_method': u'HMAC-SHA1', u'oauth_version': u'1.0', u'oauth_signature': mocked_decoded_signature } self.unit = Unit(title='Test title', addedBy=self.user) self.unit.save() self.course = Course(title='Test title', description='test description', access='Public', enrollCode='111', lockout='222', addedBy=self.user) self.course.save() self.course_ref = CourseRef( course=self.course, context_id=self.headers.get('context_id'), tc_guid=self.headers.get('tool_consumer_instance_guid') ) self.course_ref.save() self.course_ref.instructors.add(self.user) self.role1 = Role( role=Role.ENROLLED, user=self.user, course=self.course, ) self.role1.save() self.courseunit = CourseUnit( unit=self.unit, course=self.course, order=0, addedBy=self.user, releaseTime=timezone.now() ) self.courseunit.save() lesson = Lesson(title='title', text='text', addedBy=self.user) lesson.save() unitlesson = UnitLesson( unit=self.unit, order=0, lesson=lesson, addedBy=self.user, treeID=lesson.id ) unitlesson.save() self.lti_consumer = LtiConsumer( consumer_name='test', consumer_key='consumer_key', consumer_secret='test_key' ) self.lti_consumer.save()
class TestCourseView(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.enroll = EnrollUnitCode.get_code_for_user_chat(self.course_unit, True, self.user) self.history_live_chat = Chat( user=self.user, is_live=True, enroll_code=self.enroll ) self.history_live_chat.save() @patch('chat.serializers.ChatProgressSerializer') @patch('lms.views.get_object_or_404') @patch('lms.views.EnrollUnitCode.get_code') @patch('fsm.models.FSMState.find_live_sessions') @patch('chat.models.Chat.objects.filter') def test_course_view(self, chatFilterMock, find_live_sessions, get_code, get_obj_or_404, ChatProgressSerializer): """ This test tests that: - query FSMState.find_live_sessions(request.user).filter(activity__course=course).first() return live session and this live session is present in page's context """ filter_mock = Mock() filter_mock.filter = Mock() find_live_sessions.return_value = filter_mock first_mock = Mock() filter_mock.filter.return_value = first_mock first_mock.first = Mock() first_mock.first.return_value = Mock() first_mock.first.return_value.id = 1 unit = Mock() unit.unit.get_exercises.return_value=[Mock()] course_mock = Mock() course_units = Mock() course_mock.get_course_units = course_units course_units.return_value = [unit] get_obj_or_404.return_value = course_mock chatFilterMock = Mock() chatFilterMock.return_value = [Mock()] ChatProgressSerializer.data.get.return_value = 0 response = self.client.get(reverse('lms:course_view', kwargs={'course_id': 1})) self.assertEqual(response.status_code, 200) self.assertEqual(filter_mock.filter.call_count, 1) self.assertEqual(first_mock.first.call_count, 1) self.assertEqual(get_obj_or_404.call_count, 1) self.assertTemplateUsed(response, 'lms/course_page.html') # context should contain these keys: course, liveSession, courslets self.assertIn('course', response.context) self.assertIn('liveSession', response.context) self.assertIn('courslets', response.context) def test_course_view_negative(self): """ This test tests case when teacher not yet (opened) joined live session and student opens course page. Student should not see 'Join Live Session' button on the top of the page. """ response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'lms/course_page.html') self.assertIn('course', response.context) self.assertIn('liveSession', response.context) self.assertIn('courslets', response.context) self.assertEqual(response.context['liveSession'], None) self.assertFalse(response.context['liveSession']) #TODO: write test when teacher really creates Course and Courslets inside of the course and student open page. #TODO: user should see 'Join' button. @patch('chat.models.Chat.get_spent_time') def test_live_chat_history_time_spent(self, get_spent_time): get_spent_time.return_value = datetime.timedelta(days=1, hours=1) response = self.client.get(reverse('lms:course_view', kwargs={'course_id': self.course.id})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'lms/course_page.html') self.assertIn('livesessions', response.context) self.assertNotEquals(response.context['livesessions'], []) self.assertEqual(response.context['livesessions'][0].get_formatted_time_spent(), '1 day, 1:00:00')
class TestCourseletViewHistoryTab(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy') def test_click_on_courslet_creates_new_chat(self): # test that there's no history yet response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertIsNotNone(list(response.context['courslets'])) self.assertEqual(response.status_code, 200) chats_count_1 = Chat.objects.all().count() # firstly call to chat:init_chat_api function with enroll_key and chat_id=0 response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) self.assertEqual(response.status_code, 200) chats_count_2 = Chat.objects.count() self.assertNotEqual(chats_count_2, chats_count_1) response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_3 = Chat.objects.count() response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_4 = Chat.objects.count() self.assertEqual(response.status_code, 200) self.assertEqual(chats_count_4, chats_count_2) self.assertEqual(chats_count_3, chats_count_2) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) chat = Chat.objects.all().first() # get chat and set state to None it means that courslet finished. chat.state = None chat.save() response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertEqual(Chat.objects.count(), chats_count_2) self.assertEqual(len(list(response.context['courslets'])), 1) def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 4)
def setUp(self): """ Preconditions. """ from chat.fsm_plugin.chat import get_specs from chat.fsm_plugin.additional import get_specs as get_specs_additional self.client = Client() self.user = User.objects.create_user('test', '*****@*****.**', 'test') get_specs()[0].save_graph(self.user.username) get_specs_additional()[0].save_graph(self.user.username) mocked_nonce = '135685044251684026041377608307' mocked_timestamp = '1234567890' mocked_decoded_signature = 'my_signature=' self.headers = { 'user_id': 1, 'lis_person_name_full': 'Test Username', 'lis_person_name_given': 'First', 'lis_person_name_family': 'Second', 'lis_person_contact_email_primary': '*****@*****.**', 'lis_person_sourcedid': 'Test_Username', 'oauth_callback': 'about:blank', 'launch_presentation_return_url': '', 'lti_message_type': 'basic-lti-launch-request', 'lti_version': 'LTI-1p0', 'roles': 'Student', 'context_id': 1, 'tool_consumer_info_product_family_code': 'moodle', 'context_title': 'Test title', 'tool_consumer_instance_guid': 'test.dot.com', 'resource_link_id': 'dfgsfhrybvrth', 'lis_result_sourcedid': 'wesgaegagrreg', 'oauth_nonce': mocked_nonce, 'oauth_timestamp': mocked_timestamp, 'oauth_consumer_key': 'consumer_key', 'oauth_signature_method': 'HMAC-SHA1', 'oauth_version': '1.0', 'oauth_signature': mocked_decoded_signature } self.unit = Unit(title='Test title', addedBy=self.user) self.unit.save() self.course = Course(title='Test title', description='test description', access='Public', enrollCode='111', lockout='222', addedBy=self.user) self.course.save() self.course_ref = CourseRef( course=self.course, context_id=self.headers.get('context_id'), tc_guid=self.headers.get('tool_consumer_instance_guid')) self.course_ref.save() self.course_ref.instructors.add(self.user) self.role1 = Role( role=Role.ENROLLED, user=self.user, course=self.course, ) self.role1.save() self.courseunit = CourseUnit(unit=self.unit, course=self.course, order=0, addedBy=self.user, releaseTime=timezone.now()) self.courseunit.save() lesson = Lesson(title='title', text='text', addedBy=self.user) lesson.save() unitlesson = UnitLesson(unit=self.unit, order=0, lesson=lesson, addedBy=self.user, treeID=lesson.id) unitlesson.save() self.lti_consumer = LtiConsumer(consumer_name='test', consumer_key='consumer_key', consumer_secret='test_key') self.lti_consumer.save()