castle-rb 7.2.0 → 8.1.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 (140) hide show
  1. checksums.yaml +4 -4
  2. data/lib/castle/api/authenticate.rb +3 -12
  3. data/lib/castle/api/end_impersonation.rb +1 -3
  4. data/lib/castle/api/filter.rb +2 -10
  5. data/lib/castle/api/list_items/archive.rb +23 -0
  6. data/lib/castle/api/list_items/count.rb +22 -0
  7. data/lib/castle/api/list_items/create.rb +22 -0
  8. data/lib/castle/api/list_items/get.rb +22 -0
  9. data/lib/castle/api/list_items/query.rb +22 -0
  10. data/lib/castle/api/list_items/unarchive.rb +22 -0
  11. data/lib/castle/api/list_items/update.rb +22 -0
  12. data/lib/castle/api/lists/create.rb +23 -0
  13. data/lib/castle/api/lists/delete.rb +22 -0
  14. data/lib/castle/api/lists/get.rb +22 -0
  15. data/lib/castle/api/lists/get_all.rb +22 -0
  16. data/lib/castle/api/lists/query.rb +22 -0
  17. data/lib/castle/api/lists/update.rb +22 -0
  18. data/lib/castle/api/log.rb +2 -10
  19. data/lib/castle/api/risk.rb +2 -10
  20. data/lib/castle/api/start_impersonation.rb +1 -3
  21. data/lib/castle/api/track.rb +1 -3
  22. data/lib/castle/client.rb +4 -5
  23. data/lib/castle/client_actions/list_items.rb +44 -0
  24. data/lib/castle/client_actions/lists.rb +39 -0
  25. data/lib/castle/commands/list_items/archive.rb +24 -0
  26. data/lib/castle/commands/list_items/count.rb +23 -0
  27. data/lib/castle/commands/list_items/create.rb +22 -0
  28. data/lib/castle/commands/list_items/get.rb +23 -0
  29. data/lib/castle/commands/list_items/query.rb +24 -0
  30. data/lib/castle/commands/list_items/unarchive.rb +23 -0
  31. data/lib/castle/commands/list_items/update.rb +22 -0
  32. data/lib/castle/commands/lists/create.rb +21 -0
  33. data/lib/castle/commands/lists/delete.rb +20 -0
  34. data/lib/castle/commands/lists/get.rb +20 -0
  35. data/lib/castle/commands/lists/get_all.rb +17 -0
  36. data/lib/castle/commands/lists/query.rb +21 -0
  37. data/lib/castle/commands/lists/update.rb +22 -0
  38. data/lib/castle/commands/log.rb +1 -5
  39. data/lib/castle/commands/risk.rb +1 -5
  40. data/lib/castle/configuration.rb +2 -5
  41. data/lib/castle/core/process_response.rb +4 -3
  42. data/lib/castle/core/send_request.rb +1 -3
  43. data/lib/castle/errors.rb +4 -0
  44. data/lib/castle/headers/extract.rb +1 -3
  45. data/lib/castle/headers/filter.rb +2 -3
  46. data/lib/castle/payload/prepare.rb +1 -2
  47. data/lib/castle/session.rb +1 -1
  48. data/lib/castle/version.rb +1 -1
  49. data/lib/castle.rb +29 -3
  50. data/spec/integration/rails/rails_spec.rb +9 -3
  51. data/spec/integration/rails/support/application.rb +2 -2
  52. data/spec/integration/rails/support/home_controller.rb +4 -30
  53. data/spec/lib/castle/api/approve_device_spec.rb +3 -7
  54. data/spec/lib/castle/api/authenticate_spec.rb +23 -26
  55. data/spec/lib/castle/api/end_impersonation_spec.rb +9 -15
  56. data/spec/lib/castle/api/filter_spec.rb +1 -1
  57. data/spec/lib/castle/api/get_device_spec.rb +2 -4
  58. data/spec/lib/castle/api/get_devices_for_user_spec.rb +2 -4
  59. data/spec/lib/castle/api/list_items/archive_spec.rb +18 -0
  60. data/spec/lib/castle/api/list_items/count_spec.rb +21 -0
  61. data/spec/lib/castle/api/list_items/create_spec.rb +22 -0
  62. data/spec/lib/castle/api/list_items/get_spec.rb +18 -0
  63. data/spec/lib/castle/api/list_items/query_spec.rb +21 -0
  64. data/spec/lib/castle/api/list_items/unarchive_spec.rb +18 -0
  65. data/spec/lib/castle/api/list_items/update_spec.rb +22 -0
  66. data/spec/lib/castle/api/lists/create_spec.rb +21 -0
  67. data/spec/lib/castle/api/lists/delete_spec.rb +17 -0
  68. data/spec/lib/castle/api/lists/get_all_spec.rb +17 -0
  69. data/spec/lib/castle/api/lists/get_spec.rb +17 -0
  70. data/spec/lib/castle/api/lists/query_spec.rb +21 -0
  71. data/spec/lib/castle/api/lists/update_spec.rb +21 -0
  72. data/spec/lib/castle/api/log_spec.rb +1 -1
  73. data/spec/lib/castle/api/report_device_spec.rb +3 -7
  74. data/spec/lib/castle/api/risk_spec.rb +1 -1
  75. data/spec/lib/castle/api/start_impersonation_spec.rb +9 -15
  76. data/spec/lib/castle/api/track_spec.rb +10 -17
  77. data/spec/lib/castle/api_spec.rb +1 -1
  78. data/spec/lib/castle/client_id/extract_spec.rb +3 -10
  79. data/spec/lib/castle/client_spec.rb +38 -79
  80. data/spec/lib/castle/command_spec.rb +4 -4
  81. data/spec/lib/castle/commands/approve_device_spec.rb +3 -3
  82. data/spec/lib/castle/commands/authenticate_spec.rb +18 -25
  83. data/spec/lib/castle/commands/end_impersonation_spec.rb +15 -22
  84. data/spec/lib/castle/commands/filter_spec.rb +16 -16
  85. data/spec/lib/castle/commands/get_device_spec.rb +3 -3
  86. data/spec/lib/castle/commands/get_devices_for_user_spec.rb +3 -3
  87. data/spec/lib/castle/commands/list_items/archive_spec.rb +21 -0
  88. data/spec/lib/castle/commands/list_items/count_spec.rb +21 -0
  89. data/spec/lib/castle/commands/list_items/create_spec.rb +22 -0
  90. data/spec/lib/castle/commands/list_items/get_spec.rb +21 -0
  91. data/spec/lib/castle/commands/list_items/query_spec.rb +27 -0
  92. data/spec/lib/castle/commands/list_items/unarchive_spec.rb +21 -0
  93. data/spec/lib/castle/commands/list_items/update_spec.rb +21 -0
  94. data/spec/lib/castle/commands/lists/create_spec.rb +33 -0
  95. data/spec/lib/castle/commands/lists/delete_spec.rb +21 -0
  96. data/spec/lib/castle/commands/lists/get_all_spec.rb +11 -0
  97. data/spec/lib/castle/commands/lists/get_spec.rb +21 -0
  98. data/spec/lib/castle/commands/lists/query_spec.rb +27 -0
  99. data/spec/lib/castle/commands/lists/update_spec.rb +29 -0
  100. data/spec/lib/castle/commands/log_spec.rb +16 -16
  101. data/spec/lib/castle/commands/report_device_spec.rb +3 -3
  102. data/spec/lib/castle/commands/risk_spec.rb +16 -16
  103. data/spec/lib/castle/commands/start_impersonation_spec.rb +15 -22
  104. data/spec/lib/castle/commands/track_spec.rb +20 -25
  105. data/spec/lib/castle/configuration_spec.rb +2 -2
  106. data/spec/lib/castle/context/get_default_spec.rb +10 -9
  107. data/spec/lib/castle/context/merge_spec.rb +1 -1
  108. data/spec/lib/castle/context/prepare_spec.rb +4 -5
  109. data/spec/lib/castle/context/sanitize_spec.rb +1 -1
  110. data/spec/lib/castle/core/get_connection_spec.rb +1 -1
  111. data/spec/lib/castle/core/process_response_spec.rb +4 -7
  112. data/spec/lib/castle/core/process_webhook_spec.rb +13 -7
  113. data/spec/lib/castle/core/send_request_spec.rb +8 -12
  114. data/spec/lib/castle/failover/strategy_spec.rb +6 -6
  115. data/spec/lib/castle/headers/extract_spec.rb +2 -2
  116. data/spec/lib/castle/headers/filter_spec.rb +7 -4
  117. data/spec/lib/castle/headers/format_spec.rb +6 -6
  118. data/spec/lib/castle/ips/extract_spec.rb +3 -7
  119. data/spec/lib/castle/logger_spec.rb +3 -2
  120. data/spec/lib/castle/payload/prepare_spec.rb +5 -8
  121. data/spec/lib/castle/secure_mode_spec.rb +2 -4
  122. data/spec/lib/castle/session_spec.rb +2 -6
  123. data/spec/lib/castle/singleton_configuration_spec.rb +2 -2
  124. data/spec/lib/castle/utils/clean_invalid_chars_spec.rb +1 -1
  125. data/spec/lib/castle/utils/clone_spec.rb +2 -2
  126. data/spec/lib/castle/utils/deep_symbolize_keys_spec.rb +1 -1
  127. data/spec/lib/castle/utils/get_timestamp_spec.rb +1 -1
  128. data/spec/lib/castle/utils/merge_spec.rb +3 -5
  129. data/spec/lib/castle/validators/not_supported_spec.rb +2 -7
  130. data/spec/lib/castle/validators/present_spec.rb +3 -10
  131. data/spec/lib/castle/verdict_spec.rb +4 -4
  132. data/spec/lib/castle/version_spec.rb +1 -1
  133. data/spec/lib/castle/webhooks/verify_spec.rb +13 -7
  134. data/spec/lib/castle_spec.rb +6 -8
  135. data/spec/spec_helper.rb +2 -0
  136. data/spec/support/shared_examples/action_request.rb +16 -34
  137. data/spec/support/shared_examples/configuration.rb +14 -16
  138. data/spec/support/shared_examples/list_items.rb +52 -0
  139. data/spec/support/shared_examples/lists.rb +45 -0
  140. metadata +132 -48
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module ListItems
6
+ # Builds the command to query list items
7
+ class Query
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id])
13
+ options[:filters]&.each { |f| Castle::Validators::Present.call(f, %i[field op value]) }
14
+ Castle::Validators::Present.call(options[:sort], %i[field order]) if options[:sort]
15
+
16
+ list_id = options.delete(:list_id)
17
+
18
+ Castle::Command.new("lists/#{list_id}/items/query", options, :post)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module ListItems
6
+ # Builds the command to unarchive a list item
7
+ class Unarchive
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id list_item_id])
13
+
14
+ list_id = options.delete(:list_id)
15
+ list_item_id = options.delete(:list_item_id)
16
+
17
+ Castle::Command.new("lists/#{list_id}/items/#{list_item_id}/unarchive", nil, :put)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module ListItems
6
+ # Builds the command to update a list item
7
+ class Update
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id list_item_id comment])
13
+ list_id = options.delete(:list_id)
14
+ list_item_id = options.delete(:list_item_id)
15
+
16
+ Castle::Command.new("lists/#{list_id}/items/#{list_item_id}", options, :put)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ # Commands for lists endpoints
6
+ module Lists
7
+ # Builds the command to create a list
8
+ class Create
9
+ class << self
10
+ # @param options [Hash]
11
+ # @return [Castle::Command]
12
+ def build(options = {})
13
+ Castle::Validators::Present.call(options, %i[name color primary_field])
14
+
15
+ Castle::Command.new('lists', options, :post)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module Lists
6
+ # Builds the command to delete a list
7
+ class Delete
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id])
13
+
14
+ Castle::Command.new("lists/#{options[:list_id]}", nil, :delete)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module Lists
6
+ # Builds the command to get a list
7
+ class Get
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id])
13
+
14
+ Castle::Command.new("lists/#{options[:list_id]}", nil, :get)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module Lists
6
+ # Builds the command to get all lists
7
+ class GetAll
8
+ class << self
9
+ # @return [Castle::Command]
10
+ def build
11
+ Castle::Command.new('lists', nil, :get)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module Lists
6
+ # Builds the command to query lists
7
+ class Query
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ options[:filters]&.each { |f| Castle::Validators::Present.call(f, %i[field op value]) }
13
+ Castle::Validators::Present.call(options[:sort], %i[field order]) if options[:sort]
14
+
15
+ Castle::Command.new('lists/query', options, :post)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Castle
4
+ module Commands
5
+ module Lists
6
+ # Builds the command to update a list
7
+ class Update
8
+ class << self
9
+ # @param options [Hash]
10
+ # @return [Castle::Command]
11
+ def build(options = {})
12
+ Castle::Validators::Present.call(options, %i[list_id])
13
+
14
+ list_id = options.delete(:list_id)
15
+
16
+ Castle::Command.new("lists/#{list_id}", options, :put)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -10,11 +10,7 @@ module Castle
10
10
  def build(options = {})
11
11
  context = Castle::Context::Sanitize.call(options[:context])
12
12
 
13
- Castle::Command.new(
14
- 'log',
15
- options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call),
16
- :post
17
- )
13
+ Castle::Command.new('log', options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call), :post)
18
14
  end
19
15
  end
20
16
  end
@@ -10,11 +10,7 @@ module Castle
10
10
  def build(options = {})
11
11
  context = Castle::Context::Sanitize.call(options[:context])
12
12
 
13
- Castle::Command.new(
14
- 'risk',
15
- options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call),
16
- :post
17
- )
13
+ Castle::Command.new('risk', options.merge(context: context, sent_at: Castle::Utils::GetTimestamp.call), :post)
18
14
  end
19
15
  end
20
16
  end
@@ -104,9 +104,7 @@ module Castle
104
104
  # sets trusted proxies
105
105
  # @param value [Array<String,Regexp>]
106
106
  def trusted_proxies=(value)
107
- unless value.is_a?(Array)
108
- raise Castle::ConfigurationError, 'trusted proxies must be an Array'
109
- end
107
+ raise Castle::ConfigurationError, 'trusted proxies must be an Array' unless value.is_a?(Array)
110
108
 
111
109
  @trusted_proxies = value
112
110
  end
@@ -121,8 +119,7 @@ module Castle
121
119
  end
122
120
 
123
121
  def failover_strategy=(value)
124
- @failover_strategy =
125
- Castle::Failover::STRATEGIES.detect { |strategy| strategy == value.to_sym }
122
+ @failover_strategy = Castle::Failover::STRATEGIES.detect { |strategy| strategy == value.to_sym }
126
123
  raise Castle::ConfigurationError, 'unrecognized failover strategy' if @failover_strategy.nil?
127
124
  end
128
125
 
@@ -9,7 +9,8 @@ module Castle
9
9
  401 => Castle::UnauthorizedError,
10
10
  403 => Castle::ForbiddenError,
11
11
  404 => Castle::NotFoundError,
12
- 419 => Castle::UserUnauthorizedError
12
+ 419 => Castle::UserUnauthorizedError,
13
+ 429 => Castle::RateLimitError
13
14
  }.freeze
14
15
 
15
16
  INVALID_REQUEST_TOKEN = 'invalid_request_token'
@@ -51,9 +52,9 @@ module Castle
51
52
  if parsed_body.is_a?(Hash) && parsed_body.key?(:type)
52
53
  if parsed_body[:type] == INVALID_REQUEST_TOKEN
53
54
  raise Castle::InvalidRequestTokenError, parsed_body[:message]
54
- else
55
- raise Castle::InvalidParametersError, parsed_body[:message]
56
55
  end
56
+
57
+ raise Castle::InvalidParametersError, parsed_body[:message]
57
58
  end
58
59
  rescue JSON::ParserError
59
60
  end
@@ -15,9 +15,7 @@ module Castle
15
15
  # @param http [Net::HTTP]
16
16
  # @param config [Castle::Configuration, Castle::SingletonConfiguration, nil]
17
17
  def call(command, headers, http = nil, config = nil)
18
- (http || Castle::Core::GetConnection.call).request(
19
- build(command, headers.merge(DEFAULT_HEADERS), config)
20
- )
18
+ (http || Castle::Core::GetConnection.call).request(build(command, headers.merge(DEFAULT_HEADERS), config))
21
19
  end
22
20
 
23
21
  # @param command [String]
data/lib/castle/errors.rb CHANGED
@@ -61,6 +61,10 @@ module Castle
61
61
  class UnauthorizedError < Castle::ApiError
62
62
  end
63
63
 
64
+ # api error too many requests 429
65
+ class RateLimitError < Castle::ApiError
66
+ end
67
+
64
68
  # all internal server errors
65
69
  class InternalServerError < Castle::ApiError
66
70
  end
@@ -23,9 +23,7 @@ module Castle
23
23
  # Serialize HTTP headers
24
24
  # @return [Hash]
25
25
  def call
26
- @headers.each_with_object({}) do |(name, value), acc|
27
- acc[name] = header_value(name, value)
28
- end
26
+ @headers.each_with_object({}) { |(name, value), acc| acc[name] = header_value(name, value) }
29
27
  end
30
28
 
31
29
  private
@@ -12,8 +12,7 @@ module Castle
12
12
  HTTP(?:_|-).*|
13
13
  CONTENT(?:_|-)LENGTH|
14
14
  REMOTE(?:_|-)ADDR
15
- $/xi
16
- .freeze
15
+ $/xi.freeze
17
16
 
18
17
  private_constant :VALUABLE_HEADERS
19
18
 
@@ -32,7 +31,7 @@ module Castle
32
31
  next unless header_name.match(VALUABLE_HEADERS)
33
32
 
34
33
  formatted_name = @header_format.call(header_name)
35
- acc[formatted_name] = @request_env[header_name]
34
+ acc[formatted_name] = @request_env[header_name].to_s
36
35
  end
37
36
  end
38
37
  end
@@ -12,8 +12,7 @@ module Castle
12
12
  def call(payload_options, request, options = {})
13
13
  context = Castle::Context::Prepare.call(request, payload_options.merge(options))
14
14
 
15
- payload =
16
- Castle::Utils::DeepSymbolizeKeys.call(payload_options || {}).merge(context: context)
15
+ payload = Castle::Utils::DeepSymbolizeKeys.call(payload_options || {}).merge(context: context)
17
16
  payload[:timestamp] ||= Castle::Utils::GetTimestamp.call
18
17
 
19
18
  warn '[DEPRECATION] use user_traits instead of traits key' if payload.key?(:traits)
@@ -9,7 +9,7 @@ module Castle
9
9
 
10
10
  class << self
11
11
  def call(&block)
12
- return unless block_given?
12
+ return unless block
13
13
 
14
14
  Castle::Core::GetConnection.call.start(&block)
15
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Castle
4
- VERSION = '7.2.0'
4
+ VERSION = '8.1.0'
5
5
  end
data/lib/castle.rb CHANGED
@@ -31,6 +31,19 @@
31
31
  castle/commands/risk
32
32
  castle/commands/start_impersonation
33
33
  castle/commands/track
34
+ castle/commands/lists/get_all
35
+ castle/commands/lists/create
36
+ castle/commands/lists/delete
37
+ castle/commands/lists/get
38
+ castle/commands/lists/query
39
+ castle/commands/lists/update
40
+ castle/commands/list_items/archive
41
+ castle/commands/list_items/create
42
+ castle/commands/list_items/count
43
+ castle/commands/list_items/get
44
+ castle/commands/list_items/query
45
+ castle/commands/list_items/unarchive
46
+ castle/commands/list_items/update
34
47
  castle/api/approve_device
35
48
  castle/api/authenticate
36
49
  castle/api/end_impersonation
@@ -42,12 +55,27 @@
42
55
  castle/api/risk
43
56
  castle/api/start_impersonation
44
57
  castle/api/track
58
+ castle/api/lists/get_all
59
+ castle/api/lists/create
60
+ castle/api/lists/delete
61
+ castle/api/lists/get
62
+ castle/api/lists/query
63
+ castle/api/lists/update
64
+ castle/api/list_items/archive
65
+ castle/api/list_items/create
66
+ castle/api/list_items/count
67
+ castle/api/list_items/get
68
+ castle/api/list_items/query
69
+ castle/api/list_items/unarchive
70
+ castle/api/list_items/update
45
71
  castle/payload/prepare
46
72
  castle/configuration
47
73
  castle/singleton_configuration
48
74
  castle/logger
49
75
  castle/failover/prepare_response
50
76
  castle/failover/strategy
77
+ castle/client_actions/lists
78
+ castle/client_actions/list_items
51
79
  castle/client
52
80
  castle/headers/filter
53
81
  castle/headers/format
@@ -67,9 +95,7 @@
67
95
  module Castle
68
96
  class << self
69
97
  def configure(config_hash = nil)
70
- (config_hash || {}).each do |config_name, config_value|
71
- config.send("#{config_name}=", config_value)
72
- end
98
+ (config_hash || {}).each { |config_name, config_value| config.send(:"#{config_name}=", config_value) }
73
99
 
74
100
  yield(config) if block_given?
75
101
  end
@@ -24,11 +24,12 @@ RSpec.describe HomeController, type: :request do
24
24
  'Accept' =>
25
25
  'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
26
26
  'Authorization' => true,
27
- 'Content-Length' => '0',
28
27
  'Cookie' => true,
28
+ 'Content-Length' => '0',
29
29
  'Host' => 'www.example.com',
30
30
  'X-Forwarded-For' => '5.5.5.5, 1.2.3.4',
31
- 'Remote-Addr' => '127.0.0.1'
31
+ 'Remote-Addr' => '127.0.0.1',
32
+ 'Version' => 'HTTP/1.0'
32
33
  },
33
34
  'ip' => '1.2.3.4',
34
35
  'library' => {
@@ -40,7 +41,12 @@ RSpec.describe HomeController, type: :request do
40
41
  end
41
42
  let(:now) { Time.now }
42
43
  let(:headers) do
43
- { 'HTTP_AUTHORIZATION' => 'Basic 123', 'HTTP_X_FORWARDED_FOR' => '5.5.5.5, 1.2.3.4' }
44
+ {
45
+ 'HTTP_AUTHORIZATION' => 'Basic 123',
46
+ 'HTTP_X_FORWARDED_FOR' => '5.5.5.5, 1.2.3.4',
47
+ 'HTTP_VERSION' => 'HTTP/1.0',
48
+ 'HTTP_CONTENT_LENGTH' => '0'
49
+ }
44
50
  end
45
51
 
46
52
  before do
@@ -3,8 +3,8 @@
3
3
  require 'action_controller/railtie'
4
4
 
5
5
  class TestApp < Rails::Application
6
- secrets.secret_token = 'secret_token'
7
- secrets.secret_key_base = 'secret_key_base'
6
+ credentials.secret_token = 'secret_token'
7
+ credentials.secret_key_base = 'secret_key_base'
8
8
 
9
9
  config.logger = Logger.new($stdout)
10
10
  Rails.logger = config.logger
@@ -4,16 +4,7 @@ class HomeController < ActionController::Base
4
4
  # prepare context and calling track with client example
5
5
  def index1
6
6
  request_context = ::Castle::Context::Prepare.call(request)
7
- payload = {
8
- event: '$login.succeeded',
9
- user_id: '123',
10
- properties: {
11
- key: 'value'
12
- },
13
- user_traits: {
14
- key: 'value'
15
- }
16
- }
7
+ payload = { event: '$login.succeeded', user_id: '123', properties: { key: 'value' }, user_traits: { key: 'value' } }
17
8
  client = ::Castle::Client.new(context: request_context)
18
9
  client.track(payload)
19
10
 
@@ -24,16 +15,7 @@ class HomeController < ActionController::Base
24
15
  def index2
25
16
  payload =
26
17
  ::Castle::Payload::Prepare.call(
27
- {
28
- event: '$login.succeeded',
29
- user_id: '123',
30
- properties: {
31
- key: 'value'
32
- },
33
- user_traits: {
34
- key: 'value'
35
- }
36
- },
18
+ { event: '$login.succeeded', user_id: '123', properties: { key: 'value' }, user_traits: { key: 'value' } },
37
19
  request
38
20
  )
39
21
  client = ::Castle::Client.new
@@ -46,18 +28,10 @@ class HomeController < ActionController::Base
46
28
  def index3
47
29
  payload =
48
30
  ::Castle::Payload::Prepare.call(
49
- {
50
- event: '$login.succeeded',
51
- user_id: '123',
52
- properties: {
53
- key: 'value'
54
- },
55
- user_traits: {
56
- key: 'value'
57
- }
58
- },
31
+ { event: '$login.succeeded', user_id: '123', properties: { key: 'value' }, user_traits: { key: 'value' } },
59
32
  request
60
33
  )
34
+
61
35
  Castle::API::Track.call(payload)
62
36
 
63
37
  render inline: 'hello'
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- describe Castle::API::ApproveDevice do
3
+ RSpec.describe Castle::API::ApproveDevice do
4
4
  before do
5
- stub_request(:any, /api.castle.io/)
6
- .with(basic_auth: ['', 'secret'])
7
- .to_return(status: 200, body: '{}', headers: {})
5
+ stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {})
8
6
  end
9
7
 
10
8
  describe '.call' do
@@ -14,8 +12,6 @@ describe Castle::API::ApproveDevice do
14
12
 
15
13
  before { retrieve }
16
14
 
17
- it do
18
- assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/approve", times: 1
19
- end
15
+ it { assert_requested :put, "https://api.castle.io/v1/devices/#{device_token}/approve", times: 1 }
20
16
  end
21
17
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- describe Castle::API::Authenticate do
3
+ RSpec.describe Castle::API::Authenticate do
4
4
  subject(:call_subject) { described_class.call(options) }
5
5
 
6
6
  let(:ip) { '1.2.3.4' }
@@ -29,15 +29,16 @@ describe Castle::API::Authenticate do
29
29
  after { Timecop.return }
30
30
 
31
31
  describe '.call' do
32
- let(:request_body) do
33
- { event: '$login.succeeded', context: context, user_id: '1234', sent_at: time_auto }
34
- end
32
+ let(:request_body) { { event: '$login.succeeded', context: context, user_id: '1234', sent_at: time_auto } }
35
33
 
36
34
  context 'when used with symbol keys' do
37
35
  before do
38
- stub_request(:any, /api.castle.io/)
39
- .with(basic_auth: ['', 'secret'])
40
- .to_return(status: 200, body: response_body, headers: {})
36
+ stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(
37
+ status: 200,
38
+ body: response_body,
39
+ headers: {
40
+ }
41
+ )
41
42
  call_subject
42
43
  end
43
44
 
@@ -50,17 +51,9 @@ describe Castle::API::Authenticate do
50
51
  end
51
52
 
52
53
  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
54
+ let(:options) { { event: '$login.succeeded', user_id: '1234', timestamp: time_user, context: context } }
56
55
  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
- }
56
+ { event: '$login.succeeded', user_id: '1234', context: context, timestamp: time_user, sent_at: time_auto }
64
57
  end
65
58
 
66
59
  it do
@@ -78,15 +71,16 @@ describe Castle::API::Authenticate do
78
71
 
79
72
  context 'when denied without any risk policy' do
80
73
  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
74
+ let(:deny_response_without_rp) { { action: 'deny', user_id: '12345', device_token: 'abcdefg1234' } }
84
75
  let(:deny_without_rp_failover_result) { deny_response_without_rp.merge(failover_appendix) }
85
76
 
86
77
  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: {})
78
+ stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(
79
+ status: 200,
80
+ body: deny_response_without_rp.to_json,
81
+ headers: {
82
+ }
83
+ )
90
84
  call_subject
91
85
  end
92
86
 
@@ -117,9 +111,12 @@ describe Castle::API::Authenticate do
117
111
  let(:deny_with_rp_failover_result) { deny_response_with_rp.merge(failover_appendix) }
118
112
 
119
113
  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: {})
114
+ stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(
115
+ status: 200,
116
+ body: deny_response_with_rp.to_json,
117
+ headers: {
118
+ }
119
+ )
123
120
  call_subject
124
121
  end
125
122