Skip to content

Commit 9427c0a

Browse files
author
Jozsa Csongor
committed
SECURITY-1063: add filter_invalid function
Co-authored-by: krisztian.nagy@emarsys.com
1 parent c1a0ae1 commit 9427c0a

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@ gem install session-validator-client
1010

1111
## Usage
1212

13+
1314
setup up the following environment variables:
1415

1516
* `KEY_POOL`
1617
* `SESSION_VALIDATOR_KEYID`
1718
* `SESSION_VALIDATOR_URL`
1819

20+
###Validating a single Msid
21+
`valid?(msid)` returns `true` if `msid` is valid
22+
1923
```ruby
2024
require "session_validator"
2125

2226
client = SessionValidator::Client.new
2327
client.valid?("staging_int_5ad5f96f307cf9.61063404")
2428
```
2529

30+
###Batch validating multiple MSIDS
31+
`filter_invalid(msids)` returns an array of the invalid MSIDS.
32+
33+
```ruby
34+
require "session_validator"
35+
36+
client = SessionValidator::Client.new
37+
client.filter_invalid(["staging_int_5ad5f96f307cf9.61063404", "staging_int_5ad5f96f307cf9.61063405"])
38+
```
39+
2640
## Running tests
2741

2842
```bash

lib/session_validator/cached_client.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,16 @@ def valid?(msid)
1515
@cache.set msid, result if result
1616
end
1717
end
18+
19+
def filter_invalid(msids)
20+
@cache.cleanup
21+
22+
@client.filter_invalid(msids).tap do |result|
23+
msids.each do |msid|
24+
@cache.set msid, true unless result.include?(msid)
25+
end
26+
end
27+
end
28+
1829
end
1930
end

lib/session_validator/client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def valid?(msid)
2121
true
2222
end
2323

24+
def filter_invalid(msids)
25+
response = client.post("/sessions/filter", JSON.generate({msids:msids}), headers)
26+
if response.status == 200
27+
JSON.parse(response.body)
28+
else
29+
[]
30+
end
31+
rescue Faraday::TimeoutError
32+
[]
33+
end
34+
35+
2436
private
2537

2638
def client

spec/session_validator/cached_client_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,50 @@
4747
it { is_expected.to be true }
4848
end
4949
end
50+
51+
describe "#filter_invalid" do
52+
subject(:result) { cached_client.filter_invalid msids }
53+
54+
let(:msids) { ["test_12345.67890", "test_12345.67891", "test_12345.67892"] }
55+
56+
before do
57+
allow(cache).to receive(:cleanup)
58+
end
59+
60+
context "when called" do
61+
before do
62+
allow(cache).to receive(:set)
63+
allow(client).to receive(:filter_invalid).with(msids).and_return([])
64+
end
65+
it do
66+
expect(cache).to receive(:cleanup).with(no_args)
67+
result
68+
end
69+
end
70+
71+
context "when msids are valid" do
72+
before do
73+
allow(cache).to receive(:set)
74+
allow(client).to receive(:filter_invalid).with(msids).and_return([])
75+
end
76+
77+
it do
78+
msids.each do |msid|
79+
expect(cache).to receive(:set).with(msid, true)
80+
end
81+
result
82+
end
83+
it { is_expected.to eq [] }
84+
end
85+
86+
context "when msids are invalid" do
87+
before do
88+
allow(client).to receive(:filter_invalid).with(msids).and_return(msids)
89+
end
90+
91+
it { is_expected.to eq msids }
92+
end
93+
94+
end
95+
5096
end

spec/session_validator/client_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,47 @@
4545
it { is_expected.to eq true }
4646
end
4747
end
48+
49+
describe "#filter_invalid" do
50+
subject(:validation) { client.filter_invalid msids }
51+
52+
let(:msids) { ["test_12345.67890", "test_12345.67891", "test_12345.67892"] }
53+
let(:invalid_msids) { ["test_12345.67890", "test_12345.67892"] }
54+
let(:service_url) { "https://example.org" }
55+
let(:http_request) { stub_request(:post, "#{service_url}/sessions/filter") }
56+
let(:escher_keypool) { { api_key_id: 'session-validator_smart-insight_v1', api_secret: 'escher_secret' } }
57+
58+
before do
59+
stub_const 'ENV', ENV.to_h.merge('SESSION_VALIDATOR_URL' => service_url)
60+
allow(::Escher::Keypool).to receive_message_chain(:new, :get_active_key).with("session_validator")
61+
.and_return(escher_keypool)
62+
end
63+
64+
context "when request timeouted" do
65+
before { http_request.to_raise Faraday::TimeoutError }
66+
67+
it { is_expected.to eq [] }
68+
end
69+
70+
context "when given a list of msids" do
71+
before { http_request.to_return body: JSON.generate(invalid_msids) }
72+
73+
it { is_expected.to have_requested(:post, "#{service_url}/sessions/filter").
74+
with(body: JSON.generate({msids: msids})) }
75+
end
76+
77+
context "when response status code is not 200 OK" do
78+
before { http_request.to_return status: [404, "Not Found"] }
79+
80+
it { is_expected.to eq [] }
81+
end
82+
83+
context "when server replies with a list of msids" do
84+
before { http_request.to_return body: JSON.generate(invalid_msids) }
85+
86+
it { is_expected.to eq invalid_msids }
87+
end
88+
89+
end
90+
4891
end

0 commit comments

Comments
 (0)