GraphQL/Policy.pm

filter: Statementstmt 100% 106 / 106 Branchbran 84.3% 54 / 64 Conditioncond 65.0% 13 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 15 / 15 Podpod 90.0% 9 / 10 totaltotal 89.4% 203 / 227 CC 45 SCAR 27.3
CC
45
Cov
91%
new
28.6
hidden
5.1
authorize
5.0
CRAP 15.3
↑ Summary GraphQL.pm »
1 package DBIO::GraphQL::Policy;
2 # ABSTRACT: What DBIO::GraphQL exposes and who may use it (the trust model)
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '0.900007';
7  
8 14 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use DBIO::GraphQL::Error qw(client_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The policy is the one place that knows the trust model of a generated
12 # schema: which sources and columns leave the boundary, which of them a
13 # client may write, which sources have mutations and what the authorize
14 # hook says about an operation. Filter, Mutation, Relationship and
15 # DBIO::GraphQL itself ask it; none of them decides on its own.
16  
17 # schema is the connected DBIO::Schema; required at construction.
18 14 __PACKAGE__->mk_group_accessors(simple => qw(
19 schema introspection _included _mutations _hide _readonly _authorize on_error
20 default_rows max_rows max_in max_depth max_cost
21 ));
22  
23 107 ▶sub new {
Subroutine new: called CC=28 SCAR=33.5
Pod undocumented
24 107 my ($class, %args) = @_;
25 die "DBIO::GraphQL::Policy: 'schema' is required\n"
26 107 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 107 107
27 107 my $self = bless {}, $class;
28 107 my $schema = $args{schema};
29 107 $self->schema($schema);
30  
31 107 my %known = map { $_ => 1 } $schema->sources;
32  
33 die "DBIO::GraphQL: 'sources' and 'except' cannot be combined\n"
34 107 ▶ if defined $args{sources} && defined $args{except};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"sources"} and defined $args{"except"} 1 106 107
Condition100%3/3
defined $args{"sources"} and defined $args{"except"}
!ll&&!rl&&r
10421
MC/DC100%2/2
defined $args{"sources"} and defined $args{"except"}
defined $args{"sources"} defined $args{"except"}
Truth table100%3/3
A and B
A= defined $args{"sources"}B= defined $args{"except"}
ABresult
0X0
100
111
35 106 my @all = sort keys %known;
36 106 my @included = @all;
37 106 ▶ if (defined $args{sources}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'sources'}) { } 2 104 106
elsif (defined $args{'except'}) { } 1 103 104
38 2 $self->_check_monikers(sources => $args{sources}, \%known);
39 1 my %want = map { $_ => 1 } @{ $args{sources} };
40 1 @included = grep { $want{$_} } @all;
41 }
42 elsif (defined $args{except}) {
43 1 $self->_check_monikers(except => $args{except}, \%known);
44 1 my %skip = map { $_ => 1 } @{ $args{except} };
45 1 @included = grep { !$skip{$_} } @all;
46 }
47 105 $self->_included({ map { $_ => 1 } @included });
48  
49 105 my $mutations = $args{mutations};
50 105 ▶ if (!$mutations) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $mutations) { } 70 35 105
elsif (ref $mutations eq 'ARRAY') { } 2 33 35
51 70 $self->_mutations({});
52 }
53 elsif (ref $mutations eq 'ARRAY') {
54 2 $self->_check_monikers(mutations => $mutations, $self->_included);
55 1 $self->_mutations({ map { $_ => 1 } @$mutations });
56 }
57 else {
58 33 $self->_mutations({ %{ $self->_included } });
59 }
60  
61 104 for my $opt (qw(hide readonly)) {
62 208 ▶ my $map = $args{$opt} // {};
Condition100%2/2
$args{$opt} // {}
l!l
2206
MC/DC100%1/1
$args{$opt} // {}
$args{$opt}
Truth table100%2/2
A
A= $args{$opt}
Aresult
11
00
63 208 ▶ die "DBIO::GraphQL: '$opt' must be a hashref of source => [columns]\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map eq "HASH" 0 208 208
64 unless ref $map eq 'HASH';
65 208 my %set;
66 208 for my $moniker (keys %$map) {
67 die "DBIO::GraphQL: '$opt' names unknown source '$moniker'\n"
68 3 ▶ unless $known{$moniker};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$moniker} 0 3 3
69 die "DBIO::GraphQL: '$opt' for source '$moniker' must be an arrayref of columns\n"
70 3 ▶ unless ref $map->{$moniker} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map->{$moniker} eq "ARRAY" 0 3 3
71 3 my $source = $schema->source($moniker);
72 3 for my $col (@{ $map->{$moniker} }) {
73 4 ▶ die "DBIO::GraphQL: '$opt' names unknown column '$col' on source '$moniker'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $source->has_column($col) 0 4 4
74 unless $source->has_column($col);
75 4 $set{$moniker}{$col} = 1;
76 }
77 }
78 208 $self->{"_$opt"} = \%set;
79 }
80  
81 104 ▶ if (defined $args{authorize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"authorize"}) 10 94 104
82 die "DBIO::GraphQL: 'authorize' must be a code reference\n"
83 10 ▶ unless ref $args{authorize} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"authorize"} eq "CODE" 0 10 10
84 10 $self->_authorize($args{authorize});
85 }
86  
87 104 ▶ if (defined $args{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"on_error"}) 10 94 104
88 die "DBIO::GraphQL: 'on_error' must be a code reference\n"
89 10 ▶ unless ref $args{on_error} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"on_error"} eq "CODE" 1 9 10
90 9 $self->on_error($args{on_error});
91 }
92  
93 103 my %limit = (default_rows => 100, max_rows => 1000, max_in => 500,
94 max_depth => 10, max_cost => 10000);
95 103 for my $opt (sort keys %limit) {
96 475 ▶ my $value = exists $args{$opt} ? $args{$opt} : $limit{$opt};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$opt} ? : 43 432 475
97 475 ▶ die "DBIO::GraphQL: '$opt' must be a positive integer\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/ 20 455 475
Condition50%3/6
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
020455
defined $value and not ref $value
!ll&&!rl&&r
00475
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
defined $value not ref $value $value =~ /\A[1-9][0-9]*\z/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
98 unless defined $value && !ref $value && $value =~ /\A[1-9][0-9]*\z/;
99 455 $self->$opt($value + 0);
100 }
101 83 ▶ die sprintf("DBIO::GraphQL: 'default_rows' (%d) must not exceed 'max_rows' (%d)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->default_rows > $self->max_rows 2 81 83
102 $self->default_rows, $self->max_rows)
103 if $self->default_rows > $self->max_rows;
104  
105 81 ▶ $self->introspection(exists $args{introspection} ? ($args{introspection} ? 1 : 0) : 1);
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'introspection'} ? : 2 79 81
$args{'introspection'} ? : 0 2 2
106  
107 81 return $self;
108 }
109  
110 5 ▶sub _check_monikers {
Subroutine _check_monikers: called CC=4 SCAR=13.9
111 5 my ($self, $opt, $list, $known) = @_;
112 5 ▶ die "DBIO::GraphQL: '$opt' must be an arrayref of source names\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 5 5
113 unless ref $list eq 'ARRAY';
114 5 for my $moniker (@$list) {
115 die "DBIO::GraphQL: '$opt' names unknown source '$moniker'\n"
116 6 ▶ unless $known->{$moniker};
Branch: 1 branch
BranchTrueFalseTotal
unless $known->{$moniker} 2 4 6
117 }
118 }
119  
120  
121 79 ▶sub monikers { sort keys %{ $_[0]->_included } }
Subroutine monikers: called CC=1 SCAR=0.0
Pod documented
122  
123  
124 94 ▶sub included { $_[0]->_included->{ $_[1] } ? 1 : 0 }
Subroutine included: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_included->{$_[1]} ? : 92 2 94
125  
126  
127 176 ▶sub has_mutations { $_[0]->_mutations->{ $_[1] } ? 1 : 0 }
Subroutine has_mutations: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_mutations->{$_[1]} ? : 76 100 176
128  
129  
130 2731 ▶sub hidden {
Subroutine hidden: called CC=5 SCAR=16.3
Pod documented
131 2731 my ($self, $moniker, $col) = @_;
132 2731 ▶ return 1 if $self->_hide->{$moniker}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_hide->{$moniker}{$col} 7 2724 2731
133 2724 ▶ my $info = $self->schema->source($moniker)->column_info($col) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->schema->source($moniker)->column_info($col) or return 0 2724 0 2724
134 2724 ▶ return (exists $info->{is_serializable} && !$info->{is_serializable}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $info->{'is_serializable'} && !$info->{'is_serializable'} ? : 55 2669 2724
Condition66%2/3
exists $info->{"is_serializable"} and not $info->{"is_serializable"}
!ll&&!rl&&r
2669055
MC/DC50%1/2
exists $info->{"is_serializable"} and not $info->{"is_serializable"}
exists $info->{"is_serializable"} not $info->{"is_serializable"}
Truth table66%2/3
A and not(B)
A= exists $info->{"is_serializable"}B= not $info->{"is_serializable"}
ABresult
0X0
100
111
135 }
136  
137  
138 1336 ▶sub readonly { $_[0]->_readonly->{ $_[1] }{ $_[2] } ? 1 : 0 }
Subroutine readonly: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_readonly->{$_[1]}{$_[2]} ? : 12 1324 1336
139  
140  
141 650 ▶sub visible_columns {
Subroutine visible_columns: called CC=1 SCAR=0.0
Pod documented
142 650 my ($self, $moniker) = @_;
143 650 return grep { !$self->hidden($moniker, $_) }
144 $self->schema->source($moniker)->columns;
145 }
146  
147  
148 298 ▶sub writable_columns {
Subroutine writable_columns: called CC=1 SCAR=0.0
Pod documented
149 298 my ($self, $moniker) = @_;
150 298 return grep { !$self->readonly($moniker, $_) } $self->visible_columns($moniker);
151 }
152  
153  
154 57 ▶sub check_depth {
Subroutine check_depth: called CC=4 SCAR=14.2
Pod documented
155 57 my ($self, $info) = @_;
156 57 ▶ return unless $info && ref $info->{path} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $info and ref $info->{"path"} eq "ARRAY" 0 57 57
Condition33%1/3
$info and ref $info->{"path"} eq "ARRAY"
!ll&&!rl&&r
0057
MC/DC0%0/2
$info and ref $info->{"path"} eq "ARRAY"
$info ref $info->{"path"} eq "ARRAY"
Truth table33%1/3
A and B
A= $infoB= ref $info->{"path"} eq "ARRAY"
ABresult
0X0
100
111
157 57 my $depth = grep { $_ !~ /\A[0-9]+\z/ } @{ $info->{path} };
158 57 ▶ die client_error "DBIO::GraphQL: query depth exceeds max_depth (" . $self->max_depth . ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $self->max_depth 4 53 57
159 if $depth > $self->max_depth;
160 53 return;
161 }
162  
163  
164 289 ▶sub authorize {
Subroutine authorize: called CC=5 SCAR=16.1
Pod documented
165 289 my ($self, $op, $moniker, $args, $ctx, $relationship) = @_;
166 289 ▶ my $hook = $self->_authorize or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $hook = $self->_authorize or return undef 29 260 289
167 29 my $answer = $hook->($op, $moniker, $args, $ctx, $relationship);
168 29 ▶ die client_error "DBIO::GraphQL: not authorized\n" unless $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer 8 21 29
169 21 ▶ return (ref $answer eq 'HASH' && %$answer) ? $answer : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $answer eq 'HASH' && %$answer ? : 14 7 21
Condition66%2/3
ref $answer eq "HASH" and %$answer
!ll&&!rl&&r
7014
MC/DC50%1/2
ref $answer eq "HASH" and %$answer
ref $answer eq "HASH" %$answer
Truth table66%2/3
A and B
A= ref $answer eq "HASH"B= %$answer
ABresult
0X0
100
111
170 }
171  
172 14 1;
173  
174 __END__
↑ Summary GraphQL.pm »