예제 #1
0
def test_12(test_client):
    with LoginContext(test_client, 'visitor'):
        # Test an invalid class ID.
        get_valid(test_client, PAYMENT_URL + '?classId=x', b'Select a class', follow_redirects=True)
        get_valid(test_client, PAYMENT_URL + '?foo=bar', b'Select a class', follow_redirects=True)

        # Test a valid class ID.
        classId1 = db.session.query(Class_.id).filter(Class_.name == 'test_class').scalar()
        rv = get_valid(test_client, PAYMENT_URL + '?classId={}'.format(classId1), b'Payment')
        assert b'A test book' in rv.data
        assert b'test_class' in rv.data
        assert b'$10.00' in rv.data

        # Test a class with a specified price.
        classId2 = db.session.query(Class_.id).filter(Class_.name == 'Another test class').scalar()
        rv = get_valid(test_client, PAYMENT_URL + '?classId={}'.format(classId2), b'Payment')
        assert b'A test book' in rv.data
        assert b'Another test class' in rv.data
        assert b'$5.00' in rv.data

        # Test a purchase of an invalid class code.
        stripe_post(test_client, 'x', b'Select a class')

        # Test a purchase with missing fields.
        stripeToken = create_stripe_token()
        rv = test_client.post(
            PAYMENT_URL,
            data=dict(
                stripeToken=stripeToken.id,
            ),
            follow_redirects=True
        )
        assert rv.status_code == 400

        # Test a purchase with various card declined errors. See https://stripe.com/docs/testing#cards-responses.
        for card_num in (
            # Charge is declined with a card_declined code.
            '4000000000000002',
            # Charge is declined with a card_declined code and a fraudulent reason.
            '4100000000000019',
            # Charge is declined with an incorrect_cvc code.
            '4000000000000127',
            # Charge is declined with an expired_card code.
            '4000000000000069',
            # Charge is declined with a processing_error code.
            '4000000000000119',
            # Skip: Charge is declined with an incorrect_number code as the card number fails the Luhn check. This raises an error in ``create_stripe_token`` (test code).
            #'4242424242424241',
        ):
            stripe_post(test_client, classId1, b'Charge declined', create_stripe_token(card_num))

        # Test a valid purchase with no Payment beforehand.
        visitorUserId = make_user('visitor', UserType.user).id
        payment = db.session.query(Payment).filter(Payment.user_id == visitorUserId)
        assert payment.one_or_none() is None
        stripe_post(test_client, classId1, b'You have purchased A test book. Thank you for your payment of $10.00.')
        payment = db.session.query(Payment).filter(Payment.user_id == visitorUserId).scalar()
        assert payment is not None
        db.session.delete(payment)
        db.session.commit()

        # Test a valid purchase where the class specifies the price.
        payment = db.session.query(Payment).filter(Payment.user_id == visitorUserId)
        assert payment.one_or_none() is None
        stripe_post(test_client, classId2, b'You have purchased A test book. Thank you for your payment of $5.00.')
        payment = db.session.query(Payment).filter(Payment.user_id == visitorUserId).scalar()
        assert payment is not None
        db.session.delete(payment)
        db.session.commit()

    # Verify that we can't pay twice (student) or can't pay (instructor, author).
    for user in siaa_users:
        with LoginContext(test_client, user):
            # Try via a get request.
            rv = get_valid(
                test_client, PAYMENT_URL + '?classId={}'.format(classId1),
                b'You have already paid for this class.', follow_redirects=True
            )

            # Try via a post request.
            stripe_post(test_client, classId1, b'You have already paid for this class.')
예제 #2
0
def create_test_data():
    visitor = make_user('visitor', UserType.user)
    student = make_user('student', UserType.user)
    instructor = make_user('instructor', UserType.user)
    author = make_user('author', UserType.author)
    admin = make_user('admin', UserType.admin)
    if not db.session.query(Book).filter(Book.url == 'book').all():
        class_ = Class_(
            name='test_class',
            instructor=[Instructor(
               user_id=instructor.id)
            ]
        )
        book = Book(
            url='book',
            title='A test book',
            price=1000,
            page=[
                Page(
                    url='unsigned_8-_and_16-bit_ops/introduction.s.html',
                    index=1,
                    question=[
                        Question(
                            label='define_label',
                            index=1,
                            pointsPossible=2,
                            answer=[
                                Answer(
                                    user_id=student.id,
                                    string='foo:',
                                    points=2,
                                ),
                            ],
                            feedback=[
                                Feedback(
                                    answer='foo:',
                                    feedback='correct',
                                    points=2
                                ), Feedback(
                                    answer='',
                                    feedback='Wrong.',
                                    points=0,
                                ),
                            ],
                        ), Question(
                            label='comment',
                            index=2,
                            pointsPossible=1,
                            answer=[
                                Answer(
                                    user_id=student.id,
                                    string='10000',
                                    points=1,
                                ),
                            ],
                        ), Question(
                            label='mov_instruction',
                            index=3,
                            pointsPossible=1
                        ),
                    ],
                ),
            ],
            class_=[
                class_,
                Class_(
                   name='Another test class',
                   price=500,
                ),
            ],
            author=[
                Author(user_id=author.id),
            ],
        )
        db.session.add(book)
    else:
        book = db.session.query(Book).filter(Book.url == 'book').one()
        class_ = db.session.query(Class_).filter(Class_.id == book.id).filter(Class_.name == 'test_class').one()

    if not student.payment.all():
        student.payment = [Payment(
            class_id=class_.id,
            charge_id='xxx'
        )]
    db.session.commit()