Exemple #1
0
    def test_existing_user_cancel_subscription(self, mocked_date):
        user = self._make_user(
            email='*****@*****.**',
            groups=[
                Group.by_name('enabled'),
                Group.by_name('trial'),
                Group.by_name('monthly'),
            ])
        mocked_date.today.return_value = date(2013, 12, 30)

        view = IPNView(testing.DummyRequest())
        view.provider = 'clickbank'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'trans_type': 'RFND',
            'trans_id': 123,
            'product_id': 1,
        })
        resp = view.ipn()
        self.assertEqual(resp, 'Done.')
        self.assertEqual(user.enabled, False)
        self.assertEqual(user.valid_to, date(2013, 12, 30))

        self.assertEqual(len(user.audit_log_entries), 1)
        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserDisabled')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Disabled by clickbank, transaction id: 123, type: RFND, note: '
            u'removed from groups: enabled, trial, monthly',
        )
Exemple #2
0
    def test_existing_user_addon_cancel(self, mocked_date):
        user = self._make_user()
        user.groups.append(self.ipn_group)
        self.ipn_group.name = 'foo'
        self.ipn_group.addon = True
        mocked_date.today.return_value = date(2013, 12, 30)

        view = IPNView(testing.DummyRequest())
        view.provider = 'jvzoo'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'trans_type': 'RFND',
            'trans_id': 123,
            'product_id': 1,
        })

        resp = view.ipn()
        self.assertEqual(resp, 'Done.')

        # nothing should change for user's main subscription
        self.assertEqual(user.enabled, True)
        self.assertEqual([g.name for g in user.groups], ['enabled', ])

        self.assertEqual(len(user.audit_log_entries), 1)
        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserDisabled')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Addon "foo" disabled by jvzoo, transaction id: 123, type: RFND, '
            u'note: removed from groups: foo',
        )
Exemple #3
0
    def test_existing_trial_user_new_subscription_payment(
        self, mocked_date, user=None
    ):
        if not user:
            user = self._make_user(
                email='*****@*****.**',
                groups=[Group.by_name('enabled'), Group.by_name('trial')],
            )

        mocked_date.today.return_value = date(2013, 12, 30)
        view = IPNView(testing.DummyRequest())
        view.provider = 'jvzoo'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'fullname': u'Föo Bar',
            'trans_type': 'BILL',
            'trans_id': 123,
            'product_id': 1,
        })

        resp = view.ipn()
        self.assertEqual(resp, 'Done.')
        self.assertEqual(user.enabled, True)
        self.assertEqual(user.trial, False)
        self.assertEqual(user.valid_to, date(2014, 1, 30))
        self.assertEqual(user.last_payment, date(2013, 12, 30))

        self.assertEqual(len(user.audit_log_entries), 1)
        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserEnabled')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Enabled by jvzoo, transaction id: 123, type: BILL, note: '
            u'regular until 2014-01-30',
        )
Exemple #4
0
    def test_forward_ipn_url(
        self,
        Group,
        User,
        parse_request,
        ipn_transaction,
        UserDisabled,
        request_post,
    ):
        parse_request.return_value = None
        ipn_transaction.return_value = None
        group_mock = mock.Mock()
        group_mock.name = 'test'
        group_mock.forward_ipn_to_url = 'http://www.example.com'
        Group.by_product_id.return_value = group_mock
        User.by_email.return_value = mock.Mock(groups=[group_mock, ])
        request = testing.DummyRequest(post={'foo': 'bar'})
        request.registry = mock.Mock()
        request.registry.settings = {}

        view = IPNView(request)
        view.params = AttrDict({
            'email': '*****@*****.**',
            'product_id': 123,
        })

        view.ipn()
        request_post.assert_called_with(
            'http://www.example.com',
            params={'foo': 'bar'},
        )
Exemple #5
0
    def test_existing_user_billing_email_and_rejoin(self, mocked_date):
        user = self._make_user(billing_email='*****@*****.**', enabled=False)
        mocked_date.today.return_value = date(2013, 12, 30)

        view = IPNView(testing.DummyRequest())
        view.provider = 'jvzoo'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'trans_type': 'SALE',
            'trans_id': 123,
            'product_id': 1,
        })

        resp = view.ipn()
        self.assertEqual(resp, 'Done.')
        self.assertEqual(user.enabled, True)
        self.assertEqual(user.trial, True)
        self.assertEqual(user.valid_to, date(2014, 1, 6))
        self.assertEqual(user.last_payment, date(2013, 12, 30))

        self.assertEqual(len(user.audit_log_entries), 1)
        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserEnabled')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Enabled by jvzoo, transaction id: 123, type: SALE, '
            u'note: trial until 2014-01-06',
        )
Exemple #6
0
 def test_invalid_decryption(self):
     request = testing.DummyRequest()
     request.json_body = {
         'iv': '27CD0D0CA9379D32'.encode('base64'),
         'notification': 'bar4567890123456'.encode('base64'),
     }
     view = IPNView(request)
     with self.assertRaises(ValueError) as cm:
         view.clickbank()
     self.assertIn('Decryption failed: ', cm.exception.message)
Exemple #7
0
    def test_products_to_ignore(self):
        request = testing.DummyRequest()
        request.registry.settings['bimt.products_to_ignore'] = '123,456'
        view = IPNView(request)
        view.params = AttrDict({
            'email': '*****@*****.**',
            'product_id': '123',
        })

        self.assertEqual(view.ipn(), 'Done.')
Exemple #8
0
 def test_missing_cverify(self):
     post = {
         'foo': 'bar',
     }
     view = IPNView(testing.DummyRequest(post=post))
     with self.assertRaises(KeyError) as cm:
         view.jvzoo()
     self.assertEqual(
         repr(cm.exception),
         'KeyError(\'cverify\',)',
     )
Exemple #9
0
 def test_invalid_checksum(self):
     post = {
         'cverify': 'foo',
     }
     view = IPNView(testing.DummyRequest(post=post))
     with self.assertRaises(ValueError) as cm:
         view.jvzoo()
     self.assertEqual(
         str(cm.exception),
         'Checksum verification failed',
     )
Exemple #10
0
    def test_invalid_product_id(self, Group, User, parse_request):
        parse_request.return_value = None
        User.by_email = mock.Mock()
        Group.by_product_id.return_value = None
        request = testing.DummyRequest(post={'foo': 'bar'})

        view = IPNView(request)
        view.params = AttrDict({
            'email': '*****@*****.**',
            'product_id': 123,
        })

        with self.assertRaises(ValueError) as cm:
            view.ipn()
        self.assertEqual(
            str(cm.exception),
            'Cannot find group with product_id "123"',
        )
Exemple #11
0
    def test_new_user_no_trial(self, mocked_date):
        Group.by_name('monthly').trial_validity = None
        mocked_date.today.return_value = date(2013, 12, 30)

        view = IPNView(testing.DummyRequest())
        view.provider = 'clickbank'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'fullname': u'Föo Bar',
            'trans_type': 'SALE',
            'trans_id': 123,
            'product_id': 1,
            'affiliate': '*****@*****.**',
        })
        resp = view.ipn()
        self.assertEqual(resp, 'Done.')

        user = User.by_email('*****@*****.**')
        self.assertEqual(user.enabled, True)
        self.assertEqual(user.trial, False)
        self.assertEqual(user.affiliate, '*****@*****.**')
        self.assertEqual(user.valid_to, date(2014, 1, 30))
        self.assertEqual(user.last_payment, date(2013, 12, 30))

        self.assertEqual(len(user.audit_log_entries), 2)

        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserCreated')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Created by clickbank, transaction id: 123, type: SALE, note: ',
        )

        self.assertEqual(
            user.audit_log_entries[1].event_type.name, u'UserEnabled')
        self.assertEqual(
            user.audit_log_entries[1].comment,
            u'Enabled by clickbank, transaction id: 123, type: SALE, note: '
            u'regular until 2014-01-30',
        )
Exemple #12
0
    def test_existing_user_addon_subscription_new_payment(self, mocked_date):
        user = self._make_user(valid_to=date(2014, 1, 20))
        self.ipn_group.name = 'foo'
        self.ipn_group.addon = True
        mocked_date.today.return_value = date(2013, 12, 30)

        view = IPNView(testing.DummyRequest())
        view.provider = 'jvzoo'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'trans_type': 'BILL',
            'trans_id': 123,
            'product_id': 1,
        })

        resp = view.ipn()
        self.assertEqual(resp, 'Done.')

        # nothing should change for user's main subscription
        self.assertEqual(user.enabled, True)
        self.assertEqual(user.trial, False)
        self.assertEqual(user.valid_to, date(2014, 1, 20))
        self.assertEqual(user.last_payment, None)

        self.assertEqual(
            user.get_property('addon_1_valid_to'), date(2014, 1, 30))
        self.assertEqual(
            user.get_property('addon_1_last_payment'), date(2013, 12, 30))

        self.assertEqual(len(user.audit_log_entries), 1)
        self.assertEqual(
            user.audit_log_entries[0].event_type.name, u'UserEnabled')
        self.assertEqual(
            user.audit_log_entries[0].comment,
            u'Addon "foo" enabled by jvzoo, transaction id: 123, type: BILL, '
            u'note: regular until 2014-01-30',
        )
Exemple #13
0
    def test_existing_user_refund_after_upgrade(self, mocked_date):
        user = self._make_user(
            email='*****@*****.**',
            groups=[
                Group.by_name('enabled'),
                Group.by_name('trial'),
                Group.by_name('monthly'),
            ])
        user.set_property('upgrade_completed', True)

        view = IPNView(testing.DummyRequest())
        view.provider = 'clickbank'
        view.params = AttrDict({
            'email': '*****@*****.**',
            'trans_type': 'RFND',
            'trans_id': 123,
            'product_id': 1,
        })
        resp = view.ipn()
        self.assertEqual(resp, 'Done.')
        self.assertEqual(user.enabled, True)
        self.assertIn(Group.by_name('monthly'), user.groups)

        self.assertEqual(len(user.audit_log_entries), 0)
Exemple #14
0
 def test_no_JSON(self):
     request = testing.DummyRequest()
     view = IPNView(request)
     with self.assertRaises(ValueError) as cm:
         view.clickbank()
     self.assertEqual(cm.exception.message, 'No JSON request.')
Exemple #15
0
class TestIpnTransaction(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        self.user = mock.Mock()
        self.group = mock.Mock()
        self.view = IPNView(request=testing.DummyRequest())
        self.view.params = AttrDict()

    def tearDown(self):
        handler.clear()
        testing.tearDown()

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_sale_transaction')
    def test_SALE(self, sale_transaction):
        self.view.params.trans_type = 'SALE'
        self.view.ipn_transaction(self.user, self.group)
        sale_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_sale_transaction')
    def test_TEST_SALE(self, sale_transaction):
        self.view.params.trans_type = 'TEST_SALE'
        self.view.ipn_transaction(self.user, self.group)
        sale_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_bill_transaction')
    def test_BILL(self, bill_transaction):
        self.view.params.trans_type = 'BILL'
        self.view.ipn_transaction(self.user, self.group)
        bill_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_bill_transaction')
    def test_TEST_BILL(self, bill_transaction):
        self.view.params.trans_type = 'TEST_BILL'
        self.view.ipn_transaction(self.user, self.group)
        bill_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_disable_transaction')
    def test_RFND(self, disable_transaction):
        self.view.params.trans_type = 'RFND'
        self.view.ipn_transaction(self.user, self.group)
        disable_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_disable_transaction')
    def test_CGBK(self, disable_transaction):
        self.view.params.trans_type = 'CGBK'
        self.view.ipn_transaction(self.user, self.group)
        disable_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_disable_transaction')
    def test_TEST_RFND(self, disable_transaction):
        self.view.params.trans_type = 'TEST_RFND'
        self.view.ipn_transaction(self.user, self.group)
        disable_transaction.assert_called_with(self.user, self.group)

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_disable_transaction')
    def test_INSF(self, disable_transaction):
        self.view.params.trans_type = 'INSF'
        self.view.ipn_transaction(self.user, self.group)
        self.assertEqual(len(handler.records), 1)
        self.assertEqual(
            handler.records[0].message,
            'Don\'t do anything, user will be disabled when it\'s '
            'subscription runs out.',
        )

    def test_SUBSCRIPTION_CHG(self):
        self.view.params.trans_type = 'SUBSCRIPTION-CHG: SKU foo->bar'
        self.view.ipn_transaction(self.user, self.group)
        self.assertEqual(len(handler.records), 1)
        self.assertEqual(
            handler.records[0].message,
            'SUBSCRIPTION-CHG: SKU foo->bar finished correctly.',
        )

    @mock.patch('pyramid_bimt.views.ipn.IPNView.ipn_disable_transaction')
    def test_CANCEL_REBILL(self, disable_transaction):
        self.view.params.trans_type = 'CANCEL-REBILL'
        self.view.ipn_transaction(self.user, self.group)
        self.assertEqual(len(handler.records), 1)
        self.assertEqual(
            handler.records[0].message,
            'Don\'t do anything, user will be disabled when it\'s '
            'subscription runs out.',
        )

    def test_invalid(self):
        self.view.params.trans_type = 'foo'
        with self.assertRaises(ValueError) as cm:
            self.view.ipn_transaction(self.user, self.group)
        self.assertEqual(
            str(cm.exception),
            'Unknown Transaction Type: foo',
        )
Exemple #16
0
 def test_no_POST(self):
     request = testing.DummyRequest()
     view = IPNView(request)
     with self.assertRaises(ValueError) as cm:
         view.jvzoo()
     self.assertEqual(cm.exception.message, 'No POST request.')
Exemple #17
0
 def setUp(self):
     self.config = testing.setUp()
     self.user = mock.Mock()
     self.group = mock.Mock()
     self.view = IPNView(request=testing.DummyRequest())
     self.view.params = AttrDict()