App/Dochazka/REST/Util.pm

filter: Statementstmt 47.8% 22 / 46 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 45.7% 32 / 70 CC 6 SCAR 24.3
CC
6
Cov
40%
pre_update_comparison
30.0
hash_the_password
6.0
pod_to_html
2.0
CRAP 11.4
« App/Dochazka/REST/Holiday.pm ↑ Summary App/Dochazka/REST/ResourceDefs.pm »
1 # *************************************************************************
2 # Copyright (c) 2014-2017, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32  
33 package App::Dochazka::REST::Util;
34  
35 41 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 41 ▶use App::CELL qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 41 ▶use Authen::Passphrase::SaltedDigest;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 41 ▶use Pod::Simple::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44  
45 =head1 NAME
46  
47 App::Dochazka::REST::Util - miscellaneous utilities
48  
49  
50  
51  
52 =head1 SYNOPSIS
53  
54 Miscellaneous utilities
55  
56 use App::Dochazka::REST::Util;
57  
58 ...
59  
60  
61  
62  
63 =head1 EXPORTS
64  
65 This module provides the following exports:
66  
67 =over
68  
69 =item L<hash_the_password> (function)
70  
71 =item L<pod_to_html> (function)
72  
73 =item L<pre_update_comparison> (function)
74  
75 =back
76  
77 =cut
78  
79 41 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
80 41 our @EXPORT_OK = qw(
81 hash_the_password
82 pod_to_html
83 pre_update_comparison
84 );
85  
86  
87  
88  
89 =head1 FUNCTIONS
90  
91  
92 =head2 hash_the_password
93  
94 Takes a request entity (hashref) - looks for a 'password' property. If it
95 is present, adds a random salt to the request entity and hashes the
96 password with it. If there is no password property, the function does
97 nothing.
98  
99 =cut
100  
101 0 ▶sub hash_the_password {
Subroutine hash_the_password: not called CC=2 SCAR=17.9
Pod documented
102 0 my $entity = shift;
103 0 ▶ if ( $entity->{'password'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entity->{"password"}) 0 0 0
104 my $ppr = Authen::Passphrase::SaltedDigest->new(
105 algorithm => "SHA-512", salt_random => 20,
106 0 passphrase => $entity->{'password'}
107 );
108 0 delete $entity->{'password'};
109 0 $entity->{'passhash'} = $ppr->hash_hex;
110 0 $entity->{'salt'} = $ppr->salt_hex;
111 }
112 }
113  
114  
115 =head2 pod_to_html
116  
117 Each L<App::Dochazka::REST> resource definition includes a 'documentation'
118 property containing a POD string. Our 'docu/html' resource converts this
119 POD string into HTML with a little help from this routine.
120  
121 =cut
122  
123 0 ▶sub pod_to_html {
Subroutine pod_to_html: not called CC=1 SCAR=6.9
Pod documented
124 0 my ( $pod_str ) = @_;
125 0 $log->debug( "pod_to_html before: $pod_str" );
126 0 my $p = Pod::Simple::HTML->new;
127 0 $p->output_string(\my $html_str);
128 0 $p->parse_string_document($pod_str);
129  
130 # now $html contains a full-blown HTML file, of which only one part is of
131 # interest to us. That part starts with the line <!-- start doc -->
132 # and ends with <!-- end doc -->
133  
134 0 $html_str =~ s/.*<!-- start doc -->//s;
135 0 $html_str =~ s/<!-- end doc -->.*//s;
136  
137 0 $log->debug( "pod_to_html after: $html_str" );
138 0 return $html_str;
139 }
140  
141  
142 =head2 pre_update_comparison
143  
144 Given an original object and a hashref of possible changed properties,
145 compare the properties in the hashref with the corresponding properties
146 in the original object. If any properties really are changed, update
147 the object. Return the number of properties so changed.
148  
149 =cut
150  
151 0 ▶sub pre_update_comparison {
Subroutine pre_update_comparison: not called CC=5 SCAR=34.0
Pod documented
152 0 my ( $obj, $over ) = @_;
153 0 my $c = 0;
154 0 foreach my $prop (keys %$over) {
155 0 ▶ if ( exists $obj->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{$prop}) 0 0 0
156 0 ▶ next if not defined $obj->{$prop} and not defined $over->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{$prop} or defined $over->{$prop} 0 0 0
Condition0%0/3
defined $obj->{$prop} or defined $over->{$prop}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $obj->{$prop} or defined $over->{$prop}
defined $obj->{$prop} defined $over->{$prop}
Truth table0%0/3
A or B
A= defined $obj->{$prop}B= defined $over->{$prop}
ABresult
1X1
011
000
157 0 $log->debug( "pre_update_comparison: detected changed property $prop" );
158 # FIXME: how to test equality when we don't know the type?
159 0 $obj->{$prop} = $over->{$prop};
160 0 $c += 1;
161 }
162 }
163 0 return $c;
164 }
165  
166  
167 41 1;
« App/Dochazka/REST/Holiday.pm ↑ Summary App/Dochazka/REST/ResourceDefs.pm »