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.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +317 -0
  3. data/LICENSE +21 -0
  4. data/README.md +290 -112
  5. data/lib/castle/api/filter.rb +3 -1
  6. data/lib/castle/api/list_items/create_batch.rb +22 -0
  7. data/lib/castle/api/lists/create.rb +1 -1
  8. data/lib/castle/api/log.rb +2 -1
  9. data/lib/castle/api/privacy/delete_data.rb +23 -0
  10. data/lib/castle/api/privacy/request_data.rb +23 -0
  11. data/lib/castle/api/risk.rb +2 -1
  12. data/lib/castle/client.rb +12 -52
  13. data/lib/castle/client_actions/list_items.rb +7 -2
  14. data/lib/castle/client_actions/privacy.rb +20 -0
  15. data/lib/castle/commands/list_items/create_batch.rb +22 -0
  16. data/lib/castle/commands/privacy/delete_data.rb +20 -0
  17. data/lib/castle/commands/privacy/request_data.rb +20 -0
  18. data/lib/castle/core/get_connection.rb +5 -1
  19. data/lib/castle/core/process_response.rb +1 -0
  20. data/lib/castle/core/process_webhook.rb +1 -1
  21. data/lib/castle/core/send_request.rb +2 -1
  22. data/lib/castle/errors.rb +1 -5
  23. data/lib/castle/headers/filter.rb +1 -1
  24. data/lib/castle/version.rb +1 -1
  25. data/lib/castle.rb +8 -17
  26. metadata +34 -218
  27. data/lib/castle/api/approve_device.rb +0 -20
  28. data/lib/castle/api/authenticate.rb +0 -28
  29. data/lib/castle/api/end_impersonation.rb +0 -22
  30. data/lib/castle/api/get_device.rb +0 -20
  31. data/lib/castle/api/get_devices_for_user.rb +0 -20
  32. data/lib/castle/api/report_device.rb +0 -20
  33. data/lib/castle/api/start_impersonation.rb +0 -22
  34. data/lib/castle/api/track.rb +0 -19
  35. data/lib/castle/commands/approve_device.rb +0 -17
  36. data/lib/castle/commands/authenticate.rb +0 -23
  37. data/lib/castle/commands/end_impersonation.rb +0 -25
  38. data/lib/castle/commands/get_device.rb +0 -17
  39. data/lib/castle/commands/get_devices_for_user.rb +0 -17
  40. data/lib/castle/commands/report_device.rb +0 -17
  41. data/lib/castle/commands/start_impersonation.rb +0 -25
  42. data/lib/castle/commands/track.rb +0 -22
  43. data/lib/castle/support/hanami.rb +0 -15
  44. data/lib/castle/support/padrino.rb +0 -19
  45. data/spec/integration/rails/rails_spec.rb +0 -95
  46. data/spec/integration/rails/support/all.rb +0 -6
  47. data/spec/integration/rails/support/application.rb +0 -17
  48. data/spec/integration/rails/support/home_controller.rb +0 -39
  49. data/spec/lib/castle/api/approve_device_spec.rb +0 -17
  50. data/spec/lib/castle/api/authenticate_spec.rb +0 -133
  51. data/spec/lib/castle/api/end_impersonation_spec.rb +0 -59
  52. data/spec/lib/castle/api/filter_spec.rb +0 -5
  53. data/spec/lib/castle/api/get_device_spec.rb +0 -17
  54. data/spec/lib/castle/api/get_devices_for_user_spec.rb +0 -17
  55. data/spec/lib/castle/api/list_items/archive_spec.rb +0 -18
  56. data/spec/lib/castle/api/list_items/count_spec.rb +0 -21
  57. data/spec/lib/castle/api/list_items/create_spec.rb +0 -22
  58. data/spec/lib/castle/api/list_items/get_spec.rb +0 -18
  59. data/spec/lib/castle/api/list_items/query_spec.rb +0 -21
  60. data/spec/lib/castle/api/list_items/unarchive_spec.rb +0 -18
  61. data/spec/lib/castle/api/list_items/update_spec.rb +0 -22
  62. data/spec/lib/castle/api/lists/create_spec.rb +0 -21
  63. data/spec/lib/castle/api/lists/delete_spec.rb +0 -17
  64. data/spec/lib/castle/api/lists/get_all_spec.rb +0 -17
  65. data/spec/lib/castle/api/lists/get_spec.rb +0 -17
  66. data/spec/lib/castle/api/lists/query_spec.rb +0 -21
  67. data/spec/lib/castle/api/lists/update_spec.rb +0 -21
  68. data/spec/lib/castle/api/log_spec.rb +0 -5
  69. data/spec/lib/castle/api/report_device_spec.rb +0 -17
  70. data/spec/lib/castle/api/risk_spec.rb +0 -5
  71. data/spec/lib/castle/api/start_impersonation_spec.rb +0 -59
  72. data/spec/lib/castle/api/track_spec.rb +0 -65
  73. data/spec/lib/castle/api_spec.rb +0 -36
  74. data/spec/lib/castle/client_id/extract_spec.rb +0 -47
  75. data/spec/lib/castle/client_spec.rb +0 -342
  76. data/spec/lib/castle/command_spec.rb +0 -9
  77. data/spec/lib/castle/commands/approve_device_spec.rb +0 -24
  78. data/spec/lib/castle/commands/authenticate_spec.rb +0 -86
  79. data/spec/lib/castle/commands/end_impersonation_spec.rb +0 -72
  80. data/spec/lib/castle/commands/filter_spec.rb +0 -72
  81. data/spec/lib/castle/commands/get_device_spec.rb +0 -24
  82. data/spec/lib/castle/commands/get_devices_for_user_spec.rb +0 -24
  83. data/spec/lib/castle/commands/list_items/archive_spec.rb +0 -21
  84. data/spec/lib/castle/commands/list_items/count_spec.rb +0 -21
  85. data/spec/lib/castle/commands/list_items/create_spec.rb +0 -22
  86. data/spec/lib/castle/commands/list_items/get_spec.rb +0 -21
  87. data/spec/lib/castle/commands/list_items/query_spec.rb +0 -27
  88. data/spec/lib/castle/commands/list_items/unarchive_spec.rb +0 -21
  89. data/spec/lib/castle/commands/list_items/update_spec.rb +0 -21
  90. data/spec/lib/castle/commands/lists/create_spec.rb +0 -33
  91. data/spec/lib/castle/commands/lists/delete_spec.rb +0 -21
  92. data/spec/lib/castle/commands/lists/get_all_spec.rb +0 -11
  93. data/spec/lib/castle/commands/lists/get_spec.rb +0 -21
  94. data/spec/lib/castle/commands/lists/query_spec.rb +0 -27
  95. data/spec/lib/castle/commands/lists/update_spec.rb +0 -29
  96. data/spec/lib/castle/commands/log_spec.rb +0 -73
  97. data/spec/lib/castle/commands/report_device_spec.rb +0 -24
  98. data/spec/lib/castle/commands/risk_spec.rb +0 -73
  99. data/spec/lib/castle/commands/start_impersonation_spec.rb +0 -72
  100. data/spec/lib/castle/commands/track_spec.rb +0 -89
  101. data/spec/lib/castle/configuration_spec.rb +0 -14
  102. data/spec/lib/castle/context/get_default_spec.rb +0 -41
  103. data/spec/lib/castle/context/merge_spec.rb +0 -23
  104. data/spec/lib/castle/context/prepare_spec.rb +0 -42
  105. data/spec/lib/castle/context/sanitize_spec.rb +0 -27
  106. data/spec/lib/castle/core/get_connection_spec.rb +0 -43
  107. data/spec/lib/castle/core/process_response_spec.rb +0 -103
  108. data/spec/lib/castle/core/process_webhook_spec.rb +0 -52
  109. data/spec/lib/castle/core/send_request_spec.rb +0 -97
  110. data/spec/lib/castle/failover/strategy_spec.rb +0 -12
  111. data/spec/lib/castle/headers/extract_spec.rb +0 -103
  112. data/spec/lib/castle/headers/filter_spec.rb +0 -42
  113. data/spec/lib/castle/headers/format_spec.rb +0 -25
  114. data/spec/lib/castle/ips/extract_spec.rb +0 -91
  115. data/spec/lib/castle/logger_spec.rb +0 -39
  116. data/spec/lib/castle/payload/prepare_spec.rb +0 -52
  117. data/spec/lib/castle/secure_mode_spec.rb +0 -7
  118. data/spec/lib/castle/session_spec.rb +0 -61
  119. data/spec/lib/castle/singleton_configuration_spec.rb +0 -14
  120. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +0 -69
  121. data/spec/lib/castle/utils/clone_spec.rb +0 -19
  122. data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +0 -50
  123. data/spec/lib/castle/utils/get_timestamp_spec.rb +0 -16
  124. data/spec/lib/castle/utils/merge_spec.rb +0 -13
  125. data/spec/lib/castle/validators/not_supported_spec.rb +0 -19
  126. data/spec/lib/castle/validators/present_spec.rb +0 -25
  127. data/spec/lib/castle/verdict_spec.rb +0 -9
  128. data/spec/lib/castle/version_spec.rb +0 -5
  129. data/spec/lib/castle/webhooks/verify_spec.rb +0 -59
  130. data/spec/lib/castle_spec.rb +0 -58
  131. data/spec/spec_helper.rb +0 -26
  132. data/spec/support/shared_examples/action_request.rb +0 -167
  133. data/spec/support/shared_examples/configuration.rb +0 -99
  134. data/spec/support/shared_examples/list_items.rb +0 -52
  135. data/spec/support/shared_examples/lists.rb +0 -45
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Commands::Log do
4
- subject(:instance) { described_class }
5
-
6
- let(:context) { { test: { test1: '1' } } }
7
- let(:user) { { id: '1234', email: 'foobar@mail.com' } }
8
- let(:default_payload) do
9
- {
10
- request_token: '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302',
11
- event: '$login',
12
- status: '$failed',
13
- user: user,
14
- sent_at: time_auto,
15
- context: context
16
- }
17
- end
18
- let(:time_now) { Time.now }
19
- let(:time_auto) { time_now.utc.iso8601(3) }
20
-
21
- before { Timecop.freeze(time_now) }
22
-
23
- after { Timecop.return }
24
-
25
- describe '.build' do
26
- subject(:command) { instance.build(payload) }
27
-
28
- context 'with properties' do
29
- let(:payload) { default_payload.merge(properties: { test: '1' }) }
30
- let(:command_data) { default_payload.merge(properties: { test: '1' }, context: context) }
31
-
32
- it { expect(command.method).to be(:post) }
33
- it { expect(command.path).to eql('log') }
34
- it { expect(command.data).to eql(command_data) }
35
- end
36
-
37
- context 'with user_traits' do
38
- let(:payload) { default_payload.merge(user_traits: { test: '1' }) }
39
- let(:command_data) { default_payload.merge(user_traits: { test: '1' }, context: context) }
40
-
41
- it { expect(command.method).to be(:post) }
42
- it { expect(command.path).to eql('log') }
43
- it { expect(command.data).to eql(command_data) }
44
- end
45
-
46
- context 'when active true' do
47
- let(:payload) { default_payload.merge(context: context.merge(active: true)) }
48
- let(:command_data) { default_payload.merge(context: context.merge(active: true)) }
49
-
50
- it { expect(command.method).to be(:post) }
51
- it { expect(command.path).to eql('log') }
52
- it { expect(command.data).to eql(command_data) }
53
- end
54
-
55
- context 'when active false' do
56
- let(:payload) { default_payload.merge(context: context.merge(active: false)) }
57
- let(:command_data) { default_payload.merge(context: context.merge(active: false)) }
58
-
59
- it { expect(command.method).to be(:post) }
60
- it { expect(command.path).to eql('log') }
61
- it { expect(command.data).to eql(command_data) }
62
- end
63
-
64
- context 'when active string' do
65
- let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
66
- let(:command_data) { default_payload.merge(context: context) }
67
-
68
- it { expect(command.method).to be(:post) }
69
- it { expect(command.path).to eql('log') }
70
- it { expect(command.data).to eql(command_data) }
71
- end
72
- end
73
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Commands::ReportDevice do
4
- subject(:instance) { described_class }
5
-
6
- let(:context) { {} }
7
- let(:device_token) { '1234' }
8
-
9
- describe '.build' do
10
- subject(:command) { instance.build(device_token: device_token) }
11
-
12
- context 'without device_token' do
13
- let(:device_token) { '' }
14
-
15
- it { expect { command }.to raise_error(Castle::InvalidParametersError) }
16
- end
17
-
18
- context 'with device_token' do
19
- it { expect(command.method).to be(:put) }
20
- it { expect(command.path).to eql("devices/#{device_token}/report") }
21
- it { expect(command.data).to be_nil }
22
- end
23
- end
24
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Commands::Risk do
4
- subject(:instance) { described_class }
5
-
6
- let(:context) { { test: { test1: '1' } } }
7
- let(:user) { { id: '1234', email: 'foobar@mail.com' } }
8
- let(:default_payload) do
9
- {
10
- request_token: '7e51335b-f4bc-4bc7-875d-b713fb61eb23-bf021a3022a1a302',
11
- event: '$login',
12
- status: '$succeeded',
13
- user: user,
14
- sent_at: time_auto,
15
- context: context
16
- }
17
- end
18
- let(:time_now) { Time.now }
19
- let(:time_auto) { time_now.utc.iso8601(3) }
20
-
21
- before { Timecop.freeze(time_now) }
22
-
23
- after { Timecop.return }
24
-
25
- describe '.build' do
26
- subject(:command) { instance.build(payload) }
27
-
28
- context 'with properties' do
29
- let(:payload) { default_payload.merge(properties: { test: '1' }) }
30
- let(:command_data) { default_payload.merge(properties: { test: '1' }, context: context) }
31
-
32
- it { expect(command.method).to be(:post) }
33
- it { expect(command.path).to eql('risk') }
34
- it { expect(command.data).to eql(command_data) }
35
- end
36
-
37
- context 'with user_traits' do
38
- let(:payload) { default_payload.merge(user_traits: { test: '1' }) }
39
- let(:command_data) { default_payload.merge(user_traits: { test: '1' }, context: context) }
40
-
41
- it { expect(command.method).to be(:post) }
42
- it { expect(command.path).to eql('risk') }
43
- it { expect(command.data).to eql(command_data) }
44
- end
45
-
46
- context 'when active true' do
47
- let(:payload) { default_payload.merge(context: context.merge(active: true)) }
48
- let(:command_data) { default_payload.merge(context: context.merge(active: true)) }
49
-
50
- it { expect(command.method).to be(:post) }
51
- it { expect(command.path).to eql('risk') }
52
- it { expect(command.data).to eql(command_data) }
53
- end
54
-
55
- context 'when active false' do
56
- let(:payload) { default_payload.merge(context: context.merge(active: false)) }
57
- let(:command_data) { default_payload.merge(context: context.merge(active: false)) }
58
-
59
- it { expect(command.method).to be(:post) }
60
- it { expect(command.path).to eql('risk') }
61
- it { expect(command.data).to eql(command_data) }
62
- end
63
-
64
- context 'when active string' do
65
- let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
66
- let(:command_data) { default_payload.merge(context: context) }
67
-
68
- it { expect(command.method).to be(:post) }
69
- it { expect(command.path).to eql('risk') }
70
- it { expect(command.data).to eql(command_data) }
71
- end
72
- end
73
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Commands::StartImpersonation do
4
- subject(:instance) { described_class }
5
-
6
- let(:context) { { user_agent: 'test', ip: '127.0.0.1', client_id: 'test' } }
7
- let(:impersonator) { 'test@castle.io' }
8
- let(:default_payload) { { user_id: '1234', sent_at: time_auto, context: context } }
9
-
10
- let(:time_now) { Time.now }
11
- let(:time_auto) { time_now.utc.iso8601(3) }
12
-
13
- before { Timecop.freeze(time_now) }
14
-
15
- after { Timecop.return }
16
-
17
- describe '.build' do
18
- subject(:command) { instance.build(payload) }
19
-
20
- context 'with impersonator' do
21
- let(:payload) { default_payload.merge(properties: { impersonator: impersonator }) }
22
- let(:command_data) { default_payload.merge(properties: { impersonator: impersonator }, context: context) }
23
-
24
- it { expect(command.method).to be(:post) }
25
- it { expect(command.path).to eql('impersonate') }
26
- it { expect(command.data).to eql(command_data) }
27
- end
28
-
29
- context 'when active true' do
30
- let(:payload) { default_payload.merge(context: context.merge(active: true)) }
31
- let(:command_data) { default_payload.merge(context: context.merge(active: true)) }
32
-
33
- it { expect(command.method).to be(:post) }
34
- it { expect(command.path).to eql('impersonate') }
35
- it { expect(command.data).to eql(command_data) }
36
- end
37
-
38
- context 'when active false' do
39
- let(:payload) { default_payload.merge(context: context.merge(active: false)) }
40
- let(:command_data) { default_payload.merge(context: context.merge(active: false)) }
41
-
42
- it { expect(command.method).to be(:post) }
43
- it { expect(command.path).to eql('impersonate') }
44
- it { expect(command.data).to eql(command_data) }
45
- end
46
-
47
- context 'when active string' do
48
- let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
49
- let(:command_data) { default_payload.merge(context: context) }
50
-
51
- it { expect(command.method).to be(:post) }
52
- it { expect(command.path).to eql('impersonate') }
53
- it { expect(command.data).to eql(command_data) }
54
- end
55
- end
56
-
57
- describe '#validate!' do
58
- subject(:validate!) { instance.build(payload) }
59
-
60
- context 'when user_id not present' do
61
- let(:payload) { {} }
62
-
63
- it { expect { validate! }.to raise_error(Castle::InvalidParametersError, 'user_id is missing or empty') }
64
- end
65
-
66
- context 'when user_id present' do
67
- let(:payload) { { user_id: '1234', context: context } }
68
-
69
- it { expect { validate! }.not_to raise_error }
70
- end
71
- end
72
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Commands::Track do
4
- subject(:instance) { described_class }
5
-
6
- let(:context) { { test: { test1: '1' } } }
7
- let(:default_payload) { { event: '$login.track', sent_at: time_auto, context: context } }
8
-
9
- let(:time_now) { Time.now }
10
- let(:time_auto) { time_now.utc.iso8601(3) }
11
-
12
- before { Timecop.freeze(time_now) }
13
-
14
- after { Timecop.return }
15
-
16
- describe '#build' do
17
- subject(:command) { instance.build(payload) }
18
-
19
- context 'with user_id' do
20
- let(:payload) { default_payload.merge(user_id: '1234') }
21
- let(:command_data) { default_payload.merge(user_id: '1234', context: context) }
22
-
23
- it { expect(command.method).to be(:post) }
24
- it { expect(command.path).to eql('track') }
25
- it { expect(command.data).to eql(command_data) }
26
- end
27
-
28
- context 'with properties' do
29
- let(:payload) { default_payload.merge(properties: { test: '1' }) }
30
- let(:command_data) { default_payload.merge(properties: { test: '1' }, context: context) }
31
-
32
- it { expect(command.method).to be(:post) }
33
- it { expect(command.path).to eql('track') }
34
- it { expect(command.data).to eql(command_data) }
35
- end
36
-
37
- context 'with user_traits' do
38
- let(:payload) { default_payload.merge(user_traits: { test: '1' }) }
39
- let(:command_data) { default_payload.merge(user_traits: { test: '1' }, context: context) }
40
-
41
- it { expect(command.method).to be(:post) }
42
- it { expect(command.path).to eql('track') }
43
- it { expect(command.data).to eql(command_data) }
44
- end
45
-
46
- context 'when active true' do
47
- let(:payload) { default_payload.merge(context: context.merge(active: true)) }
48
- let(:command_data) { default_payload.merge(context: context.merge(active: true)) }
49
-
50
- it { expect(command.method).to be(:post) }
51
- it { expect(command.path).to eql('track') }
52
- it { expect(command.data).to eql(command_data) }
53
- end
54
-
55
- context 'when active false' do
56
- let(:payload) { default_payload.merge(context: context.merge(active: false)) }
57
- let(:command_data) { default_payload.merge(context: context.merge(active: false)) }
58
-
59
- it { expect(command.method).to be(:post) }
60
- it { expect(command.path).to eql('track') }
61
- it { expect(command.data).to eql(command_data) }
62
- end
63
-
64
- context 'when active string' do
65
- let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
66
- let(:command_data) { default_payload.merge(context: context) }
67
-
68
- it { expect(command.method).to be(:post) }
69
- it { expect(command.path).to eql('track') }
70
- it { expect(command.data).to eql(command_data) }
71
- end
72
- end
73
-
74
- describe '#validate!' do
75
- subject(:validate!) { instance.build(payload) }
76
-
77
- context 'when event not present' do
78
- let(:payload) { {} }
79
-
80
- it { expect { validate! }.to raise_error(Castle::InvalidParametersError, 'event is missing or empty') }
81
- end
82
-
83
- context 'when event present' do
84
- let(:payload) { { event: '$login.track' } }
85
-
86
- it { expect { validate! }.not_to raise_error }
87
- end
88
- end
89
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Configuration do
4
- subject(:config) { described_class.new }
5
-
6
- it_behaves_like 'configuration_host'
7
- it_behaves_like 'configuration_request_timeout'
8
- it_behaves_like 'configuration_allowlisted'
9
- it_behaves_like 'configuration_denylisted'
10
- it_behaves_like 'configuration_failover_strategy'
11
- it_behaves_like 'configuration_api_secret'
12
-
13
- it { expect(config.api_secret).to eql('') }
14
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Context::GetDefault do
4
- subject { described_class.new(request, nil) }
5
-
6
- let(:ip) { '1.2.3.4' }
7
- let(:client_id) { 'abcd' }
8
-
9
- let(:env) do
10
- Rack::MockRequest.env_for(
11
- '/',
12
- 'HTTP_X_FORWARDED_FOR' => ip,
13
- 'HTTP_ACCEPT_LANGUAGE' => 'en',
14
- 'HTTP_USER_AGENT' => 'test',
15
- 'HTTP_COOKIE' => "__cid=#{client_id};other=efgh",
16
- 'HTTP_CONTENT_LENGTH' => '0'
17
- )
18
- end
19
- let(:request) { Rack::Request.new(env) }
20
- let(:default_context) { subject.call }
21
- let(:version) { '2.2.0' }
22
- let(:result_headers) do
23
- {
24
- 'X-Forwarded-For' => '1.2.3.4',
25
- 'Accept-Language' => 'en',
26
- 'User-Agent' => 'test',
27
- 'Content-Length' => '0',
28
- 'Cookie' => true
29
- }
30
- end
31
-
32
- before { stub_const('Castle::VERSION', version) }
33
-
34
- it { expect(default_context[:active]).to be(true) }
35
- it { expect(default_context[:headers]).to eql(result_headers) }
36
- it { expect(default_context[:ip]).to eql(ip) }
37
- it { expect(default_context[:client_id]).to eql(client_id) }
38
- it { expect(default_context[:library][:name]).to eql('castle-rb') }
39
- it { expect(default_context[:library][:version]).to eql(version) }
40
- it { expect(default_context[:user_agent]).to eql('test') }
41
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Context::Merge do
4
- let(:first) { { test: { test1: { c: '4' } } } }
5
-
6
- describe '#call' do
7
- subject { described_class.call(first, second) }
8
-
9
- let(:result) { { test: { test1: { c: '4', d: '5' } } } }
10
-
11
- context 'with symbol keys' do
12
- let(:second) { { test: { test1: { d: '5' } } } }
13
-
14
- it { is_expected.to eq(result) }
15
- end
16
-
17
- context 'with string keys' do
18
- let(:second) { { 'test' => { 'test1' => { 'd' => '5' } } } }
19
-
20
- it { is_expected.to eq(result) }
21
- end
22
- end
23
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Context::Prepare do
4
- let(:ip) { '1.2.3.4' }
5
- let(:cookie_id) { 'abcd' }
6
- let(:ua) { 'Chrome' }
7
- let(:env) do
8
- Rack::MockRequest.env_for(
9
- '/',
10
- 'HTTP_USER_AGENT' => ua,
11
- 'HTTP_X_FORWARDED_FOR' => ip,
12
- 'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh",
13
- 'HTTP_CONTENT_LENGTH' => '0'
14
- )
15
- end
16
- let(:request) { Rack::Request.new(env) }
17
- let(:context) do
18
- {
19
- client_id: 'abcd',
20
- active: true,
21
- user_agent: ua,
22
- headers: headers,
23
- ip: ip,
24
- library: {
25
- name: 'castle-rb',
26
- version: '6.0.0'
27
- }
28
- }
29
- end
30
-
31
- let(:headers) { { 'Content-Length': '0', 'User-Agent': ua, 'X-Forwarded-For': ip.to_s, Cookie: true } }
32
-
33
- before { stub_const('Castle::VERSION', '6.0.0') }
34
-
35
- describe '#call' do
36
- subject(:generated) { described_class.call(request) }
37
-
38
- context 'when active true' do
39
- it { is_expected.to eql(context) }
40
- end
41
- end
42
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Context::Sanitize do
4
- let(:paylod) { { test: 'test' } }
5
-
6
- describe '#call' do
7
- subject { described_class.call(context) }
8
-
9
- context 'when active true' do
10
- let(:context) { paylod.merge(active: true) }
11
-
12
- it { is_expected.to eql(context) }
13
- end
14
-
15
- context 'when active false' do
16
- let(:context) { paylod.merge(active: false) }
17
-
18
- it { is_expected.to eql(context) }
19
- end
20
-
21
- context 'when active string' do
22
- let(:context) { paylod.merge(active: 'uknown') }
23
-
24
- it { is_expected.to eql(paylod) }
25
- end
26
- end
27
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Core::GetConnection do
4
- describe '.call' do
5
- subject(:class_call) { described_class.call }
6
-
7
- context 'when ssl false' do
8
- let(:localhost) { 'localhost' }
9
- let(:port) { 3002 }
10
- let(:api_url) { '/test' }
11
-
12
- before do
13
- Castle.config.base_url = 'http://localhost:3002'
14
-
15
- allow(Net::HTTP).to receive(:new).with(localhost, port).and_call_original
16
- end
17
-
18
- it do
19
- class_call
20
-
21
- expect(Net::HTTP).to have_received(:new).with(localhost, port)
22
- end
23
-
24
- it { expect(class_call).to be_an_instance_of(Net::HTTP) }
25
- end
26
-
27
- context 'when ssl true' do
28
- let(:localhost) { 'localhost' }
29
- let(:port) { 443 }
30
-
31
- before { Castle.config.base_url = 'https://localhost' }
32
-
33
- context 'with block' do
34
- let(:api_url) { '/test' }
35
- let(:request) { Net::HTTP::Get.new(api_url) }
36
-
37
- before { allow(Net::HTTP).to receive(:new).with(localhost, port).and_call_original }
38
-
39
- it { expect(class_call).to be_an_instance_of(Net::HTTP) }
40
- end
41
- end
42
- end
43
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Core::ProcessResponse do
4
- describe '#call' do
5
- subject(:call) { described_class.call(response) }
6
-
7
- context 'when success' do
8
- let(:response) { OpenStruct.new(body: '{"user":1}', code: 200) }
9
-
10
- it { expect(call).to eql(user: 1) }
11
- end
12
-
13
- describe 'authenticate' do
14
- context 'when allow without any additional props' do
15
- let(:response) { OpenStruct.new(body: '{"action":"allow","user_id":"12345"}', code: 200) }
16
-
17
- it { expect(call).to eql({ action: 'allow', user_id: '12345' }) }
18
- end
19
-
20
- context 'when allow with additional props' do
21
- let(:response) { OpenStruct.new(body: '{"action":"allow","user_id":"12345","internal":{}}', code: 200) }
22
-
23
- it { expect(call).to eql({ action: 'allow', user_id: '12345', internal: {} }) }
24
- end
25
-
26
- context 'when deny without risk policy' do
27
- let(:response) { OpenStruct.new(body: '{"action":"deny","user_id":"1","device_token":"abc"}', code: 200) }
28
-
29
- it { expect(call).to eql({ action: 'deny', user_id: '1', device_token: 'abc' }) }
30
- end
31
-
32
- context 'when deny with risk policy' do
33
- let(:body) do
34
- '{"action":"deny","user_id":"1","device_token":"abc",
35
- "risk_policy":{"id":"123","revision_id":"abc","name":"def","type":"bot"}}'
36
- end
37
- let(:response) { OpenStruct.new({ body: body, code: 200 }) }
38
-
39
- let(:result) do
40
- {
41
- action: 'deny',
42
- user_id: '1',
43
- device_token: 'abc',
44
- risk_policy: {
45
- id: '123',
46
- revision_id: 'abc',
47
- name: 'def',
48
- type: 'bot'
49
- }
50
- }
51
- end
52
-
53
- it { expect(call).to eql(result) }
54
- end
55
- end
56
-
57
- context 'when response empty' do
58
- let(:response) { OpenStruct.new(body: '', code: 200) }
59
-
60
- it { expect(call).to eql({}) }
61
- end
62
-
63
- context 'when response nil' do
64
- let(:response) { OpenStruct.new(code: 200) }
65
-
66
- it { expect(call).to eql({}) }
67
- end
68
-
69
- context 'when json is malformed' do
70
- let(:response) { OpenStruct.new(body: '{a', code: 200) }
71
-
72
- it { expect { call }.to raise_error(Castle::ApiError) }
73
- end
74
- end
75
-
76
- describe '#verify!' do
77
- subject(:verify!) { described_class.verify!(response) }
78
-
79
- context 'without error when response is 2xx' do
80
- let(:response) { OpenStruct.new(code: 200) }
81
-
82
- it { expect { verify! }.not_to raise_error }
83
- end
84
-
85
- shared_examples 'response_failed' do |code, error|
86
- let(:response) { OpenStruct.new(code: code) }
87
-
88
- it "fail when response is #{code}" do
89
- expect { verify! }.to raise_error(error)
90
- end
91
- end
92
-
93
- it_behaves_like 'response_failed', '400', Castle::BadRequestError
94
- it_behaves_like 'response_failed', '401', Castle::UnauthorizedError
95
- it_behaves_like 'response_failed', '403', Castle::ForbiddenError
96
- it_behaves_like 'response_failed', '404', Castle::NotFoundError
97
- it_behaves_like 'response_failed', '419', Castle::UserUnauthorizedError
98
- it_behaves_like 'response_failed', '422', Castle::InvalidParametersError
99
- it_behaves_like 'response_failed', '429', Castle::RateLimitError
100
- it_behaves_like 'response_failed', '499', Castle::ApiError
101
- it_behaves_like 'response_failed', '500', Castle::InternalServerError
102
- end
103
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Castle::Core::ProcessWebhook do
4
- describe '#call' do
5
- subject(:call) { described_class.call(webhook) }
6
-
7
- let(:webhook_body) do
8
- {
9
- api_version: 'v1',
10
- app_id: '12345',
11
- type: '$incident.confirmed',
12
- created_at: '2020-12-18T12:55:21.779Z',
13
- data: {
14
- id: 'test',
15
- device_token: 'token',
16
- user_id: '',
17
- trigger: '$login.succeeded',
18
- context: {
19
- },
20
- location: {
21
- },
22
- user_agent: {
23
- }
24
- },
25
- user_traits: {
26
- },
27
- properties: {
28
- },
29
- policy: {
30
- }
31
- }.to_json
32
- end
33
-
34
- let(:webhook) { OpenStruct.new(body: StringIO.new(webhook_body)) }
35
-
36
- context 'when success' do
37
- it { expect(call).to eql(webhook_body) }
38
- end
39
-
40
- context 'when webhook empty' do
41
- let(:webhook) { OpenStruct.new(body: StringIO.new('')) }
42
-
43
- it { expect { call }.to raise_error(Castle::ApiError, 'Invalid webhook from Castle API') }
44
- end
45
-
46
- context 'when webhook nil' do
47
- let(:webhook) { OpenStruct.new(body: StringIO.new) }
48
-
49
- it { expect { call }.to raise_error(Castle::ApiError, 'Invalid webhook from Castle API') }
50
- end
51
- end
52
- end