securenative 0.1.22 → 0.1.23

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +62 -64
  3. data/lib/config/configuration_builder.rb +4 -3
  4. data/lib/config/configuration_manager.rb +2 -1
  5. data/lib/config/securenative_options.rb +4 -3
  6. data/lib/utils/request_utils.rb +20 -5
  7. data/out/production/securenative-ruby/api_manager.rb +13 -5
  8. data/out/production/securenative-ruby/config/configuration_builder.rb +6 -9
  9. data/out/production/securenative-ruby/config/configuration_manager.rb +24 -23
  10. data/out/production/securenative-ruby/config/securenative_options.rb +8 -5
  11. data/out/production/securenative-ruby/context/hanami_context.rb +42 -0
  12. data/out/production/securenative-ruby/context/rails_context.rb +44 -0
  13. data/out/production/securenative-ruby/context/securenative_context.rb +35 -8
  14. data/out/production/securenative-ruby/context/sinatra_context.rb +42 -0
  15. data/out/production/securenative-ruby/event_manager.rb +15 -14
  16. data/out/production/securenative-ruby/http/{http_response.rb → secure_native_http_response.rb} +1 -1
  17. data/out/production/securenative-ruby/http/securenative_http_client.rb +23 -5
  18. data/out/production/securenative-ruby/models/event_options.rb +23 -1
  19. data/out/production/securenative-ruby/models/request_context.rb +2 -2
  20. data/out/production/securenative-ruby/models/sdk_event.rb +22 -6
  21. data/out/production/securenative-ruby/models/user_traits.rb +1 -1
  22. data/out/production/securenative-ruby/models/verify_result.rb +5 -1
  23. data/out/production/securenative-ruby/securenative.rb +2 -10
  24. data/out/production/securenative-ruby/utils/date_utils.rb +1 -1
  25. data/out/production/securenative-ruby/utils/encryption_utils.rb +38 -24
  26. data/out/production/securenative-ruby/utils/request_utils.rb +53 -7
  27. data/out/production/securenative-ruby/utils/secure_native_logger.rb +6 -6
  28. data/out/production/securenative-ruby/utils/version_utils.rb +5 -6
  29. data/out/test/securenative-ruby/spec_api_manager.rb +37 -31
  30. data/out/test/securenative-ruby/spec_context_builder.rb +52 -34
  31. data/out/test/securenative-ruby/spec_encryption_utils.rb +13 -13
  32. data/out/test/securenative-ruby/spec_event_manager.rb +49 -15
  33. data/out/test/securenative-ruby/spec_helper.rb +8 -0
  34. data/out/test/securenative-ruby/spec_request_utils.rb +25 -0
  35. data/out/test/securenative-ruby/spec_sdk_event.rb +24 -0
  36. data/out/test/securenative-ruby/spec_securenative.rb +35 -39
  37. data/out/test/securenative-ruby/spec_securenative_http_client.rb +13 -5
  38. data/out/test/securenative-ruby/spec_signature_utils.rb +1 -1
  39. data/out/test/securenative-ruby/spec_version_util.rb +10 -0
  40. data/securenative.gemspec +1 -1
  41. metadata +9 -4
  42. data/out/production/securenative-ruby/event_options.rb +0 -32
@@ -4,7 +4,7 @@ class UserTraits
4
4
  attr_reader :name, :email, :phone, :created_at
5
5
  attr_writer :name, :email, :phone, :created_at
6
6
 
7
- def initialize(name = nil, email = nil, phone = nil, created_at = nil)
7
+ def initialize(name: nil, email: nil, phone: nil, created_at: nil)
8
8
  @name = name
9
9
  @email = email
10
10
  @created_at = created_at
@@ -4,9 +4,13 @@ class VerifyResult
4
4
  attr_reader :risk_level, :score, :triggers
5
5
  attr_writer :risk_level, :score, :triggers
6
6
 
7
- def initialize(risk_level = nil, score = nil, triggers = nil)
7
+ def initialize(risk_level: nil, score: nil, triggers: nil)
8
8
  @risk_level = risk_level
9
9
  @score = score
10
10
  @triggers = triggers
11
11
  end
12
+
13
+ def to_s
14
+ "risk_level: #{@risk_level}, score: #{@score}, triggers: #{@triggers}"
15
+ end
12
16
  end
@@ -8,7 +8,9 @@ require 'errors/securenative_sdk_Illegal_state_error'
8
8
  require 'errors/securenative_config_error'
9
9
  require 'enums/failover_strategy'
10
10
  require 'config/configuration_builder'
11
+ require 'config/configuration_manager'
11
12
  require 'event_manager'
13
+ require 'api_manager'
12
14
 
13
15
  class SecureNative
14
16
  attr_reader :options
@@ -60,16 +62,6 @@ class SecureNative
60
62
  @securenative
61
63
  end
62
64
 
63
- def self.config_builder(api_key = nil, api_url = 'https://api.securenative.com/collector/api/v1', interval = 1000,
64
- max_events = 1000, timeout = 1500, auto_send = true, disable = false, log_level = 'FATAL',
65
- fail_over_strategy = FailOverStrategy::FAIL_OPEN)
66
- ConfigurationBuilder.new(api_key, api_url, interval, max_events, timeout, auto_send, disable, log_level, fail_over_strategy)
67
- end
68
-
69
- def self.context_builder(client_token = nil, ip = nil, remote_ip = nil, headers = nil, url = nil, method = nil, body = nil)
70
- ContextBuilder.new(client_token, ip, remote_ip, headers, url, method, body)
71
- end
72
-
73
65
  def track(event_options)
74
66
  @api_manager.track(event_options)
75
67
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class DateUtils
4
4
  def self.to_timestamp(date)
5
- return Time.now.strftime('%Y-%m-%dT%H:%M:%S%Z') if date.nil?
5
+ return Time.now.utc.iso8601 if date.nil?
6
6
 
7
7
  Time.parse(date).iso8601
8
8
  end
@@ -1,35 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'openssl'
4
+ require 'digest'
5
+ require 'base64'
6
+ require 'models/client_token'
4
7
 
5
8
  class EncryptionUtils
6
- BLOCK_SIZE = 16
7
- KEY_SIZE = 32
8
-
9
- def self.encrypt(text, cipher_key)
10
- cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).encrypt
11
- cipher.padding = 0
12
-
13
- if text.size % BLOCK_SIZE != 0
14
- return nil
9
+ def self.padding_key(key, length)
10
+ if key.length == length
11
+ key
12
+ else
13
+ if key.length > length
14
+ key.slice(0, length)
15
+ else
16
+ (length - key.length).times { key << '0' }
17
+ key
18
+ end
15
19
  end
16
-
17
- cipher_key = Digest::SHA1.hexdigest cipher_key
18
- cipher.key = cipher_key.slice(0, BLOCK_SIZE)
19
- s = cipher.update(text) + cipher.final
20
-
21
- s.unpack('H*')[0].upcase
22
20
  end
23
21
 
24
- def self.decrypt(encrypted, cipher_key)
25
- cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).decrypt
26
- cipher.padding = 0
27
-
28
- cipher_key = Digest::SHA1.hexdigest cipher_key
29
- cipher.key = cipher_key.slice(0, BLOCK_SIZE)
30
- s = [encrypted].pack('H*').unpack('C*').pack('c*')
22
+ def self.encrypt(plain_text, secret_key)
23
+ begin
24
+ cipher = OpenSSL::Cipher.new('aes-256-cbc')
25
+ cipher.encrypt
26
+ iv = cipher.random_iv
27
+ cipher.key = padding_key(secret_key, 32)
28
+ encrypted = cipher.update(plain_text) + cipher.final
29
+ (iv + encrypted).unpack1('H*')
30
+ rescue StandardError
31
+ ''
32
+ end
33
+ end
31
34
 
32
- rv = cipher.update(s) + cipher.final
33
- rv.strip
35
+ def self.decrypt(cipher_text, secret_key)
36
+ begin
37
+ cipher = OpenSSL::Cipher.new('aes-256-cbc')
38
+ cipher.decrypt
39
+ raw_data = [cipher_text].pack('H*')
40
+ cipher.iv = raw_data.slice(0, 16)
41
+ cipher.key = padding_key(secret_key, 32)
42
+ decrypted = JSON.parse(cipher.update(raw_data.slice(16, raw_data.length)) + cipher.final)
43
+
44
+ return ClientToken.new(decrypted['cid'], decrypted['vid'], decrypted['fp'])
45
+ rescue StandardError
46
+ ClientToken.new('', '','')
47
+ end
34
48
  end
35
49
  end
@@ -5,19 +5,65 @@ class RequestUtils
5
5
  SECURENATIVE_HEADER = 'x-securenative'
6
6
 
7
7
  def self.get_secure_header_from_request(headers)
8
- return headers[RequestUtils.SECURENATIVE_HEADER] unless headers.nil?
9
-
8
+ begin
9
+ return headers[SECURENATIVE_HEADER] unless headers.nil?
10
+ rescue StandardError
11
+ []
12
+ end
10
13
  []
11
14
  end
12
15
 
13
- def self.get_client_ip_from_request(request)
14
- x_forwarded_for = request.env['HTTP_X_FORWARDED_FOR']
15
- return x_forwarded_for unless x_forwarded_for.nil?
16
+ def self.get_client_ip_from_request(request, options = nil)
17
+ begin
18
+ return request.ip unless request.ip.nil?
19
+ rescue NoMethodError
20
+ end
21
+
22
+ begin
23
+ x_forwarded_for = request.env['HTTP_X_FORWARDED_FOR']
24
+ return x_forwarded_for unless x_forwarded_for.nil?
25
+ rescue NoMethodError
26
+ begin
27
+ x_forwarded_for = request['HTTP_X_FORWARDED_FOR']
28
+ return x_forwarded_for unless x_forwarded_for.nil?
29
+ rescue NoMethodError
30
+ end
31
+ end
32
+
33
+ begin
34
+ x_forwarded_for = request.env['REMOTE_ADDR']
35
+ return x_forwarded_for unless x_forwarded_for.nil?
36
+ rescue NoMethodError
37
+ begin
38
+ x_forwarded_for = request['REMOTE_ADDR']
39
+ return x_forwarded_for unless x_forwarded_for.nil?
40
+ rescue NoMethodError
41
+ end
42
+ end
43
+
44
+ unless options.nil?
45
+ for header in options.proxy_headers do
46
+ begin
47
+ h = request.env[header]
48
+ return h unless h.nil?
49
+ rescue NoMethodError
50
+ begin
51
+ h = request[header]
52
+ return h unless h.nil?
53
+ rescue NoMethodError
54
+ end
55
+ end
56
+ end
57
+ end
16
58
 
17
- request.env['REMOTE_ADDR']
59
+ ''
18
60
  end
19
61
 
20
62
  def self.get_remote_ip_from_request(request)
21
- request.remote_ip
63
+ begin
64
+ request.remote_ip
65
+ rescue NoMethodError
66
+ ''
67
+ end
22
68
  end
23
69
  end
@@ -8,17 +8,17 @@ class SecureNativeLogger
8
8
  def self.init_logger(level = 'DEBUG')
9
9
  @logger.level = case level
10
10
  when 'WARN'
11
- SecureNativeLogger::WARN
11
+ Logger::WARN
12
12
  when 'DEBUG'
13
- SecureNativeLogger::DEBUG
13
+ Logger::DEBUG
14
14
  when 'ERROR'
15
- SecureNativeLogger::ERROR
15
+ Logger::ERROR
16
16
  when 'FATAL'
17
- SecureNativeLogger::FATAL
17
+ Logger::FATAL
18
18
  when 'INFO'
19
- SecureNativeLogger::INFO
19
+ Logger::INFO
20
20
  else
21
- SecureNativeLogger::FATAL
21
+ Logger::FATAL
22
22
  end
23
23
 
24
24
  @logger.formatter = proc do |severity, datetime, progname, msg|
@@ -2,11 +2,10 @@
2
2
 
3
3
  class VersionUtils
4
4
  def self.version
5
- path = 'VERSION'
6
- file = File.open(path)
7
- version = file.read
8
- file.close
9
-
10
- version
5
+ begin
6
+ Gem.loaded_specs['securenative'].version.to_s
7
+ rescue StandardError
8
+ 'unknown'
9
+ end
11
10
  end
12
11
  end
@@ -2,45 +2,39 @@
2
2
 
3
3
  require 'api_manager'
4
4
  require 'webmock/rspec'
5
+ require 'config/configuration_builder'
6
+ require 'errors/securenative_invalid_options_error'
7
+ require 'models/event_options'
8
+ require 'models/verify_result'
9
+ require 'models/user_traits'
10
+ require 'enums/event_types'
11
+ require 'enums/risk_level'
12
+ require 'event_manager'
5
13
  require 'rspec'
6
14
 
7
15
  RSpec.describe ApiManager do
8
- let(:context) do
9
- ContextBuilder(ip: '127.0.0.1', client_token: 'SECURED_CLIENT_TOKEN',
10
- headers: { 'user-agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
11
- AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405' })
12
- end
13
- let(:event_options) do
14
- EventOptions(event_type: EventTypes.LOG_IN, user_id: 'USER_ID',
15
- user_traits: UserTraits('USER_NAME', 'USER_EMAIL', '+1234567890'),
16
- properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 }).build
17
- end
18
-
19
16
  it 'tracks an event' do
20
17
  options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
21
18
 
22
- expected = '{"eventType":"sn.user.login","userId":"USER_ID","userTraits":{' \
23
- '"name":"USER_NAME","email":"USER_EMAIL","phone":"+1234567890","createdAt":null},"request":{' \
24
- '"cid":null,"vid":null,"fp":null,"ip":"127.0.0.1","remoteIp":null,"headers":{' \
25
- '"user-agent":"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) ' \
26
- 'AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"},"url":null,"method":null},' \
27
- '"properties":{"prop2":true,"prop1":"CUSTOM_PARAM_VALUE","prop3":3}}'
28
-
29
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
30
- .with(body: JSON.parse(expected)).to_return(status: 200)
19
+ stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track').to_return(status: 200)
31
20
  event_manager = EventManager.new(options)
32
21
  event_manager.start_event_persist
33
22
  api_manager = ApiManager.new(event_manager, options)
23
+ event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
24
+ user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
25
+ properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
34
26
 
35
27
  begin
36
- api_manager.track(:event_options)
28
+ res = api_manager.track(event_options)
37
29
  ensure
38
30
  event_manager.stop_event_persist
39
31
  end
32
+
33
+ expect(res).to_not be_nil
40
34
  end
41
35
 
42
36
  it 'uses invalid options' do
43
- options = ConfigurationBuilder(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
37
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
44
38
 
45
39
  properties = {}
46
40
  (0..12).each do |i|
@@ -53,7 +47,7 @@ RSpec.describe ApiManager do
53
47
  api_manager = ApiManager.new(event_manager, options)
54
48
 
55
49
  begin
56
- expect { api_manager.track(EventOptions(event_type: EventTypes.LOG_IN, properties: properties).build) }
50
+ expect { api_manager.track(EventOptions.new(event: EventTypes::LOG_IN, properties: properties)) }
57
51
  .to raise_error(SecureNativeInvalidOptionsError)
58
52
  ensure
59
53
  event_manager.stop_event_persist
@@ -61,21 +55,33 @@ RSpec.describe ApiManager do
61
55
  end
62
56
 
63
57
  it 'verifies an event' do
64
- options = ConfigurationBuilder(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
58
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
59
+
60
+ stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/verify").
61
+ with(
62
+ headers: {
63
+ 'Accept'=>'*/*',
64
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
65
+ 'Authorization'=>'YOUR_API_KEY',
66
+ 'Content-Type'=>'application/json',
67
+ 'Sn-Version'=>'0.1.22',
68
+ 'User-Agent'=>'SecureNative-ruby'
69
+ }).
70
+ to_return(status: 200, body: "", headers: {})
65
71
 
66
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
67
- .with(body: { riskLevel: 'medium', score: 0.32, triggers: ['New IP', 'New City'] }).to_return(status: 200)
68
- verify_result = VerifyResult.new(RiskLevel.LOW, 0, nil)
69
72
 
70
73
  event_manager = EventManager.new(options)
71
74
  event_manager.start_event_persist
72
75
  api_manager = ApiManager.new(event_manager, options)
76
+ event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
77
+ user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
78
+ properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
73
79
 
74
- result = api_manager.verify(:event_options)
80
+ result = api_manager.verify(event_options)
75
81
 
76
82
  expect(result).not_to be_nil
77
- expect(result.risk_level).to eq(verify_result.risk_level)
78
- expect(result.score).to eq(verify_result.score)
79
- expect(result.triggers).to eq(verify_result.triggers)
83
+ expect(result.risk_level).to eq('low')
84
+ expect(result.score).to eq(0)
85
+ expect(result.triggers).to eq(nil)
80
86
  end
81
87
  end
@@ -2,61 +2,79 @@
2
2
 
3
3
  require 'context/securenative_context'
4
4
  require 'webmock/rspec'
5
+ require 'rails'
6
+ require 'hanami'
7
+ require 'sinatra'
5
8
  require 'rspec'
6
9
 
7
10
  RSpec.describe SecureNativeContext do
8
- it 'creates context from request' do
11
+ it 'creates context from ruby default request' do
9
12
  stub_request(:any, 'www.example.com')
10
- .to_return(body: nil, status: 200,
11
- headers: { 'x-securenative': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' },
12
- remote_ip: '', uri: 'www.securenative.com', http_method: 'Post', ip: '51.68.201.122',
13
- client_token: '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a')
13
+ .to_return(status: 200,
14
+ headers: { '_sn': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
14
15
 
15
- request = Net::HTTP.get('www.example.com', '/')
16
+ request = Net::HTTP.get_response('www.example.com', '/')
16
17
  context = SecureNativeContext.from_http_request(request)
17
18
 
18
- expect(context.client_token).to eq('71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a')
19
- expect(context.ip).to eq('51.68.201.122')
20
- expect(context.http_method).to eq('Post')
21
- expect(context.uri).to eq('www.securenative.com')
19
+ expect(context.ip).to eq('')
20
+ expect(context.http_method).to eq('')
21
+ expect(context.url).to eq('')
22
22
  expect(context.remote_ip).to eq('')
23
- expect(context.headers).to eq({ 'x-securenative': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
24
- expect(context.body).to be_nil
23
+ expect(context.headers['-sn']).to eq(['71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a'])
24
+ expect(context.body).to eq('')
25
25
  end
26
26
 
27
- it 'creates context from request with cookie' do
28
- stub_request(:any, 'www.example.com')
29
- .to_return(body: nil, status: 200,
30
- cookies: { '_sn': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' },
31
- remote_ip: '', uri: 'www.securenative.com', http_method: 'Post', ip: '51.68.201.122',
32
- client_token: '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a')
27
+ it 'creates context from rails request' do
28
+ request = ActionDispatch::Request.new(nil)
29
+ context = SecureNativeContext.from_http_request(request)
30
+
31
+ expect(context.ip).to eq('')
32
+ expect(context.http_method).to eq('')
33
+ expect(context.url).to eq('')
34
+ expect(context.remote_ip).to eq('')
35
+ expect(context.headers).to eq([])
36
+ expect(context.body).to eq('')
37
+ end
33
38
 
34
- request = Net::HTTP.get('www.example.com', '/')
35
- con = SecureNativeContext.from_http_request(request)
39
+ it 'creates context from sinatra request' do
40
+ request = Sinatra::Request.new(nil)
41
+ context = SecureNativeContext.from_http_request(request)
36
42
 
37
- expect(con.context.client_token).to eq('71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a')
38
- expect(con.context.ip).to eq('51.68.201.122')
39
- expect(con.context.http_method).to eq('Post')
40
- expect(con.context.uri).to eq('www.securenative.com')
41
- expect(con.context.remote_ip).to eq('')
42
- expect(con.context.body).to be_nil
43
+ expect(context.ip).to eq('')
44
+ expect(context.http_method).to eq('')
45
+ expect(context.url).to eq('')
46
+ expect(context.remote_ip).to eq('')
47
+ expect(context.headers).to eq([])
48
+ expect(context.body).to eq('')
49
+ end
50
+
51
+ it 'creates context from hanami request' do
52
+ request = Hanami::Action::Request
53
+ context = SecureNativeContext.from_http_request(request)
54
+
55
+ expect(context.ip).to eq('')
56
+ expect(context.http_method).to eq('')
57
+ expect(context.url).to eq('')
58
+ expect(context.remote_ip).to eq('')
59
+ expect(context.headers).to eq([])
60
+ expect(context.body).to eq('')
43
61
  end
44
62
 
45
63
  it 'creates default context builder' do
46
64
  context = SecureNativeContext.default_context_builder
47
65
 
48
- expect(context.client_token).to be_nil
49
- expect(context.ip).to be_nil
50
- expect(context.http_method).to be_nil
51
- expect(context.url).to be_nil
52
- expect(context.remote_ip).to be_nil
66
+ expect(context.client_token).to eq('')
67
+ expect(context.ip).to eq('')
68
+ expect(context.http_method).to eq('')
69
+ expect(context.url).to eq('')
70
+ expect(context.remote_ip).to eq('')
53
71
  expect(context.headers).to be_nil
54
- expect(context.body).to be_nil
72
+ expect(context.body).to eq('')
55
73
  end
56
74
 
57
75
  it 'creates custom context with context builder' do
58
- context = SecureNativeContext.new('SECRET_TOKEN', '10.0.0.0', '10.0.0.0',
59
- { 'header' => 'value1' }, '/some-url', 'Get', nil)
76
+ context = SecureNativeContext.new(client_token: 'SECRET_TOKEN', ip: '10.0.0.0', remote_ip: '10.0.0.0',
77
+ headers: { 'header' => 'value1' }, url: '/some-url', http_method: 'Get', body: nil)
60
78
 
61
79
  expect(context.url).to eq('/some-url')
62
80
  expect(context.client_token).to eq('SECRET_TOKEN')