5 use Test::More tests => 13;
9 use FS::cust_main; # to load all other tables
11 my $fs = FS::Test->new( user => 'admin' );
12 my $conf = FS::Conf->new;
14 my @tables = qw(cust_payby cust_pay_pending cust_pay cust_pay_void cust_refund);
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('');
19 ### we need to unencrypt our test db before we can test turning it on
21 # temporarily load all payinfo into memory
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');
32 foreach my $config ( qw(encryption encryptionmodule encryptionpublickey encryptionprivatekey) ) {
33 $conf->delete($config);
34 ok( !$conf->exists($config), "deleted $config" ) or BAIL_OUT('');
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'));
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;
54 ok( !$err, "save unencrypted values" ) or BAIL_OUT($err);
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';
73 ok( !$err, "all values unencrypted" ) or BAIL_OUT($err);
76 $err = system('freeside-upgrade','admin');
77 ok( !$err, 'upgrade ran' ) or BAIL_OUT('Error string: '.$!);
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('');
84 # check that known records got encrypted
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';
101 ok( !$err, "all values encrypted" ) or BAIL_OUT($err);