Skip to content

Commit

Permalink
Flatten Clickatell message from the ConfigStore
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouzierinverse committed Nov 22, 2024
1 parent 4e634bb commit 341c5f2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 14 deletions.
27 changes: 14 additions & 13 deletions lib/pf/ConfigStore/Source.pm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ our %TYPE_TO_EXPANDED_FIELDS = (
GoogleWorkspaceLDAP => [qw(searchattributes host)],
);

our %TYPE_TO_FLATTEN = (
SMS => [qw(message)],
Twilio => [qw(message)],
Clickatell => [qw(message)],
Email => [qw(allowed_domains banned_domains)],
RADIUS => [qw(options)],
);

sub _fields_expanded {
my ($self, $item) = @_;
my $type = $item->{type} // '';
Expand Down Expand Up @@ -133,25 +141,18 @@ sub cleanupAfterRead {
push @{$item->{"${class}_rules"}}, $rule;
}
my $type = $item->{type};

if ($type eq 'SMS' || $type eq "Twilio") {
# This can be an array if it's fresh out of the file. We make it separated by newlines so it works fine the frontend
if(ref($item->{message}) eq 'ARRAY'){
$item->{message} = $self->join_options($item->{message});
}
} elsif ($type eq 'Email') {
for my $f (qw(allowed_domains banned_domains)) {
if (exists $TYPE_TO_FLATTEN{$type}) {
for my $f (@{$TYPE_TO_FLATTEN{$type}}) {
next unless exists $item->{$f};
my $val = $item->{$f};
if (ref($val) eq 'ARRAY') {
$item->{$f} = $self->join_options($val);
}
}
} elsif ($type eq 'RADIUS') {
if(ref($item->{options}) eq 'ARRAY'){
$item->{options} = $self->join_options($item->{options});
}
} elsif ($type eq 'OpenID') {

}

if ($type eq 'OpenID') {
$self->expand_ordered_array($item, 'person_mappings', 'person_mapping');
}

Expand Down
7 changes: 7 additions & 0 deletions t/data/authentication.conf
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,10 @@ port=33389
type=AD
host=127.0.0.1
cache_match=1

[ClickatellSource]
type=Clickatell
message=<<EOT
line1 $pin
line2
EOT
109 changes: 109 additions & 0 deletions t/unittest/UnifiedApi/Controller/Config/Sources/Clickatell.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/perl

=head1 NAME
Clickatell
=head1 DESCRIPTION
unit test for Clickatell
=cut

use strict;
use warnings;

BEGIN {
#include test libs
use lib qw(/usr/local/pf/t);
#Module for overriding configuration paths
use setup_test_config;
}

use Test::More tests => 7;

#This test will running last
use Test::NoWarnings;
use Test::Mojo;

my $t = Test::Mojo->new('pf::UnifiedApi');
use pf::ConfigStore::Source;
use Utils;
my ($fh, $filename) = Utils::tempfileForConfigStore("pf::ConfigStore::Source");
my $true = bless( do { \( my $o = 1 ) }, 'JSON::PP::Boolean' );
my $false = bless( do { \( my $o = 0 ) }, 'JSON::PP::Boolean' );

my $collection_base_url = '/api/v1/config/sources';

my $base_url = '/api/v1/config/source';
my $id1 = "id_$$";
my $id2 = "id2_$$";

#This is the second test
$t->post_ok("$collection_base_url" =>
json => {
type => 'Clickatell',
description => 'das',
hash_passwords => 'plaintext',
password_length => '10',
id => $id1,
message => qq{
Hello
World
},
api_key => 'asasasaas',
}
)
->status_is(201)
;
#This is the second test
$t->patch_ok("$base_url/$id1" =>
json => {
type => 'Clickatell',
description => 'das',
hash_passwords => 'plaintext',
password_length => '10',
message => qq{
Hello
World
},
api_key => 'asasasaas',
}
)
->status_is(200)
;

$t->get_ok("$collection_base_url")
->status_is(200)
;
use Data::Dumper; print Dumper($t->tx->res->json);


=head1 AUTHOR
Inverse inc. <[email protected]>
=head1 COPYRIGHT
Copyright (C) 2005-2023 Inverse inc.
=head1 LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
=cut

1;
10 changes: 9 additions & 1 deletion t/unittest/authentication.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BEGIN {

use Date::Parse;

use Test::More tests => 60; # last test to print
use Test::More tests => 61; # last test to print

use Test::NoWarnings;

Expand Down Expand Up @@ -560,6 +560,14 @@ is_deeply(
);
}

{
my $source = pf::authentication::getAuthenticationSource("ClickatellSource");
is_deeply(
$source->{message},
"line1 \$pin\nline2",
);
}

=head1 AUTHOR
Inverse inc. <[email protected]>
Expand Down

0 comments on commit 341c5f2

Please sign in to comment.