castle-rb 8.1.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +317 -0
- data/LICENSE +21 -0
- data/README.md +290 -112
- data/lib/castle/api/filter.rb +3 -1
- data/lib/castle/api/list_items/create_batch.rb +22 -0
- data/lib/castle/api/lists/create.rb +1 -1
- data/lib/castle/api/log.rb +2 -1
- data/lib/castle/api/privacy/delete_data.rb +23 -0
- data/lib/castle/api/privacy/request_data.rb +23 -0
- data/lib/castle/api/risk.rb +2 -1
- data/lib/castle/client.rb +12 -52
- data/lib/castle/client_actions/list_items.rb +7 -2
- data/lib/castle/client_actions/privacy.rb +20 -0
- data/lib/castle/commands/list_items/create_batch.rb +22 -0
- data/lib/castle/commands/privacy/delete_data.rb +20 -0
- data/lib/castle/commands/privacy/request_data.rb +20 -0
- data/lib/castle/core/get_connection.rb +5 -1
- data/lib/castle/core/process_response.rb +1 -0
- data/lib/castle/core/process_webhook.rb +1 -1
- data/lib/castle/core/send_request.rb +2 -1
- data/lib/castle/errors.rb +1 -5
- data/lib/castle/headers/filter.rb +1 -1
- data/lib/castle/version.rb +1 -1
- data/lib/castle.rb +8 -17
- metadata +34 -218
- data/lib/castle/api/approve_device.rb +0 -20
- data/lib/castle/api/authenticate.rb +0 -28
- data/lib/castle/api/end_impersonation.rb +0 -22
- data/lib/castle/api/get_device.rb +0 -20
- data/lib/castle/api/get_devices_for_user.rb +0 -20
- data/lib/castle/api/report_device.rb +0 -20
- data/lib/castle/api/start_impersonation.rb +0 -22
- data/lib/castle/api/track.rb +0 -19
- data/lib/castle/commands/approve_device.rb +0 -17
- data/lib/castle/commands/authenticate.rb +0 -23
- data/lib/castle/commands/end_impersonation.rb +0 -25
- data/lib/castle/commands/get_device.rb +0 -17
- data/lib/castle/commands/get_devices_for_user.rb +0 -17
- data/lib/castle/commands/report_device.rb +0 -17
- data/lib/castle/commands/start_impersonation.rb +0 -25
- data/lib/castle/commands/track.rb +0 -22
- data/lib/castle/support/hanami.rb +0 -15
- data/lib/castle/support/padrino.rb +0 -19
- data/spec/integration/rails/rails_spec.rb +0 -95
- data/spec/integration/rails/support/all.rb +0 -6
- data/spec/integration/rails/support/application.rb +0 -17
- data/spec/integration/rails/support/home_controller.rb +0 -39
- data/spec/lib/castle/api/approve_device_spec.rb +0 -17
- data/spec/lib/castle/api/authenticate_spec.rb +0 -133
- data/spec/lib/castle/api/end_impersonation_spec.rb +0 -59
- data/spec/lib/castle/api/filter_spec.rb +0 -5
- data/spec/lib/castle/api/get_device_spec.rb +0 -17
- data/spec/lib/castle/api/get_devices_for_user_spec.rb +0 -17
- data/spec/lib/castle/api/list_items/archive_spec.rb +0 -18
- data/spec/lib/castle/api/list_items/count_spec.rb +0 -21
- data/spec/lib/castle/api/list_items/create_spec.rb +0 -22
- data/spec/lib/castle/api/list_items/get_spec.rb +0 -18
- data/spec/lib/castle/api/list_items/query_spec.rb +0 -21
- data/spec/lib/castle/api/list_items/unarchive_spec.rb +0 -18
- data/spec/lib/castle/api/list_items/update_spec.rb +0 -22
- data/spec/lib/castle/api/lists/create_spec.rb +0 -21
- data/spec/lib/castle/api/lists/delete_spec.rb +0 -17
- data/spec/lib/castle/api/lists/get_all_spec.rb +0 -17
- data/spec/lib/castle/api/lists/get_spec.rb +0 -17
- data/spec/lib/castle/api/lists/query_spec.rb +0 -21
- data/spec/lib/castle/api/lists/update_spec.rb +0 -21
- data/spec/lib/castle/api/log_spec.rb +0 -5
- data/spec/lib/castle/api/report_device_spec.rb +0 -17
- data/spec/lib/castle/api/risk_spec.rb +0 -5
- data/spec/lib/castle/api/start_impersonation_spec.rb +0 -59
- data/spec/lib/castle/api/track_spec.rb +0 -65
- data/spec/lib/castle/api_spec.rb +0 -36
- data/spec/lib/castle/client_id/extract_spec.rb +0 -47
- data/spec/lib/castle/client_spec.rb +0 -342
- data/spec/lib/castle/command_spec.rb +0 -9
- data/spec/lib/castle/commands/approve_device_spec.rb +0 -24
- data/spec/lib/castle/commands/authenticate_spec.rb +0 -86
- data/spec/lib/castle/commands/end_impersonation_spec.rb +0 -72
- data/spec/lib/castle/commands/filter_spec.rb +0 -72
- data/spec/lib/castle/commands/get_device_spec.rb +0 -24
- data/spec/lib/castle/commands/get_devices_for_user_spec.rb +0 -24
- data/spec/lib/castle/commands/list_items/archive_spec.rb +0 -21
- data/spec/lib/castle/commands/list_items/count_spec.rb +0 -21
- data/spec/lib/castle/commands/list_items/create_spec.rb +0 -22
- data/spec/lib/castle/commands/list_items/get_spec.rb +0 -21
- data/spec/lib/castle/commands/list_items/query_spec.rb +0 -27
- data/spec/lib/castle/commands/list_items/unarchive_spec.rb +0 -21
- data/spec/lib/castle/commands/list_items/update_spec.rb +0 -21
- data/spec/lib/castle/commands/lists/create_spec.rb +0 -33
- data/spec/lib/castle/commands/lists/delete_spec.rb +0 -21
- data/spec/lib/castle/commands/lists/get_all_spec.rb +0 -11
- data/spec/lib/castle/commands/lists/get_spec.rb +0 -21
- data/spec/lib/castle/commands/lists/query_spec.rb +0 -27
- data/spec/lib/castle/commands/lists/update_spec.rb +0 -29
- data/spec/lib/castle/commands/log_spec.rb +0 -73
- data/spec/lib/castle/commands/report_device_spec.rb +0 -24
- data/spec/lib/castle/commands/risk_spec.rb +0 -73
- data/spec/lib/castle/commands/start_impersonation_spec.rb +0 -72
- data/spec/lib/castle/commands/track_spec.rb +0 -89
- data/spec/lib/castle/configuration_spec.rb +0 -14
- data/spec/lib/castle/context/get_default_spec.rb +0 -41
- data/spec/lib/castle/context/merge_spec.rb +0 -23
- data/spec/lib/castle/context/prepare_spec.rb +0 -42
- data/spec/lib/castle/context/sanitize_spec.rb +0 -27
- data/spec/lib/castle/core/get_connection_spec.rb +0 -43
- data/spec/lib/castle/core/process_response_spec.rb +0 -103
- data/spec/lib/castle/core/process_webhook_spec.rb +0 -52
- data/spec/lib/castle/core/send_request_spec.rb +0 -97
- data/spec/lib/castle/failover/strategy_spec.rb +0 -12
- data/spec/lib/castle/headers/extract_spec.rb +0 -103
- data/spec/lib/castle/headers/filter_spec.rb +0 -42
- data/spec/lib/castle/headers/format_spec.rb +0 -25
- data/spec/lib/castle/ips/extract_spec.rb +0 -91
- data/spec/lib/castle/logger_spec.rb +0 -39
- data/spec/lib/castle/payload/prepare_spec.rb +0 -52
- data/spec/lib/castle/secure_mode_spec.rb +0 -7
- data/spec/lib/castle/session_spec.rb +0 -61
- data/spec/lib/castle/singleton_configuration_spec.rb +0 -14
- data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +0 -69
- data/spec/lib/castle/utils/clone_spec.rb +0 -19
- data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +0 -50
- data/spec/lib/castle/utils/get_timestamp_spec.rb +0 -16
- data/spec/lib/castle/utils/merge_spec.rb +0 -13
- data/spec/lib/castle/validators/not_supported_spec.rb +0 -19
- data/spec/lib/castle/validators/present_spec.rb +0 -25
- data/spec/lib/castle/verdict_spec.rb +0 -9
- data/spec/lib/castle/version_spec.rb +0 -5
- data/spec/lib/castle/webhooks/verify_spec.rb +0 -59
- data/spec/lib/castle_spec.rb +0 -58
- data/spec/spec_helper.rb +0 -26
- data/spec/support/shared_examples/action_request.rb +0 -167
- data/spec/support/shared_examples/configuration.rb +0 -99
- data/spec/support/shared_examples/list_items.rb +0 -52
- data/spec/support/shared_examples/lists.rb +0 -45
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Utils::GetTimestamp do
|
|
4
|
-
subject(:timestamp) { described_class.call }
|
|
5
|
-
|
|
6
|
-
let(:time_string) { '2018-01-10T14:14:24.407Z' }
|
|
7
|
-
let(:time) { Time.parse(time_string) }
|
|
8
|
-
|
|
9
|
-
before { Timecop.freeze(time) }
|
|
10
|
-
|
|
11
|
-
after { Timecop.return }
|
|
12
|
-
|
|
13
|
-
describe '#call' do
|
|
14
|
-
it { expect(timestamp).to eql(time_string) }
|
|
15
|
-
end
|
|
16
|
-
end
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Utils::Merge do
|
|
4
|
-
subject(:merge) { described_class }
|
|
5
|
-
|
|
6
|
-
describe 'call' do
|
|
7
|
-
let(:first) { { test: { test1: { c: '4' }, test2: { c: '3' }, a: '1', b: '2' } } }
|
|
8
|
-
let(:second) { { test2: '2', test: { 'test1' => { d: '5' }, :test2 => '6', :a => nil, :b => '3' } } }
|
|
9
|
-
let(:result) { { test2: '2', test: { test1: { c: '4', d: '5' }, test2: '6', b: '3' } } }
|
|
10
|
-
|
|
11
|
-
it { expect(merge.call(first, second)).to eql(result) }
|
|
12
|
-
end
|
|
13
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Validators::NotSupported do
|
|
4
|
-
describe '#call' do
|
|
5
|
-
subject(:call) { described_class.call({ first: 1 }, keys) }
|
|
6
|
-
|
|
7
|
-
context 'when keys is present' do
|
|
8
|
-
let(:keys) { %i[first second] }
|
|
9
|
-
|
|
10
|
-
it { expect { call }.to raise_error(Castle::InvalidParametersError, 'first is/are not supported') }
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context 'when key is not present' do
|
|
14
|
-
let(:keys) { %i[second third] }
|
|
15
|
-
|
|
16
|
-
it { expect { call }.not_to raise_error }
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Validators::Present do
|
|
4
|
-
describe '#call' do
|
|
5
|
-
subject(:call) { described_class.call({ first: 1, second: '2', invalid: '' }, keys) }
|
|
6
|
-
|
|
7
|
-
context 'when keys is not present' do
|
|
8
|
-
let(:keys) { %i[second third] }
|
|
9
|
-
|
|
10
|
-
it { expect { call }.to raise_error(Castle::InvalidParametersError, 'third is missing or empty') }
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context 'when keys is empty' do
|
|
14
|
-
let(:keys) { %i[second invalid] }
|
|
15
|
-
|
|
16
|
-
it { expect { call }.to raise_error(Castle::InvalidParametersError, 'invalid is missing or empty') }
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
context 'when key is present' do
|
|
20
|
-
let(:keys) { %i[first second] }
|
|
21
|
-
|
|
22
|
-
it { expect { call }.not_to raise_error }
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Verdict do
|
|
4
|
-
subject(:verdict) { described_class }
|
|
5
|
-
|
|
6
|
-
it { expect(verdict::ALLOW).to eql('allow') }
|
|
7
|
-
it { expect(verdict::DENY).to eql('deny') }
|
|
8
|
-
it { expect(verdict::CHALLENGE).to eql('challenge') }
|
|
9
|
-
end
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle::Webhooks::Verify do
|
|
4
|
-
describe '#call' do
|
|
5
|
-
subject(:call) { described_class.call(webhook) }
|
|
6
|
-
|
|
7
|
-
let(:env) { Rack::MockRequest.env_for('/', 'HTTP_X_CASTLE_SIGNATURE' => signature) }
|
|
8
|
-
|
|
9
|
-
let(:webhook) { Rack::Request.new(env) }
|
|
10
|
-
let(:user_id) { '12345' }
|
|
11
|
-
let(:webhook_body) do
|
|
12
|
-
{
|
|
13
|
-
api_version: 'v1',
|
|
14
|
-
app_id: '12345',
|
|
15
|
-
type: '$incident.confirmed',
|
|
16
|
-
created_at: '2020-12-18T12:55:21.779Z',
|
|
17
|
-
data: {
|
|
18
|
-
id: 'test',
|
|
19
|
-
device_token: 'token',
|
|
20
|
-
user_id: user_id,
|
|
21
|
-
trigger: '$login.succeeded',
|
|
22
|
-
context: {
|
|
23
|
-
},
|
|
24
|
-
location: {
|
|
25
|
-
},
|
|
26
|
-
user_agent: {
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
user_traits: {
|
|
30
|
-
},
|
|
31
|
-
properties: {
|
|
32
|
-
},
|
|
33
|
-
policy: {
|
|
34
|
-
}
|
|
35
|
-
}.to_json
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
context 'when success' do
|
|
39
|
-
let(:signature) { '3ptx3rUOBnGEqPjMrbcJn2UUfzwTKP54dFyP5uyPY+Y=' }
|
|
40
|
-
|
|
41
|
-
before { allow(Castle::Core::ProcessWebhook).to receive(:call).and_return(webhook_body) }
|
|
42
|
-
|
|
43
|
-
it { expect { call }.not_to raise_error }
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
context 'when signature is malformed' do
|
|
47
|
-
let(:signature) { '123' }
|
|
48
|
-
|
|
49
|
-
before { allow(Castle::Core::ProcessWebhook).to receive(:call).and_return(webhook_body) }
|
|
50
|
-
|
|
51
|
-
it do
|
|
52
|
-
expect { call }.to raise_error(
|
|
53
|
-
Castle::WebhookVerificationError,
|
|
54
|
-
'Signature not matching the expected signature'
|
|
55
|
-
)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
data/spec/lib/castle_spec.rb
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe Castle do
|
|
4
|
-
subject(:castle) { described_class }
|
|
5
|
-
|
|
6
|
-
describe 'config' do
|
|
7
|
-
it { expect(castle.config).to be_a(Castle::Configuration) }
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
describe 'api_secret setter' do
|
|
11
|
-
let(:value) { 'new_secret' }
|
|
12
|
-
|
|
13
|
-
before { castle.api_secret = value }
|
|
14
|
-
|
|
15
|
-
it { expect(castle.config.api_secret).to eql(value) }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
describe 'configure' do
|
|
19
|
-
let(:value) { 'new_secret' }
|
|
20
|
-
let(:timeout) { 60 }
|
|
21
|
-
|
|
22
|
-
shared_examples 'config_setup' do
|
|
23
|
-
it { expect(castle.config.api_secret).to eql(value) }
|
|
24
|
-
it { expect(castle.config.request_timeout).to eql(timeout) }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context 'with block' do
|
|
28
|
-
before do
|
|
29
|
-
castle.configure do |config|
|
|
30
|
-
config.api_secret = value
|
|
31
|
-
config.request_timeout = timeout
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it_behaves_like 'config_setup'
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
context 'with options' do
|
|
39
|
-
before { castle.configure(request_timeout: timeout, api_secret: value) }
|
|
40
|
-
|
|
41
|
-
it_behaves_like 'config_setup'
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
context 'with block and options' do
|
|
45
|
-
before { castle.configure(request_timeout: timeout) { |config| config.api_secret = value } }
|
|
46
|
-
|
|
47
|
-
it_behaves_like 'config_setup'
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
describe 'configure wrongly' do
|
|
52
|
-
let(:value) { 'new_secret' }
|
|
53
|
-
|
|
54
|
-
it do
|
|
55
|
-
expect { castle.configure { |config| config.wrong_config = value } }.to raise_error(Castle::ConfigurationError)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'rubygems'
|
|
4
|
-
require 'bundler/setup'
|
|
5
|
-
require 'rack'
|
|
6
|
-
require 'webmock/rspec'
|
|
7
|
-
require 'byebug'
|
|
8
|
-
require 'timecop'
|
|
9
|
-
|
|
10
|
-
require 'coveralls'
|
|
11
|
-
Coveralls.wear!
|
|
12
|
-
|
|
13
|
-
require 'castle'
|
|
14
|
-
|
|
15
|
-
WebMock.disable_net_connect!(allow_localhost: true)
|
|
16
|
-
|
|
17
|
-
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
|
18
|
-
|
|
19
|
-
RSpec.configure do |config|
|
|
20
|
-
config.before do
|
|
21
|
-
Castle.config.reset
|
|
22
|
-
Castle.configure { |cfg| cfg.api_secret = 'secret' }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
config.disable_monkey_patching!
|
|
26
|
-
end
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.shared_examples_for 'action request' do |action|
|
|
4
|
-
subject(:request_response) { client.send(action.to_sym, options) }
|
|
5
|
-
|
|
6
|
-
let(:options) do
|
|
7
|
-
{ request_token: request_token, event: event, status: status, user: user, context: context, properties: properties }
|
|
8
|
-
end
|
|
9
|
-
let(:request_token) { '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302' }
|
|
10
|
-
let(:event) { '$login' }
|
|
11
|
-
let(:status) { '$succeeded' }
|
|
12
|
-
let(:user) { { id: '1234', email: 'foobar@mail.com' } }
|
|
13
|
-
let(:properties) { {} }
|
|
14
|
-
let(:request_body) do
|
|
15
|
-
{
|
|
16
|
-
request_token: request_token,
|
|
17
|
-
event: event,
|
|
18
|
-
status: status,
|
|
19
|
-
user: user,
|
|
20
|
-
context: context,
|
|
21
|
-
properties: properties,
|
|
22
|
-
timestamp: time_auto,
|
|
23
|
-
sent_at: time_auto
|
|
24
|
-
}
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context 'when used with symbol keys' do
|
|
28
|
-
it do
|
|
29
|
-
request_response
|
|
30
|
-
|
|
31
|
-
assert_requested :post, "https://api.castle.io/v1/#{action}", times: 1 do |req|
|
|
32
|
-
JSON.parse(req.body) == JSON.parse(request_body.to_json)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
context 'when passed timestamp in options and no defined timestamp' do
|
|
37
|
-
let(:client) { client_with_no_timestamp }
|
|
38
|
-
|
|
39
|
-
before do
|
|
40
|
-
options[:timestamp] = time_user
|
|
41
|
-
request_body[:timestamp] = time_user
|
|
42
|
-
|
|
43
|
-
request_response
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it do
|
|
47
|
-
assert_requested :post, "https://api.castle.io/v1/#{action}", times: 1 do |req|
|
|
48
|
-
JSON.parse(req.body) == JSON.parse(request_body.to_json)
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
context 'with client initialized with timestamp' do
|
|
54
|
-
let(:client) { client_with_user_timestamp }
|
|
55
|
-
|
|
56
|
-
before do
|
|
57
|
-
request_body[:timestamp] = time_user
|
|
58
|
-
|
|
59
|
-
request_response
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it do
|
|
63
|
-
assert_requested :post, "https://api.castle.io/v1/#{action}", times: 1 do |req|
|
|
64
|
-
JSON.parse(req.body) == JSON.parse(request_body.to_json)
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context 'when used with string keys' do
|
|
71
|
-
before { request_response }
|
|
72
|
-
|
|
73
|
-
it do
|
|
74
|
-
assert_requested :post, "https://api.castle.io/v1/#{action}", times: 1 do |req|
|
|
75
|
-
JSON.parse(req.body) == JSON.parse(request_body.to_json)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
context 'when tracking enabled' do
|
|
81
|
-
before { request_response }
|
|
82
|
-
|
|
83
|
-
it do
|
|
84
|
-
assert_requested :post, "https://api.castle.io/v1/#{action}", times: 1 do |req|
|
|
85
|
-
JSON.parse(req.body) == JSON.parse(request_body.to_json)
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
it { expect(request_response[:failover]).to be false }
|
|
90
|
-
it { expect(request_response[:failover_reason]).to be_nil }
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
context 'when tracking disabled' do
|
|
94
|
-
before do
|
|
95
|
-
client.disable_tracking
|
|
96
|
-
request_response
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
it { assert_not_requested :post, "https://api.castle.io/v1/#{action}" }
|
|
100
|
-
it { expect(request_response[:policy][:action]).to eql(Castle::Verdict::ALLOW) }
|
|
101
|
-
it { expect(request_response[:action]).to eql(Castle::Verdict::ALLOW) }
|
|
102
|
-
it { expect(request_response[:user_id]).to eql('1234') }
|
|
103
|
-
it { expect(request_response[:failover]).to be true }
|
|
104
|
-
it { expect(request_response[:failover_reason]).to eql('Castle is set to do not track.') }
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
context 'when request with fail' do
|
|
108
|
-
before { allow(Castle::API).to receive(:send_request).and_raise(Castle::RequestError.new(Timeout::Error)) }
|
|
109
|
-
|
|
110
|
-
context 'with request error and throw strategy' do
|
|
111
|
-
before { allow(Castle.config).to receive(:failover_strategy).and_return(:throw) }
|
|
112
|
-
|
|
113
|
-
it { expect { request_response }.to raise_error(Castle::RequestError) }
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
context 'with request error and not throw on eg deny strategy' do
|
|
117
|
-
it { assert_not_requested :post, "https:/:secret@api.castle.io/v1/#{action}" }
|
|
118
|
-
it { expect(request_response[:policy][:action]).to eql('allow') }
|
|
119
|
-
it { expect(request_response[:action]).to eql('allow') }
|
|
120
|
-
it { expect(request_response[:user_id]).to eql('1234') }
|
|
121
|
-
it { expect(request_response[:failover]).to be true }
|
|
122
|
-
it { expect(request_response[:failover_reason]).to eql('Castle::RequestError') }
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
context 'when request is internal server error' do
|
|
127
|
-
before { allow(Castle::API).to receive(:send_request).and_raise(Castle::InternalServerError) }
|
|
128
|
-
|
|
129
|
-
describe 'throw strategy' do
|
|
130
|
-
before { allow(Castle.config).to receive(:failover_strategy).and_return(:throw) }
|
|
131
|
-
|
|
132
|
-
it { expect { request_response }.to raise_error(Castle::InternalServerError) }
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
describe 'not throw on eg deny strategy' do
|
|
136
|
-
it { assert_not_requested :post, "https:/:secret@api.castle.io/v1/#{action}" }
|
|
137
|
-
it { expect(request_response[:policy][:action]).to eql('allow') }
|
|
138
|
-
it { expect(request_response[:action]).to eql('allow') }
|
|
139
|
-
it { expect(request_response[:user_id]).to eql('1234') }
|
|
140
|
-
it { expect(request_response[:failover]).to be true }
|
|
141
|
-
it { expect(request_response[:failover_reason]).to eql('Castle::InternalServerError') }
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
context 'when request is 422' do
|
|
146
|
-
describe 'throw InvalidParametersError' do
|
|
147
|
-
let(:response_body) { { type: 'bad_request', message: 'wrong params' }.to_json }
|
|
148
|
-
let(:response_code) { 422 }
|
|
149
|
-
|
|
150
|
-
it { expect { request_response }.to raise_error(Castle::InvalidParametersError, 'wrong params') }
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
describe 'throw InvalidParametersError for legacy endpoints' do
|
|
154
|
-
let(:response_body) { {}.to_json }
|
|
155
|
-
let(:response_code) { 422 }
|
|
156
|
-
|
|
157
|
-
it { expect { request_response }.to raise_error(Castle::InvalidParametersError) }
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
describe 'throw InvalidRequestTokenError' do
|
|
161
|
-
let(:response_body) { { type: 'invalid_request_token', message: 'invalid token' }.to_json }
|
|
162
|
-
let(:response_code) { 422 }
|
|
163
|
-
|
|
164
|
-
it { expect { request_response }.to raise_error(Castle::InvalidRequestTokenError, 'invalid token') }
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
end
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
shared_examples 'configuration_host' do
|
|
4
|
-
describe 'host' do
|
|
5
|
-
context 'with default' do
|
|
6
|
-
it { expect(config.base_url.host).to eql('api.castle.io') }
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
context 'with setter' do
|
|
10
|
-
before { config.base_url = 'http://api.castle.dev/v2' }
|
|
11
|
-
|
|
12
|
-
it { expect(config.base_url.host).to eql('api.castle.dev') }
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
shared_examples 'configuration_request_timeout' do
|
|
18
|
-
describe 'request_timeout' do
|
|
19
|
-
it { expect(config.request_timeout).to be(1000) }
|
|
20
|
-
|
|
21
|
-
context 'with setter' do
|
|
22
|
-
let(:value) { 50.0 }
|
|
23
|
-
|
|
24
|
-
before { config.request_timeout = value }
|
|
25
|
-
|
|
26
|
-
it { expect(config.request_timeout).to eql(value) }
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
shared_examples 'configuration_allowlisted' do
|
|
32
|
-
describe 'allowlisted' do
|
|
33
|
-
it { expect(config.allowlisted.size).to be(0) }
|
|
34
|
-
|
|
35
|
-
context 'with setter' do
|
|
36
|
-
before { config.allowlisted = ['header'] }
|
|
37
|
-
|
|
38
|
-
it { expect(config.allowlisted).to eql(['Header']) }
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
shared_examples 'configuration_denylisted' do
|
|
44
|
-
describe 'denylisted' do
|
|
45
|
-
it { expect(config.denylisted.size).to be(0) }
|
|
46
|
-
|
|
47
|
-
context 'with setter' do
|
|
48
|
-
before { config.denylisted = ['header'] }
|
|
49
|
-
|
|
50
|
-
it { expect(config.denylisted).to eql(['Header']) }
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
shared_examples 'configuration_failover_strategy' do
|
|
56
|
-
describe 'failover_strategy' do
|
|
57
|
-
it { expect(config.failover_strategy).to eql(Castle::Failover::Strategy::ALLOW) }
|
|
58
|
-
|
|
59
|
-
context 'with setter' do
|
|
60
|
-
before { config.failover_strategy = Castle::Failover::Strategy::DENY }
|
|
61
|
-
|
|
62
|
-
it { expect(config.failover_strategy).to eql(Castle::Failover::Strategy::DENY) }
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
context 'when broken' do
|
|
66
|
-
it { expect { config.failover_strategy = :unicorn }.to raise_error(Castle::ConfigurationError) }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
shared_examples 'configuration_api_secret' do
|
|
72
|
-
describe 'api_secret' do
|
|
73
|
-
context 'with env' do
|
|
74
|
-
let(:secret_key_env) { 'secret_key_env' }
|
|
75
|
-
let(:secret_key) { 'secret_key' }
|
|
76
|
-
|
|
77
|
-
before do
|
|
78
|
-
allow(ENV).to receive(:fetch).with('CASTLE_API_SECRET', '').and_return(secret_key_env)
|
|
79
|
-
config.reset
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
it { expect(config.api_secret).to eql(secret_key_env) }
|
|
83
|
-
|
|
84
|
-
context 'when key is overwritten' do
|
|
85
|
-
before { config.api_secret = secret_key }
|
|
86
|
-
|
|
87
|
-
it { expect(config.api_secret).to eql(secret_key) }
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
context 'with setter' do
|
|
92
|
-
let(:value) { 'new_secret' }
|
|
93
|
-
|
|
94
|
-
before { config.api_secret = value }
|
|
95
|
-
|
|
96
|
-
it { expect(config.api_secret).to eql(value) }
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.shared_examples 'it has list item actions' do
|
|
4
|
-
describe 'archive_list_item' do
|
|
5
|
-
it do
|
|
6
|
-
client.archive_list_item(list_id: '1234', list_item_id: '5678')
|
|
7
|
-
assert_requested :delete, 'https://api.castle.io/v1/lists/1234/items/5678/archive', times: 1
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe 'count_list_items' do
|
|
12
|
-
it do
|
|
13
|
-
client.count_list_items(list_id: '1234', filters: [{ field: 'email', op: 'eq', value: 'test' }])
|
|
14
|
-
assert_requested :post, 'https://api.castle.io/v1/lists/1234/items/count', times: 1
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
describe 'create_list_item' do
|
|
19
|
-
it do
|
|
20
|
-
client.create_list_item(list_id: '1234', author: { type: 'other', identifier: '1234' }, primary_value: 'email')
|
|
21
|
-
assert_requested :post, 'https://api.castle.io/v1/lists/1234/items', times: 1
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
describe 'get_list_item' do
|
|
26
|
-
it do
|
|
27
|
-
client.get_list_item(list_id: '1234', list_item_id: '5678')
|
|
28
|
-
assert_requested :get, 'https://api.castle.io/v1/lists/1234/items/5678', times: 1
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
describe 'query_list_items' do
|
|
33
|
-
it do
|
|
34
|
-
client.query_list_items(list_id: '1234', filters: [{ field: 'email', op: 'eq', value: 'test' }])
|
|
35
|
-
assert_requested :post, 'https://api.castle.io/v1/lists/1234/items/query', times: 1
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
describe 'unarchive_list_item' do
|
|
40
|
-
it do
|
|
41
|
-
client.unarchive_list_item(list_id: '1234', list_item_id: '5678')
|
|
42
|
-
assert_requested :put, 'https://api.castle.io/v1/lists/1234/items/5678/unarchive', times: 1
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
describe 'update_list_item' do
|
|
47
|
-
it do
|
|
48
|
-
client.update_list_item(list_id: '1234', list_item_id: '5678', comment: 'updating comment')
|
|
49
|
-
assert_requested :put, 'https://api.castle.io/v1/lists/1234/items/5678', times: 1
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.shared_examples 'it has list actions' do
|
|
4
|
-
describe 'create_list' do
|
|
5
|
-
it do
|
|
6
|
-
client.create_list(name: 'list name', color: '$green', primary_field: 'user.email')
|
|
7
|
-
assert_requested :post, 'https://api.castle.io/v1/lists', times: 1
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe 'delete_list' do
|
|
12
|
-
it do
|
|
13
|
-
client.delete_list(list_id: 'list_id')
|
|
14
|
-
assert_requested :delete, 'https://api.castle.io/v1/lists/list_id', times: 1
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
describe 'get_all_lists' do
|
|
19
|
-
it do
|
|
20
|
-
client.get_all_lists
|
|
21
|
-
assert_requested :get, 'https://api.castle.io/v1/lists', times: 1
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
describe 'get_list' do
|
|
26
|
-
it do
|
|
27
|
-
client.get_list(list_id: 'list_id')
|
|
28
|
-
assert_requested :get, 'https://api.castle.io/v1/lists/list_id', times: 1
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
describe 'query_lists' do
|
|
33
|
-
it do
|
|
34
|
-
client.query_lists(filters: [{ field: 'user.email', op: 'eq', value: 'test' }])
|
|
35
|
-
assert_requested :post, 'https://api.castle.io/v1/lists/query', times: 1
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
describe 'update_list' do
|
|
40
|
-
it do
|
|
41
|
-
client.update_list(list_id: 'list_id', name: 'list name', color: '$green', primary_field: 'user.email')
|
|
42
|
-
assert_requested :put, 'https://api.castle.io/v1/lists/list_id', times: 1
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|