73085: Enable credit card/ach encryption on a live system
[freeside.git] / FS / t / suite / 15-activate_encryption.t
1 #!/usr/bin/perl
2
3 use strict;
4 use FS::Test;
5 use Test::More tests => 13;
6 use FS::Conf;
7 use FS::UID qw( dbh );
8 use DateTime;
9 use FS::cust_main; # to load all other tables
10
11 my $fs = FS::Test->new( user => 'admin' );
12 my $conf = FS::Conf->new;
13 my $err;
14 my @tables = qw(cust_payby cust_pay_pending cust_pay cust_pay_void cust_refund);
15
16 ### can only run on test database (company name "Freeside Test")
17 like( $conf->config('company_name'), qr/^Freeside Test/, 'using test database' ) or BAIL_OUT('');
18
19 ### we need to unencrypt our test db before we can test turning it on
20
21 # temporarily load all payinfo into memory
22 my %payinfo = ();
23 foreach my $table (@tables) {
24   $payinfo{$table} = {};
25   foreach my $record ($fs->qsearch({ table => $table })) {
26     next unless grep { $record->payby eq $_ } @FS::Record::encrypt_payby;
27     $payinfo{$table}{$record->get($record->primary_key)} = $record->get('payinfo');
28   }
29 }
30
31 # turn off encryption
32 foreach my $config ( qw(encryption encryptionmodule encryptionpublickey encryptionprivatekey) ) {
33   $conf->delete($config);
34   ok( !$conf->exists($config), "deleted $config" ) or BAIL_OUT('');
35 }
36 $FS::Record::conf_encryption           = $conf->exists('encryption');
37 $FS::Record::conf_encryptionmodule     = $conf->config('encryptionmodule');
38 $FS::Record::conf_encryptionpublickey  = join("\n",$conf->config('encryptionpublickey'));
39 $FS::Record::conf_encryptionprivatekey = join("\n",$conf->config('encryptionprivatekey'));
40
41 # save unencrypted values
42 foreach my $table (@tables) {
43   local $FS::payinfo_Mixin::allow_closed_replace = 1;
44   local $FS::Record::no_update_diff = 1;
45   local $FS::UID::AutoCommit = 1;
46   my $tclass = 'FS::'.$table;
47   foreach my $key (keys %{$payinfo{$table}}) {
48     my $record = $tclass->by_key($key);
49     $record->payinfo($payinfo{$table}{$key});
50     $err = $record->replace;
51     last if $err;
52   }
53 }
54 ok( !$err, "save unencrypted values" ) or BAIL_OUT($err);
55
56 # make sure it worked
57 CHECKDECRYPT:
58 foreach my $table (@tables) {
59   my $tclass = 'FS::'.$table;
60   foreach my $key (sort {$a <=> $b} keys %{$payinfo{$table}}) {
61     my $sql = 'SELECT * FROM '.$table.
62               ' WHERE payinfo LIKE \'M%\''.
63               ' AND char_length(payinfo) > 80'.
64               ' AND '.$tclass->primary_key.' = '.$key;
65     my $sth = dbh->prepare($sql) or BAIL_OUT(dbh->errstr);
66     $sth->execute or BAIL_OUT($sth->errstr);
67     if (my $hashrec = $sth->fetchrow_hashref) {
68       $err = $table.' '.$key.' encrypted';
69       last CHECKDECRYPT;
70     }
71   }
72 }
73 ok( !$err, "all values unencrypted" ) or BAIL_OUT($err);
74
75 ### now, run upgrade
76 $err = system('freeside-upgrade','admin');
77 ok( !$err, 'upgrade ran' ) or BAIL_OUT('Error string: '.$!);
78
79 # check that confs got set
80 foreach my $config ( qw(encryption encryptionmodule encryptionpublickey encryptionprivatekey) ) {
81   ok( $conf->exists($config), "$config was set" ) or BAIL_OUT('');
82 }
83
84 # check that known records got encrypted
85 CHECKENCRYPT:
86 foreach my $table (@tables) {
87   my $tclass = 'FS::'.$table;
88   foreach my $key (sort {$a <=> $b} keys %{$payinfo{$table}}) {
89     my $sql = 'SELECT * FROM '.$table.
90               ' WHERE payinfo LIKE \'M%\''.
91               ' AND char_length(payinfo) > 80'.
92               ' AND '.$tclass->primary_key.' = '.$key;
93     my $sth = dbh->prepare($sql) or BAIL_OUT(dbh->errstr);
94     $sth->execute or BAIL_OUT($sth->errstr);
95     unless ($sth->fetchrow_hashref) {
96       $err = $table.' '.$key.' not encrypted';
97       last CHECKENCRYPT;
98     }
99   }
100 }
101 ok( !$err, "all values encrypted" ) or BAIL_OUT($err);
102
103 exit;
104
105 1;
106