aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/Cobrand/Bromley.pm14
-rw-r--r--perllib/Memcached.pm11
-rw-r--r--t/cobrand/bromley.t24
3 files changed, 47 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm
index c9e85a484..263549e71 100644
--- a/perllib/FixMyStreet/Cobrand/Bromley.pm
+++ b/perllib/FixMyStreet/Cobrand/Bromley.pm
@@ -11,6 +11,7 @@ use Sort::Key::Natural qw(natkeysort_inplace);
use Try::Tiny;
use FixMyStreet::DateRange;
use FixMyStreet::WorkingDays;
+use Memcached;
sub council_area_id { return 2482; }
sub council_area { return 'Bromley'; }
@@ -435,8 +436,17 @@ sub look_up_property {
my $self = shift;
my $uprn = shift;
- my $echo = $self->feature('echo');
- $echo = Integrations::Echo->new(%$echo);
+ my $cfg = $self->feature('echo');
+ my $echo = Integrations::Echo->new(%$cfg);
+
+ if ($cfg->{max_per_day}) {
+ my $today = DateTime->today->set_time_zone(FixMyStreet->local_time_zone)->ymd;
+ my $ip = $self->{c}->req->address;
+ my $key = FixMyStreet->test_mode ? "bromley-test" : "bromley-$ip-$today";
+ my $count = Memcached::increment($key, 86400) || 0;
+ $self->{c}->detach('/page_error_403_access_denied', []) if $count > $cfg->{max_per_day};
+ }
+
my $result = $echo->GetPointAddress($uprn);
return {
id => $result->{Id},
diff --git a/perllib/Memcached.pm b/perllib/Memcached.pm
index d03897e5a..099440f82 100644
--- a/perllib/Memcached.pm
+++ b/perllib/Memcached.pm
@@ -33,4 +33,15 @@ sub delete {
instance->delete(@_);
}
+sub increment {
+ my $key = shift;
+ my $timeout = shift;
+ my $count = instance->incr($key);
+ if (!defined $count) {
+ instance->add($key, 0, $timeout);
+ $count = instance->incr($key);
+ };
+ return $count;
+}
+
1;
diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t
index f2b1c7a39..045fdd56d 100644
--- a/t/cobrand/bromley.t
+++ b/t/cobrand/bromley.t
@@ -1,8 +1,13 @@
use CGI::Simple;
+use Test::MockModule;
use FixMyStreet::TestMech;
use FixMyStreet::Script::Reports;
my $mech = FixMyStreet::TestMech->new;
+# Mock fetching bank holidays
+my $uk = Test::MockModule->new('FixMyStreet::Cobrand::UK');
+$uk->mock('_fetch_url', sub { '{}' });
+
# Create test data
my $user = $mech->create_user_ok( 'bromley@example.com', name => 'Bromley' );
my $body = $mech->create_body_ok( 2482, 'Bromley Council');
@@ -247,4 +252,23 @@ subtest 'check heatmap page' => sub {
};
};
+subtest 'test waste max-per-day' => sub {
+ FixMyStreet::override_config {
+ ALLOWED_COBRANDS => 'bromley',
+ COBRAND_FEATURES => {
+ echo => { bromley => { max_per_day => 1, sample_data => 1 } },
+ waste => { bromley => 1 }
+ },
+ }, sub {
+ SKIP: {
+ skip( "No memcached", 2 ) unless Memcached::increment('bromley-test');
+ Memcached::delete("bromley-test");
+ $mech->get_ok('/waste/uprn/12345');
+ $mech->get('/waste/uprn/12345');
+ is $mech->res->code, 403, 'Now forbidden';
+ }
+ };
+
+};
+
done_testing();