securenative 0.1.5 → 0.1.21

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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +49 -0
  3. data/.github/workflows/publish.yml +60 -0
  4. data/.github/workflows/test.yml +48 -0
  5. data/.gitignore +3 -1
  6. data/.rakeTasks +7 -0
  7. data/.rspec +3 -0
  8. data/Gemfile +6 -1
  9. data/Gemfile.lock +254 -17
  10. data/README.md +140 -67
  11. data/Rakefile +5 -1
  12. data/lib/api_manager.rb +39 -0
  13. data/lib/config/configuration_builder.rb +26 -0
  14. data/lib/config/configuration_manager.rb +55 -0
  15. data/lib/config/securenative_options.rb +22 -0
  16. data/lib/context/hanami_context.rb +42 -0
  17. data/lib/context/rails_context.rb +44 -0
  18. data/lib/context/securenative_context.rb +67 -0
  19. data/lib/context/sinatra_context.rb +42 -0
  20. data/lib/enums/api_route.rb +6 -0
  21. data/lib/enums/event_types.rb +23 -0
  22. data/lib/enums/failover_strategy.rb +6 -0
  23. data/lib/enums/risk_level.rb +7 -0
  24. data/lib/errors/securenative_config_error.rb +4 -0
  25. data/lib/errors/securenative_http_error.rb +4 -0
  26. data/lib/errors/securenative_invalid_options_error.rb +4 -0
  27. data/lib/errors/securenative_invalid_uri_error.rb +4 -0
  28. data/lib/errors/securenative_parse_error.rb +4 -0
  29. data/lib/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  30. data/lib/errors/securenative_sdk_error.rb +4 -0
  31. data/lib/event_manager.rb +157 -0
  32. data/lib/http/secure_native_http_response.rb +12 -0
  33. data/lib/http/securenative_http_client.rb +50 -0
  34. data/lib/models/client_token.rb +12 -0
  35. data/lib/models/device.rb +10 -0
  36. data/lib/models/event_options.rb +37 -0
  37. data/lib/models/request_context.rb +18 -0
  38. data/lib/models/request_options.rb +12 -0
  39. data/lib/models/sdk_event.rb +40 -0
  40. data/lib/models/user_traits.rb +13 -0
  41. data/lib/models/verify_result.rb +16 -0
  42. data/lib/securenative.rb +69 -25
  43. data/lib/utils/date_utils.rb +9 -0
  44. data/lib/utils/encryption_utils.rb +49 -0
  45. data/lib/utils/ip_utils.rb +23 -0
  46. data/lib/utils/request_utils.rb +54 -0
  47. data/lib/utils/secure_native_logger.rb +44 -0
  48. data/lib/utils/signature_utils.rb +16 -0
  49. data/lib/utils/utils.rb +9 -0
  50. data/lib/utils/version_utils.rb +11 -0
  51. data/out/production/securenative-ruby/api_manager.rb +31 -0
  52. data/out/production/securenative-ruby/config/configuration_builder.rb +30 -0
  53. data/out/production/securenative-ruby/config/configuration_manager.rb +55 -0
  54. data/out/production/securenative-ruby/config/securenative_options.rb +20 -0
  55. data/out/production/securenative-ruby/context/securenative_context.rb +40 -0
  56. data/out/production/securenative-ruby/enums/api_route.rb +6 -0
  57. data/out/production/securenative-ruby/enums/event_types.rb +23 -0
  58. data/out/production/securenative-ruby/enums/failover_strategy.rb +6 -0
  59. data/out/production/securenative-ruby/enums/risk_level.rb +7 -0
  60. data/out/production/securenative-ruby/errors/securenative_config_error.rb +4 -0
  61. data/out/production/securenative-ruby/errors/securenative_http_error.rb +4 -0
  62. data/out/production/securenative-ruby/errors/securenative_invalid_options_error.rb +4 -0
  63. data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +4 -0
  64. data/out/production/securenative-ruby/errors/securenative_parse_error.rb +4 -0
  65. data/out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  66. data/out/production/securenative-ruby/errors/securenative_sdk_error.rb +4 -0
  67. data/out/production/securenative-ruby/event_manager.rb +156 -0
  68. data/out/production/securenative-ruby/event_options.rb +32 -0
  69. data/out/production/securenative-ruby/http/http_response.rb +12 -0
  70. data/out/production/securenative-ruby/http/securenative_http_client.rb +32 -0
  71. data/out/production/securenative-ruby/models/client_token.rb +12 -0
  72. data/out/production/securenative-ruby/models/device.rb +10 -0
  73. data/out/production/securenative-ruby/models/event_options.rb +15 -0
  74. data/out/production/securenative-ruby/models/request_context.rb +18 -0
  75. data/out/production/securenative-ruby/models/request_options.rb +12 -0
  76. data/out/production/securenative-ruby/models/sdk_event.rb +33 -0
  77. data/out/production/securenative-ruby/models/user_traits.rb +13 -0
  78. data/out/production/securenative-ruby/models/verify_result.rb +12 -0
  79. data/out/production/securenative-ruby/securenative.rb +91 -0
  80. data/out/production/securenative-ruby/utils/date_utils.rb +9 -0
  81. data/out/production/securenative-ruby/utils/encryption_utils.rb +35 -0
  82. data/out/production/securenative-ruby/utils/ip_utils.rb +23 -0
  83. data/out/production/securenative-ruby/utils/request_utils.rb +23 -0
  84. data/out/production/securenative-ruby/utils/secure_native_logger.rb +44 -0
  85. data/out/production/securenative-ruby/utils/signature_utils.rb +16 -0
  86. data/out/production/securenative-ruby/utils/utils.rb +9 -0
  87. data/out/production/securenative-ruby/utils/version_utils.rb +12 -0
  88. data/out/test/securenative-ruby/spec_api_manager.rb +81 -0
  89. data/out/test/securenative-ruby/spec_context_builder.rb +69 -0
  90. data/out/test/securenative-ruby/spec_date_utils.rb +13 -0
  91. data/out/test/securenative-ruby/spec_encryption_utils.rb +26 -0
  92. data/out/test/securenative-ruby/spec_event_manager.rb +59 -0
  93. data/out/test/securenative-ruby/spec_helper.rb +20 -0
  94. data/out/test/securenative-ruby/spec_ip_utils.rb +41 -0
  95. data/out/test/securenative-ruby/spec_securenative.rb +65 -0
  96. data/out/test/securenative-ruby/spec_securenative_http_client.rb +23 -0
  97. data/out/test/securenative-ruby/spec_signature_utils.rb +18 -0
  98. data/securenative.gemspec +4 -4
  99. metadata +96 -15
  100. data/lib/securenative/config.rb +0 -9
  101. data/lib/securenative/event_manager.rb +0 -88
  102. data/lib/securenative/event_options.rb +0 -86
  103. data/lib/securenative/event_type.rb +0 -21
  104. data/lib/securenative/http_client.rb +0 -20
  105. data/lib/securenative/secure_native_sdk.rb +0 -62
  106. data/lib/securenative/securenative_options.rb +0 -17
  107. data/lib/securenative/sn_exception.rb +0 -5
  108. data/lib/securenative/utils.rb +0 -41
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+
5
+ class SignatureUtils
6
+ SIGNATURE_HEADER = 'x-securenative'
7
+
8
+ def self.valid_signature?(api_key, payload, header_signature)
9
+ key = api_key.encode('utf-8')
10
+ body = payload.encode('utf-8')
11
+ calculated_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), key, body)
12
+ calculated_signature.eql? header_signature
13
+ rescue StandardError
14
+ false
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Utils
4
+ def self.null_or_empty?(string)
5
+ return true if !string || string.empty? || string.nil?
6
+
7
+ false
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class VersionUtils
4
+ def self.version
5
+ path = 'VERSION'
6
+ file = File.open(path)
7
+ version = file.read
8
+ file.close
9
+
10
+ version
11
+ end
12
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'api_manager'
4
+ require 'webmock/rspec'
5
+ require 'rspec'
6
+
7
+ 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
+ it 'tracks an event' do
20
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
21
+
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)
31
+ event_manager = EventManager.new(options)
32
+ event_manager.start_event_persist
33
+ api_manager = ApiManager.new(event_manager, options)
34
+
35
+ begin
36
+ api_manager.track(:event_options)
37
+ ensure
38
+ event_manager.stop_event_persist
39
+ end
40
+ end
41
+
42
+ 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')
44
+
45
+ properties = {}
46
+ (0..12).each do |i|
47
+ properties[i] = i
48
+ end
49
+
50
+ stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track').to_return(status: 200)
51
+ event_manager = EventManager.new(options)
52
+ event_manager.start_event_persist
53
+ api_manager = ApiManager.new(event_manager, options)
54
+
55
+ begin
56
+ expect { api_manager.track(EventOptions(event_type: EventTypes.LOG_IN, properties: properties).build) }
57
+ .to raise_error(SecureNativeInvalidOptionsError)
58
+ ensure
59
+ event_manager.stop_event_persist
60
+ end
61
+ end
62
+
63
+ it 'verifies an event' do
64
+ options = ConfigurationBuilder(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
65
+
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
+
70
+ event_manager = EventManager.new(options)
71
+ event_manager.start_event_persist
72
+ api_manager = ApiManager.new(event_manager, options)
73
+
74
+ result = api_manager.verify(:event_options)
75
+
76
+ 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)
80
+ end
81
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'context/securenative_context'
4
+ require 'webmock/rspec'
5
+ require 'rspec'
6
+
7
+ RSpec.describe SecureNativeContext do
8
+ it 'creates context from request' do
9
+ 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')
14
+
15
+ request = Net::HTTP.get('www.example.com', '/')
16
+ context = SecureNativeContext.from_http_request(request)
17
+
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')
22
+ expect(context.remote_ip).to eq('')
23
+ expect(context.headers).to eq({ 'x-securenative': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
24
+ expect(context.body).to be_nil
25
+ end
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')
33
+
34
+ request = Net::HTTP.get('www.example.com', '/')
35
+ con = SecureNativeContext.from_http_request(request)
36
+
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
+ end
44
+
45
+ it 'creates default context builder' do
46
+ context = SecureNativeContext.default_context_builder
47
+
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
53
+ expect(context.headers).to be_nil
54
+ expect(context.body).to be_nil
55
+ end
56
+
57
+ 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)
60
+
61
+ expect(context.url).to eq('/some-url')
62
+ expect(context.client_token).to eq('SECRET_TOKEN')
63
+ expect(context.ip).to eq('10.0.0.0')
64
+ expect(context.body).to be_nil
65
+ expect(context.http_method).to eq('Get')
66
+ expect(context.remote_ip).to eq('10.0.0.0')
67
+ expect(context.headers).to eq({ 'header' => 'value1' })
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utils/date_utils'
4
+ require 'rspec'
5
+
6
+ RSpec.describe DateUtils do
7
+ it 'converts to timestamp' do
8
+ iso_8601_date = '2020-05-20T15:07:13Z'
9
+ result = DateUtils.to_timestamp(iso_8601_date)
10
+
11
+ expect(result).to eq(iso_8601_date)
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utils/encryption_utils'
4
+ require 'rspec'
5
+
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)
14
+
15
+ expect(result).not_to be_nil
16
+ expect(result.length).to eq(:PAYLOAD.length)
17
+ end
18
+
19
+ it 'encrypts' do
20
+ encrypted_payload = '5208ae703cc2fa0851347f55d3b76d3fd6035ee081d71a401e8bc92ebdc25d42440f62310bda60628537744ac03f200d78da9e61f1019ce02087b7ce6c976e7b2d8ad6aa978c532cea8f3e744cc6a5cafedc4ae6cd1b08a4ef75d6e37aa3c0c76954d16d57750be2980c2c91ac7ef0bbd0722abd59bf6be22493ea9b9759c3ff4d17f17ab670b0b6fc320e6de982313f1c4e74c0897f9f5a32d58e3e53050ae8fdbebba9009d0d1250fe34dcde1ebb42acbc22834a02f53889076140f0eb8db1'
21
+ result = EncryptionUtils.decrypt(encrypted_payload, :SECRET_KEY)
22
+
23
+ expect(result.cid).to eq(:CID)
24
+ expect(result.fp).to eq(:FP)
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'event_manager'
4
+ require 'config/configuration_builder'
5
+ require 'rspec'
6
+ require 'webmock/rspec'
7
+
8
+ class SampleEvent
9
+ attr_reader :event_type, :timestamp, :rid, :user_id, :user_traits, :request, :properties
10
+
11
+ def initialize
12
+ @event_type = 'custom-event'
13
+ @timestamp = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%Z')
14
+ @rid = '432532'
15
+ @user_id = '1'
16
+ @user_traits = UserTraits('some user', 'email@securenative.com', '+1234567890')
17
+ @request = RequestContext()
18
+ @properties = []
19
+ end
20
+ end
21
+
22
+ RSpec.describe EventManager do
23
+ let(:event) { SampleEvent() }
24
+
25
+ it 'successfully sends sync event with status code 200' do
26
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
27
+
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
+ event_manager = EventManager.new(options)
32
+
33
+ event_manager.start_event_persist
34
+ data = event_manager.send_sync(:event, 'some-path/to-api', false)
35
+ event_manager.stop_event_persist
36
+
37
+ expect(res_body).to eq(data.text)
38
+ end
39
+
40
+ it 'fails when send sync event status code is 401' do
41
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
42
+
43
+ stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 401)
44
+ event_manager = EventManager.new(options)
45
+ res = event_manager.send_sync(:event, 'some-path/to-api', false)
46
+
47
+ expect(res.status_code).to eq(401)
48
+ end
49
+
50
+ it 'fails when send sync event status code is 500' do
51
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
52
+
53
+ stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api').to_return(status: 500)
54
+ event_manager = EventManager.new(options)
55
+ res = event_manager.send_sync(:event, 'some-path/to-api', false)
56
+
57
+ expect(res.status_code).to eq(500)
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'simplecov'
5
+ require 'codecov'
6
+
7
+ SimpleCov.start
8
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
9
+
10
+ RSpec.configure do |config|
11
+ # Enable flags like --only-failures and --next-failure
12
+ config.example_status_persistence_file_path = '.rspec_status'
13
+
14
+ # Disable RSpec exposing methods globally on `Module` and `main`
15
+ config.disable_monkey_patching!
16
+
17
+ config.expect_with :rspec do |c|
18
+ c.syntax = :expect
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utils/ip_utils'
4
+ require 'rspec'
5
+
6
+ RSpec.describe IpUtils do
7
+ it 'checks if ip address valid ipv4' do
8
+ valid_ipv4 = '172.16.254.1'
9
+ expect(IpUtils.ip_address?(valid_ipv4)).to be_truthy
10
+ end
11
+
12
+ it 'checks if ip address valid ipv6' do
13
+ valid_ipv6 = '2001:db8:1234:0000:0000:0000:0000:0000'
14
+ expect(IpUtils.ip_address?(valid_ipv6)).to be_truthy
15
+ end
16
+
17
+ it 'checks if ip address invalid ipv4' do
18
+ invalid_ipv4 = '172.16.2541'
19
+ expect(IpUtils.ip_address?(invalid_ipv4)).to be_falsey
20
+ end
21
+
22
+ it 'checks if ip address invalid ipv6' do
23
+ invalid_ipv6 = '2001:db8:1234:0000'
24
+ expect(IpUtils.ip_address?(invalid_ipv6)).to be_falsey
25
+ end
26
+
27
+ it 'checks if valid public ip' do
28
+ ip = '64.71.222.37'
29
+ expect(IpUtils.valid_public_ip?(ip)).to be_truthy
30
+ end
31
+
32
+ it 'checks if not valid public ip' do
33
+ ip = '10.0.0.0'
34
+ expect(IpUtils.valid_public_ip?(ip)).to be_falsey
35
+ end
36
+
37
+ it 'checks if valid loopback ip' do
38
+ ip = '127.0.0.1'
39
+ expect(IpUtils.loop_back?(ip)).to be_truthy
40
+ end
41
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securenative'
4
+ require 'config/configuration_manager'
5
+ require 'errors/securenative_sdk_error'
6
+ require 'errors/securenative_sdk_Illegal_state_error'
7
+ require 'errors/securenative_config_error'
8
+ require 'rspec'
9
+
10
+ RSpec.describe SecureNative do
11
+ it 'gets sdk instance without init throws' do
12
+ expect { SecureNative.instance }.to raise_error(SecureNativeSDKIllegalStateError)
13
+ end
14
+
15
+ it 'inits sdk without api key and throws' do
16
+ expect { SecureNative.init_with_options(ConfigurationManager.config_builder) }.to raise_error(SecureNativeSDKError)
17
+ end
18
+
19
+ it 'inits sdk with empty api key and throws' do
20
+ expect { SecureNative.init_with_api_key('') }.to raise_error(SecureNativeConfigError)
21
+ end
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
40
+
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
45
+
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
54
+
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
65
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http/securenative_http_client'
4
+ require 'config/configuration_builder'
5
+ require 'webmock/rspec'
6
+ require 'rspec'
7
+
8
+ RSpec.describe SecureNativeHttpClient do
9
+ it 'makes a simple post call' do
10
+ options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
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)
14
+ client = SecureNativeHttpClient.new(options)
15
+ payload = '{"event": "SOME_EVENT_NAME"}'
16
+
17
+ res = client.post('track', payload)
18
+
19
+ expect(res.ok).to eq(true)
20
+ expect(res.status_code).to eq(200)
21
+ expect(res.text).to eq(payload)
22
+ end
23
+ end