Skip to content
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
securenative (0.1.21)
securenative (0.1.22)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ require 'models/user_traits'

def track
securenative = SecureNative.instance
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
context = SecureNativeContext.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
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' })

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


def track
def track(request)
securenative = SecureNative.instance
context = SecureNativeContext.from_http_request(request)

Expand Down
9 changes: 9 additions & 0 deletions lib/models/sdk_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'context/securenative_context'
require 'errors/securenative_invalid_options_error'
require 'utils/encryption_utils'
require 'utils/date_utils'
require 'models/request_context'
Expand All @@ -11,6 +12,14 @@ class SDKEvent
attr_writer :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties

def initialize(event_options, securenative_options)
if event_options.user_id.nil? || event_options.user_id.length <= 0 || event_options.user_id == ''
raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; User Id is missing'
end

if event_options.event.nil? || event_options.event.length <= 0 || event_options.event == ''
raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; Event Type is missing'
end

@context = if !event_options.context.nil?
event_options.context
else
Expand Down
2 changes: 1 addition & 1 deletion securenative.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require_relative "lib/utils/version_utils"

Gem::Specification.new do |spec|
spec.name = "securenative"
spec.version = "0.1.21"
spec.version = "0.1.22"
spec.authors = ["SecureNative"]
spec.email = ["support@securenative.com"]

Expand Down
29 changes: 14 additions & 15 deletions spec/spec_api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,18 @@
it 'verifies an event' do
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')

# stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
# .with(body: { riskLevel: 'medium', score: 0.32, triggers: ['New IP', 'New City'] }).to_return(status: 200)
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/verify").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'YOUR_API_KEY',
'Content-Type'=>'application/json',
'Sn-Version'=>'0.1.22',
'User-Agent'=>'SecureNative-ruby'
}).
to_return(status: 200, body: "", headers: {})

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

event_manager = EventManager.new(options)
event_manager.start_event_persist
Expand All @@ -81,8 +80,8 @@
result = api_manager.verify(event_options)

expect(result).not_to be_nil
expect(result.risk_level).to eq(verify_result.risk_level)
expect(result.score).to eq(verify_result.score)
expect(result.triggers).to eq(verify_result.triggers)
expect(result.risk_level).to eq('low')
expect(result.score).to eq(0)
expect(result.triggers).to eq(nil)
end
end
62 changes: 35 additions & 27 deletions spec/spec_event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ def initialize
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
event = SampleEvent.new

stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
.with(headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.21',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 200, body: '', headers: {})
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'YOUR_API_KEY',
'Content-Type'=>'application/json',
'Sn-Version'=>'0.1.22',
'User-Agent'=>'SecureNative-ruby'
}).
to_return(status: 200, body: "", headers: {})

event_manager = EventManager.new(options)

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

stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
.with(headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.21',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 401, body: '', headers: {})
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'YOUR_API_KEY',
'Content-Type'=>'application/json',
'Sn-Version'=>'0.1.22',
'User-Agent'=>'SecureNative-ruby'
}).
to_return(status: 401, body: "", headers: {})

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

stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/some-path/to-api')
.with(headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.21',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 500, body: '', headers: {})
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/some-path/to-api").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'YOUR_API_KEY',
'Content-Type'=>'application/json',
'Sn-Version'=>'0.1.22',
'User-Agent'=>'SecureNative-ruby'
}).
to_return(status: 500, body: "", headers: {})

event_manager = EventManager.new(options)
res = event_manager.send_sync(event, 'some-path/to-api', false)

Expand Down
24 changes: 24 additions & 0 deletions spec/spec_sdk_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'models/sdk_event'
require 'models/event_options'
require 'config/securenative_options'
require 'enums/event_types'
require 'errors/securenative_invalid_options_error'
require 'rspec'

RSpec.describe SDKEvent do
it 'throws when event created without user id' do
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: nil)
options = SecureNativeOptions.new

expect { SDKEvent.new(event_options, options) }.to raise_error(SecureNativeInvalidOptionsError)
end

it 'throws when event created without event type' do
event_options = EventOptions.new(event: nil, user_id: '1234')
options = SecureNativeOptions.new

expect { SDKEvent.new(event_options, options) }.to raise_error(SecureNativeInvalidOptionsError)
end
end
20 changes: 11 additions & 9 deletions spec/spec_securenative_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
it 'makes a simple post call' do
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')

stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
.with(body: '{"event": "SOME_EVENT_NAME"}',
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/track").
with(
body: "{\"event\": \"SOME_EVENT_NAME\"}",
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'YOUR_API_KEY',
'Content-Type' => 'application/json',
'Sn-Version' => '0.1.21',
'User-Agent' => 'SecureNative-ruby'
}).to_return(status: 200, body: '', headers: {})
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'YOUR_API_KEY',
'Content-Type'=>'application/json',
'Sn-Version'=>'0.1.22',
'User-Agent'=>'SecureNative-ruby'
}).
to_return(status: 200, body: "", headers: {})
client = SecureNativeHttpClient.new(options)
payload = '{"event": "SOME_EVENT_NAME"}'

Expand Down