Skip to content

Commit ffb2cec

Browse files
authored
Merge pull request #16 from securenative/dev
Dev
2 parents bb5472f + c7d062d commit ffb2cec

8 files changed

Lines changed: 97 additions & 55 deletions

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.21)
4+
securenative (0.1.22)
55

66
GEM
77
remote: https://rubygems.org/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ require 'models/user_traits'
9696

9797
def track
9898
securenative = SecureNative.instance
99-
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
99+
context = SecureNativeContext.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
100100
headers: { 'user-agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4' })
101101

102102
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
@@ -118,7 +118,7 @@ require 'enums/event_types'
118118
require 'models/user_traits'
119119

120120

121-
def track
121+
def track(request)
122122
securenative = SecureNative.instance
123123
context = SecureNativeContext.from_http_request(request)
124124

lib/models/sdk_event.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'context/securenative_context'
4+
require 'errors/securenative_invalid_options_error'
45
require 'utils/encryption_utils'
56
require 'utils/date_utils'
67
require 'models/request_context'
@@ -11,6 +12,14 @@ class SDKEvent
1112
attr_writer :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
1213

1314
def initialize(event_options, securenative_options)
15+
if event_options.user_id.nil? || event_options.user_id.length <= 0 || event_options.user_id == ''
16+
raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; User Id is missing'
17+
end
18+
19+
if event_options.event.nil? || event_options.event.length <= 0 || event_options.event == ''
20+
raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; Event Type is missing'
21+
end
22+
1423
@context = if !event_options.context.nil?
1524
event_options.context
1625
else

securenative.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require_relative "lib/utils/version_utils"
44

55
Gem::Specification.new do |spec|
66
spec.name = "securenative"
7-
spec.version = "0.1.21"
7+
spec.version = "0.1.22"
88
spec.authors = ["SecureNative"]
99
spec.email = ["support@securenative.com"]
1010

spec/spec_api_manager.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,18 @@
5757
it 'verifies an event' do
5858
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
5959

60-
# stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
61-
# .with(body: { riskLevel: 'medium', score: 0.32, triggers: ['New IP', 'New City'] }).to_return(status: 200)
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: {})
6271

63-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/verify')
64-
.with(headers: {
65-
'Accept' => '*/*',
66-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
67-
'Authorization' => 'YOUR_API_KEY',
68-
'Content-Type' => 'application/json',
69-
'Sn-Version' => '0.1.21',
70-
'User-Agent' => 'SecureNative-ruby'
71-
}).to_return(status: 200, body: '', headers: {})
72-
verify_result = VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
7372

7473
event_manager = EventManager.new(options)
7574
event_manager.start_event_persist
@@ -81,8 +80,8 @@
8180
result = api_manager.verify(event_options)
8281

8382
expect(result).not_to be_nil
84-
expect(result.risk_level).to eq(verify_result.risk_level)
85-
expect(result.score).to eq(verify_result.score)
86-
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)
8786
end
8887
end

spec/spec_event_manager.rb

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ def initialize
2626
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
2727
event = SampleEvent.new
2828

29-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
30-
.with(headers: {
31-
'Accept' => '*/*',
32-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
33-
'Authorization' => 'YOUR_API_KEY',
34-
'Content-Type' => 'application/json',
35-
'Sn-Version' => '0.1.21',
36-
'User-Agent' => 'SecureNative-ruby'
37-
}).to_return(status: 200, body: '', headers: {})
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: {})
40+
3841
event_manager = EventManager.new(options)
3942

4043
event_manager.start_event_persist
@@ -48,15 +51,17 @@ def initialize
4851
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
4952
event = SampleEvent.new
5053

51-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
52-
.with(headers: {
53-
'Accept' => '*/*',
54-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
55-
'Authorization' => 'YOUR_API_KEY',
56-
'Content-Type' => 'application/json',
57-
'Sn-Version' => '0.1.21',
58-
'User-Agent' => 'SecureNative-ruby'
59-
}).to_return(status: 401, body: '', headers: {})
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: {})
6065

6166
event_manager = EventManager.new(options)
6267
res = event_manager.send_sync(event, 'some-path/to-api', false)
@@ -68,15 +73,18 @@ def initialize
6873
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
6974
event = SampleEvent.new
7075

71-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
72-
.with(headers: {
73-
'Accept' => '*/*',
74-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
75-
'Authorization' => 'YOUR_API_KEY',
76-
'Content-Type' => 'application/json',
77-
'Sn-Version' => '0.1.21',
78-
'User-Agent' => 'SecureNative-ruby'
79-
}).to_return(status: 500, body: '', headers: {})
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: {})
87+
8088
event_manager = EventManager.new(options)
8189
res = event_manager.send_sync(event, 'some-path/to-api', false)
8290

spec/spec_sdk_event.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/spec_securenative_http_client.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
it 'makes a simple post call' do
1010
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
1111

12-
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
13-
.with(body: '{"event": "SOME_EVENT_NAME"}',
12+
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/track").
13+
with(
14+
body: "{\"event\": \"SOME_EVENT_NAME\"}",
1415
headers: {
15-
'Accept' => '*/*',
16-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
17-
'Authorization' => 'YOUR_API_KEY',
18-
'Content-Type' => 'application/json',
19-
'Sn-Version' => '0.1.21',
20-
'User-Agent' => 'SecureNative-ruby'
21-
}).to_return(status: 200, body: '', 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: {})
2224
client = SecureNativeHttpClient.new(options)
2325
payload = '{"event": "SOME_EVENT_NAME"}'
2426

0 commit comments

Comments
 (0)