-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
744 lines (614 loc) · 22 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
from config import BAD_ACTOR_NOTIFICATION_URL
from datetime import datetime
from decimal import Decimal
from pytz import timezone
from wtforms import validators
import pytest
from batch import amount_to_charge
from npsp import RDO, Contact, Opportunity, SalesforceConnection, Account
from util import clean, construct_slack_message
from forms import format_amount, validate_amount
from charges import generate_stripe_description
from bad_actor import (
BadActor,
BadActorJudgmentType,
BadActorResponse,
BadActorResponseItem,
)
class SalesforceConnectionSubClass(SalesforceConnection):
def __init__(self):
pass
@property
def instance_url(self):
return "quux"
sf = SalesforceConnectionSubClass()
bad_actor_request = {
"email": "foo@bar.org",
"ip": "127.0.0.1",
"country_code": "US",
"zipcode": "78701",
"given_name": "nick",
"family_name": "cage",
"referer": "https://foo.com/foobar?foobar=baz",
"captcha_token": "captcha_token",
"reason": "because I care",
"amount": 1.34,
}
def test_bad_actor_init():
bad_actor_response_suspect = BadActorResponse(
overall_judgment=BadActorJudgmentType.suspect, items=[]
)
bad_actor_response_good = BadActorResponse(
overall_judgment=BadActorJudgmentType.good, items=[]
)
bad_actor = BadActor(bad_actor_request=bad_actor_request)
assert bad_actor.quarantine is False
bad_actor.bad_actor_api_response = bad_actor_response_suspect
assert bad_actor.quarantine is True
bad_actor.bad_actor_api_response = bad_actor_response_good
assert bad_actor.quarantine is False
def test_bad_actor_slackify_items():
bad_actor_item1 = BadActorResponseItem(
label="foo", value="bar", judgment=BadActorJudgmentType.suspect
)
bad_actor_item2 = BadActorResponseItem(
label="foo", value="bar", judgment=BadActorJudgmentType.good
)
actual = BadActor._slackify_items([bad_actor_item1, bad_actor_item2])
expected = [
{"type": "mrkdwn", "text": ":eyes: *foo*: bar"},
{"type": "mrkdwn", "text": ":white_check_mark: *foo*: bar"},
]
assert actual == expected
def test_slackify_all():
bad_actor_item1 = BadActorResponseItem(
label="foo", value="bar", judgment=BadActorJudgmentType.suspect
)
bad_actor_item2 = BadActorResponseItem(
label="foo", value="bar", judgment=BadActorJudgmentType.good
)
bad_actor_response = BadActorResponse(
overall_judgment=BadActorJudgmentType.suspect,
items=[bad_actor_item1, bad_actor_item2],
)
opportunity = Opportunity(sf_connection=sf)
opportunity.id = "baz"
bad_actor = BadActor(bad_actor_request=None)
bad_actor.bad_actor_api_response = bad_actor_response
bad_actor.transaction = opportunity
bad_actor.transaction_type = "Opportunity"
expected = [
{
"fields": [{"text": "<quux/baz|Salesforce>", "type": "mrkdwn"}],
"type": "section",
},
{
"fields": [
{"text": ":eyes: *foo*: bar", "type": "mrkdwn"},
{"text": ":white_check_mark: *foo*: bar", "type": "mrkdwn"},
],
"type": "section",
},
{
"block_id": "choices",
"elements": [
{
"action_id": "approve",
"style": "primary",
"text": {"emoji": True, "text": "Approve", "type": "plain_text"},
"type": "button",
"value": "Opportunity:baz",
},
{
"action_id": "reject",
"style": "danger",
"text": {"emoji": True, "text": "Reject", "type": "plain_text"},
"type": "button",
"value": "Opportunity:baz",
},
],
"type": "actions",
},
]
actual = bad_actor._slackify_all()
assert actual == expected
def test_generate_stripe_description():
# if description is blank use type
opp = Opportunity(sf_connection=sf)
opp.type = "Recurring Donation"
opp.description = ""
actual = generate_stripe_description(opp)
assert actual == "Texas Tribune Sustaining Membership"
# strip leading "The "
opp = Opportunity(sf_connection=sf)
opp.description = "The Cuddly Kitty"
actual = generate_stripe_description(opp)
assert actual == "Cuddly Kitty"
# description overrides type
opp = Opportunity(sf_connection=sf)
opp.type = "Recurring Donation"
opp.description = "Cats in Hats Are Cute!"
actual = generate_stripe_description(opp)
assert actual == "Cats in Hats Are Cute!"
# if we can't find anything else at least they'll know it's from us
opp = Opportunity(sf_connection=sf)
opp.type = "Something Bogus"
opp.description = ""
actual = generate_stripe_description(opp)
assert actual == "Texas Tribune"
def test_net_amount_none():
opp = Opportunity(sf_connection=sf)
opp.net_amount = None
assert opp.net_amount == "0.00"
def test__clean():
form = {
"a": "None",
"b": "True",
"c": "False",
"d": "None",
"e": "none",
"f": None,
"g": True,
"h": False,
"i": 9,
"j": 8.1,
"k": "3.2",
"l": "4",
"m": "string",
}
expected = {
"a": None,
"b": True,
"c": False,
"d": None,
"e": "none",
"f": None,
"g": True,
"h": False,
"i": 9,
"j": 8.1,
"k": 3.2,
"l": 4,
"m": "string",
}
actual = clean(form)
assert expected == actual
assert actual["bogus"] is None
def test__format_amount():
opp = Opportunity(sf_connection=sf)
opp.amount = "1500.123"
actual = opp.amount
expected = "1500.12"
assert actual == expected
opp.amount = "1500"
actual = opp.amount
expected = "1500.00"
assert actual == expected
opp.amount = "1500.00"
actual = opp.amount
expected = "1500.00"
assert actual == expected
opp.amount = "1500.126"
actual = opp.amount
expected = "1500.13"
assert actual == expected
class Response(object):
pass
def test_check_response():
response = Response()
response.status_code = 204
with pytest.raises(Exception):
sf.check_response(response)
assert sf.check_response(response, expected_status=204)
response.status_code = 500
with pytest.raises(Exception):
sf.check_response(response)
response.status_code = 404
with pytest.raises(Exception):
sf.check_response(response)
response.status_code = 200
response = sf.check_response(response)
assert response is True
zone = timezone("US/Central")
today = datetime.now(tz=zone).strftime("%Y-%m-%d")
def test__format_slack():
opportunity = Opportunity(sf_connection=sf)
opportunity.account_id = "0011700000BpR8PAAV"
opportunity.amount = 9
opportunity.encouraged_by = "Because I love the Trib!"
opportunity.name = "D C (dcraigmile+test6@texastribune.org)"
opportunity.stripe_id = "cus_78MqJSBejMN9gn"
opportunity.agreed_to_pay_fees = True
opportunity.referral_id = "1234"
opportunity.lead_source = "Stripe"
opportunity.description = "The Texas Tribune Membership"
opportunity.stripe_customer = "cus_78MqJSBejMN9gn"
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.encouraged_by = "Because I love the Trib!"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "yearly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 100
rdo.name = "foo"
rdo.installments = 3
rdo.open_ended_status = None
rdo.description = "Texas Tribune Circle Membership"
rdo.agreed_to_pay_fees = True
rdo.type = "Giving Circle"
contact = Contact(sf_connection=sf)
contact.email = "dcraigmile+test6@texastribune.org"
contact.first_name = "D"
contact.last_name = "C"
contact.lead_source = "Stripe"
contact.work_email = "dcraigmile+test6@texastribune.org"
account = Account(sf_connection=sf)
account.name = "Acme Inc."
account.website = "http://acme.com"
account.shipping_street = "Street"
account.shipping_city = "Austin"
account.shipping_postalcode = "78701"
account.shipping_state = "TX"
account.record_type_name = "Household"
actual = construct_slack_message(
account=account, rdo=rdo, opportunity=None, contact=None
)
expected = "Acme Inc. pledged $100 [yearly] (Because I love the Trib!)"
assert actual == expected
actual = construct_slack_message(
account=None, rdo=rdo, opportunity=None, contact=contact
)
expected = "D C pledged $100 [yearly] (Because I love the Trib!)"
assert actual == expected
actual = construct_slack_message(
account=None, rdo=None, opportunity=opportunity, contact=contact
)
expected = "D C pledged $9 [one-time] (Because I love the Trib!)"
assert actual == expected
def test__format_opportunity():
opportunity = Opportunity(sf_connection=sf)
opportunity.account_id = "0011700000BpR8PAAV"
opportunity.amount = 9
opportunity.net_amount = 8
opportunity.encouraged_by = "Because I love the Trib!"
opportunity.name = "D C (dcraigmile+test6@texastribune.org)"
opportunity.stripe_id = "cus_78MqJSBejMN9gn"
opportunity.agreed_to_pay_fees = True
opportunity.referral_id = "1234"
opportunity.lead_source = "Stripe"
opportunity.description = "The Texas Tribune Membership"
opportunity.stripe_customer = "cus_78MqJSBejMN9gn"
response = opportunity._format()
expected = {
"AccountId": "0011700000BpR8PAAV",
"CampaignId": None,
"Amount": "9.00",
"CloseDate": today,
"Encouraged_to_contribute_by__c": "Because I love the Trib!",
"LeadSource": "Stripe",
"Name": "D C (dcraigmile+test6@texastribune.org)",
"RecordType": {"Name": "Membership"},
"StageName": "Pledged",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"Referral_ID__c": "1234",
"Description": "The Texas Tribune Membership",
"Stripe_Agreed_to_pay_fees__c": True,
"Type": "Single",
"Stripe_Card__c": None,
"Stripe_Transaction_ID__c": None,
"npsp__Closed_Lost_Reason__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Expiration__c": None,
"Stripe_Card_Last_4__c": None,
"Amazon_Order_Id__c": None,
"Net_Amount__c": "8.00",
"Donor_Selected_Amount__c": 0,
"Quarantined__c": False,
}
assert response == expected
def test__format_circle_donation():
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.encouraged_by = "Because I love the Trib!"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "yearly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 100
rdo.name = "foo"
rdo.installments = 3
rdo.open_ended_status = None
rdo.description = "Texas Tribune Circle Membership"
rdo.agreed_to_pay_fees = True
rdo.type = "Giving Circle"
rdo.quarantined = True
response = rdo._format()
expected_response = {
"Referral_ID__c": "1234",
"Encouraged_to_contribute_by__c": "Because I love the Trib!",
"npe03__Date_Established__c": today,
"Lead_Source__c": "Stripe",
"npe03__Contact__c": "0031700000BHQzBAAX",
"npe03__Installment_Period__c": "yearly",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"Billing_Email__c": None,
"Blast_Subscription_Email__c": None,
"npe03__Organization__c": None,
"npe03__Amount__c": "300.0", # 3 * 100
"Name": "foo",
"npe03__Installments__c": 3,
"npe03__Open_Ended_Status__c": None,
"Stripe_Description__c": "Texas Tribune Circle Membership",
"Stripe_Agreed_to_pay_fees__c": True,
"Type__c": "Giving Circle",
"npe03__Recurring_Donation_Campaign__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Expiration__c": None,
"Stripe_Card_Last_4__c": None,
"Quarantined__c": True,
}
assert response == expected_response
def test__format_cent_circle_donation():
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.encouraged_by = "Because I love the Trib!"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "yearly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 1501.01
rdo.name = "foo"
rdo.installments = 3
rdo.open_ended_status = None
rdo.description = "Texas Tribune Circle Membership"
rdo.agreed_to_pay_fees = True
rdo.type = "Giving Circle"
response = rdo._format()
expected_response = {
"Referral_ID__c": "1234",
"Encouraged_to_contribute_by__c": "Because I love the Trib!",
"npe03__Date_Established__c": today,
"Lead_Source__c": "Stripe",
"npe03__Organization__c": None,
"npe03__Contact__c": "0031700000BHQzBAAX",
"npe03__Installment_Period__c": "yearly",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"npe03__Amount__c": "4503.03", # 3 * 1501.01
"Name": "foo",
"npe03__Installments__c": 3,
"npe03__Open_Ended_Status__c": None,
"Stripe_Description__c": "Texas Tribune Circle Membership",
"Stripe_Agreed_to_pay_fees__c": True,
"Type__c": "Giving Circle",
"npe03__Recurring_Donation_Campaign__c": None,
"Billing_Email__c": None,
"Blast_Subscription_Email__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Expiration__c": None,
"Stripe_Card_Last_4__c": None,
"Quarantined__c": False,
}
response["Name"] = "foo"
assert response == expected_response
def test__format_recurring_donation():
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.encouraged_by = "Because I love the Trib!"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "monthly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 9
rdo.name = "foo"
rdo.installments = 0
rdo.open_ended_status = None
rdo.description = "Texas Tribune Membership"
rdo.agreed_to_pay_fees = True
response = rdo._format()
expected_response = {
"Referral_ID__c": "1234",
"npe03__Organization__c": None,
"Encouraged_to_contribute_by__c": "Because I love the Trib!",
"npe03__Date_Established__c": today,
"Lead_Source__c": "Stripe",
"npe03__Contact__c": "0031700000BHQzBAAX",
"npe03__Installment_Period__c": "monthly",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"npe03__Amount__c": "9.00",
"Name": "foo",
"npe03__Installments__c": 0,
"npe03__Open_Ended_Status__c": None,
"Stripe_Description__c": "Texas Tribune Membership",
"Stripe_Agreed_to_pay_fees__c": True,
"Type__c": "Recurring Donation",
"npe03__Recurring_Donation_Campaign__c": None,
"Billing_Email__c": None,
"Blast_Subscription_Email__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Expiration__c": None,
"Stripe_Card_Last_4__c": None,
"Quarantined__c": False,
}
response["Name"] = "foo"
assert response == expected_response
def test__format_recurring_donation_decimal():
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.encouraged_by = "Because I love the Trib!"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "monthly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 9.15
rdo.name = "foo"
rdo.installments = 0
rdo.open_ended_status = None
rdo.description = "Texas Tribune Membership"
rdo.agreed_to_pay_fees = True
rdo.quarantined = True
response = rdo._format()
expected_response = {
"Referral_ID__c": "1234",
"npe03__Organization__c": None,
"Billing_Email__c": None,
"Encouraged_to_contribute_by__c": "Because I love the Trib!",
"npe03__Date_Established__c": today,
"Lead_Source__c": "Stripe",
"Blast_Subscription_Email__c": None,
"npe03__Contact__c": "0031700000BHQzBAAX",
"npe03__Installment_Period__c": "monthly",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"npe03__Amount__c": "9.15",
"Name": "foo",
"npe03__Installments__c": 0,
"npe03__Open_Ended_Status__c": None,
"Stripe_Description__c": "Texas Tribune Membership",
"Stripe_Agreed_to_pay_fees__c": True,
"Type__c": "Recurring Donation",
"npe03__Recurring_Donation_Campaign__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Expiration__c": None,
"Stripe_Card_Last_4__c": None,
"Quarantined__c": True,
}
response["Name"] = "foo"
assert response == expected_response
def test__format_blast_rdo():
rdo = RDO(sf_connection=sf)
rdo.referral_id = "1234"
rdo.lead_source = "Stripe"
rdo.contact_id = "0031700000BHQzBAAX"
rdo.installment_period = "monthly"
rdo.stripe_customer = "cus_78MqJSBejMN9gn"
rdo.amount = 40
rdo.name = "foo"
rdo.installments = 0
rdo.open_ended_status = "Open"
rdo.description = "Monthly Blast Subscription"
rdo.agreed_to_pay_fees = True
rdo.type = "The Blast"
rdo.billing_email = "dcraigmile+test6@texastribune.org"
rdo.blast_subscription_email = "subscriber@foo.bar"
rdo.quarantined = True
response = rdo._format()
expected_response = {
"Referral_ID__c": "1234",
"Encouraged_to_contribute_by__c": None,
"npe03__Date_Established__c": today,
"Lead_Source__c": "Stripe",
"npe03__Contact__c": "0031700000BHQzBAAX",
"npe03__Installment_Period__c": "monthly",
"Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn",
"npe03__Amount__c": "40.00",
"Name": "foo",
"npe03__Installments__c": 0,
"npe03__Open_Ended_Status__c": "Open",
"Stripe_Description__c": "Monthly Blast Subscription",
"Stripe_Agreed_to_pay_fees__c": True,
"Type__c": "The Blast",
"Billing_Email__c": "dcraigmile+test6@texastribune.org",
"Blast_Subscription_Email__c": "subscriber@foo.bar",
"npe03__Organization__c": None,
"npe03__Recurring_Donation_Campaign__c": None,
"Stripe_Card_Brand__c": None,
"Stripe_Card_Last_4__c": None,
"Stripe_Card_Expiration__c": None,
"Quarantined__c": True,
}
response["Name"] = "foo"
assert response == expected_response
def test__format_contact():
contact = Contact(sf_connection=sf)
contact.email = "dcraigmile+test6@texastribune.org"
contact.first_name = "D"
contact.last_name = "C"
contact.lead_source = "Stripe"
contact.work_email = "dcraigmile+test6@texastribune.org"
response = contact._format()
expected_response = {
"Email": "dcraigmile+test6@texastribune.org",
"FirstName": "D",
"LastName": "C",
"LeadSource": "Stripe",
"MailingPostalCode": None,
"npe01__WorkEmail__c": "dcraigmile+test6@texastribune.org",
}
assert response == expected_response
def test_amount_to_charge_cents_just_fees_false():
opp = Opportunity()
opp.amount = 10.50
opp.agreed_to_pay_fees = False
actual = amount_to_charge(opp)
expected = Decimal("10.50")
assert actual == expected
def test_amount_to_charge_just_fees_false():
opp = Opportunity()
opp.amount = 10
opp.agreed_to_pay_fees = False
actual = amount_to_charge(opp)
expected = Decimal("10.00")
assert actual == expected
def test_amount_to_charge_cents_and_fees_true():
opp = Opportunity()
opp.amount = 10.50
opp.agreed_to_pay_fees = True
actual = amount_to_charge(opp)
expected = Decimal("11.04")
assert actual == expected
def test_amount_to_charge_just_fees_true():
opp = Opportunity()
opp.amount = 10
opp.agreed_to_pay_fees = True
actual = amount_to_charge(opp)
expected = Decimal("10.53")
assert actual == expected
def test_base_form_amount_filter_with_leading_dollar_sign():
amount = "$8.67"
actual = format_amount(amount)
expected = Decimal(8.67)
assert actual == expected
def test_base_form_amount_filter_without_leading_dollar_sign():
amount = "101.91"
actual = format_amount(amount)
expected = Decimal(101.91)
assert actual == expected
def test_base_form_amount_filter_with_non_numeric_value():
amount = "$89.a&4"
actual = format_amount(amount)
expected = None
assert actual == expected
class Form(object):
pass
class Field(object):
def __init__(self, value):
self.data = value
def test_base_form_amount_validator_with_valid_value():
form = Form()
amount_field = Field(13) # valid amount
try:
validate_amount(form, amount_field)
except:
raise Exception("An error was raised despite a valid amount being provided")
def test_base_form_amount_validator_with_non_numeric_value():
form = Form()
# None is sent from filter func if value can't be casted to a float
amount_field = Field(None)
try:
validate_amount(form, amount_field)
raise Exception("A validation error should have been raised")
except validators.ValidationError as e:
assert str(e) == "Non-numeric amount provided"
except:
raise Exception("An error was raised, but not a validation one")
def test_base_form_amount_validator_with_too_small_value():
form = Form()
# None is sent if value can't be casted to a float
amount_field = Field(0.87)
try:
validate_amount(form, amount_field)
raise Exception("A validation error should have been raised")
except validators.ValidationError as e:
assert str(e) == "Amount is less than 1"
except:
raise Exception("An error was raised, but not a validation one")