Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change remote_ip #179

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dfe/analytics/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def with_user(user)
end

def with_namespace(namespace)
@event_hash.merge!(namespace: namespace)
@event_hash.merge!(namespace:)

self
end
Expand Down Expand Up @@ -126,7 +126,7 @@ def hash_to_kv_pairs(hash)
end

def anonymised_user_agent_and_ip(rack_request)
DfE::Analytics.anonymise(rack_request.user_agent.to_s + rack_request.remote_ip.to_s) if rack_request.remote_ip.present?
DfE::Analytics.anonymise(rack_request.user_agent.to_s + rack_request.headers['X-REAL-IP'].to_s) if rack_request.headers['X-REAL-IP'].present?
end

def user_identifier(user)
Expand Down
12 changes: 6 additions & 6 deletions spec/dfe/analytics/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
describe 'anonymised_user_agent_and_ip' do
subject do
request = fake_request(
remote_ip: remote_ip,
headers: headers,
user_agent: user_agent
)

Expand All @@ -33,28 +33,28 @@

context 'user agent and IP are both present' do
let(:user_agent) { 'SomeClient' }
let(:remote_ip) { '1.2.3.4' }
let(:headers) { { 'X-REAL-IP' => '1.2.3.4' } }

it { is_expected.to eq '90d5c396fe8da875d25688dfec3f2881c52e81507614ba1958262c8443db29c5' }
end

context 'user agent is present but IP is not' do
let(:user_agent) { 'SomeClient' }
let(:remote_ip) { nil }
let(:headers) { { 'X-REAL-IP' => nil } }

it { is_expected.to be_nil }
end

context 'IP is present but user agent is not' do
let(:user_agent) { nil }
let(:remote_ip) { '1.2.3.4' }
let(:headers) { { 'X-REAL-IP' => '1.2.3.4' } }

it { is_expected.to eq '6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c' }
end

context 'neither IP not user agent is present' do
let(:user_agent) { nil }
let(:remote_ip) { nil }
let(:headers) { { 'X-REAL-IP' => nil } }

it { is_expected.to be_nil }
end
Expand Down Expand Up @@ -238,7 +238,7 @@ def fake_request(overrides = {})
query_string: 'a=b',
referer: nil,
user_agent: 'SomeClient',
remote_ip: '1.2.3.4'
headers: { 'X-REAL-IP' => '1.2.3.4' }
}.merge(overrides)

instance_double(ActionDispatch::Request, attrs)
Expand Down
14 changes: 8 additions & 6 deletions spec/dfe/analytics/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def index
{ key: 'array_param[]',
value: %w[1 2] }],
request_referer: nil,
anonymised_user_agent_and_ip: '16859db7ca4ec906925a0a2cb227bf307740a0c919ab9e2f7efeadf37779e770',
anonymised_user_agent_and_ip: '6b3f52c670279e133a78a03e34eea436c65395417ec264eb9f8c1e6da4f5ed56',
response_content_type: 'text/plain; charset=utf-8',
response_status: 200,
namespace: 'example_namespace',
Expand All @@ -80,7 +80,7 @@ def index
perform_enqueued_jobs do
get('/example/path',
params: { page: '1', per_page: '25', array_param: %w[1 2] },
headers: { 'HTTP_USER_AGENT' => 'Test agent' })
headers: { 'HTTP_USER_AGENT' => 'Test agent', 'X-REAL-IP' => '1.2.3.4' })
end
end

Expand All @@ -100,7 +100,7 @@ def index
request_path: '/unauthenticated_example',
request_query: [],
request_referer: nil,
anonymised_user_agent_and_ip: '12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0',
anonymised_user_agent_and_ip: '6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c',
response_content_type: 'text/plain; charset=utf-8',
response_status: 200 }
end
Expand All @@ -110,7 +110,8 @@ def index

DfE::Analytics::Testing.webmock! do
perform_enqueued_jobs do
get('/unauthenticated_example')
get('/unauthenticated_example',
headers: { 'X-REAL-IP' => '1.2.3.4' })
end
end

Expand All @@ -131,7 +132,7 @@ def index
request_path: '/unauthenticated_example',
request_query: [],
request_referer: nil,
anonymised_user_agent_and_ip: '12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0',
anonymised_user_agent_and_ip: '6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c',
response_content_type: 'text/html; charset=utf-8',
response_status: 304 }
end
Expand All @@ -156,7 +157,8 @@ def index

DfE::Analytics::Testing.webmock! do
perform_enqueued_jobs do
get('/unauthenticated_example')
get('/unauthenticated_example',
headers: { 'X-REAL-IP' => '1.2.3.4' })
end
end

Expand Down
Loading