Skip to content

Commit

Permalink
Merge pull request #376 from alphagov/record-ip-address-when-signing
Browse files Browse the repository at this point in the history
Record IP address when signing a petition
  • Loading branch information
alanth committed Aug 25, 2015
2 parents e762d38 + 8194c87 commit 3145ebc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/controllers/signatures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SignaturesController < ApplicationController

def new
assign_stage
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_new, @petition, params[:stage], params[:move])
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_new, request, @petition, params[:stage], params[:move])
respond_with @stage_manager.stage_object
end

Expand Down Expand Up @@ -114,7 +114,7 @@ def handle_existing_signatures(signatures, petition)
def handle_new_signature(petition)
assign_move
assign_stage
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_create, petition, params[:stage], params[:move])
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_create, request, petition, params[:stage], params[:move])
if @stage_manager.create_signature
@stage_manager.signature.store_constituency_id
send_email_to_petition_signer(@stage_manager.signature)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/sponsors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class SponsorsController < ApplicationController
def show
assign_stage
@sponsor = @petition.sponsors.build
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_new, @petition, params[:stage], params[:move])
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_new, request, @petition, params[:stage], params[:move])
end

def update
assign_move
assign_stage
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_create, @petition, params[:stage], params[:move])
@stage_manager = Staged::PetitionSigner.manage(signature_params_for_create, request, @petition, params[:stage], params[:move])
if @stage_manager.create_signature
@signature = @stage_manager.signature
@signature.store_constituency_id
Expand Down
11 changes: 7 additions & 4 deletions app/models/staged/petition_signer.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module Staged
module PetitionSigner
def self.manage(params, petition, stage, move)
Manager.new(params, petition, stage, move, self::Stages)
def self.manage(params, request, petition, stage, move)
Manager.new(params, request, petition, stage, move, self::Stages)
end

def self.stages
self::Stages.stage_names
end

class Manager
def initialize(params, petition, stage, move, stages)
def initialize(params, request, petition, stage, move, stages)
@params = params
@request = request
@petition = petition
@previous_stage = stage
@move = move
Expand Down Expand Up @@ -51,7 +52,9 @@ def sanitize!
end

def build_signature
@petition.signatures.build(@params)
@petition.signatures.build(@params) do |signature|
signature.ip_address = @request.remote_ip
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/models/staged/petition_signer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

RSpec.describe Staged::PetitionSigner, type: :model do
let(:signature_params) { {} }
let(:request) { double(remote_ip: '192.168.0.1') }
let(:move) { '' }
let(:stage) { '' }
let(:petition) { FactoryGirl.create(:open_petition) }
subject { described_class.manage(signature_params, petition, stage, move) }
subject { described_class.manage(signature_params, request, petition, stage, move) }
let(:signature) { subject.signature }

describe '#create_signature' do
Expand All @@ -32,6 +33,12 @@
allow(signature).to receive(:save).and_return false
expect(subject.create_signature).to eq false
end

it 'assigns the remote_ip of the request to the ip_address of the signature' do
allow(signature).to receive(:save).and_return true
expect(subject.create_signature).to eq true
expect(signature.ip_address).to eq '192.168.0.1'
end
end

describe 'when the stage is not "done"' do
Expand Down

0 comments on commit 3145ebc

Please sign in to comment.