securenative 0.1.20 → 0.1.26

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 (117) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +76 -82
  4. data/README.md +45 -40
  5. data/lib/securenative/api_manager.rb +41 -0
  6. data/lib/securenative/config/configuration_builder.rb +29 -0
  7. data/lib/securenative/config/configuration_manager.rb +58 -0
  8. data/lib/securenative/config/securenative_options.rb +25 -0
  9. data/lib/securenative/context/hanami_context.rb +44 -0
  10. data/lib/securenative/context/rails_context.rb +46 -0
  11. data/lib/securenative/context/securenative_context.rb +69 -0
  12. data/lib/securenative/context/sinatra_context.rb +44 -0
  13. data/lib/securenative/enums/api_route.rb +8 -0
  14. data/lib/securenative/enums/event_types.rb +25 -0
  15. data/lib/securenative/enums/failover_strategy.rb +8 -0
  16. data/lib/securenative/enums/risk_level.rb +9 -0
  17. data/lib/securenative/errors/securenative_config_error.rb +6 -0
  18. data/lib/securenative/errors/securenative_http_error.rb +6 -0
  19. data/lib/securenative/errors/securenative_invalid_options_error.rb +6 -0
  20. data/lib/securenative/errors/securenative_invalid_uri_error.rb +6 -0
  21. data/lib/securenative/errors/securenative_parse_error.rb +6 -0
  22. data/lib/securenative/errors/securenative_sdk_Illegal_state_error.rb +6 -0
  23. data/lib/securenative/errors/securenative_sdk_error.rb +6 -0
  24. data/lib/securenative/event_manager.rb +159 -0
  25. data/lib/securenative/http/secure_native_http_response.rb +14 -0
  26. data/lib/securenative/http/securenative_http_client.rb +52 -0
  27. data/lib/securenative/models/client_token.rb +14 -0
  28. data/lib/securenative/models/device.rb +12 -0
  29. data/lib/securenative/models/event_options.rb +39 -0
  30. data/lib/securenative/models/request_context.rb +20 -0
  31. data/lib/securenative/models/request_options.rb +14 -0
  32. data/lib/securenative/models/sdk_event.rb +51 -0
  33. data/lib/securenative/models/user_traits.rb +15 -0
  34. data/lib/securenative/models/verify_result.rb +18 -0
  35. data/lib/securenative/sdk.rb +85 -0
  36. data/lib/securenative/utils/date_utils.rb +11 -0
  37. data/lib/securenative/utils/encryption_utils.rb +51 -0
  38. data/lib/securenative/utils/ip_utils.rb +25 -0
  39. data/lib/securenative/utils/request_utils.rb +71 -0
  40. data/lib/securenative/utils/secure_native_logger.rb +46 -0
  41. data/lib/securenative/utils/signature_utils.rb +18 -0
  42. data/lib/securenative/utils/utils.rb +11 -0
  43. data/lib/securenative/utils/version_utils.rb +13 -0
  44. data/out/production/securenative-ruby/api_manager.rb +13 -5
  45. data/out/production/securenative-ruby/config/configuration_builder.rb +6 -9
  46. data/out/production/securenative-ruby/config/configuration_manager.rb +24 -23
  47. data/out/production/securenative-ruby/config/securenative_options.rb +8 -5
  48. data/{lib → out/production/securenative-ruby}/context/hanami_context.rb +9 -2
  49. data/{lib → out/production/securenative-ruby}/context/rails_context.rb +4 -1
  50. data/out/production/securenative-ruby/context/securenative_context.rb +35 -8
  51. data/{lib → out/production/securenative-ruby}/context/sinatra_context.rb +11 -4
  52. data/out/production/securenative-ruby/event_manager.rb +15 -14
  53. data/{lib → out/production/securenative-ruby}/http/secure_native_http_response.rb +0 -0
  54. data/out/production/securenative-ruby/http/securenative_http_client.rb +23 -5
  55. data/out/production/securenative-ruby/models/event_options.rb +23 -1
  56. data/out/production/securenative-ruby/models/request_context.rb +2 -2
  57. data/out/production/securenative-ruby/models/sdk_event.rb +22 -6
  58. data/out/production/securenative-ruby/models/user_traits.rb +1 -1
  59. data/out/production/securenative-ruby/models/verify_result.rb +5 -1
  60. data/out/production/securenative-ruby/securenative.rb +2 -10
  61. data/out/production/securenative-ruby/utils/date_utils.rb +1 -1
  62. data/out/production/securenative-ruby/utils/encryption_utils.rb +38 -24
  63. data/out/production/securenative-ruby/utils/request_utils.rb +53 -7
  64. data/out/production/securenative-ruby/utils/secure_native_logger.rb +6 -6
  65. data/out/production/securenative-ruby/utils/version_utils.rb +5 -6
  66. data/out/test/securenative-ruby/spec_api_manager.rb +37 -31
  67. data/out/test/securenative-ruby/spec_context_builder.rb +52 -34
  68. data/out/test/securenative-ruby/spec_encryption_utils.rb +13 -13
  69. data/out/test/securenative-ruby/spec_event_manager.rb +49 -15
  70. data/out/test/securenative-ruby/spec_helper.rb +8 -0
  71. data/out/test/securenative-ruby/spec_request_utils.rb +25 -0
  72. data/out/test/securenative-ruby/spec_sdk_event.rb +24 -0
  73. data/out/test/securenative-ruby/spec_securenative.rb +35 -39
  74. data/out/test/securenative-ruby/spec_securenative_http_client.rb +13 -5
  75. data/out/test/securenative-ruby/spec_signature_utils.rb +1 -1
  76. data/out/test/securenative-ruby/spec_version_util.rb +10 -0
  77. data/securenative.gemspec +16 -14
  78. metadata +48 -45
  79. data/VERSION +0 -1
  80. data/lib/api_manager.rb +0 -35
  81. data/lib/config/configuration_builder.rb +0 -30
  82. data/lib/config/configuration_manager.rb +0 -55
  83. data/lib/config/securenative_options.rb +0 -22
  84. data/lib/context/securenative_context.rb +0 -67
  85. data/lib/enums/api_route.rb +0 -6
  86. data/lib/enums/event_types.rb +0 -23
  87. data/lib/enums/failover_strategy.rb +0 -6
  88. data/lib/enums/risk_level.rb +0 -7
  89. data/lib/errors/securenative_config_error.rb +0 -4
  90. data/lib/errors/securenative_http_error.rb +0 -4
  91. data/lib/errors/securenative_invalid_options_error.rb +0 -4
  92. data/lib/errors/securenative_invalid_uri_error.rb +0 -4
  93. data/lib/errors/securenative_parse_error.rb +0 -4
  94. data/lib/errors/securenative_sdk_Illegal_state_error.rb +0 -4
  95. data/lib/errors/securenative_sdk_error.rb +0 -4
  96. data/lib/event_manager.rb +0 -156
  97. data/lib/event_options.rb +0 -32
  98. data/lib/http/securenative_http_client.rb +0 -38
  99. data/lib/models/client_token.rb +0 -12
  100. data/lib/models/device.rb +0 -10
  101. data/lib/models/event_options.rb +0 -15
  102. data/lib/models/request_context.rb +0 -18
  103. data/lib/models/request_options.rb +0 -12
  104. data/lib/models/sdk_event.rb +0 -35
  105. data/lib/models/user_traits.rb +0 -13
  106. data/lib/models/verify_result.rb +0 -12
  107. data/lib/securenative.rb +0 -95
  108. data/lib/utils/date_utils.rb +0 -9
  109. data/lib/utils/encryption_utils.rb +0 -35
  110. data/lib/utils/ip_utils.rb +0 -23
  111. data/lib/utils/request_utils.rb +0 -54
  112. data/lib/utils/secure_native_logger.rb +0 -44
  113. data/lib/utils/signature_utils.rb +0 -16
  114. data/lib/utils/utils.rb +0 -9
  115. data/lib/utils/version_utils.rb +0 -12
  116. data/out/production/securenative-ruby/event_options.rb +0 -32
  117. data/out/production/securenative-ruby/http/http_response.rb +0 -12
@@ -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')
@@ -4,23 +4,23 @@ require 'utils/encryption_utils'
4
4
  require 'rspec'
5
5
 
6
6
  RSpec.describe EncryptionUtils do
7
- let(:SECRET_KEY) { 'B00C42DAD33EAC6F6572DA756EA4915349C0A4F6' }
8
- let(:PAYLOAD) { '{"cid":"198a41ff-a10f-4cda-a2f3-a9ca80c0703b","vi":"148a42ff-b40f-4cda-a2f3-a8ca80c0703b","fp":"6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed"}' }
9
- let(:CID) { '198a41ff-a10f-4cda-a2f3-a9ca80c0703b' }
10
- let(:FP) { '6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed' }
11
-
12
- it 'decrypts' do
13
- result = EncryptionUtils.encrypt(:PAYLOAD, :SECRET_KEY)
7
+ it 'encrypts' do
8
+ secret_key = 'AFD16D89150FD7FB19EE9E936DC1AE3547CE119B'
9
+ payload = '{"cid":"198a41ff-a10f-4cda-a2f3-a9ca80c0703b","vi":"148a42ff-b40f-4cda-a2f3-a8ca80c0703b","fp":"6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed"}'
10
+ result = EncryptionUtils.encrypt(payload, secret_key)
14
11
 
15
12
  expect(result).not_to be_nil
16
- expect(result.length).to eq(:PAYLOAD.length)
17
13
  end
18
14
 
19
- it 'encrypts' do
20
- encrypted_payload = '5208ae703cc2fa0851347f55d3b76d3fd6035ee081d71a401e8bc92ebdc25d42440f62310bda60628537744ac03f200d78da9e61f1019ce02087b7ce6c976e7b2d8ad6aa978c532cea8f3e744cc6a5cafedc4ae6cd1b08a4ef75d6e37aa3c0c76954d16d57750be2980c2c91ac7ef0bbd0722abd59bf6be22493ea9b9759c3ff4d17f17ab670b0b6fc320e6de982313f1c4e74c0897f9f5a32d58e3e53050ae8fdbebba9009d0d1250fe34dcde1ebb42acbc22834a02f53889076140f0eb8db1'
21
- result = EncryptionUtils.decrypt(encrypted_payload, :SECRET_KEY)
15
+ it 'decrypts' do
16
+ secret_key = 'AFD16D89150FD7FB19EE9E936DC1AE3547CE119B'
17
+ encrypted_payload = 'dfcc35bc71653771d4541f08937c35cbc98faea2c061ff7904f80abf7c072f0029157ed97a55b00efe09fb0d2f86f5693ecbba3f6339862ed3908f0d746533133c8c838be641dad76cf3f9cce67dc1b48cbc8574f24637be4aa90f802ec4b7e5d50b5f9cb3d64e6887ef99b8b941e69370ac7994ccafaf17ceff1d7a68ac30e4b0fe4eb1b844460d5f7687f16902cea61d0ccc085f7ea6087fae38482cd1ee1c7574dc4b0e996bc4e5946eeb8e8509fbdd9f1884eb3f02cbbaefe4566c999d50'
18
+ cid = '12946065-65af-4825-9893-fce901c8da49'
19
+ fp = '9a6e6a7d636ca772924bd2219853d73c.24700f9f1986800ab4fcc880530dd0ed'
20
+
21
+ result = EncryptionUtils.decrypt(encrypted_payload, secret_key)
22
22
 
23
- expect(result.cid).to eq(:CID)
24
- expect(result.fp).to eq(:FP)
23
+ expect(result.cid).to eq(cid)
24
+ expect(result.fp).to eq(fp)
25
25
  end
26
26
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'event_manager'
4
4
  require 'config/configuration_builder'
5
+ require 'models/user_traits'
6
+ require 'models/request_context'
5
7
  require 'rspec'
6
8
  require 'webmock/rspec'
7
9
 
@@ -13,47 +15,79 @@ class SampleEvent
13
15
  @timestamp = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%Z')
14
16
  @rid = '432532'
15
17
  @user_id = '1'
16
- @user_traits = UserTraits('some user', 'email@securenative.com', '+1234567890')
17
- @request = RequestContext()
18
+ @user_traits = UserTraits.new(name: 'some user', email: 'email@securenative.com', phone: '+1234567890')
19
+ @request = RequestContext.new
18
20
  @properties = []
19
21
  end
20
22
  end
21
23
 
22
24
  RSpec.describe EventManager do
23
- let(:event) { SampleEvent() }
24
-
25
25
  it 'successfully sends sync event with status code 200' do
26
26
  options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
27
+ event = SampleEvent.new
28
+
29
+ stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
30
+ with(
31
+ headers: {
32
+ 'Accept'=>'*/*',
33
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
34
+ 'Authorization'=>'YOUR_API_KEY',
35
+ 'Content-Type'=>'application/json',
36
+ 'Sn-Version'=>'0.1.22',
37
+ 'User-Agent'=>'SecureNative-ruby'
38
+ }).
39
+ to_return(status: 200, body: "", headers: {})
27
40
 
28
- res_body = '{"data": true}'
29
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
30
- .with(body: JSON.parse(res_body)).to_return(status: 200)
31
41
  event_manager = EventManager.new(options)
32
42
 
33
43
  event_manager.start_event_persist
34
- data = event_manager.send_sync(:event, 'some-path/to-api', false)
44
+ res = event_manager.send_sync(event, 'some-path/to-api', false)
35
45
  event_manager.stop_event_persist
36
46
 
37
- expect(res_body).to eq(data.text)
47
+ expect(res.code).to eq('200')
38
48
  end
39
49
 
40
50
  it 'fails when send sync event status code is 401' do
41
51
  options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
52
+ event = SampleEvent.new
53
+
54
+ stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
55
+ with(
56
+ headers: {
57
+ 'Accept'=>'*/*',
58
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
59
+ 'Authorization'=>'YOUR_API_KEY',
60
+ 'Content-Type'=>'application/json',
61
+ 'Sn-Version'=>'0.1.22',
62
+ 'User-Agent'=>'SecureNative-ruby'
63
+ }).
64
+ to_return(status: 401, body: "", headers: {})
42
65
 
43
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 401)
44
66
  event_manager = EventManager.new(options)
45
- res = event_manager.send_sync(:event, 'some-path/to-api', false)
67
+ res = event_manager.send_sync(event, 'some-path/to-api', false)
46
68
 
47
- expect(res.status_code).to eq(401)
69
+ expect(res.code).to eq('401')
48
70
  end
49
71
 
50
72
  it 'fails when send sync event status code is 500' do
51
73
  options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
74
+ event = SampleEvent.new
75
+
76
+ stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
77
+ with(
78
+ headers: {
79
+ 'Accept'=>'*/*',
80
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
81
+ 'Authorization'=>'YOUR_API_KEY',
82
+ 'Content-Type'=>'application/json',
83
+ 'Sn-Version'=>'0.1.22',
84
+ 'User-Agent'=>'SecureNative-ruby'
85
+ }).
86
+ to_return(status: 500, body: "", headers: {})
52
87
 
53
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 500)
54
88
  event_manager = EventManager.new(options)
55
- res = event_manager.send_sync(:event, 'some-path/to-api', false)
89
+ res = event_manager.send_sync(event, 'some-path/to-api', false)
56
90
 
57
- expect(res.status_code).to eq(500)
91
+ expect(res.code).to eq('500')
58
92
  end
59
93
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/setup'
4
+ require 'webmock/rspec'
4
5
  require 'simplecov'
5
6
  require 'codecov'
6
7
 
@@ -17,4 +18,11 @@ RSpec.configure do |config|
17
18
  config.expect_with :rspec do |c|
18
19
  c.syntax = :expect
19
20
  end
21
+
22
+ config.mock_with :rspec do |mocks|
23
+ # Prevents you from mocking or stubbing a method that does not exist on
24
+ # a real object. This is generally recommended, and will default to
25
+ # `true` in RSpec 4.
26
+ mocks.verify_partial_doubles = true
27
+ end
20
28
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'config/securenative_options'
4
+ require 'utils/request_utils'
5
+ require 'webmock/rspec'
6
+ require 'rspec'
7
+
8
+ RSpec.describe RequestUtils do
9
+ it 'extract a request with proxy headers' do
10
+ options = SecureNativeOptions.new
11
+ options.proxy_headers = [
12
+ 'CF-Connecting-IP'
13
+ ]
14
+
15
+ stub_request(:get, 'http://www.example.com/').with(
16
+ headers: {
17
+ 'Accept' => '*/*',
18
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
19
+ 'User-Agent' => 'Ruby'
20
+ }).to_return(status: 200, body: '', headers: { 'CF-Connecting-IP' => 'CF-Connecting-IP: 203.0.113.1' })
21
+
22
+ request = Net::HTTP.get_response('www.example.com', '/')
23
+ RequestUtils.get_client_ip_from_request(request, options)
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'models/sdk_event'
4
+ require 'models/event_options'
5
+ require 'config/securenative_options'
6
+ require 'enums/event_types'
7
+ require 'errors/securenative_invalid_options_error'
8
+ require 'rspec'
9
+
10
+ RSpec.describe SDKEvent do
11
+ it 'throws when event created without user id' do
12
+ event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: nil)
13
+ options = SecureNativeOptions.new
14
+
15
+ expect { SDKEvent.new(event_options, options) }.to raise_error(SecureNativeInvalidOptionsError)
16
+ end
17
+
18
+ it 'throws when event created without event type' do
19
+ event_options = EventOptions.new(event: nil, user_id: '1234')
20
+ options = SecureNativeOptions.new
21
+
22
+ expect { SDKEvent.new(event_options, options) }.to raise_error(SecureNativeInvalidOptionsError)
23
+ end
24
+ end
@@ -20,46 +20,42 @@ RSpec.describe SecureNative do
20
20
  expect { SecureNative.init_with_api_key('') }.to raise_error(SecureNativeConfigError)
21
21
  end
22
22
 
23
- # TODO: fix event manager
24
- # it 'inits sdk with api key and defaults' do
25
- # SecureNative._flush
26
- # api_key = 'API_KEY'
27
- # securenative = SecureNative.init_with_api_key(api_key)
28
- # options = securenative.options
29
- #
30
- # expect(options.api_key).to eq(api_key)
31
- # expect(options.api_url).to eq('https://api.securenative.com/collector/api/v1')
32
- # expect(options.interval).to eq(1000)
33
- # expect(options.timeout).to eq(1500)
34
- # expect(options.max_events).to eq(100)
35
- # expect(options.auto_send).to eq(true)
36
- # expect(options.disable).to eq(false)
37
- # expect(options.log_level).to eq('FATAL')
38
- # expect(options.fail_over_strategy).to eq(FailOverStrategy::FAIL_OPEN)
39
- # end
23
+ it 'inits sdk with api key and defaults' do
24
+ SecureNative._flush
25
+ api_key = 'API_KEY'
26
+ securenative = SecureNative.init_with_api_key(api_key)
27
+ options = securenative.options
28
+
29
+ expect(options.api_key).to eq(api_key)
30
+ expect(options.api_url).to eq('https://api.securenative.com/collector/api/v1')
31
+ expect(options.interval).to eq(1000)
32
+ expect(options.timeout).to eq(1500)
33
+ expect(options.max_events).to eq(1000)
34
+ expect(options.auto_send).to eq(true)
35
+ expect(options.disable).to eq(false)
36
+ expect(options.log_level).to eq('FATAL')
37
+ expect(options.fail_over_strategy).to eq(FailOverStrategy::FAIL_OPEN)
38
+ end
39
+
40
+ it 'inits sdk twice and throws' do
41
+ expect { SecureNative.init_with_api_key('API_KEY') }.to raise_error(SecureNativeSDKError)
42
+ end
40
43
 
41
- # TODO: fix event manager
42
- # it 'inits sdk twice and throws' do
43
- # expect { SecureNative.init_with_api_key('API_KEY') }.to raise_error(SecureNativeSDKError)
44
- # end
44
+ it 'inits sdk with api key and gets instance' do
45
+ SecureNative._flush
46
+ api_key = 'API_KEY'
47
+ securenative = SecureNative.init_with_api_key(api_key)
45
48
 
46
- # TODO: fix event manager
47
- # it 'inits sdk with api key and gets instance' do
48
- # SecureNative._flush
49
- # api_key = 'API_KEY'
50
- # securenative = SecureNative.init_with_api_key(api_key)
51
- #
52
- # expect(securenative).to eq(SecureNative.instance)
53
- # end
49
+ expect(securenative).to eq(SecureNative.instance)
50
+ end
51
+
52
+ it 'inits sdk with builder' do
53
+ SecureNative._flush
54
+ securenative = SecureNative.init_with_options(ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR'))
55
+ options = securenative.options
54
56
 
55
- # TODO: fix event manager
56
- # it 'inits sdk with builder' do
57
- # SecureNative._flush
58
- # securenative = SecureNative.init_with_options(SecureNative.config_builder(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR'))
59
- # options = securenative.options
60
- #
61
- # expect(options.api_key).to eq('API_KEY')
62
- # expect(options.max_events).to eq(10)
63
- # expect(options.log_level).to eq('ERROR')
64
- # end
57
+ expect(options.api_key).to eq('API_KEY')
58
+ expect(options.max_events).to eq(10)
59
+ expect(options.log_level).to eq('ERROR')
60
+ end
65
61
  end
@@ -9,15 +9,23 @@ RSpec.describe SecureNativeHttpClient do
9
9
  it 'makes a simple post call' do
10
10
  options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
11
11
 
12
- stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
13
- .with(body: { event: 'SOME_EVENT_NAME' }).to_return(status: 200)
12
+ stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/track").
13
+ with(
14
+ body: "{\"event\": \"SOME_EVENT_NAME\"}",
15
+ headers: {
16
+ 'Accept'=>'*/*',
17
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
18
+ 'Authorization'=>'YOUR_API_KEY',
19
+ 'Content-Type'=>'application/json',
20
+ 'Sn-Version'=>'0.1.22',
21
+ 'User-Agent'=>'SecureNative-ruby'
22
+ }).
23
+ to_return(status: 200, body: "", headers: {})
14
24
  client = SecureNativeHttpClient.new(options)
15
25
  payload = '{"event": "SOME_EVENT_NAME"}'
16
26
 
17
27
  res = client.post('track', payload)
18
28
 
19
- expect(res.ok).to eq(true)
20
- expect(res.status_code).to eq(200)
21
- expect(res.text).to eq(payload)
29
+ expect(res.code).to eq('200')
22
30
  end
23
31
  end
@@ -15,4 +15,4 @@ RSpec.describe SignatureUtils do
15
15
  it 'verifies request empty signature' do
16
16
  expect(SignatureUtils.valid_signature?('', '', 'B00C42DAD33EAC6F6572DA756EA4915349C0A4F6')).to be_falsey
17
17
  end
18
- end
18
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utils/version_utils'
4
+ require 'rspec'
5
+
6
+ RSpec.describe VersionUtils do
7
+ it 'checks that parsing version is valid' do
8
+ expect(VersionUtils.version).not_to eq('unknown')
9
+ end
10
+ end