def test_lesson_loggedin_and_enrolled_on_course(self): user = User.objects.get(pk=3) # gaby in fixture self.client.login(username=user.username, password='******') l1 = Lesson.objects.get(pk=1) url1 = '/courses/{0}/lesson/{1}/'.format(l1.course.pk, l1.pk) # Enrol the user on the course uc = UserCourse(user=user, course=l1.course) uc.save() # Visit the page response = self.client.get(url1) self.assertIn('attachments', response.context, \ "Missing template var: attachments") self.assertEqual(response.status_code, 200) hist = response.context['history'].pop() self.assertIsInstance(hist[0], datetime, "Problem with lesson history timestamp") self.assertEqual(hist[1], 'VISITING', "Problem with lesson history activity") self.assertNotEqual(response.context['ul'], None, "There should be a userlesson - authenticated") #see that lesson complete button works response = self.client.post(url1, {'lesson_complete': 'Complete'}) self.assertIn('lesson_reopen', response.content) self.assertEqual(response.context['ul'].completed, True) #see that lesson reopen button works response = self.client.post(url1, {'lesson_reopen': 'Re-open'}) self.assertIn('lesson_complete', response.content) self.assertEqual(response.context['ul'].completed, False)
def test_lesson_loggedin_and_enrolled_on_course(self): user = User.objects.get(pk=3) # gaby in fixture self.client.login(username=user.username, password='******') l1 = Lesson.objects.get(pk=1) url1 = '/courses/{0}/lesson/{1}/'.format(l1.course.pk, l1.pk) # Enrol the user on the course uc = UserCourse(user=user, course=l1.course) uc.save() # Visit the page response = self.client.get(url1) self.assertIn('attachments', response.context, \ "Missing template var: attachments") self.assertEqual(response.status_code, 200) hist = response.context['history'].pop() self.assertIsInstance(hist[0], datetime, "Problem with lesson history timestamp") self.assertEqual(hist[1], 'VISITING', "Problem with lesson history activity") self.assertNotEqual(response.context['ul'], None, "There should be a userlesson - authenticated") #see that lesson complete button works response = self.client.post(url1, {'lesson_complete':'Complete'}) self.assertIn('lesson_reopen', response.content) self.assertEqual(response.context['ul'].completed, True) #see that lesson reopen button works response = self.client.post(url1, {'lesson_reopen':'Re-open'}) self.assertIn('lesson_complete', response.content) self.assertEqual(response.context['ul'].completed, False)
class AttachmentViewTests(TestCase): """Test views for user interaction with attachments""" course1_data = { 'code': 'EDU02', 'name': 'A Course of Leeches', 'abstract': 'Learn practical benefits of leeches', } lesson1_data = { 'name': 'Introduction to Music', 'abstract': 'A summary of what we cover', } att1_data = { 'name': 'Reading List', 'desc': 'Useful stuff you might need', 'seq': 3, 'attachment': 'empty_attachment_test.txt', } att2_data = { 'name': 'Grammar Guide', 'desc': 'How do you even spell grammer?', 'seq': 2, 'attachment': 'empty_attachment_test.txt', } def setUp(self): self.user1 = User.objects.create_user( 'bertie', '*****@*****.**', 'bertword') self.user1.is_active = True self.user1.save() self.user2 = User.objects.create_user('dave', '*****@*****.**', 'dave') self.user2.is_active = True self.user2.save() self.course1 = Course(**self.course1_data) self.course1.organiser = self.user1 self.course1.instructor = self.user1 self.course1.save() self.lesson1 = Lesson(course=self.course1, **self.lesson1_data) self.lesson1.save() self.uc = UserCourse(course=self.course1, user=self.user2) self.uc.save() self.att1 = Attachment(course=self.course1, **self.att1_data) self.att2 = Attachment(lesson=self.lesson1, **self.att2_data) self.att1.save() self.att2.save() def test_view_metadata(self): """Verify that the relevant metadata get rendered""" response = self.client.get(self.att1.get_metadata_url()) self.assertEqual(response.status_code, 200) self.assertTrue(x in response.context for x in ['attachment']) self.assertIn("Reading List", response.content, u"detail missing from response")
def test_course_enrol_page_status_auth_enrolled(self): """An authenticated and enrolled user status is passed to template""" self.client.login(username='******', password='******') #Enrol the user on the course (bertie is not organiser) uc = UserCourse(user=self.user1, course=self.course3) uc.save() response = self.client.get('/courses/3/enrol/') self.assertEqual('auth_enrolled', response.context['status'], "Registration status should be auth_enrolled")
def setUp(self): self.user1 = User.objects.create_user( 'bertie', '*****@*****.**', 'bertword') self.user1.is_active = True self.user1.save() self.user2 = User.objects.create_user('dave', '*****@*****.**', 'dave') self.user2.is_active = True self.user2.save() self.course1 = Course(**self.course1_data) self.course1.organiser = self.user1 self.course1.instructor = self.user1 self.course1.save() self.lesson1 = Lesson(course=self.course1, **self.lesson1_data) self.lesson1.save() self.uc = UserCourse(course=self.course1, user=self.user2) self.uc.save() self.att1 = Attachment(course=self.course1, **self.att1_data) self.att2 = Attachment(lesson=self.lesson1, **self.att2_data) self.att1.save() self.att2.save()
def setUp(self): self.user1 = User.objects.create_user("bertie", "*****@*****.**", "bertword") self.user1.is_active = True self.user1.save() self.user2 = User.objects.create_user("dave", "*****@*****.**", "dave") self.user2.is_active = True self.user2.save() self.course1 = Course(**self.course1_data) self.course1.instructor = self.user1 self.course1.organiser = self.user1 self.course1.save() self.lesson1 = Lesson(course=self.course1, **self.lesson1_data) self.lesson1.save() # att1 attached to course self.att1 = Attachment(course=self.course1, **self.att1_data) self.att1.save() # att2 attached to lesson self.att2 = Attachment(lesson=self.lesson1, **self.att1_data) self.att2.save() self.uc = UserCourse(course=self.course1, user=self.user2) self.uc.save()
class AttachmentModelTests(TestCase): """Test models user interaction with courses""" course1_data = {"code": "EDU01", "name": "A Course of Leeches", "abstract": "Learn practical benefits of leeches"} lesson1_data = {"name": "Introduction to Music", "abstract": "A summary of what we cover"} att1_data = { "name": "Reading List", "desc": "Useful stuff you might need", "seq": 3, "attachment": "empty_attachment_test.txt", } att2_data = { "name": "Grammar Guide", "desc": "How do you even spell grammer?", "seq": 2, "attachment": "empty_attachment_test.txt", } def setUp(self): self.user1 = User.objects.create_user("bertie", "*****@*****.**", "bertword") self.user1.is_active = True self.user1.save() self.user2 = User.objects.create_user("dave", "*****@*****.**", "dave") self.user2.is_active = True self.user2.save() self.course1 = Course(**self.course1_data) self.course1.instructor = self.user1 self.course1.organiser = self.user1 self.course1.save() self.lesson1 = Lesson(course=self.course1, **self.lesson1_data) self.lesson1.save() # att1 attached to course self.att1 = Attachment(course=self.course1, **self.att1_data) self.att1.save() # att2 attached to lesson self.att2 = Attachment(lesson=self.lesson1, **self.att1_data) self.att2.save() self.uc = UserCourse(course=self.course1, user=self.user2) self.uc.save() def test_checkrep(self): """Test the internal representation checker with attachments""" self.assertTrue(self.uc._checkrep(), "New attachment checkrep failed") def test___str__(self): """Test that the desired info is in the __str__ method""" s = self.att1.__str__() target = u"Attachment %s, '%s...'" % (self.att1.pk, self.att1.name[:10]) self.assertEqual(s, target, "Incorrect __str__ return") def test___unicode__(self): """Test that the desired info is in the unicode method""" unicod = self.att1.__unicode__() target = u"Att. ID:%s, '%s...'" % (self.att1.pk, self.att1.name[:10]) self.assertEqual(unicod, target, "Incorrect __unicode__ return") def test_get_absolute_url(self): """Test the correct url is returned""" url = self.att1.get_absolute_url() target = self.att1.attachment self.assertEqual(target.url, url, "attachment URL error") def test_get_metadata_url(self): """Test that the correct metadata url is returned""" url = self.att1.get_metadata_url() target = u"/attachment/%s/metadata/" % self.att1.pk self.assertEqual(target, url, "attachment metadata URL error")
def detail(request, course_id): """View for detail of a single course""" logger.debug('Course id=' + str(course_id) + ' view') course = get_object_or_404(Course, pk=course_id) course_type = ContentType.objects.get_for_model(Course) priced_item = get_object_or_404(PricedItem, content_type_id=course_type.id, object_id=course_id) fee_value = priced_item.fee_value user_can_edit = False history = None lessons = None attachments = None context = { 'course': course, 'fee_value': fee_value, 'fee_value_cents': fee_value * 100, 'currency': priced_item.currency, } if request.user.is_authenticated(): try: uc = request.user.usercourse_set.get(course__id=course_id) status = 'auth_enrolled' except ObjectDoesNotExist: uc = None if (request.user == course.organiser or request.user == course.instructor): status = 'auth_bar_enrol' user_can_edit = True else: status = 'auth_not_enrolled' else: uc = None status = 'noauth' if request.method == 'POST': if 'stripeToken' in request.POST and status == 'auth_not_enrolled': stripe.api_key = settings.STRIPE_SECRET_KEY token = request.POST['stripeToken'] try: charge = stripe.Charge.create( amount=int(fee_value * 100), # amount in cents, again currency=priced_item.currency, source=token, description="Example charge") current_time = datetime.utcnow().replace(tzinfo=timezone.utc) #Now record payment payment = Payment(content_object=course, paying_user=request.user, fee_value=fee_value, currency=priced_item.currency, datestamp=current_time) payment.save() #Enrol the student uc = UserCourse(user=request.user, course=course) uc.save() status = 'auth_enrolled' logger.info(str(uc) + 'enrols') except stripe.error.CardError, e: pass if 'course_enrol' in request.POST: course_type = ContentType.objects.get_for_model(course) if not (fee_value == 0): payment = Payment.objects.get(content_type__pk=course_type.id, object_id=course.id, paying_user=request.user) uc = UserCourse(user=request.user, course=course) uc.save() status = 'auth_enrolled' logger.info(str(uc) + 'enrols') if 'course_complete' in request.POST and status == 'auth_enrolled': if uc.active: uc.complete() logger.info(str(uc) + 'completes') else: logger.error("Can't complete course, reason: not active") if 'course_withdraw' in request.POST and status == 'auth_enrolled': if uc.active: uc.withdraw() logger.info(str(uc) + 'withdraws') else: logger.error("Can't withdraw, reason: not active") if 'course_reopen' in request.POST and status == 'auth_enrolled': if not uc.active: uc.reopen() logger.info(str(uc) + 'reopens') else: logger.error("Can't reopen, reason: already active")
def enrol(request, course_id): """View to encourage enrolment. Suggest user enrols when non-enrolled user tries to access certain resources. Course instructor or organiser gets error message""" course = get_object_or_404(Course, pk=course_id) course_type = ContentType.objects.get_for_model(Course) priced_item = get_object_or_404(PricedItem, content_type_id=course_type.id, object_id=course_id) fee_value = priced_item.fee_value if request.user.is_authenticated(): try: uc = request.user.usercourse_set.get(course__id=course_id) status = 'auth_enrolled' except ObjectDoesNotExist: uc = None if (request.user == course.organiser or request.user == course.instructor): status = 'auth_bar_enrol' user_can_edit = True else: status = 'auth_not_enrolled' else: uc = None status = 'noauth' if request.method == 'POST': if 'stripeToken' in request.POST and status == 'auth_not_enrolled': stripe.api_key = settings.STRIPE_SECRET_KEY token = request.POST['stripeToken'] try: charge = stripe.Charge.create( amount=int(fee_value * 100), # amount in cents, again currency=priced_item.currency, source=token, description="Example charge") current_time = datetime.utcnow().replace(tzinfo=timezone.utc) #Now record payment payment = Payment(content_object=course, paying_user=request.user, fee_value=fee_value, currency=priced_item.currency, datestamp=current_time) payment.save() #Enrol the student uc = UserCourse(user=request.user, course=course) uc.save() status = 'auth_enrolled' logger.info(str(uc) + 'enrols') except stripe.error.CardError, e: pass if 'course_enrol' in request.POST: course_type = ContentType.objects.get_for_model(course) if not (fee_value == 0): payment = Payment.objects.get(content_type__pk=course_type.id, object_id=course.id, paying_user=request.user) uc = UserCourse(user=request.user, course=course) uc.save() status = 'auth_enrolled' logger.info(str(uc) + 'enrols')
def test_learning_intention(self): """Test view of a single learning intention""" les1 = self.lesson1.id lint1 = self.learningintention1.id url1 = "/lesson/{0}/lint/{1}/".format(les1,lint1) response = self.client.get(url1) self.assertEqual(response.status_code, 200) self.assertTrue(x in response.context for x in ['lesson_id', 'lesson_intention_id']) self.assertIn("Choose Topaz", response.content, "SC missing") self.assertIn("Calculate 6*9", response.content, "LO missing") cycle1 = "cycle{0}".format(self.lid1.id) cycle2 = "cycle{0}".format(self.lid2.id) self.assertIn(cycle1, response.content, "Cycle button missing") self.assertIn(cycle2, response.content, "Cycle button missing") #test non-existing LI response = self.client.get('/lesson/1/lint/5000/') self.assertEqual(response.status_code, 404) #test not logged in response = self.client.get(url1) self.assertNotIn('progressSC', response.context) ### Success Criteria Cycle Tests #press some buttons and see what happens self.client.login(username='******', password='******') #Register user on course first: uc = UserCourse(course=self.course1, user=self.user1) uc.save() #cycle to amber response = self.client.post(url1, {cycle1:'Cycle'}) self.assertEqual(response.status_code, 200) trafficlight = response.context['usc_list'][0][2].condition self.assertEqual(trafficlight, 1) self.assertInHTML( "<img id='id_SC1' class='tl-amber' "\ "src='/static/images/img_trans.png'>", response.content) self.assertContains( response, '<li class="criterion" data-id="1">') self.assertIn( '<li class="learning_outcome" data-id="3">', response.content) self.assertEqual( response.context['progressSC'], (0,2,2,100)) #progress bar self.assertEqual( response.context['progressLO'], (0,1,1,100)) #progress bar #cycle to green response = self.client.post(url1, {cycle1:'Cycle'}) self.assertEqual(response.status_code, 200) trafficlight = response.context['usc_list'][0][2].condition self.assertEqual(trafficlight, 2) self.assertInHTML( "<img id='id_SC1' class='tl-green' "\ "src='/static/images/img_trans.png'>", response.content) self.assertEqual( response.context['progressSC'], (1,1,2,100)) #progress bar self.assertEqual( response.context['progressLO'], (0,1,1,100)) #progress bar #cycle to red response = self.client.post(url1, {cycle1:'Cycle'}) self.assertEqual(response.status_code, 200) trafficlight = response.context['usc_list'][0][2].condition self.assertEqual(trafficlight, 0) self.assertInHTML( "<img id='id_SC1' class='tl-red' "\ "src='/static/images/img_trans.png'>", response.content) self.assertEqual( response.context['progressSC'], (0,2,2,100)) #progress bar self.assertEqual( response.context['progressLO'], (0,1,1,100)) #progress bar
class AttachmentModelTests(TestCase): """Test models user interaction with courses""" course1_data = { 'code': 'EDU01', 'name': 'A Course of Leeches', 'abstract': 'Learn practical benefits of leeches', } lesson1_data = { 'name': 'Introduction to Music', 'abstract': 'A summary of what we cover', } att1_data = { 'name': 'Reading List', 'desc': 'Useful stuff you might need', 'seq': 3, 'attachment': 'empty_attachment_test.txt', } att2_data = { 'name': 'Grammar Guide', 'desc': 'How do you even spell grammer?', 'seq': 2, 'attachment': 'empty_attachment_test.txt', } def setUp(self): self.user1 = User.objects.create_user( 'bertie', '*****@*****.**', 'bertword') self.user1.is_active = True self.user1.save() self.user2 = User.objects.create_user('dave', '*****@*****.**', 'dave') self.user2.is_active = True self.user2.save() self.course1 = Course(**self.course1_data) self.course1.instructor = self.user1 self.course1.organiser = self.user1 self.course1.save() self.lesson1 = Lesson(course=self.course1, **self.lesson1_data) self.lesson1.save() #att1 attached to course self.att1 = Attachment(course=self.course1, **self.att1_data) self.att1.save() #att2 attached to lesson self.att2 = Attachment(lesson=self.lesson1, **self.att1_data) self.att2.save() self.uc = UserCourse(course=self.course1, user=self.user2) self.uc.save() def test_checkrep(self): """Test the internal representation checker with attachments""" self.assertTrue(self.uc._checkrep(), "New attachment checkrep failed") def test___str__(self): """Test that the desired info is in the __str__ method""" s = self.att1.__str__() target = u"Attachment %s, '%s...'" % (self.att1.pk, self.att1.name[:10]) self.assertEqual(s, target, "Incorrect __str__ return") def test___unicode__(self): """Test that the desired info is in the unicode method""" unicod = self.att1.__unicode__() target = u"Att. ID:%s, '%s...'" % (self.att1.pk, self.att1.name[:10]) self.assertEqual(unicod, target, "Incorrect __unicode__ return") def test_get_absolute_url(self): """Test the correct url is returned""" url = self.att1.get_absolute_url() target = self.att1.attachment self.assertEqual(target.url, url, "attachment URL error") def test_get_metadata_url(self): """Test that the correct metadata url is returned""" url = self.att1.get_metadata_url() target = u"/attachment/%s/metadata/" % self.att1.pk self.assertEqual(target, url, "attachment metadata URL error")