def testCardWithoutHolderName(self, mock_authorize): form = CreditCardForm({ 'cvv': '123', 'expires_0': '12', 'expires_1': '22', 'card_number': '1111222233334444', 'holder_name': '' }) self.assertFalse(form.is_valid()) self.assertFalse(form.non_field_errors()) self.assertTrue(form.errors) self.assertEqual(len(form.errors), 1) self.assertTrue('holder_name' in form.errors)
def testEmptyFail(self, mock_authorize): form = CreditCardForm({}) self.assertFalse(form.is_valid()) self.assertFalse(form.non_field_errors()) self.assertTrue(form.errors) self.assertTrue('cvv' in form.errors) self.assertTrue('expires' in form.errors) self.assertTrue('card_number' in form.errors) self.assertTrue('holder_name' in form.errors) self.assertFalse(mock_authorize.called)
def testExpiredValid(self, mock_authorize): mock_authorize.return_value = ('0001234', '000012345') form = CreditCardForm( { 'cvv': '123', 'expires_0': '02', 'expires_1': '22', 'card_number': '1111222233334444', 'holder_name': 'xxx' } ) self.assertTrue(form.is_valid()) self.assertTrue(mock_authorize.called) self.assertFalse(form.errors) self.assertFalse(form.non_field_errors())
def testSave(self, mock_authorize, mock_modify, mock_subscribe): mock_authorize.return_value = ('0001234', '000012345') form = CreditCardForm({ 'cvv': '123', 'expires_0': '12', 'expires_1': '22', 'card_number': '1111222233334444', 'holder_name': 'xxx', }, instance=CreditCardForm._meta.model(holder=Patron.objects.get(pk=1))) self.assertTrue(form.is_valid()) self.assertFalse(form.non_field_errors()) self.assertFalse(form.errors) self.assertTrue(isinstance(form.save(commit=False), CreditCardForm._meta.model)) self.assertTrue(mock_authorize.called) self.assertTrue(mock_subscribe.called) self.assertFalse(mock_modify.called)
def testExpiredFail(self, mock_authorize): mock_authorize.side_effect = PayboxException(11, '') form = CreditCardForm( { 'cvv': '123', 'expires_0': '02', 'expires_1': '14', 'card_number': '1111222233334444', 'holder_name': 'xxx' } ) self.assertFalse(form.is_valid()) self.assertTrue('__all__' in form.errors) self.assertEqual(len(form.errors), 1) self.assertTrue(form.non_field_errors()) self.assertEqual(form.non_field_errors(), form.errors['__all__']) self.assertTrue(mock_authorize.called)
def testSaveCommitFail(self, mock_authorize, mock_modify, mock_subscribe): mock_authorize.return_value = ('0001234', '000012345') mock_subscribe.side_effect = PayboxException(16, 'Abonne deja existant') form = CreditCardForm({ 'cvv': '123', 'expires_0': '12', 'expires_1': '22', 'card_number': '1111222233334444', 'holder_name': 'xxx' }, instance=CreditCardForm._meta.model(holder=Patron.objects.get(pk=1))) self.assertTrue(form.is_valid()) self.assertFalse(form.non_field_errors()) self.assertFalse(form.errors) self.assertRaises(PayboxException, form.save, commit=True) self.assertTrue(mock_authorize.called) self.assertTrue(mock_subscribe.called) self.assertFalse(mock_modify.called)
def testSaveCommitExisting(self, mock_authorize, mock_modify, mock_subscribe): mock_authorize.return_value = ('0001234', '000012345') mock_modify.return_value = 'SLDLrcsLMPC' form = CreditCardForm({ 'cvv': '123', 'expires_0': '12', 'expires_1': '22', 'card_number': '1111222233334444', 'holder_name': 'xxx' }, instance=CreditCardForm._meta.model( holder=Patron.objects.get(pk=1), card_number='SLDLrcsLMPC', pk=1, expires='1222', masked_number='1XXXXXXXXXXXX444', keep=False, holder_name='xxx' ) ) self.assertTrue(form.is_valid()) self.assertFalse(form.non_field_errors()) self.assertFalse(form.errors) self.assertTrue(isinstance(form.save(commit=True), CreditCardForm._meta.model)) self.assertTrue(mock_authorize.called) self.assertFalse(mock_subscribe.called) self.assertTrue(mock_modify.called)
def testLuhnFail(self, mock_authorize): self.assertFalse( CreditCardForm( { 'cvv': '123', 'expires_0': '12', 'expires_1': '22', 'card_number': '1111222233334445', 'holder_name': 'xxx' } ).is_valid() ) self.assertFalse(mock_authorize.called)
def validate(self, attrs): keep = attrs['keep'] if not keep: attrs.pop('holder', None) self.form = form = CreditCardForm(attrs) if not form.is_valid(): raise ValidationError(form.errors) new_attrs = form.clean() new_attrs['keep'] = keep return new_attrs