rt 4.2.13 ticket#13852
[freeside.git] / rt / lib / RT / Test / SMIME.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 use strict;
50 use warnings;
51 use 5.010;
52
53 package RT::Test::SMIME;
54
55 use Test::More;
56 use base qw(RT::Test);
57 use File::Temp qw(tempdir);
58
59 sub import {
60     my $class = shift;
61     my %args  = @_;
62     my $t     = $class->builder;
63
64     $t->plan( skip_all => 'openssl executable is required.' )
65         unless RT::Test->find_executable('openssl');
66
67     require RT::Crypt;
68     $class->SUPER::import(%args);
69
70     $class->set_rights(
71         Principal => 'Everyone',
72         Right => ['CreateTicket', 'ShowTicket', 'SeeQueue', 'ReplyToTicket', 'ModifyTicket'],
73     );
74
75     $class->export_to_level(1);
76 }
77
78 sub bootstrap_more_config {
79     my $self = shift;
80     my $handle = shift;
81     my $args = shift;
82
83     $self->SUPER::bootstrap_more_config($handle, $args, @_);
84
85     my $openssl = $self->find_executable('openssl');
86
87     my $keyring = $self->keyring_path;
88     mkdir($keyring);
89
90     my $ca = $self->key_path("demoCA", "cacert.pem");
91
92     print $handle qq{
93         Set(\%GnuPG, Enable => 0);
94         Set(\%SMIME =>
95             Enable => 1,
96             Passphrase => {
97                 'root\@example.com' => '123456',
98                 'sender\@example.com' => '123456',
99             },
100             OpenSSL => q{$openssl},
101             Keyring => q{$keyring},
102             CAPath  => q{$ca},
103         );
104         Set(\@MailPlugins => qw(Auth::MailFrom Auth::Crypt));
105     };
106
107 }
108
109 sub keyring_path {
110     return File::Spec->catfile( RT::Test->temp_directory, "smime" );
111 }
112
113 sub key_path {
114     my $self = shift;
115     my $keys = RT::Test::get_abs_relocatable_dir(
116         (File::Spec->updir()) x 2,
117         qw(data smime keys),
118     );
119     return File::Spec->catfile( $keys => @_ ),
120 }
121
122 sub mail_set_path {
123     my $self = shift;
124     return RT::Test::get_abs_relocatable_dir(
125         (File::Spec->updir()) x 2,
126         qw(data smime mails),
127     );
128 }
129
130 sub import_key {
131     my $self = shift;
132     my $key  = shift;
133     my $user = shift;
134
135     my $path = RT::Test::find_relocatable_path( 'data', 'smime', 'keys' );
136     die "can't find the dir where smime keys are stored"
137         unless $path;
138
139     my $keyring = RT->Config->Get('SMIME')->{'Keyring'};
140     die "SMIME keyring '$keyring' doesn't exist"
141         unless $keyring && -e $keyring;
142
143     $key .= ".pem" unless $key =~ /\.(pem|crt|key)$/;
144
145     my $content = RT::Test->file_content( [ $path, $key ] );
146
147     if ( $user ) {
148         my ($status, $msg) = $user->SetSMIMECertificate( $content );
149         die "Couldn't set CF: $msg" unless $status;
150     } else {
151         my $keyring = RT->Config->Get('SMIME')->{'Keyring'};
152         die "SMIME keyring '$keyring' doesn't exist"
153             unless $keyring && -e $keyring;
154
155         open my $fh, '>:raw', File::Spec->catfile($keyring, $key)
156             or die "can't open file: $!";
157         print $fh $content;
158         close $fh;
159     }
160
161     return;
162 }
163
164 1;