def test_POST_course_enrol_context_has_paid(self):
        """POSTing form course_enrol reloads with proper context variables"""

        #create payment record for gaby on course 1
        #payment ultimately should obtain its information from a PricedItem
        course1 = Course.objects.get(pk=1)
        user = User.objects.get(pk=3)  #Gaby
        priced_item = PricedItem.objects.get(object_id=course1.id)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=course1,
            fee_value=priced_item.fee_value,
            datestamp=current_time,
        )
        payment.save()

        #presence of payment record should permit enrolment
        self.client.login(username='******', password='******')
        form_data = {'course_enrol': 'Enrol'}
        response = self.client.post('/courses/1/', form_data)
        self.assertIn('uc', response.context)
        self.assertIsNotNone(response.context['lessons'])
        self.assertIsNotNone(response.context['history'])
        self.assertEqual(response.context['course'].pk, 1)
        self.assertEqual(response.context['status'], 'auth_enrolled')
        self.assertFalse(response.context['user_can_edit'])
        self.assertEqual(response.context['uc'].active, True)
        self.assertEqual(response.context['uc'].withdrawn, False)
        self.assertEqual(response.context['uc'].completed, False)
    def test_POST_course_enrol_context_has_paid(self):
        """POSTing form course_enrol reloads with proper context variables"""
        
        #create payment record for gaby on course 1
        #payment ultimately should obtain its information from a PricedItem
        course1 = Course.objects.get(pk=1)
        user = User.objects.get(pk=3)   #Gaby
        priced_item = PricedItem.objects.get(object_id=course1.id)   
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=course1,
            fee_value=priced_item.fee_value,
            datestamp=current_time,
        )
        payment.save()

        #presence of payment record should permit enrolment
        self.client.login(username='******', password='******')
        form_data = {'course_enrol':'Enrol'}
        response = self.client.post('/courses/1/', form_data)
        self.assertIn('uc', response.context)
        self.assertIsNotNone(response.context['lessons'])
        self.assertIsNotNone(response.context['history'])
        self.assertEqual(response.context['course'].pk, 1)
        self.assertEqual(response.context['status'], 'auth_enrolled')
        self.assertFalse(response.context['user_can_edit'])
        self.assertEqual(response.context['uc'].active, True)        
        self.assertEqual(response.context['uc'].withdrawn, False)        
        self.assertEqual(response.context['uc'].completed, False)        
    def test_POST_course_enrol_correct_template(self):
        """POSTing form course_enrol reloads page with correct template"""
        #create payment record for gaby on course 1
        #payment ultimately should obtain its information from a PricedItem
        course1 = Course.objects.get(pk=1)
        user = User.objects.get(pk=3)  #Gaby
        priced_item = PricedItem.objects.get(object_id=course1.id)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=course1,
            fee_value=priced_item.fee_value,
            datestamp=current_time,
        )
        payment.save()

        self.client.login(username='******', password='******')
        form_data = {'course_enrol': 'Enrol'}
        response = self.client.post('/courses/1/', form_data)
        self.assertTemplateUsed(response, 'courses/course_detail.html')
    def test_POST_course_enrol_correct_template(self):
        """POSTing form course_enrol reloads page with correct template"""
        #create payment record for gaby on course 1
        #payment ultimately should obtain its information from a PricedItem
        course1 = Course.objects.get(pk=1)
        user = User.objects.get(pk=3)   #Gaby
        priced_item = PricedItem.objects.get(object_id=course1.id)   
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=course1,
            fee_value=priced_item.fee_value,
            datestamp=current_time,
        )
        payment.save()

        self.client.login(username='******', password='******')
        form_data = {'course_enrol':'Enrol'}
        response = self.client.post('/courses/1/', form_data)
        self.assertTemplateUsed(response, 'courses/course_detail.html')
Beispiel #5
0
    def test__user_can_view_lesson_Checks_paid_status(self):
        lesson = Lesson.objects.get(pk=2)
        lesson_url = lesson.get_absolute_url

        # Gaby hasn't paid
        user = User.objects.get(pk=3)  # gaby in fixture
        self.client.login(username=user.username, password='******')
        self.assertFalse(_user_can_view_lesson(user, lesson))

        # Urvasi has paid
        user = User.objects.get(pk=4)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1,
            datestamp=current_time,
        )
        payment.save()
        self.client.login(username=user.username, password='******')
        self.assertTrue(_user_can_view_lesson(user, lesson))
Beispiel #6
0
    def test__user_can_view_lesson_Checks_paid_status(self):
        lesson = Lesson.objects.get(pk=2)
        lesson_url = lesson.get_absolute_url

        # Gaby hasn't paid
        user = User.objects.get(pk=3) # gaby in fixture
        self.client.login(username=user.username, password='******')        
        self.assertFalse(_user_can_view_lesson(user, lesson))

        # Urvasi has paid
        user = User.objects.get(pk=4)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1,
            datestamp=current_time,
        )
        payment.save()
        self.client.login(username=user.username, password='******')        
        self.assertTrue(_user_can_view_lesson(user, lesson))
 def test_can_access_lesson_2_when_enrolled(self):
     course = Course.objects.get(pk=3)
     user = User.objects.get(pk=1)
     lesson2 = course.lesson_set.first().get_next()
     self.client.login(username='******', password='******')
     priced_item = PricedItem.objects.get(object_id=course.id)
     current_time = datetime.utcnow().replace(tzinfo=utc)
     payment = Payment(
         paying_user=user,
         content_object=course,
         fee_value=priced_item.fee_value,
         datestamp=current_time,
     ).save()
     response = self.client.get(lesson2.get_absolute_url())
     self.assertEqual(response.status_code, 200)
    def test_POST_course_enrol_no_enrol_buttons(self):
        #create payment record for gaby on course 1
        #payment ultimately should obtain its information from a PricedItem
        course1 = Course.objects.get(pk=1)
        user = User.objects.get(pk=3)  #Gaby
        priced_item = PricedItem.objects.get(object_id=course1.id)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=course1,
            fee_value=priced_item.fee_value,
            datestamp=current_time,
        ).save()

        self.client.login(username='******', password='******')
        form_data = {'course_enrol': 'Enrol'}
        response = self.client.post('/courses/1/', form_data)
        self.assertNotIn('id_enrol_button', response.content)
        self.assertNotIn('id_enrol_button2', response.content)
Beispiel #9
0
 def test_lesson_visit_requires_payment(self):
     lesson = Lesson.objects.get(pk=2)
     lesson_url = lesson.get_absolute_url()
     course_url = lesson.course.get_absolute_url()
     # Gaby hasn't paid
     user = User.objects.get(pk=3)  # gaby in fixture
     self.client.login(username=user.username, password='******')
     response = self.client.get(lesson_url)
     self.assertRedirects(response, course_url)
     # Urvasi has paid
     user = User.objects.get(pk=4)
     current_time = datetime.utcnow().replace(tzinfo=utc)
     payment = Payment(
         paying_user=user,
         content_object=lesson.course,
         fee_value=1,
         datestamp=current_time,
     ).save()
     self.client.login(username=user.username, password='******')
     response = self.client.get(lesson_url)
     self.assertEqual(response.status_code, 200)
Beispiel #10
0
    def test__user_can_view_lesson_Returns_true_if_duplicate_payments(self):
        """Duplicate payments shouldn't occur, but if they do the user
        should be able to view the lesson"""

        lesson = Lesson.objects.get(pk=2)
        # Urvasi has paid twice
        user = User.objects.get(pk=4)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1,
            datestamp=current_time,
        )
        payment.save()
        payment2 = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1.1,
            datestamp=current_time,
        )
        payment2.save()
        self.client.login(username=user.username, password='******')
        self.assertTrue(_user_can_view_lesson(user, lesson))
Beispiel #11
0
    def test__user_can_view_lesson_Returns_true_if_duplicate_payments(self):
        """Duplicate payments shouldn't occur, but if they do the user
        should be able to view the lesson"""

        lesson = Lesson.objects.get(pk=2)
        # Urvasi has paid twice
        user = User.objects.get(pk=4)
        current_time = datetime.utcnow().replace(tzinfo=utc)
        payment = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1,
            datestamp=current_time,
        )
        payment.save()
        payment2 = Payment(
            paying_user=user,
            content_object=lesson.course,
            fee_value=1.1,
            datestamp=current_time,
        )
        payment2.save()
        self.client.login(username=user.username, password='******')        
        self.assertTrue(_user_can_view_lesson(user, lesson))
Beispiel #12
0
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")
Beispiel #13
0
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')