castle-rb 5.0.0 → 7.1.1

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 (144) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +113 -39
  3. data/lib/castle.rb +49 -29
  4. data/lib/castle/api.rb +20 -16
  5. data/lib/castle/api/approve_device.rb +20 -0
  6. data/lib/castle/api/authenticate.rb +37 -0
  7. data/lib/castle/api/end_impersonation.rb +24 -0
  8. data/lib/castle/api/filter.rb +37 -0
  9. data/lib/castle/api/get_device.rb +20 -0
  10. data/lib/castle/api/get_devices_for_user.rb +20 -0
  11. data/lib/castle/api/log.rb +37 -0
  12. data/lib/castle/api/report_device.rb +20 -0
  13. data/lib/castle/api/risk.rb +37 -0
  14. data/lib/castle/api/start_impersonation.rb +24 -0
  15. data/lib/castle/api/track.rb +21 -0
  16. data/lib/castle/client.rb +74 -68
  17. data/lib/castle/{extractors/client_id.rb → client_id/extract.rb} +2 -2
  18. data/lib/castle/commands/approve_device.rb +17 -0
  19. data/lib/castle/commands/authenticate.rb +13 -13
  20. data/lib/castle/commands/end_impersonation.rb +25 -0
  21. data/lib/castle/commands/filter.rb +22 -0
  22. data/lib/castle/commands/get_device.rb +17 -0
  23. data/lib/castle/commands/get_devices_for_user.rb +17 -0
  24. data/lib/castle/commands/log.rb +22 -0
  25. data/lib/castle/commands/report_device.rb +17 -0
  26. data/lib/castle/commands/risk.rb +22 -0
  27. data/lib/castle/commands/start_impersonation.rb +25 -0
  28. data/lib/castle/commands/track.rb +12 -13
  29. data/lib/castle/configuration.rb +31 -23
  30. data/lib/castle/context/{default.rb → get_default.rb} +5 -6
  31. data/lib/castle/context/{merger.rb → merge.rb} +3 -3
  32. data/lib/castle/context/prepare.rb +18 -0
  33. data/lib/castle/context/{sanitizer.rb → sanitize.rb} +1 -1
  34. data/lib/castle/core/get_connection.rb +27 -0
  35. data/lib/castle/{api/response.rb → core/process_response.rb} +8 -3
  36. data/lib/castle/core/process_webhook.rb +25 -0
  37. data/lib/castle/core/send_request.rb +42 -0
  38. data/lib/castle/errors.rb +38 -12
  39. data/lib/castle/failover/prepare_response.rb +28 -0
  40. data/lib/castle/failover/strategy.rb +23 -0
  41. data/lib/castle/{extractors/headers.rb → headers/extract.rb} +8 -6
  42. data/lib/castle/headers/filter.rb +40 -0
  43. data/lib/castle/headers/format.rb +24 -0
  44. data/lib/castle/{extractors/ip.rb → ips/extract.rb} +11 -7
  45. data/lib/castle/logger.rb +19 -0
  46. data/lib/castle/payload/prepare.rb +26 -0
  47. data/lib/castle/secure_mode.rb +7 -2
  48. data/lib/castle/session.rb +18 -0
  49. data/lib/castle/singleton_configuration.rb +9 -0
  50. data/lib/castle/support/hanami.rb +2 -6
  51. data/lib/castle/support/rails.rb +1 -3
  52. data/lib/castle/utils/clean_invalid_chars.rb +22 -0
  53. data/lib/castle/utils/clone.rb +15 -0
  54. data/lib/castle/utils/deep_symbolize_keys.rb +45 -0
  55. data/lib/castle/utils/get_timestamp.rb +15 -0
  56. data/lib/castle/utils/{merger.rb → merge.rb} +3 -3
  57. data/lib/castle/utils/secure_compare.rb +22 -0
  58. data/lib/castle/validators/not_supported.rb +1 -0
  59. data/lib/castle/validators/present.rb +1 -0
  60. data/lib/castle/verdict.rb +15 -0
  61. data/lib/castle/version.rb +1 -1
  62. data/lib/castle/webhooks/verify.rb +45 -0
  63. data/spec/integration/rails/rails_spec.rb +42 -14
  64. data/spec/integration/rails/support/application.rb +3 -1
  65. data/spec/integration/rails/support/home_controller.rb +50 -6
  66. data/spec/lib/castle/api/approve_device_spec.rb +21 -0
  67. data/spec/lib/castle/api/authenticate_spec.rb +136 -0
  68. data/spec/lib/castle/api/end_impersonation_spec.rb +65 -0
  69. data/spec/lib/castle/api/filter_spec.rb +5 -0
  70. data/spec/lib/castle/api/get_device_spec.rb +19 -0
  71. data/spec/lib/castle/api/get_devices_for_user_spec.rb +19 -0
  72. data/spec/lib/castle/api/log_spec.rb +5 -0
  73. data/spec/lib/castle/api/report_device_spec.rb +21 -0
  74. data/spec/lib/castle/api/risk_spec.rb +5 -0
  75. data/spec/lib/castle/api/start_impersonation_spec.rb +65 -0
  76. data/spec/lib/castle/api/track_spec.rb +72 -0
  77. data/spec/lib/castle/api_spec.rb +14 -15
  78. data/spec/lib/castle/{extractors/client_id_spec.rb → client_id/extract_spec.rb} +6 -15
  79. data/spec/lib/castle/client_spec.rb +110 -92
  80. data/spec/lib/castle/commands/approve_device_spec.rb +24 -0
  81. data/spec/lib/castle/commands/authenticate_spec.rb +15 -31
  82. data/spec/lib/castle/commands/end_impersonation_spec.rb +79 -0
  83. data/spec/lib/castle/commands/filter_spec.rb +72 -0
  84. data/spec/lib/castle/commands/get_device_spec.rb +24 -0
  85. data/spec/lib/castle/commands/{review_spec.rb → get_devices_for_user_spec.rb} +7 -7
  86. data/spec/lib/castle/commands/log_spec.rb +73 -0
  87. data/spec/lib/castle/commands/report_device_spec.rb +24 -0
  88. data/spec/lib/castle/commands/risk_spec.rb +73 -0
  89. data/spec/lib/castle/commands/{impersonate_spec.rb → start_impersonation_spec.rb} +13 -41
  90. data/spec/lib/castle/commands/track_spec.rb +14 -34
  91. data/spec/lib/castle/configuration_spec.rb +8 -141
  92. data/spec/lib/castle/context/{default_spec.rb → get_default_spec.rb} +9 -10
  93. data/spec/lib/castle/context/{merger_spec.rb → merge_spec.rb} +1 -1
  94. data/spec/lib/castle/context/prepare_spec.rb +43 -0
  95. data/spec/lib/castle/context/{sanitizer_spec.rb → sanitize_spec.rb} +1 -1
  96. data/spec/lib/castle/core/get_connection_spec.rb +43 -0
  97. data/spec/lib/castle/{api/response_spec.rb → core/process_response_spec.rb} +49 -1
  98. data/spec/lib/castle/core/process_webhook_spec.rb +46 -0
  99. data/spec/lib/castle/{api/request_spec.rb → core/send_request_spec.rb} +16 -37
  100. data/spec/lib/castle/failover/strategy_spec.rb +12 -0
  101. data/spec/lib/castle/{extractors/headers_spec.rb → headers/extract_spec.rb} +7 -9
  102. data/spec/lib/castle/headers/filter_spec.rb +39 -0
  103. data/spec/lib/castle/headers/format_spec.rb +25 -0
  104. data/spec/lib/castle/{extractors/ip_spec.rb → ips/extract_spec.rb} +5 -14
  105. data/spec/lib/castle/logger_spec.rb +38 -0
  106. data/spec/lib/castle/payload/prepare_spec.rb +55 -0
  107. data/spec/lib/castle/session_spec.rb +65 -0
  108. data/spec/lib/castle/singleton_configuration_spec.rb +14 -0
  109. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +69 -0
  110. data/spec/lib/castle/utils/{cloner_spec.rb → clone_spec.rb} +3 -3
  111. data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +50 -0
  112. data/spec/lib/castle/utils/{timestamp_spec.rb → get_timestamp_spec.rb} +1 -1
  113. data/spec/lib/castle/utils/merge_spec.rb +15 -0
  114. data/spec/lib/castle/validators/present_spec.rb +5 -6
  115. data/spec/lib/castle/verdict_spec.rb +9 -0
  116. data/spec/lib/castle/webhooks/verify_spec.rb +53 -0
  117. data/spec/lib/castle_spec.rb +4 -10
  118. data/spec/spec_helper.rb +3 -3
  119. data/spec/support/shared_examples/action_request.rb +155 -0
  120. data/spec/support/shared_examples/configuration.rb +101 -0
  121. metadata +144 -67
  122. data/lib/castle/api/connection.rb +0 -24
  123. data/lib/castle/api/request.rb +0 -42
  124. data/lib/castle/api/session.rb +0 -20
  125. data/lib/castle/commands/identify.rb +0 -23
  126. data/lib/castle/commands/impersonate.rb +0 -26
  127. data/lib/castle/commands/review.rb +0 -14
  128. data/lib/castle/events.rb +0 -49
  129. data/lib/castle/failover_auth_response.rb +0 -21
  130. data/lib/castle/headers_filter.rb +0 -35
  131. data/lib/castle/headers_formatter.rb +0 -22
  132. data/lib/castle/review.rb +0 -11
  133. data/lib/castle/utils.rb +0 -55
  134. data/lib/castle/utils/cloner.rb +0 -11
  135. data/lib/castle/utils/timestamp.rb +0 -12
  136. data/spec/lib/castle/api/connection_spec.rb +0 -59
  137. data/spec/lib/castle/api/session_spec.rb +0 -86
  138. data/spec/lib/castle/commands/identify_spec.rb +0 -88
  139. data/spec/lib/castle/events_spec.rb +0 -5
  140. data/spec/lib/castle/headers_filter_spec.rb +0 -38
  141. data/spec/lib/castle/headers_formatter_spec.rb +0 -25
  142. data/spec/lib/castle/review_spec.rb +0 -19
  143. data/spec/lib/castle/utils/merger_spec.rb +0 -13
  144. data/spec/lib/castle/utils_spec.rb +0 -156
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::ApproveDevice do
4
+ before do
5
+ stub_request(:any, /api.castle.io/)
6
+ .with(basic_auth: ['', 'secret'])
7
+ .to_return(status: 200, body: '{}', headers: {})
8
+ end
9
+
10
+ describe '.call' do
11
+ subject(:retrieve) { described_class.call(device_token: device_token) }
12
+
13
+ let(:device_token) { '1234' }
14
+
15
+ before { retrieve }
16
+
17
+ it do
18
+ assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/approve", times: 1
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::Authenticate do
4
+ subject(:call_subject) { described_class.call(options) }
5
+
6
+ let(:ip) { '1.2.3.4' }
7
+ let(:cookie_id) { 'abcd' }
8
+ let(:ua) { 'Chrome' }
9
+ let(:env) do
10
+ Rack::MockRequest.env_for(
11
+ '/',
12
+ 'HTTP_USER_AGENT' => ua,
13
+ 'HTTP_X_FORWARDED_FOR' => ip,
14
+ 'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
15
+ )
16
+ end
17
+ let(:request) { Rack::Request.new(env) }
18
+ let(:context) { Castle::Context::Prepare.call(request) }
19
+ let(:time_now) { Time.now }
20
+ let(:time_auto) { time_now.utc.iso8601(3) }
21
+ let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }
22
+ let(:response_body) { {}.to_json }
23
+
24
+ before do
25
+ Timecop.freeze(time_now)
26
+ stub_const('Castle::VERSION', '2.2.0')
27
+ end
28
+
29
+ after { Timecop.return }
30
+
31
+ describe '.call' do
32
+ let(:request_body) do
33
+ { event: '$login.succeeded', context: context, user_id: '1234', sent_at: time_auto }
34
+ end
35
+
36
+ context 'when used with symbol keys' do
37
+ before do
38
+ stub_request(:any, /api.castle.io/)
39
+ .with(basic_auth: ['', 'secret'])
40
+ .to_return(status: 200, body: response_body, headers: {})
41
+ call_subject
42
+ end
43
+
44
+ let(:options) { { event: '$login.succeeded', user_id: '1234', context: context } }
45
+
46
+ it do
47
+ assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
48
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
49
+ end
50
+ end
51
+
52
+ context 'when passed timestamp in options and no defined timestamp' do
53
+ let(:options) do
54
+ { event: '$login.succeeded', user_id: '1234', timestamp: time_user, context: context }
55
+ end
56
+ let(:request_body) do
57
+ {
58
+ event: '$login.succeeded',
59
+ user_id: '1234',
60
+ context: context,
61
+ timestamp: time_user,
62
+ sent_at: time_auto
63
+ }
64
+ end
65
+
66
+ it do
67
+ assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
68
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ context 'when denied' do
75
+ let(:failover_appendix) { { failover: false, failover_reason: nil } }
76
+
77
+ let(:options) { { event: '$login.succeeded', user_id: '1234', context: context } }
78
+
79
+ context 'when denied without any risk policy' do
80
+ let(:response_body) { deny_response_without_rp.to_json }
81
+ let(:deny_response_without_rp) do
82
+ { action: 'deny', user_id: '12345', device_token: 'abcdefg1234' }
83
+ end
84
+ let(:deny_without_rp_failover_result) { deny_response_without_rp.merge(failover_appendix) }
85
+
86
+ before do
87
+ stub_request(:any, /api.castle.io/)
88
+ .with(basic_auth: ['', 'secret'])
89
+ .to_return(status: 200, body: deny_response_without_rp.to_json, headers: {})
90
+ call_subject
91
+ end
92
+
93
+ it do
94
+ assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
95
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
96
+ end
97
+ end
98
+
99
+ it { expect(call_subject).to eql(deny_without_rp_failover_result) }
100
+ end
101
+
102
+ context 'when denied with risk policy' do
103
+ let(:deny_response_with_rp) do
104
+ {
105
+ action: 'deny',
106
+ user_id: '12345',
107
+ device_token: 'abcdefg1234',
108
+ risk_policy: {
109
+ id: 'q-rbeMzBTdW2Fd09sbz55A',
110
+ revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
111
+ name: 'Block Users from X',
112
+ type: 'bot'
113
+ }
114
+ }
115
+ end
116
+ let(:response_body) { deny_response_with_rp.to_json }
117
+ let(:deny_with_rp_failover_result) { deny_response_with_rp.merge(failover_appendix) }
118
+
119
+ before do
120
+ stub_request(:any, /api.castle.io/)
121
+ .with(basic_auth: ['', 'secret'])
122
+ .to_return(status: 200, body: deny_response_with_rp.to_json, headers: {})
123
+ call_subject
124
+ end
125
+
126
+ it do
127
+ assert_requested :post, 'https://api.castle.io/v1/authenticate', times: 1 do |req|
128
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
129
+ end
130
+ end
131
+
132
+ it { expect(call_subject).to eql(deny_with_rp_failover_result) }
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::EndImpersonation do
4
+ subject(:call) { described_class.call(options) }
5
+
6
+ let(:ip) { '1.2.3.4' }
7
+ let(:cookie_id) { 'abcd' }
8
+ let(:ua) { 'Chrome' }
9
+ let(:env) do
10
+ Rack::MockRequest.env_for(
11
+ '/',
12
+ 'HTTP_USER_AGENT' => ua,
13
+ 'HTTP_X_FORWARDED_FOR' => ip,
14
+ 'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
15
+ )
16
+ end
17
+ let(:request) { Rack::Request.new(env) }
18
+ let(:context) { Castle::Context::Prepare.call(request) }
19
+ let(:time_now) { Time.now }
20
+ let(:time_auto) { time_now.utc.iso8601(3) }
21
+
22
+ before do
23
+ Timecop.freeze(time_now)
24
+ stub_const('Castle::VERSION', '2.2.0')
25
+ stub_request(:any, /api.castle.io/)
26
+ .with(basic_auth: ['', 'secret'])
27
+ .to_return(status: 200, body: response_body, headers: {})
28
+ end
29
+
30
+ after { Timecop.return }
31
+
32
+ describe 'call' do
33
+ let(:impersonator) { 'test@castle.io' }
34
+ let(:request_body) do
35
+ {
36
+ user_id: '1234',
37
+ sent_at: time_auto,
38
+ properties: {
39
+ impersonator: impersonator
40
+ },
41
+ context: context
42
+ }
43
+ end
44
+ let(:response_body) { { success: true }.to_json }
45
+ let(:options) do
46
+ { user_id: '1234', properties: { impersonator: impersonator }, context: context }
47
+ end
48
+
49
+ context 'when used with symbol keys' do
50
+ before { call }
51
+
52
+ it do
53
+ assert_requested :delete, 'https://api.castle.io/v1/impersonate', times: 1 do |req|
54
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'when request is not successful' do
60
+ let(:response_body) { {}.to_json }
61
+
62
+ it { expect { call }.to raise_error(Castle::ImpersonationFailed) }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::Filter do
4
+ pending
5
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::GetDevice do
4
+ before do
5
+ stub_request(:any, /api.castle.io/)
6
+ .with(basic_auth: ['', 'secret'])
7
+ .to_return(status: 200, body: '{}', headers: {})
8
+ end
9
+
10
+ describe '.call' do
11
+ subject(:retrieve) { described_class.call(device_token: device_token) }
12
+
13
+ let(:device_token) { '1234' }
14
+
15
+ before { retrieve }
16
+
17
+ it { assert_requested :get, "https://api.castle.io/v1/devices/#{device_token}", times: 1 }
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::GetDevicesForUser do
4
+ before do
5
+ stub_request(:any, /api.castle.io/)
6
+ .with(basic_auth: ['', 'secret'])
7
+ .to_return(status: 200, body: '{}', headers: {})
8
+ end
9
+
10
+ describe '.call' do
11
+ subject(:retrieve) { described_class.call(user_id: user_id) }
12
+
13
+ let(:user_id) { '1234' }
14
+
15
+ before { retrieve }
16
+
17
+ it { assert_requested :get, "https://api.castle.io/v1/users/#{user_id}/devices", times: 1 }
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::Log do
4
+ pending
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::ReportDevice do
4
+ before do
5
+ stub_request(:any, /api.castle.io/)
6
+ .with(basic_auth: ['', 'secret'])
7
+ .to_return(status: 200, body: '{}', headers: {})
8
+ end
9
+
10
+ describe '.call' do
11
+ subject(:retrieve) { described_class.call(device_token: device_token) }
12
+
13
+ let(:device_token) { '1234' }
14
+
15
+ before { retrieve }
16
+
17
+ it do
18
+ assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/report", times: 1
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::Risk do
4
+ pending
5
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::StartImpersonation do
4
+ subject(:call) { described_class.call(options) }
5
+
6
+ let(:ip) { '1.2.3.4' }
7
+ let(:cookie_id) { 'abcd' }
8
+ let(:ua) { 'Chrome' }
9
+ let(:env) do
10
+ Rack::MockRequest.env_for(
11
+ '/',
12
+ 'HTTP_USER_AGENT' => ua,
13
+ 'HTTP_X_FORWARDED_FOR' => ip,
14
+ 'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
15
+ )
16
+ end
17
+ let(:request) { Rack::Request.new(env) }
18
+ let(:context) { Castle::Context::Prepare.call(request) }
19
+ let(:time_now) { Time.now }
20
+ let(:time_auto) { time_now.utc.iso8601(3) }
21
+
22
+ before do
23
+ Timecop.freeze(time_now)
24
+ stub_const('Castle::VERSION', '2.2.0')
25
+ stub_request(:any, /api.castle.io/)
26
+ .with(basic_auth: ['', 'secret'])
27
+ .to_return(status: 200, body: response_body, headers: {})
28
+ end
29
+
30
+ after { Timecop.return }
31
+
32
+ describe 'call' do
33
+ let(:impersonator) { 'test@castle.io' }
34
+ let(:request_body) do
35
+ {
36
+ user_id: '1234',
37
+ sent_at: time_auto,
38
+ properties: {
39
+ impersonator: impersonator
40
+ },
41
+ context: context
42
+ }
43
+ end
44
+ let(:response_body) { { success: true }.to_json }
45
+ let(:options) do
46
+ { user_id: '1234', properties: { impersonator: impersonator }, context: context }
47
+ end
48
+
49
+ context 'when used with symbol keys' do
50
+ before { call }
51
+
52
+ it do
53
+ assert_requested :post, 'https://api.castle.io/v1/impersonate', times: 1 do |req|
54
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'when request is not successful' do
60
+ let(:response_body) { {}.to_json }
61
+
62
+ it { expect { call }.to raise_error(Castle::ImpersonationFailed) }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::API::Track do
4
+ subject(:call) { described_class.call(options) }
5
+
6
+ let(:ip) { '1.2.3.4' }
7
+ let(:cookie_id) { 'abcd' }
8
+ let(:ua) { 'Chrome' }
9
+ let(:env) do
10
+ Rack::MockRequest.env_for(
11
+ '/',
12
+ 'HTTP_USER_AGENT' => ua,
13
+ 'HTTP_X_FORWARDED_FOR' => ip,
14
+ 'HTTP_COOKIE' => "__cid=#{cookie_id};other=efgh"
15
+ )
16
+ end
17
+ let(:request) { Rack::Request.new(env) }
18
+ let(:context) { Castle::Context::Prepare.call(request) }
19
+ let(:time_now) { Time.now }
20
+ let(:time_auto) { time_now.utc.iso8601(3) }
21
+ let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }
22
+ let(:response_body) { {}.to_json }
23
+
24
+ before do
25
+ Timecop.freeze(time_now)
26
+ stub_const('Castle::VERSION', '2.2.0')
27
+ stub_request(:any, /api.castle.io/)
28
+ .with(basic_auth: ['', 'secret'])
29
+ .to_return(status: 200, body: response_body, headers: {})
30
+ end
31
+
32
+ after { Timecop.return }
33
+
34
+ describe 'track' do
35
+ let(:request_body) do
36
+ { event: '$login.succeeded', context: context, user_id: '1234', sent_at: time_auto }
37
+ end
38
+
39
+ before { call }
40
+
41
+ context 'when used with symbol keys' do
42
+ let(:options) { { event: '$login.succeeded', user_id: '1234', context: context } }
43
+
44
+ it do
45
+ assert_requested :post, 'https://api.castle.io/v1/track', times: 1 do |req|
46
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
47
+ end
48
+ end
49
+
50
+ context 'when passed timestamp in options and no defined timestamp' do
51
+ let(:options) do
52
+ { event: '$login.succeeded', user_id: '1234', timestamp: time_user, context: context }
53
+ end
54
+ let(:request_body) do
55
+ {
56
+ event: '$login.succeeded',
57
+ user_id: '1234',
58
+ context: context,
59
+ timestamp: time_user,
60
+ sent_at: time_auto
61
+ }
62
+ end
63
+
64
+ it do
65
+ assert_requested :post, 'https://api.castle.io/v1/track', times: 1 do |req|
66
+ JSON.parse(req.body) == JSON.parse(request_body.to_json)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end