Source

cciw-website / cciw / bookings / tests.py

The branch 'prefetch_related' does not exist.
   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
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from decimal import Decimal
import re

from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import simplejson

from cciw.bookings.management.commands.expire_bookings import Command as ExpireBookingsCommand
from cciw.bookings.models import BookingAccount, Price, Booking, Payment, ChequePayment, book_basket_now
from cciw.bookings.models import PRICE_FULL, PRICE_2ND_CHILD, PRICE_3RD_CHILD, PRICE_CUSTOM, PRICE_SOUTH_WALES_TRANSPORT, PRICE_DEPOSIT, BOOKING_APPROVED, BOOKING_INFO_COMPLETE, BOOKING_BOOKED, BOOKING_CANCELLED
from cciw.cciwmain.common import get_thisyear
from cciw.cciwmain.models import Camp
from cciw.cciwmain.tests.mailhelpers import read_email_url
from cciw.officers.tests.references import OFFICER_USERNAME, OFFICER_PASSWORD, BOOKING_SEC_USERNAME, BOOKING_SEC_PASSWORD
from cciw.sitecontent.models import HtmlChunk


DISABLED_BOOK_NOW_BTN = "id_book_now_btn\" disabled>"
ENABLED_BOOK_NOW_BUTTON = "id_book_now_btn\">"


### Mixins to reduce duplication ###

class CreateCampMixin(object):

    camp_minimum_age = 11
    camp_maximum_age = 17

    def create_camp(self):
        # Need to create a Camp that we can choose i.e. is in the future
        self.camp = Camp.objects.create(year=get_thisyear(), number=1,
                                        minimum_age=self.camp_minimum_age,
                                        maximum_age=self.camp_maximum_age,
                                        start_date=datetime.now() + timedelta(20),
                                        end_date=datetime.now() + timedelta(27),
                                        site_id=1)


class CreatePricesMixin(object):
    def add_prices(self):
        year = get_thisyear()
        Price.objects.get_or_create(year=year,
                                    price_type=PRICE_FULL,
                                    price=Decimal('100.00'))
        Price.objects.get_or_create(year=year,
                                    price_type=PRICE_2ND_CHILD,
                                    price=Decimal('75.00'))
        Price.objects.get_or_create(year=year,
                                    price_type=PRICE_3RD_CHILD,
                                    price=Decimal('50.00'))
        Price.objects.get_or_create(year=year,
                                    price_type=PRICE_SOUTH_WALES_TRANSPORT,
                                    price=Decimal('20.00'))
        Price.objects.get_or_create(year=year,
                                    price_type=PRICE_DEPOSIT,
                                    price=Decimal('20.00'))


class LogInMixin(object):
    email = 'booker@bookers.com'

    def login(self, add_account_details=True):
        if hasattr(self, '_logged_in'):
            return
        # Easiest way is to simulate what the user actually has to do
        self.client.post(reverse('cciw.bookings.views.start'),
                         {'email': self.email})
        url, path, querydata = read_email_url(mail.outbox[-1], "https?://.*/booking/v/.*")
        mail.outbox.pop()
        self.client.get(path, querydata)
        if add_account_details:
            BookingAccount.objects.filter(email=self.email).update(name='Joe',
                                                                   address='123',
                                                                   post_code='XYZ')
        self._logged_in = True

    def get_account(self):
        return BookingAccount.objects.get(email=self.email)


class CreatePlaceMixin(CreatePricesMixin, CreateCampMixin, LogInMixin):
    @property
    def place_details(self):
        return {
            'camp': self.camp.id,
            'name': u'Frédéric Bloggs',
            'sex': 'm',
            'date_of_birth': '%d-01-01' % (get_thisyear() - 14),
            'address': 'x',
            'post_code': 'ABC 123',
            'contact_name': 'Mary Bloggs',
            'contact_phone_number': '01982 987654',
            'gp_name': 'Doctor Who',
            'gp_address': 'The Tardis',
            'gp_phone_number': '01234 456789',
            'medical_card_number': 'asdfasdf',
            'agreement': '1',
            'price_type': '0',
            }

    def create_place(self, extra=None):
        # We use public views to create place, to ensure that they are created
        # in the same way that a user would.
        self.login()
        self.add_prices()

        data = self.place_details.copy()
        if extra is not None:
            data.update(extra)

        # Sanity check:
        resp0 = self.client.get(reverse('cciw.bookings.views.add_place'))
        self.assertEqual(resp0.status_code, 200)
        resp = self.client.post(reverse('cciw.bookings.views.add_place'), data)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.list_bookings')
        self.assertTrue(resp['Location'].endswith(newpath))

    def setUp(self):
        super(CreatePlaceMixin, self).setUp()
        self.create_camp()


### Test cases ###


class TestBookingIndex(CreatePricesMixin, CreateCampMixin, TestCase):

    fixtures = ['basic.json']

    def setUp(self):
        super(TestBookingIndex, self).setUp()
        HtmlChunk.objects.get_or_create(name="bookingform_post_to")

    def test_show_with_no_prices(self):
        resp = self.client.get(reverse('cciw.bookings.views.index'))
        self.assertContains(resp, "Prices for %d have not been finalised yet" % get_thisyear())


    def test_show_with_prices(self):
        self.add_prices()
        self.create_camp() # need for booking to be open
        resp = self.client.get(reverse('cciw.bookings.views.index'))
        self.assertContains(resp, "£100")
        self.assertContains(resp, "£20") # Deposit price


class TestBookingStart(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.start')

    def test_show_form(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

        self.assertContains(resp, 'id_email')

    def test_complete_form(self):
        self.assertEqual(BookingAccount.objects.all().count(), 0)
        resp = self.client.post(self.url,
                                {'email': 'booker@bookers.com'})
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(BookingAccount.objects.all().count(), 1)
        b = BookingAccount.objects.get(email='booker@bookers.com')
        self.assertEqual(len(mail.outbox), 1)

    def test_complete_form_existing_email(self):
        BookingAccount.objects.create(email="booker@bookers.com")
        self.assertEqual(BookingAccount.objects.all().count(), 1)
        resp = self.client.post(self.url,
                                {'email': 'booker@bookers.com'})
        self.assertEqual(BookingAccount.objects.all().count(), 1)
        self.assertEqual(len(mail.outbox), 1)

    def test_skip_if_logged_in(self):
        # This assumes verification process works
        # Check redirect to step 3 - account details
        self.login(add_account_details=False)
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.account_details')
        self.assertTrue(resp['Location'].endswith(newpath))

    def test_skip_if_account_details(self):
        # Check redirect to step 4 - add place
        self.login()
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.add_place')
        self.assertTrue(resp['Location'].endswith(newpath))

    def test_skip_if_has_place_details(self):
        # Check redirect to step 5 - checkout
        self.login()
        self.create_place()
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)
        self.assertTrue(resp['Location'].endswith(reverse('cciw.bookings.views.list_bookings')))

class TestBookingVerify(TestCase):

    fixtures = ['basic.json']

    def _read_email_verify_email(self, email):
        return read_email_url(email, "https?://.*/booking/v/.*")

    def test_verify_correct(self):
        """
        Test the email verification stage when the URL is correct
        """
        # Assumes booking_start works:
        self.client.post(reverse('cciw.bookings.views.start'),
                         {'email': 'booker@bookers.com'})
        acc = BookingAccount.objects.get(email='booker@bookers.com')
        self.assertTrue(acc.last_login is None)
        self.assertTrue(acc.first_login is None)
        url, path, querydata = self._read_email_verify_email(mail.outbox[-1])
        resp = self.client.get(path, querydata)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.account_details')
        self.assertTrue(resp['Location'].endswith(newpath))
        acc = BookingAccount.objects.get(email='booker@bookers.com')
        self.assertEqual(str(acc.id),
                         resp.cookies['bookingaccount'].value.split(':')[0])
        self.assertTrue(acc.last_login is not None)
        self.assertTrue(acc.first_login is not None)


    def test_verify_correct_and_has_details(self):
        """
        Test the email verification stage when the URL is correct and the
        account already has name and address
        """
        # Assumes booking_start works:
        self.client.post(reverse('cciw.bookings.views.start'),
                         {'email': 'booker@bookers.com'})
        b = BookingAccount.objects.get(email='booker@bookers.com')
        b.name = "Joe"
        b.address = "Home"
        b.post_code = "XY1 D45"
        b.save()

        url, path, querydata = self._read_email_verify_email(mail.outbox[-1])
        resp = self.client.get(path, querydata)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.add_place')
        self.assertTrue(resp['Location'].endswith(newpath))

    def test_verify_incorrect(self):
        """
        Test the email verification stage when the URL is incorrect
        """
        # Assumes booking_start works:
        self.client.post(reverse('cciw.bookings.views.start'),
                         {'email': 'booker@bookers.com'})
        url, path, querydata = self._read_email_verify_email(mail.outbox[-1])
        badpath = path.replace('-', '-1')
        resp = self.client.get(badpath, querydata, follow=True)
        self.assertContains(resp, "failed")
        self.assertTrue('bookingaccount' not in resp.cookies)

    def test_verify_invalid_account(self):
        """
        Test the email verification stage when the URL contains an invalid
        BookingAccount id
        """
        # Assumes booking_start works:
        self.client.post(reverse('cciw.bookings.views.start'),
                         {'email': 'booker@bookers.com'})
        url, path, querydata = self._read_email_verify_email(mail.outbox[-1])
        b = BookingAccount.objects.get(email='booker@bookers.com')
        badpath = path.replace('%s-' % b.id, '1000-')
        resp = self.client.get(badpath, querydata, follow=True)
        self.assertContains(resp, "failed")
        self.assertTrue('bookingaccount' not in resp.cookies)


class TestAccountDetails(LogInMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.account_details')

    def test_redirect_if_not_logged_in(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)

    def test_show_if_logged_in(self):
        self.login(add_account_details=False)
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

    def test_missing_name(self):
        self.login(add_account_details=False)
        resp = self.client.post(self.url, {})
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "This field is required")

    def test_complete(self):
        """
        Test that we can complete the account details page
        """
        self.login(add_account_details=False)
        resp = self.client.post(self.url,
                                {'name': 'Mr Booker',
                                 'address': '123, A Street',
                                 'post_code': 'XY1 D45',
                                 })
        self.assertEqual(resp.status_code, 302)
        b = BookingAccount.objects.get(email=self.email)
        self.assertEqual(b.name, 'Mr Booker')


class TestAddPlace(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.add_place')

    def test_redirect_if_not_logged_in(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)

    def test_redirect_if_no_account_details(self):
        self.login(add_account_details=False)
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)

    def test_show_if_logged_in(self):
        self.login()
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

    def test_show_error_if_no_prices(self):
        self.login()
        resp = self.client.get(self.url, follow=True)
        self.assertContains(resp, "prices have not been set")

    def test_post_not_allowed_if_no_prices(self):
        self.login()
        resp = self.client.post(self.url, {}, follow=True)
        self.assertContains(resp, "prices have not been set")

    def test_allowed_if_prices_set(self):
        self.login()
        self.add_prices()
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)
        self.assertNotContains(resp, "Prices have not been set")

    def test_incomplete(self):
        self.login()
        self.add_prices()
        resp = self.client.post(self.url, {})
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "This field is required")

    def test_complete(self):
        self.login()
        self.add_prices()
        b = BookingAccount.objects.get(email=self.email)
        self.assertEqual(b.bookings.count(), 0)

        data = self.place_details.copy()
        resp = self.client.post(self.url, data)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.list_bookings')
        self.assertTrue(resp['Location'].endswith(newpath))

        # Did we create it?
        self.assertEqual(b.bookings.count(), 1)

        # Check amount_due
        self.assertEqual(b.bookings.all()[0].amount_due, Price.objects.get(price_type=PRICE_FULL,
                                                                           year=get_thisyear()).price)

    def test_south_wales_surcharge(self):
        self.login()
        self.add_prices()
        b = BookingAccount.objects.get(email=self.email)
        self.assertEqual(b.bookings.count(), 0)

        data = self.place_details.copy()
        data['south_wales_transport'] = '1'
        resp = self.client.post(self.url, data)
        p = Price.objects.get(price_type=PRICE_FULL, year=get_thisyear()).price + \
            Price.objects.get(price_type=PRICE_SOUTH_WALES_TRANSPORT, year=get_thisyear()).price
        self.assertEqual(b.bookings.all()[0].amount_due, p)


class TestEditPlace(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    # Most functionality is shared with the 'add' form, so doesn't need testing separately.

    def test_redirect_if_not_logged_in(self):
        resp = self.client.get(reverse('cciw.bookings.views.edit_place', kwargs={'id':'1'}))
        self.assertEqual(resp.status_code, 302)

    def test_show_if_owner(self):
        self.login()
        self.add_prices()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        resp = self.client.get(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "id_save_btn")

    def test_404_if_not_owner(self):
        self.login()
        self.add_prices()
        self.create_place()
        other_account = BookingAccount.objects.create(email='other@mail.com')
        Booking.objects.all().update(account=other_account)
        b = Booking.objects.all()[0]
        resp = self.client.get(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}))
        self.assertEqual(resp.status_code, 404)

    def test_incomplete(self):
        self.login()
        self.add_prices()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        resp = self.client.post(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}), {})
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "This field is required")

    def test_complete(self):
        self.login()
        self.add_prices()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]

        data = self.place_details.copy()
        data['name'] = "A New Name"
        resp = self.client.post(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}), data)
        self.assertEqual(resp.status_code, 302)
        newpath = reverse('cciw.bookings.views.list_bookings')
        self.assertTrue(resp['Location'].endswith(newpath))

        # Did we alter it?
        self.assertEqual(acc.bookings.all()[0].name, "A New Name")

    def test_edit_booked(self):
        """
        Test we can't edit a booking when it is already booked.
        (or anything but BOOKING_INFO_COMPLETE)
        """
        self.login()
        self.add_prices()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]

        for state in [BOOKING_APPROVED, BOOKING_BOOKED]:
            b.state = state
            b.save()

            # Check there is no save button
            resp = self.client.get(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}))
            self.assertNotContains(resp, "id_save_btn")
            # Check for message
            self.assertContains(resp, "can only be changed by an admin.")

            # Attempt a post
            data = self.place_details.copy()
            data['name'] = "A New Name"
            resp = self.client.post(reverse('cciw.bookings.views.edit_place', kwargs={'id':str(b.id)}), data)
            # Check we didn't alter it
            self.assertNotEqual(acc.bookings.all()[0].name, "A New Name")


class TestListBookings(CreatePlaceMixin, TestCase):
    # This includes tests for most of the business logic

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.list_bookings')

    def test_redirect_if_not_logged_in(self):
        resp = self.client.get(self.url)
        self.assertEqual(302, resp.status_code)

    def test_show_bookings(self):
        self.login()
        self.create_place()
        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, "Camp 1")
        self.assertContains(resp, "Frédéric Bloggs")
        self.assertContains(resp, "£100")
        self.assertContains(resp, "This place can be booked")
        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)

    def test_handle_custom_price(self):
        self.login()
        self.create_place({'price_type': PRICE_CUSTOM})
        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, "Camp 1")
        self.assertContains(resp, "Frédéric Bloggs")
        self.assertContains(resp, "TBA")
        self.assertContains(resp, "A custom discount needs to be arranged by the booking secretary")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)
        self.assertContains(resp, "This place cannot be booked for the reasons described above")

    def test_2nd_child_discount_allowed(self):
        self.login()
        self.create_place({'price_type': PRICE_2ND_CHILD})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You cannot use a 2nd child discount")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

        # 2 places, both at 2nd child discount, is not allowed.
        self.create_place({'price_type': PRICE_2ND_CHILD})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You cannot use a 2nd child discount")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_3rd_child_discount_allowed(self):
        self.login()
        self.create_place({'price_type': PRICE_FULL})
        self.create_place({'price_type': PRICE_3RD_CHILD})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You cannot use a 3rd child discount")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

        # 3 places, with 2 at 3rd child discount, is not allowed.
        self.create_place({'price_type': PRICE_3RD_CHILD})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You cannot use a 3rd child discount")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_handle_serious_illness(self):
        self.login()
        self.create_place({'serious_illness': '1'})

        resp = self.client.get(self.url)
        self.assertContains(resp, "Must be approved by leader due to serious illness/condition")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_minimum_age(self):
        self.login()
        # if born Aug 31st 2001, and thisyear == 2012, should be allowed on camp with
        # minimum_age == 11
        Booking.objects.all().delete()
        self.create_place({'date_of_birth': '%d-08-31' %
                           (get_thisyear() - self.camp_minimum_age)})
        resp = self.client.get(self.url)
        self.assertNotContains(resp, "below the minimum age")

        # if born 1st Sept 2001, and thisyear == 2012, should not be allowed on camp with
        # minimum_age == 11
        Booking.objects.all().delete()
        self.create_place({'date_of_birth': '%d-09-01' %
                           (get_thisyear() - self.camp_minimum_age)})
        resp = self.client.get(self.url)
        self.assertContains(resp, "below the minimum age")

    def test_maximum_age(self):
        self.login()
        # if born 1st Sept 2001, and thisyear == 2019, should be allowed on camp with
        # maximum_age == 17
        Booking.objects.all().delete()
        self.create_place({'date_of_birth': '%d-09-01' %
                           (get_thisyear() - (self.camp_maximum_age + 1))})
        resp = self.client.get(self.url)
        self.assertNotContains(resp, "above the maximum age")

        # if born Aug 31st 2001, and thisyear == 2019, should not be allowed on camp with
        # maximum_age == 17
        Booking.objects.all().delete()
        self.create_place({'date_of_birth': '%d-08-31' %
                           (get_thisyear() - (self.camp_maximum_age + 1))})
        resp = self.client.get(self.url)
        self.assertContains(resp, "above the maximum age")

    def test_no_places_left(self):
        self.login()
        for i in range(0, self.camp.max_campers):
            self.create_place({'sex':'m'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'m'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are no places left on this camp")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

        # Don't want a redundant message
        self.assertNotContains(resp, "There are no places left for boys")

    def test_no_male_places_left(self):
        self.login()
        for i in range(0, self.camp.max_male_campers):
            self.create_place({'sex': 'm'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'m'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are no places left for boys")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

        # Check that we can still book female places
        Booking.objects.filter(state=BOOKING_INFO_COMPLETE).delete()
        self.create_place({'sex':'f'})
        resp = self.client.get(self.url)
        self.assertNotContains(resp, "There are no places left")
        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)

    def test_no_female_places_left(self):
        self.login()
        for i in range(0, self.camp.max_female_campers):
            self.create_place({'sex': 'f'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'f'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are no places left for girls")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_not_enough_places_left(self):
        self.login()
        for i in range(0, self.camp.max_campers - 1):
            self.create_place({'sex':'m'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'f'})
        self.create_place({'sex':'f'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are not enough places left on this camp")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_not_enough_male_places_left(self):
        self.login()
        for i in range(0, self.camp.max_male_campers - 1):
            self.create_place({'sex':'m'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'m'})
        self.create_place({'sex':'m'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are not enough places for boys left on this camp")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_not_enough_female_places_left(self):
        self.login()
        for i in range(0, self.camp.max_female_campers - 1):
            self.create_place({'sex':'f'})
        self.camp.bookings.update(state=BOOKING_BOOKED)

        self.create_place({'sex':'f'})
        self.create_place({'sex':'f'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "There are not enough places for girls left on this camp")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)

    def test_handle_two_problem_bookings(self):
        # Test the error we get for more than one problem booking
        self.login()
        self.create_place({'price_type': PRICE_CUSTOM})
        self.create_place({'name': 'Another Child',
                           'price_type': PRICE_CUSTOM})
        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, "Camp 1")
        self.assertContains(resp, "Frédéric Bloggs")
        self.assertContains(resp, "TBA")
        self.assertContains(resp, "A custom discount needs to be arranged by the booking secretary")
        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)
        self.assertContains(resp, "These places cannot be booked for the reasons described above")

    def test_handle_mixed_problem_and_non_problem(self):
        # Test the message we get if one place is bookable and the other is not
        self.login()
        self.create_place() # bookable
        self.create_place({'name': 'Another Child',
                           'price_type': PRICE_CUSTOM}) # not bookable
        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, DISABLED_BOOK_NOW_BTN)
        self.assertContains(resp, "One or more of the places cannot be booked")

    def test_total(self):
        self.login()
        self.create_place()
        self.create_place({'name': 'Another Child'})

        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, "£200")

    def test_manually_approved(self):
        # manually approved places should appear as OK to book
        self.login()
        self.create_place() # bookable
        self.create_place({'name': 'Another Child',
                           'price_type': PRICE_CUSTOM}) # not bookable
        Booking.objects.filter(price_type=PRICE_CUSTOM).update(state=BOOKING_APPROVED,
                                                               amount_due=Decimal('0.01'))
        resp = self.client.get(self.url)
        self.assertEqual(200, resp.status_code)

        self.assertContains(resp, "Camp 1")
        self.assertContains(resp, "Frédéric Bloggs")
        self.assertContains(resp, "£100")
        self.assertContains(resp, "This place can be booked")

        self.assertContains(resp, "Another Child")
        self.assertContains(resp, "£0.01")

        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)
        # Total:
        self.assertContains(resp, "£100.01")

    def test_add_another_btn(self):
        self.login()
        self.create_place()
        resp = self.client.post(self.url, {'add_another': '1'})
        self.assertEqual(302, resp.status_code)
        newpath = reverse('cciw.bookings.views.add_place')
        self.assertTrue(resp['Location'].endswith(newpath))

    def test_move_to_shelf(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        self.assertEqual(b.shelved, False)
        resp = self.client.post(self.url)

        # Move to shelf button should be there
        self.assertContains(resp, "name=\"shelve_%s\"" % b.id)

        # Now click it
        resp2 = self.client.post(self.url, {'shelve_%s' % b.id: '1'})

        # Should be changed
        b2 = acc.bookings.all()[0]
        self.assertEqual(b2.shelved, True)

        # Different button should appear
        self.assertNotContains(resp2, "name=\"shelve_%s\"" % b.id)
        self.assertContains(resp2, "name=\"unshelve_%s\"" % b.id)

        self.assertContains(resp2, "<h2>Shelf</h2>")

    def test_move_to_basket(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        b.shelved = True
        b.save()

        # Move to basket button should be there
        resp = self.client.get(self.url)
        self.assertContains(resp, "name=\"unshelve_%s\"" % b.id)

        # Now click it
        resp2 = self.client.post(self.url, {'unshelve_%s' % b.id: '1'})

        # Should be changed
        b2 = acc.bookings.all()[0]
        self.assertEqual(b2.shelved, False)

        # Shelf section should disappear.
        self.assertNotContains(resp2, "<h2>Shelf</h2>")

    def test_delete_place(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        resp = self.client.post(self.url)

        # Delete button should be there
        self.assertContains(resp, "name=\"delete_%s\"" % b.id)

        # Now click it
        resp2 = self.client.post(self.url, {'delete_%s' % b.id: '1'})

        # Should be gone
        self.assertEqual(0, acc.bookings.count())

    def test_edit_place_btn(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        b = acc.bookings.all()[0]
        resp = self.client.post(self.url)

        # Delete button should be there
        self.assertContains(resp, "name=\"edit_%s\"" % b.id)

        # Now click it
        resp2 = self.client.post(self.url, {'edit_%s' % b.id: '1'})
        self.assertEqual(resp2.status_code, 302)
        self.assertTrue(resp2['Location'].endswith(reverse('cciw.bookings.views.edit_place', kwargs={'id':b.id})))

    def test_book_ok(self):
        """
        Test that we can book a place
        """
        self.login()
        self.create_place()
        resp = self.client.get(self.url)
        state_token = re.search(r'name="state_token" value="(.*)"', resp.content).groups()[0]
        resp2 = self.client.post(self.url, {'state_token': state_token,
                                            'book_now': '1'})
        acc = self.get_account()
        b = acc.bookings.all()[0]
        self.assertEqual(b.state, BOOKING_BOOKED)
        self.assertEqual(resp2.status_code, 302)
        self.assertTrue(resp2['Location'].endswith(reverse('cciw.bookings.views.pay')))

    def test_book_unbookable(self):
        """
        Test that an unbookable place can't be booked
        """
        self.login()
        self.create_place({'serious_illness': '1'})
        resp = self.client.get(self.url)
        state_token = re.search(r'name="state_token" value="(.*)"', resp.content).groups()[0]
        resp2 = self.client.post(self.url, {'state_token': state_token,
                                            'book_now': '1'})
        acc = self.get_account()
        b = acc.bookings.all()[0]
        self.assertEqual(b.state, BOOKING_INFO_COMPLETE)
        self.assertContains(resp2, "These places cannot be booked")

    def test_book_one_unbookable(self):
        """
        Test that if one places is unbookable, no place can be booked
        """
        self.login()
        self.create_place()
        self.create_place({'serious_illness': '1'})
        resp = self.client.get(self.url)
        state_token = re.search(r'name="state_token" value="(.*)"', resp.content).groups()[0]
        resp2 = self.client.post(self.url, {'state_token': state_token,
                                            'book_now': '1'})
        acc = self.get_account()
        for b in acc.bookings.all():
            self.assertEqual(b.state, BOOKING_INFO_COMPLETE)
        self.assertContains(resp2, "These places cannot be booked")

    def test_same_name_same_camp(self):
        self.login()
        self.create_place()
        self.create_place() # Identical

        resp = self.client.get(self.url)
        self.assertContains(resp, "You have entered another set of place details for a camper called")
        # This is only a warning:
        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)

    def test_warn_about_multiple_full_price(self):
        self.login()
        self.create_place()
        self.create_place({'name': 'Mary Bloggs'})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You have multiple places at &#39;Full price")
        self.assertContains(resp, "If Mary Bloggs and Frédéric Bloggs")
        # This is only a warning:
        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)

        # Check for more than 2
        self.create_place({'name': 'Peter Bloggs'})
        resp = self.client.get(self.url)
        self.assertContains(resp, "If Mary Bloggs, Peter Bloggs and Frédéric Bloggs")

    def test_warn_about_multiple_2nd_child(self):
        self.login()
        self.create_place()
        self.create_place({'name': 'Mary Bloggs',
                           'price_type': PRICE_2ND_CHILD})
        self.create_place({'name': 'Peter Bloggs',
                           'price_type': PRICE_2ND_CHILD})

        resp = self.client.get(self.url)
        self.assertContains(resp, "You have multiple places at &#39;2nd child")
        self.assertContains(resp, "If Peter Bloggs and Mary Bloggs")
        self.assertContains(resp, "one is eligible")
        # This is only a warning:
        self.assertContains(resp, ENABLED_BOOK_NOW_BUTTON)


        self.create_place({'name': 'Zac Bloggs',
                           'price_type': PRICE_2ND_CHILD})
        resp = self.client.get(self.url)
        self.assertContains(resp, "2 are eligible")


    def test_book_now_safeguard(self):
        # It might be possible to alter the list of items in the basket in one
        # tab, and then press 'Book now' from an out-of-date representation of
        # the basket. We need a safeguard against this.

        # Must include at least id,price,camp choice for each booking
        self.login()
        self.create_place()
        resp = self.client.get(self.url)
        state_token = re.search(r'name="state_token" value="(.*)"', resp.content).groups()[0]

        # Now modify
        acc = self.get_account()
        b = acc.bookings.all()[0]
        b.south_wales_transport = True
        b.auto_set_amount_due()
        b.save()

        resp2 = self.client.post(self.url, {'state_token': state_token,
                                            'book_now': '1'})

        # Should not be modified
        b = acc.bookings.all()[0]
        self.assertEqual(b.state, BOOKING_INFO_COMPLETE)
        self.assertContains(resp2, "Places were not booked due to modifications made")

    def test_book_with_money_in_account(self):
        self.login()
        self.create_place()

        # Put some money in my account.
        acc = self.get_account()
        acc.receive_payment(acc.bookings.all()[0].amount_due)
        acc.save()

        # Book
        resp = self.client.get(self.url)
        state_token = re.search(r'name="state_token" value="(.*)"', resp.content).groups()[0]

        resp2 = self.client.post(self.url, {'state_token': state_token,
                                            'book_now': '1'},
                                 follow=True)

        # Place should be booked AND should not expire
        b = acc.bookings.all()[0]
        self.assertEqual(b.state, BOOKING_BOOKED)
        self.assertEqual(b.booking_expires, None)

        # balance should be zero
        acc = self.get_account()
        self.assertEqual(acc.get_balance(), Decimal('0.00'))
        self.assertEqual(acc.get_balance(confirmed_only=True), Decimal('0.00'))


class TestPay(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.list_bookings')

    def test_balance_empty(self):
        self.login()
        self.add_prices()
        resp = self.client.get(reverse('cciw.bookings.views.pay'))
        self.assertContains(resp, '£0.00')

    def test_balance_after_booking(self):
        self.login()
        self.create_place()
        self.create_place()
        acc = self.get_account()
        acc.bookings.all().update(state=BOOKING_BOOKED)

        resp = self.client.get(reverse('cciw.bookings.views.pay'))

        # 2 places:
        expected_price = 2 * Price.objects.get(year=get_thisyear(),
                                               price_type=PRICE_FULL).price
        self.assertContains(resp, %s' % expected_price)


class TestPayReturnPoints(LogInMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.list_bookings')

    def test_pay_done(self):
        self.login()
        resp = self.client.get(reverse('cciw.bookings.views.pay_done'))
        self.assertEqual(resp.status_code, 200)
        # Paypal posts to these, check we support that
        resp = self.client.post(reverse('cciw.bookings.views.pay_done'), {})
        self.assertEqual(resp.status_code, 200)

    def test_pay_cancelled(self):
        self.login()
        resp = self.client.get(reverse('cciw.bookings.views.pay_cancelled'))
        self.assertEqual(resp.status_code, 200)
        # Paypal posts to these, check we support that
        resp = self.client.post(reverse('cciw.bookings.views.pay_cancelled'), {})
        self.assertEqual(resp.status_code, 200)


class TestPaymentReceived(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    def test_receive_payment(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        book_basket_now(acc.bookings.basket(self.camp.year))
        self.assertTrue(acc.bookings.all()[0].booking_expires is not None)

        p = Price.objects.get(year=get_thisyear(), price_type=PRICE_FULL).price
        acc.receive_payment(p)

        acc = self.get_account()

        # Check we updated the account
        self.assertEqual(acc.total_received, p)

        # Check we updated the bookings
        self.assertTrue(acc.bookings.all()[0].booking_expires is None)

    def test_insufficient_receive_payment(self):
        self.login()
        self.create_place()
        self.create_place({'price_type': PRICE_2ND_CHILD})
        acc = self.get_account()
        book_basket_now(acc.bookings.basket(self.camp.year))
        self.assertTrue(acc.bookings.all()[0].booking_expires is not None)

        p1 = Price.objects.get(year=get_thisyear(), price_type=PRICE_FULL).price
        p2 = Price.objects.get(year=get_thisyear(), price_type=PRICE_2ND_CHILD).price

        # Between the two
        p = (p1 + p2) / 2
        acc.receive_payment(p)

        # Check we updated the account
        self.assertEqual(acc.total_received, p)

        # Check we updated the one we had enough funds for
        self.assertTrue(acc.bookings.filter(price_type=PRICE_2ND_CHILD)[0].booking_expires is None)
        # but not the one which was too much.
        self.assertTrue(acc.bookings.filter(price_type=PRICE_FULL)[0].booking_expires is not None)


        # We can rectify it with a payment of the rest
        acc.receive_payment((p1 + p2) - p)
        self.assertTrue(acc.bookings.filter(price_type=PRICE_FULL)[0].booking_expires is None)

    def test_email_for_bad_payment_1(self):
        from cciw.bookings.models import paypal_payment_received

        class IpnMock(object):
            pass

        ipn_1 = IpnMock()
        ipn_1.id = 123
        ipn_1.mc_gross = Decimal('1.00')
        ipn_1.custom = "x" # wrong format

        mail.outbox = []
        self.assertEqual(len(mail.outbox), 0)
        paypal_payment_received(ipn_1)

        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue('/admin/ipn/paypal' in mail.outbox[0].body)

    def test_email_for_bad_payment_2(self):
        from cciw.bookings.models import paypal_payment_received

        class IpnMock(object):
            pass

        ipn_1 = IpnMock()
        ipn_1.id = 123
        ipn_1.mc_gross = Decimal('1.00')
        ipn_1.custom = "account:1234;" # bad id

        mail.outbox = []
        self.assertEqual(len(mail.outbox), 0)
        paypal_payment_received(ipn_1)

        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue('/admin/ipn/paypal' in mail.outbox[0].body)

    def test_receive_payment_handler(self):
        # Use the actual signal handler, check the good path.
        from cciw.bookings.models import paypal_payment_received
        self.login()

        from paypal.standard.ipn.models import PayPalIPN
        class IpnMock(object):
            pass

        ipn_1 = PayPalIPN.objects.create(mc_gross = Decimal('1.00'),
                                         custom = "account:%s;" % self.get_account().id,
                                         ipaddress='127.0.0.1',
                                         )
        mail.outbox = []
        self.assertEqual(len(mail.outbox), 0)
        paypal_payment_received(ipn_1)

        # Since payments are processed in a separate process, we cannot
        # test that the account was updated in this process.
        # But we can test for Payment objects
        self.assertEqual(Payment.objects.count(), 1)
        self.assertEqual(Payment.objects.all()[0].amount, ipn_1.mc_gross)

    def test_email_for_good_payment(self):
        # This email could be triggered by whenever BookingAccount.distribute_funds
        # is called, which can be from multiple routes. So we test it directly.

        self.login()
        self.create_place()
        acc = self.get_account()
        book_basket_now(acc.bookings.basket(self.camp.year))

        mail.outbox = []
        acc.receive_payment(acc.bookings.all()[0].amount_due)

        self.assertEqual(len(mail.outbox), 1)

        self.assertEqual(mail.outbox[0].subject, "CCIW booking - place confirmed")
        self.assertEqual(mail.outbox[0].to, [self.email])
        self.assertTrue("Thank you for your payment" in mail.outbox[0].body)

    def test_only_one_email_for_multiple_places(self):
        self.login()
        self.create_place()
        self.create_place({'name': 'Another Child'})

        acc = self.get_account()
        book_basket_now(acc.bookings.basket(self.camp.year))

        mail.outbox = []
        acc.receive_payment(acc.get_balance())

        self.assertEqual(len(mail.outbox), 1)

        self.assertEqual(mail.outbox[0].subject, "CCIW booking - place confirmed")
        self.assertEqual(mail.outbox[0].to, [self.email])
        self.assertTrue(self.place_details['name'] in mail.outbox[0].body)
        self.assertTrue('Another Child' in mail.outbox[0].body)

    def test_concurrent_save(self):
        acc1 = BookingAccount.objects.create(email='foo@foo.com')
        acc2 = BookingAccount.objects.get(email='foo@foo.com')

        acc1.receive_payment(Decimal('100.00'))

        self.assertEqual(BookingAccount.objects.get(email='foo@foo.com').total_received,
                         Decimal('100.00'))

        acc2.save() # this will have total_received = 0.00

        self.assertEqual(BookingAccount.objects.get(email='foo@foo.com').total_received,
                         Decimal('100.00'))


class TestAjaxViews(CreatePlaceMixin, TestCase):
    # Basic tests to ensure that the views that serve AJAX return something
    # sensible

    fixtures = ['basic.json', 'officers_users.json']

    def test_places_json(self):
        self.login()
        self.create_place()
        resp = self.client.get(reverse('cciw.bookings.views.places_json'))
        json = simplejson.loads(resp.content)
        self.assertEqual(json['places'][0]['name'], self.place_details['name'])

    def test_places_json_with_exclusion(self):
        self.login()
        self.create_place()
        acc = self.get_account()
        resp = self.client.get(reverse('cciw.bookings.views.places_json') +
                               ("?exclude=%d" % acc.bookings.all()[0].id))
        json = simplejson.loads(resp.content)
        self.assertEqual(json['places'], [])

    def test_places_json_with_bad_exclusion(self):
        self.login()
        resp = self.client.get(reverse('cciw.bookings.views.places_json') +"?exclude=x")
        json = simplejson.loads(resp.content)
        self.assertEqual(json['places'], [])

    def test_account_json(self):
        self.login()
        acc = self.get_account()
        acc.address = '123 Main Street'
        acc.save()

        resp = self.client.get(reverse('cciw.bookings.views.account_json'))
        json = simplejson.loads(resp.content)
        self.assertEqual(json['account']['address'], '123 Main Street')

    def test_all_account_json(self):
        acc1 = BookingAccount.objects.create(email="foo@foo.com",
                                             post_code="ABC",
                                             name="Mr Foo")
        acc2 = BookingAccount.objects.create(email="goo@foo.com",
                                             post_code="XYZ",
                                             name="Mr Goo")

        self.client.login(username=OFFICER_USERNAME, password=OFFICER_PASSWORD)
        resp = self.client.get(reverse('cciw.bookings.views.all_account_json'))
        self.assertEqual(resp.status_code, 403)

        # Now as booking secretary
        self.client.login(username=BOOKING_SEC_USERNAME, password=BOOKING_SEC_PASSWORD)
        resp = self.client.get(reverse('cciw.bookings.views.all_account_json') + "?id=%d" % acc1.id)
        self.assertEqual(resp.status_code, 200)

        json = simplejson.loads(resp.content)
        self.assertEqual(json['account']['post_code'], 'ABC')

    def test_booking_problems(self):
        acc1 = BookingAccount.objects.create(email="foo@foo.com",
                                             post_code="ABC",
                                             name="Mr Foo")
        self.client.login(username=BOOKING_SEC_USERNAME, password=BOOKING_SEC_PASSWORD)
        resp = self.client.post(reverse('cciw.bookings.views.booking_problems_json'),
                                {'account':str(acc1.id)})


        self.assertEqual(resp.status_code, 200)
        json = simplejson.loads(resp.content)
        self.assertEqual(json['valid'], False)

        data = self.place_details.copy()
        data['account'] = str(acc1.id)
        data['created_0'] = '1970-01-01' # Simulate form, which doesn't supply created
        data['created_1'] = '00:00:00'
        data['state'] = BOOKING_APPROVED
        data['amount_due'] = '100.00'
        data['price_type'] = PRICE_CUSTOM
        resp = self.client.post(reverse('cciw.bookings.views.booking_problems_json'),
                                data)

        json = simplejson.loads(resp.content)
        self.assertEqual(json['valid'], True)
        problems = json['problems']
        self.assertTrue(u"A custom discount needs to be arranged by the booking secretary" in
                        problems)


    def test_booking_problems_price_check(self):
        # Test that the price is checked.
        # This is a check that is only run for booking secretary
        self.add_prices()
        acc1 = BookingAccount.objects.create(email="foo@foo.com",
                                             post_code="ABC",
                                             name="Mr Foo")
        self.client.login(username=BOOKING_SEC_USERNAME, password=BOOKING_SEC_PASSWORD)

        data = self.place_details.copy()
        data['account'] = str(acc1.id)
        data['created_0'] = '1970-01-01'
        data['created_1'] = '00:00:00'
        data['state'] = BOOKING_BOOKED
        data['amount_due'] = '0.00'
        data['price_type'] = PRICE_FULL
        resp = self.client.post(reverse('cciw.bookings.views.booking_problems_json'),
                                data)

        json = simplejson.loads(resp.content)
        problems = json['problems']
        p_full = Price.objects.get(price_type=PRICE_FULL, year=get_thisyear())
        self.assertTrue(any(p.startswith(u"The 'amount due' is not the expected value of £%s"
                                         % p_full.price)
                            for p in problems))


    def test_booking_problems_deposit_check(self):
        # Test that the price is checked.
        # This is a check that is only run for booking secretary
        self.add_prices()
        acc1 = BookingAccount.objects.create(email="foo@foo.com",
                                             post_code="ABC",
                                             name="Mr Foo")
        self.client.login(username=BOOKING_SEC_USERNAME, password=BOOKING_SEC_PASSWORD)

        data = self.place_details.copy()
        data['account'] = str(acc1.id)
        data['created_0'] = '1970-01-01'
        data['created_1'] = '00:00:00'
        data['state'] = BOOKING_CANCELLED
        data['amount_due'] = '0.00'
        data['price_type'] = PRICE_FULL
        resp = self.client.post(reverse('cciw.bookings.views.booking_problems_json'),
                                data)

        json = simplejson.loads(resp.content)
        problems = json['problems']
        p_deposit = Price.objects.get(price_type=PRICE_DEPOSIT, year=get_thisyear())
        self.assertTrue(any(p.startswith(u"The 'amount due' is not the expected value of £%s"
                                         % p_deposit.price)
                            for p in problems))


class TestAccountOverview(CreatePlaceMixin, TestCase):

    fixtures = ['basic.json']

    url = reverse('cciw.bookings.views.account_overview')

    def test_show(self):
        self.login()

        acc = self.get_account()

        # Book a place and pay
        self.create_place()
        book_basket_now(acc.bookings.basket(self.camp.year))
        acc.receive_payment(acc.bookings.all()[0].amount_due)

        # Book another
        self.create_place({'name': 'Another Child'})
        book_basket_now(acc.bookings.basket(self.camp.year))

        # 3rd place, not booked at all
        self.create_place({'name': '3rd child'})

        resp = self.client.get(self.url)

        # Another one, so that messages are cleared
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

        # Confirmed place
        self.assertContains(resp, self.place_details['name'])


        # Booked place
        self.assertContains(resp, 'Another Child')
        self.assertContains(resp, 'remember to pay')

        # Basket/Shelf
        self.assertContains(resp, 'items in your basket')


class TestLogOut(LogInMixin, TestCase):

    fixtures = ['basic']

    url = reverse('cciw.bookings.views.logout')

    def test_get(self):
        self.login()
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)

    def test_logout(self):
        self.login()
        resp = self.client.post(self.url, {'logout': '1'})
        self.assertEqual(resp.status_code, 302)

        # Try accessing a page which is restricted
        resp2 = self.client.get(reverse('cciw.bookings.views.account_overview'))
        self.assertEqual(resp2.status_code, 302)


class TestExpireBookingsCommand(CreatePlaceMixin, TestCase):

    fixtures = ['basic']

    def test_just_created(self):
        """
        Test no mail if just created
        """
        self.login()
        self.create_place()

        acc = self.get_account()
        book_basket_now(acc.bookings.basket(get_thisyear()))

        mail.outbox = []

        ExpireBookingsCommand().handle()
        self.assertEqual(len(mail.outbox), 0)

    def test_warning(self):
        """
        Test that we get a warning email after 12 hours
        """
        self.login()
        self.create_place()

        acc = self.get_account()
        book_basket_now(acc.bookings.basket(get_thisyear()))
        b = acc.bookings.all()[0]
        b.booking_expires = b.booking_expires - timedelta(0.49)
        b.save()

        mail.outbox = []
        ExpireBookingsCommand().handle()
        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue("warning" in mail.outbox[0].subject)

        b = acc.bookings.all()[0]
        self.assertNotEqual(b.booking_expires, None)
        self.assertEqual(b.state, BOOKING_BOOKED)

    def test_expires(self):
        """
        Test that we get an expiry email after 24 hours
        """
        self.login()
        self.create_place()

        acc = self.get_account()
        book_basket_now(acc.bookings.basket(get_thisyear()))
        b = acc.bookings.all()[0]
        b.booking_expires = b.booking_expires - timedelta(1.01)
        b.save()

        mail.outbox = []
        ExpireBookingsCommand().handle()
        # NB - should get one, not two (shouldn't get warning)
        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue("expired" in mail.outbox[0].subject)
        self.assertTrue("have expired" in mail.outbox[0].body)

        b = acc.bookings.all()[0]
        self.assertEqual(b.booking_expires, None)
        self.assertEqual(b.state, BOOKING_INFO_COMPLETE)

    def test_grouping(self):
        """
        Test the emails are grouped as we expect
        """
        self.login()
        self.create_place({'name':'Child One'})
        self.create_place({'name':'Child Two'})

        acc = self.get_account()
        book_basket_now(acc.bookings.basket(get_thisyear()))
        acc.bookings.update(booking_expires = datetime.now() - timedelta(1))

        mail.outbox = []
        ExpireBookingsCommand().handle()

        # Should get one, not two, because they will be grouped.
        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue("expired" in mail.outbox[0].subject)
        self.assertTrue("have expired" in mail.outbox[0].body)
        self.assertTrue("Child One" in mail.outbox[0].body)
        self.assertTrue("Child Two" in mail.outbox[0].body)

        for b in acc.bookings.all():
            self.assertEqual(b.booking_expires, None)
            self.assertEqual(b.state, BOOKING_INFO_COMPLETE)


class TestChequePayment(TestCase):

    def test_create(self):
        acc = BookingAccount.objects.create(email='foo@foo.com')
        self.assertEqual(Payment.objects.count(), 0)
        ChequePayment.objects.create(account=acc,
                                     amount=Decimal('100.00'))
        self.assertEqual(Payment.objects.count(), 1)

    def test_delete(self):
        # Setup
        acc = BookingAccount.objects.create(email='foo@foo.com')
        cp = ChequePayment.objects.create(account=acc,
                                          amount=Decimal('100.00'))
        Payment.objects.all().delete() # reset

        # Test
        cp.delete()
        self.assertEqual(Payment.objects.count(), 1)
        self.assertEqual(Payment.objects.all()[0].amount, -cp.amount)

    def test_edit(self):
        # Setup
        acc = BookingAccount.objects.create(email='foo@foo.com')
        cp = ChequePayment.objects.create(account=acc,
                                          amount=Decimal('100.00'))

        cp.amount=Decimal("101.00")
        self.assertRaises(Exception, cp.save)


class TestCancel(CreatePlaceMixin, TestCase):
    """
    Tests covering what happens when a user cancels.
    """
    fixtures = ['basic.json']

    def test_amount_due(self):
        self.create_place()
        acc = self.get_account()
        place = acc.bookings.all()[0]
        place.state = BOOKING_CANCELLED
        self.assertEqual(place.expected_amount_due(), Price.objects.get(price_type=PRICE_DEPOSIT).price)

    def test_account_amount_due(self):
        self.create_place()
        acc = self.get_account()
        place = acc.bookings.all()[0]
        place.state = BOOKING_CANCELLED
        place.auto_set_amount_due()
        place.save()

        acc = self.get_account()
        self.assertEqual(acc.get_balance(), place.amount_due)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.