castle-rb 5.0.0 → 6.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 (126) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +107 -33
  3. data/lib/castle.rb +46 -22
  4. data/lib/castle/api.rb +22 -13
  5. data/lib/castle/api/approve_device.rb +25 -0
  6. data/lib/castle/api/authenticate.rb +34 -0
  7. data/lib/castle/api/end_impersonation.rb +29 -0
  8. data/lib/castle/api/get_device.rb +25 -0
  9. data/lib/castle/api/get_devices_for_user.rb +25 -0
  10. data/lib/castle/api/identify.rb +26 -0
  11. data/lib/castle/api/report_device.rb +25 -0
  12. data/lib/castle/api/review.rb +24 -0
  13. data/lib/castle/api/start_impersonation.rb +29 -0
  14. data/lib/castle/api/track.rb +26 -0
  15. data/lib/castle/client.rb +48 -62
  16. data/lib/castle/{extractors/client_id.rb → client_id/extract.rb} +2 -2
  17. data/lib/castle/commands/approve_device.rb +21 -0
  18. data/lib/castle/commands/authenticate.rb +13 -13
  19. data/lib/castle/commands/end_impersonation.rb +25 -0
  20. data/lib/castle/commands/get_device.rb +21 -0
  21. data/lib/castle/commands/get_devices_for_user.rb +21 -0
  22. data/lib/castle/commands/identify.rb +12 -13
  23. data/lib/castle/commands/report_device.rb +21 -0
  24. data/lib/castle/commands/review.rb +6 -3
  25. data/lib/castle/commands/start_impersonation.rb +25 -0
  26. data/lib/castle/commands/track.rb +12 -13
  27. data/lib/castle/configuration.rb +17 -19
  28. data/lib/castle/context/{default.rb → get_default.rb} +5 -6
  29. data/lib/castle/context/{merger.rb → merge.rb} +3 -3
  30. data/lib/castle/context/prepare.rb +18 -0
  31. data/lib/castle/context/{sanitizer.rb → sanitize.rb} +1 -1
  32. data/lib/castle/core/get_connection.rb +25 -0
  33. data/lib/castle/{api/response.rb → core/process_response.rb} +4 -2
  34. data/lib/castle/core/process_webhook.rb +20 -0
  35. data/lib/castle/core/send_request.rb +50 -0
  36. data/lib/castle/errors.rb +2 -0
  37. data/lib/castle/events.rb +1 -1
  38. data/lib/castle/failover/prepare_response.rb +23 -0
  39. data/lib/castle/failover/strategy.rb +20 -0
  40. data/lib/castle/{extractors/headers.rb → headers/extract.rb} +8 -6
  41. data/lib/castle/headers/filter.rb +37 -0
  42. data/lib/castle/headers/format.rb +24 -0
  43. data/lib/castle/{extractors/ip.rb → ip/extract.rb} +8 -7
  44. data/lib/castle/logger.rb +19 -0
  45. data/lib/castle/payload/prepare.rb +27 -0
  46. data/lib/castle/secure_mode.rb +6 -2
  47. data/lib/castle/session.rb +18 -0
  48. data/lib/castle/singleton_configuration.rb +9 -0
  49. data/lib/castle/utils/clean_invalid_chars.rb +24 -0
  50. data/lib/castle/utils/clone.rb +15 -0
  51. data/lib/castle/utils/deep_symbolize_keys.rb +45 -0
  52. data/lib/castle/utils/get_timestamp.rb +15 -0
  53. data/lib/castle/utils/{merger.rb → merge.rb} +3 -3
  54. data/lib/castle/utils/secure_compare.rb +22 -0
  55. data/lib/castle/validators/not_supported.rb +1 -0
  56. data/lib/castle/validators/present.rb +1 -0
  57. data/lib/castle/verdict.rb +13 -0
  58. data/lib/castle/version.rb +1 -1
  59. data/lib/castle/webhooks/verify.rb +43 -0
  60. data/spec/integration/rails/rails_spec.rb +33 -7
  61. data/spec/integration/rails/support/application.rb +3 -1
  62. data/spec/integration/rails/support/home_controller.rb +47 -5
  63. data/spec/lib/castle/api/approve_device_spec.rb +21 -0
  64. data/spec/lib/castle/api/authenticate_spec.rb +140 -0
  65. data/spec/lib/castle/api/end_impersonation_spec.rb +59 -0
  66. data/spec/lib/castle/api/get_device_spec.rb +19 -0
  67. data/spec/lib/castle/api/get_devices_for_user_spec.rb +19 -0
  68. data/spec/lib/castle/api/identify_spec.rb +68 -0
  69. data/spec/lib/castle/api/report_device_spec.rb +21 -0
  70. data/spec/lib/castle/{review_spec.rb → api/review_spec.rb} +3 -3
  71. data/spec/lib/castle/api/start_impersonation_spec.rb +59 -0
  72. data/spec/lib/castle/api/track_spec.rb +68 -0
  73. data/spec/lib/castle/api_spec.rb +16 -1
  74. data/spec/lib/castle/{extractors/client_id_spec.rb → client_id/extract_spec.rb} +2 -2
  75. data/spec/lib/castle/client_spec.rb +39 -21
  76. data/spec/lib/castle/commands/approve_device_spec.rb +24 -0
  77. data/spec/lib/castle/commands/authenticate_spec.rb +7 -16
  78. data/spec/lib/castle/commands/end_impersonation_spec.rb +82 -0
  79. data/spec/lib/castle/commands/get_device_spec.rb +24 -0
  80. data/spec/lib/castle/commands/get_devices_for_user_spec.rb +24 -0
  81. data/spec/lib/castle/commands/identify_spec.rb +5 -16
  82. data/spec/lib/castle/commands/report_device_spec.rb +24 -0
  83. data/spec/lib/castle/commands/review_spec.rb +1 -1
  84. data/spec/lib/castle/commands/{impersonate_spec.rb → start_impersonation_spec.rb} +7 -32
  85. data/spec/lib/castle/commands/track_spec.rb +5 -16
  86. data/spec/lib/castle/configuration_spec.rb +9 -138
  87. data/spec/lib/castle/context/{default_spec.rb → get_default_spec.rb} +1 -2
  88. data/spec/lib/castle/context/{merger_spec.rb → merge_spec.rb} +1 -1
  89. data/spec/lib/castle/context/prepare_spec.rb +44 -0
  90. data/spec/lib/castle/context/{sanitizer_spec.rb → sanitize_spec.rb} +1 -1
  91. data/spec/lib/castle/{api/connection_spec.rb → core/get_connection_spec.rb} +3 -3
  92. data/spec/lib/castle/{api/response_spec.rb → core/process_response_spec.rb} +56 -1
  93. data/spec/lib/castle/core/process_webhook_spec.rb +46 -0
  94. data/spec/lib/castle/{api/request_spec.rb → core/send_request_spec.rb} +20 -16
  95. data/spec/lib/castle/failover/strategy_spec.rb +12 -0
  96. data/spec/lib/castle/{extractors/headers_spec.rb → headers/extract_spec.rb} +7 -7
  97. data/spec/lib/castle/{headers_filter_spec.rb → headers/filter_spec.rb} +3 -3
  98. data/spec/lib/castle/headers/format_spec.rb +25 -0
  99. data/spec/lib/castle/{extractors/ip_spec.rb → ip/extract_spec.rb} +1 -1
  100. data/spec/lib/castle/logger_spec.rb +42 -0
  101. data/spec/lib/castle/payload/prepare_spec.rb +54 -0
  102. data/spec/lib/castle/{api/session_spec.rb → session_spec.rb} +6 -4
  103. data/spec/lib/castle/singleton_configuration_spec.rb +18 -0
  104. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +69 -0
  105. data/spec/lib/castle/utils/{cloner_spec.rb → clone_spec.rb} +3 -3
  106. data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +50 -0
  107. data/spec/lib/castle/utils/{timestamp_spec.rb → get_timestamp_spec.rb} +1 -1
  108. data/spec/lib/castle/utils/{merger_spec.rb → merge_spec.rb} +3 -3
  109. data/spec/lib/castle/verdict_spec.rb +9 -0
  110. data/spec/lib/castle/webhooks/verify_spec.rb +69 -0
  111. data/spec/spec_helper.rb +2 -0
  112. data/spec/support/shared_examples/configuration.rb +129 -0
  113. metadata +129 -57
  114. data/lib/castle/api/connection.rb +0 -24
  115. data/lib/castle/api/request.rb +0 -42
  116. data/lib/castle/api/session.rb +0 -20
  117. data/lib/castle/commands/impersonate.rb +0 -26
  118. data/lib/castle/failover_auth_response.rb +0 -21
  119. data/lib/castle/headers_filter.rb +0 -35
  120. data/lib/castle/headers_formatter.rb +0 -22
  121. data/lib/castle/review.rb +0 -11
  122. data/lib/castle/utils.rb +0 -55
  123. data/lib/castle/utils/cloner.rb +0 -11
  124. data/lib/castle/utils/timestamp.rb +0 -12
  125. data/spec/lib/castle/headers_formatter_spec.rb +0 -25
  126. data/spec/lib/castle/utils_spec.rb +0 -156
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe Castle::Commands::Authenticate do
4
- subject(:instance) { described_class.new(context) }
4
+ subject(:instance) { described_class }
5
5
 
6
6
  let(:context) { { test: { test1: '1' } } }
7
- let(:default_payload) { { event: '$login.authenticate', user_id: '1234', sent_at: time_auto } }
7
+ let(:default_payload) do
8
+ { event: '$login.authenticate', user_id: '1234', sent_at: time_auto, context: context }
9
+ end
8
10
 
9
11
  let(:time_now) { Time.now }
10
12
  let(:time_auto) { time_now.utc.iso8601(3) }
@@ -16,17 +18,6 @@ describe Castle::Commands::Authenticate do
16
18
  describe '.build' do
17
19
  subject(:command) { instance.build(payload) }
18
20
 
19
- context 'with simple merger' do
20
- let(:payload) { default_payload.merge(context: { test: { test2: '1' } }) }
21
- let(:command_data) do
22
- default_payload.merge(context: { test: { test1: '1', test2: '1' } })
23
- end
24
-
25
- it { expect(command.method).to be_eql(:post) }
26
- it { expect(command.path).to be_eql('authenticate') }
27
- it { expect(command.data).to be_eql(command_data) }
28
- end
29
-
30
21
  context 'with properties' do
31
22
  let(:payload) { default_payload.merge(properties: { test: '1' }) }
32
23
  let(:command_data) do
@@ -50,7 +41,7 @@ describe Castle::Commands::Authenticate do
50
41
  end
51
42
 
52
43
  context 'when active true' do
53
- let(:payload) { default_payload.merge(context: { active: true }) }
44
+ let(:payload) { default_payload.merge(context: context.merge(active: true)) }
54
45
  let(:command_data) do
55
46
  default_payload.merge(context: context.merge(active: true))
56
47
  end
@@ -61,7 +52,7 @@ describe Castle::Commands::Authenticate do
61
52
  end
62
53
 
63
54
  context 'when active false' do
64
- let(:payload) { default_payload.merge(context: { active: false }) }
55
+ let(:payload) { default_payload.merge(context: context.merge(active: false)) }
65
56
  let(:command_data) do
66
57
  default_payload.merge(context: context.merge(active: false))
67
58
  end
@@ -72,7 +63,7 @@ describe Castle::Commands::Authenticate do
72
63
  end
73
64
 
74
65
  context 'when active string' do
75
- let(:payload) { default_payload.merge(context: { active: 'string' }) }
66
+ let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
76
67
  let(:command_data) { default_payload.merge(context: context) }
77
68
 
78
69
  it { expect(command.method).to be_eql(:post) }
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::Commands::EndImpersonation 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) do
23
+ default_payload.merge(properties: { impersonator: impersonator }, context: context)
24
+ end
25
+
26
+ it { expect(command.method).to be_eql(:delete) }
27
+ it { expect(command.path).to be_eql('impersonate') }
28
+ it { expect(command.data).to be_eql(command_data) }
29
+ end
30
+
31
+ context 'when active true' do
32
+ let(:payload) { default_payload.merge(context: context.merge(active: true)) }
33
+ let(:command_data) do
34
+ default_payload.merge(context: context.merge(active: true))
35
+ end
36
+
37
+ it { expect(command.method).to be_eql(:delete) }
38
+ it { expect(command.path).to be_eql('impersonate') }
39
+ it { expect(command.data).to be_eql(command_data) }
40
+ end
41
+
42
+ context 'when active false' do
43
+ let(:payload) { default_payload.merge(context: context.merge(active: false)) }
44
+ let(:command_data) do
45
+ default_payload.merge(context: context.merge(active: false))
46
+ end
47
+
48
+ it { expect(command.method).to be_eql(:delete) }
49
+ it { expect(command.path).to be_eql('impersonate') }
50
+ it { expect(command.data).to be_eql(command_data) }
51
+ end
52
+
53
+ context 'when active string' do
54
+ let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
55
+ let(:command_data) { default_payload.merge(context: context) }
56
+
57
+ it { expect(command.method).to be_eql(:delete) }
58
+ it { expect(command.path).to be_eql('impersonate') }
59
+ it { expect(command.data).to be_eql(command_data) }
60
+ end
61
+ end
62
+
63
+ describe '#validate!' do
64
+ subject(:validate!) { instance.build(payload) }
65
+
66
+ context 'when user_id not present' do
67
+ let(:payload) { {} }
68
+
69
+ it do
70
+ expect do
71
+ validate!
72
+ end.to raise_error(Castle::InvalidParametersError, 'user_id is missing or empty')
73
+ end
74
+ end
75
+
76
+ context 'when user_id present' do
77
+ let(:payload) { { user_id: '1234', context: context } }
78
+
79
+ it { expect { validate! }.not_to raise_error }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::Commands::GetDevice 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_eql(:get) }
20
+ it { expect(command.path).to be_eql("devices/#{device_token}") }
21
+ it { expect(command.data).to be_nil }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Castle::Commands::GetDevicesForUser do
4
+ subject(:instance) { described_class }
5
+
6
+ let(:context) { {} }
7
+ let(:user_id) { '1234' }
8
+
9
+ describe '.build' do
10
+ subject(:command) { instance.build(user_id: user_id) }
11
+
12
+ context 'without user_id' do
13
+ let(:user_id) { '' }
14
+
15
+ it { expect { command }.to raise_error(Castle::InvalidParametersError) }
16
+ end
17
+
18
+ context 'with user_id' do
19
+ it { expect(command.method).to be_eql(:get) }
20
+ it { expect(command.path).to be_eql("users/#{user_id}/devices") }
21
+ it { expect(command.data).to be_nil }
22
+ end
23
+ end
24
+ end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe Castle::Commands::Identify do
4
- subject(:instance) { described_class.new(context) }
4
+ subject(:instance) { described_class }
5
5
 
6
6
  let(:context) { { test: { test1: '1' } } }
7
- let(:default_payload) { { user_id: '1234', sent_at: time_auto } }
7
+ let(:default_payload) { { user_id: '1234', sent_at: time_auto, context: context } }
8
8
 
9
9
  let(:time_now) { Time.now }
10
10
  let(:time_auto) { time_now.utc.iso8601(3) }
@@ -16,17 +16,6 @@ describe Castle::Commands::Identify do
16
16
  describe '.build' do
17
17
  subject(:command) { instance.build(payload) }
18
18
 
19
- context 'with simple merger' do
20
- let(:payload) { default_payload.merge(context: { test: { test2: '1' } }) }
21
- let(:command_data) do
22
- default_payload.merge(context: { test: { test1: '1', test2: '1' } })
23
- end
24
-
25
- it { expect(command.method).to be_eql(:post) }
26
- it { expect(command.path).to be_eql('identify') }
27
- it { expect(command.data).to be_eql(command_data) }
28
- end
29
-
30
19
  context 'with user_traits' do
31
20
  let(:payload) { default_payload.merge(user_traits: { test: '1' }) }
32
21
  let(:command_data) do
@@ -39,7 +28,7 @@ describe Castle::Commands::Identify do
39
28
  end
40
29
 
41
30
  context 'when active true' do
42
- let(:payload) { default_payload.merge(context: { active: true }) }
31
+ let(:payload) { default_payload.merge(context: context.merge(active: true)) }
43
32
  let(:command_data) do
44
33
  default_payload.merge(context: context.merge(active: true))
45
34
  end
@@ -50,7 +39,7 @@ describe Castle::Commands::Identify do
50
39
  end
51
40
 
52
41
  context 'when active false' do
53
- let(:payload) { default_payload.merge(context: { active: false }) }
42
+ let(:payload) { default_payload.merge(context: context.merge(active: false)) }
54
43
  let(:command_data) do
55
44
  default_payload.merge(context: context.merge(active: false))
56
45
  end
@@ -61,7 +50,7 @@ describe Castle::Commands::Identify do
61
50
  end
62
51
 
63
52
  context 'when active string' do
64
- let(:payload) { default_payload.merge(context: { active: 'string' }) }
53
+ let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
65
54
  let(:command_data) { default_payload.merge(context: context) }
66
55
 
67
56
  it { expect(command.method).to be_eql(:post) }
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ 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_eql(:put) }
20
+ it { expect(command.path).to be_eql("devices/#{device_token}/report") }
21
+ it { expect(command.data).to be_nil }
22
+ end
23
+ end
24
+ end
@@ -7,7 +7,7 @@ describe Castle::Commands::Review do
7
7
  let(:review_id) { '1234' }
8
8
 
9
9
  describe '.build' do
10
- subject(:command) { instance.build(review_id) }
10
+ subject(:command) { instance.build(review_id: review_id) }
11
11
 
12
12
  context 'without review_id' do
13
13
  let(:review_id) { '' }
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- describe Castle::Commands::Impersonate do
4
- subject(:instance) { described_class.new(context) }
3
+ describe Castle::Commands::StartImpersonation do
4
+ subject(:instance) { described_class }
5
5
 
6
6
  let(:context) { { user_agent: 'test', ip: '127.0.0.1', client_id: 'test' } }
7
7
  let(:impersonator) { 'test@castle.io' }
8
- let(:default_payload) { { user_id: '1234', sent_at: time_auto } }
8
+ let(:default_payload) { { user_id: '1234', sent_at: time_auto, context: context } }
9
9
 
10
10
  let(:time_now) { Time.now }
11
11
  let(:time_auto) { time_now.utc.iso8601(3) }
@@ -17,24 +17,6 @@ describe Castle::Commands::Impersonate do
17
17
  describe '.build' do
18
18
  subject(:command) { instance.build(payload) }
19
19
 
20
- context 'with simple merger' do
21
- let(:payload) { default_payload.merge(context: { test: { test2: '1' } }) }
22
- let(:command_data) do
23
- default_payload.merge(
24
- context: {
25
- test: { test2: '1' },
26
- user_agent: 'test',
27
- ip: '127.0.0.1',
28
- client_id: 'test'
29
- }
30
- )
31
- end
32
-
33
- it { expect(command.method).to be_eql(:post) }
34
- it { expect(command.path).to be_eql('impersonate') }
35
- it { expect(command.data).to be_eql(command_data) }
36
- end
37
-
38
20
  context 'with impersonator' do
39
21
  let(:payload) { default_payload.merge(properties: { impersonator: impersonator }) }
40
22
  let(:command_data) do
@@ -47,7 +29,7 @@ describe Castle::Commands::Impersonate do
47
29
  end
48
30
 
49
31
  context 'when active true' do
50
- let(:payload) { default_payload.merge(context: { active: true }) }
32
+ let(:payload) { default_payload.merge(context: context.merge(active: true)) }
51
33
  let(:command_data) do
52
34
  default_payload.merge(context: context.merge(active: true))
53
35
  end
@@ -58,7 +40,7 @@ describe Castle::Commands::Impersonate do
58
40
  end
59
41
 
60
42
  context 'when active false' do
61
- let(:payload) { default_payload.merge(context: { active: false }) }
43
+ let(:payload) { default_payload.merge(context: context.merge(active: false)) }
62
44
  let(:command_data) do
63
45
  default_payload.merge(context: context.merge(active: false))
64
46
  end
@@ -69,20 +51,13 @@ describe Castle::Commands::Impersonate do
69
51
  end
70
52
 
71
53
  context 'when active string' do
72
- let(:payload) { default_payload.merge(context: { active: 'string' }) }
54
+ let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
73
55
  let(:command_data) { default_payload.merge(context: context) }
74
56
 
75
57
  it { expect(command.method).to be_eql(:post) }
76
58
  it { expect(command.path).to be_eql('impersonate') }
77
59
  it { expect(command.data).to be_eql(command_data) }
78
60
  end
79
-
80
- context 'when reset' do
81
- let(:payload) { default_payload.merge(reset: true) }
82
-
83
- it { expect(command.method).to be_eql(:delete) }
84
- it { expect(command.path).to be_eql('impersonate') }
85
- end
86
61
  end
87
62
 
88
63
  describe '#validate!' do
@@ -99,7 +74,7 @@ describe Castle::Commands::Impersonate do
99
74
  end
100
75
 
101
76
  context 'when user_id present' do
102
- let(:payload) { { user_id: '1234' } }
77
+ let(:payload) { { user_id: '1234', context: context } }
103
78
 
104
79
  it { expect { validate! }.not_to raise_error }
105
80
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe Castle::Commands::Track do
4
- subject(:instance) { described_class.new(context) }
4
+ subject(:instance) { described_class }
5
5
 
6
6
  let(:context) { { test: { test1: '1' } } }
7
- let(:default_payload) { { event: '$login.track', sent_at: time_auto } }
7
+ let(:default_payload) { { event: '$login.track', sent_at: time_auto, context: context } }
8
8
 
9
9
  let(:time_now) { Time.now }
10
10
  let(:time_auto) { time_now.utc.iso8601(3) }
@@ -16,17 +16,6 @@ describe Castle::Commands::Track do
16
16
  describe '#build' do
17
17
  subject(:command) { instance.build(payload) }
18
18
 
19
- context 'with simple merger' do
20
- let(:payload) { default_payload.merge(context: { test: { test2: '1' } }) }
21
- let(:command_data) do
22
- default_payload.merge(context: { test: { test1: '1', test2: '1' } })
23
- end
24
-
25
- it { expect(command.method).to be_eql(:post) }
26
- it { expect(command.path).to be_eql('track') }
27
- it { expect(command.data).to be_eql(command_data) }
28
- end
29
-
30
19
  context 'with user_id' do
31
20
  let(:payload) { default_payload.merge(user_id: '1234') }
32
21
  let(:command_data) do
@@ -61,7 +50,7 @@ describe Castle::Commands::Track do
61
50
  end
62
51
 
63
52
  context 'when active true' do
64
- let(:payload) { default_payload.merge(context: { active: true }) }
53
+ let(:payload) { default_payload.merge(context: context.merge(active: true)) }
65
54
  let(:command_data) do
66
55
  default_payload.merge(context: context.merge(active: true))
67
56
  end
@@ -72,7 +61,7 @@ describe Castle::Commands::Track do
72
61
  end
73
62
 
74
63
  context 'when active false' do
75
- let(:payload) { default_payload.merge(context: { active: false }) }
64
+ let(:payload) { default_payload.merge(context: context.merge(active: false)) }
76
65
  let(:command_data) do
77
66
  default_payload.merge(context: context.merge(active: false))
78
67
  end
@@ -83,7 +72,7 @@ describe Castle::Commands::Track do
83
72
  end
84
73
 
85
74
  context 'when active string' do
86
- let(:payload) { default_payload.merge(context: { active: 'string' }) }
75
+ let(:payload) { default_payload.merge(context: context.merge(active: 'string')) }
87
76
  let(:command_data) { default_payload.merge(context: context) }
88
77
 
89
78
  it { expect(command.method).to be_eql(:post) }
@@ -2,146 +2,17 @@
2
2
 
3
3
  describe Castle::Configuration do
4
4
  subject(:config) do
5
- described_class.instance
5
+ described_class.new
6
6
  end
7
7
 
8
- describe 'host' do
9
- context 'with default' do
10
- it { expect(config.url.host).to be_eql('api.castle.io') }
11
- end
8
+ it_behaves_like 'configuration_host'
9
+ it_behaves_like 'configuration_request_timeout'
10
+ it_behaves_like 'configuration_allowlisted'
11
+ it_behaves_like 'configuration_denylisted'
12
+ it_behaves_like 'configuration_failover_strategy'
13
+ it_behaves_like 'configuration_api_secret'
12
14
 
13
- context 'with setter' do
14
- before { config.url = 'http://api.castle.dev/v2' }
15
-
16
- it { expect(config.url.host).to be_eql('api.castle.dev') }
17
- end
18
- end
19
-
20
- describe 'post' do
21
- context 'with default' do
22
- it { expect(config.url.port).to be_eql(443) }
23
- end
24
-
25
- context 'with setter' do
26
- before { config.url = 'http://api.castle.dev:3001/v2' }
27
-
28
- it { expect(config.url.port).to be_eql(3001) }
29
- end
30
- end
31
-
32
- describe 'api_secret' do
33
- context 'with env' do
34
- let(:secret_key_env) { 'secret_key_env' }
35
- let(:secret_key) { 'secret_key' }
36
-
37
- before do
38
- allow(ENV).to receive(:fetch).with(
39
- 'CASTLE_API_SECRET', ''
40
- ).and_return(secret_key_env)
41
- config.reset
42
- end
43
-
44
- it do
45
- expect(config.api_secret).to be_eql(secret_key_env)
46
- end
47
-
48
- context 'when key is overwritten' do
49
- before { config.api_secret = secret_key }
50
-
51
- it do
52
- expect(config.api_secret).to be_eql(secret_key)
53
- end
54
- end
55
- end
56
-
57
- context 'with setter' do
58
- let(:value) { 'new_secret' }
59
-
60
- before do
61
- config.api_secret = value
62
- end
63
-
64
- it do
65
- expect(config.api_secret).to be_eql(value)
66
- end
67
- end
68
-
69
- it do
70
- expect(config.api_secret).to be_eql('secret')
71
- end
72
- end
73
-
74
- describe 'request_timeout' do
75
- it do
76
- expect(config.request_timeout).to be_eql(500)
77
- end
78
-
79
- context 'with setter' do
80
- let(:value) { 50.0 }
81
-
82
- before do
83
- config.request_timeout = value
84
- end
85
-
86
- it do
87
- expect(config.request_timeout).to be_eql(value)
88
- end
89
- end
90
- end
91
-
92
- describe 'allowlisted' do
93
- it do
94
- expect(config.allowlisted.size).to be_eql(0)
95
- end
96
-
97
- context 'with setter' do
98
- before do
99
- config.allowlisted = ['header']
100
- end
101
-
102
- it do
103
- expect(config.allowlisted).to be_eql(['Header'])
104
- end
105
- end
106
- end
107
-
108
- describe 'denylisted' do
109
- it do
110
- expect(config.denylisted.size).to be_eql(0)
111
- end
112
-
113
- context 'with setter' do
114
- before do
115
- config.denylisted = ['header']
116
- end
117
-
118
- it do
119
- expect(config.denylisted).to be_eql(['Header'])
120
- end
121
- end
122
- end
123
-
124
- describe 'failover_strategy' do
125
- it do
126
- expect(config.failover_strategy).to be_eql(:allow)
127
- end
128
-
129
- context 'with setter' do
130
- before do
131
- config.failover_strategy = :deny
132
- end
133
-
134
- it do
135
- expect(config.failover_strategy).to be_eql(:deny)
136
- end
137
- end
138
-
139
- context 'when broken' do
140
- it do
141
- expect do
142
- config.failover_strategy = :unicorn
143
- end.to raise_error(Castle::ConfigurationError)
144
- end
145
- end
15
+ it do
16
+ expect(config.api_secret).to be_eql('')
146
17
  end
147
18
  end