Skip to content

Commit f2444b8

Browse files
author
Inbal Tako
committed
Add missing tests and bump version
1 parent d447a4a commit f2444b8

9 files changed

Lines changed: 150 additions & 200 deletions

securenative/context/context_builder.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ def default_context_builder():
4242

4343
@staticmethod
4444
def from_http_request(request):
45-
headers = request.headers
45+
try:
46+
client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
47+
except AttributeError:
48+
client_token = None
49+
50+
try:
51+
headers = request.headers
52+
except AttributeError:
53+
headers = None
4654

47-
client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
4855
if Utils.is_null_or_empty(client_token):
4956
client_token = RequestUtils.get_secure_header_from_request(headers)
5057

securenative/event_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def send_sync(self, event, resource_path, retry):
7777
json.dumps(EventManager.serialize(event)),
7878
retry
7979
)
80-
self.queue.append(0, item)
80+
self.queue.append(item)
8181
if self._is_queue_full():
8282
self.queue = self.queue[:len(self.queue - 1)]
8383
return res
@@ -136,7 +136,7 @@ def stop_event_persist(self):
136136
def serialize(obj):
137137
return {
138138
"rid": obj.rid,
139-
"eventType": obj.event_type.value,
139+
"eventType": obj.event_type,
140140
"userId": obj.user_id,
141141
"userTraits": {
142142
"name": obj.user_traits.name,

securenative/models/sdk_event.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def __init__(self, event_options, securenative_options):
2121
self.user_id = event_options.user_id
2222
self.user_traits = event_options.user_traits
2323
self.request = RequestContextBuilder() \
24-
.with_cid(client_token.cid) \
25-
.with_vid(client_token.vid) \
26-
.with_fp(client_token.fp) \
24+
.with_cid(client_token.cid if client_token else "") \
25+
.with_vid(client_token.vid if client_token else "") \
26+
.with_fp(client_token.fp if client_token else "") \
2727
.with_ip(self.context.ip) \
2828
.with_remote_ip(self.context.remote_ip) \
2929
.with_method(self.context.method) \

securenative/utils/request_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class RequestUtils(object):
44

55
@staticmethod
66
def get_secure_header_from_request(headers):
7-
return headers[RequestUtils.SECURENATIVE_HEADER]
7+
if headers:
8+
return headers[RequestUtils.SECURENATIVE_HEADER]
9+
return []
810

911
@staticmethod
1012
def get_client_ip_from_request(request):
@@ -17,4 +19,7 @@ def get_client_ip_from_request(request):
1719

1820
@staticmethod
1921
def get_remote_ip_from_request(request):
20-
return request.raw._original_response.fp.raw._sock.getpeername()[0]
22+
try:
23+
return request.raw._original_response.fp.raw._sock.getpeername()[0]
24+
except AttributeError:
25+
return ""

tests/api_manager_test.py

Lines changed: 34 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import unittest
3+
import responses
24

35
from securenative.api_manager import ApiManager
46
from securenative.config.configuration_manager import ConfigurationManager
@@ -10,7 +12,6 @@
1012
from securenative.exceptions.securenative_invalid_options_exception import SecureNativeInvalidOptionsException
1113
from securenative.models.user_traits import UserTraits
1214
from securenative.models.verify_result import VerifyResult
13-
from securenative.utils.date_utils import DateUtils
1415

1516

1617
class ApiManagerTest(unittest.TestCase):
@@ -30,122 +31,77 @@ def setUp(self):
3031
with_context(self.context). \
3132
with_properties({"prop1": "CUSTOM_PARAM_VALUE",
3233
"prop2": True,
33-
"prop3": 3}).with_timestamp(DateUtils.to_timestamp(None)).build()
34+
"prop3": 3}).build()
3435

3536
def test_track_event(self):
3637
options = ConfigurationManager.config_builder(). \
3738
with_api_key("YOUR_API_KEY"). \
3839
with_auto_send(True). \
39-
with_interval(10)
40-
41-
client = MockHttpClient() # TODO!
42-
event_manager = EventManager(options, client)
40+
with_interval(10). \
41+
with_api_url("https://api.securenative-stg.com/collector/api/v1")
42+
43+
expected = "{\"eventType\":\"sn.user.login\",\"userId\":\"USER_ID\",\"userTraits\":{" \
44+
"\"name\":\"USER_NAME\",\"email\":\"USER_EMAIL\",\"createdAt\":null},\"request\":{" \
45+
"\"cid\":null,\"vid\":null,\"fp\":null,\"ip\":\"127.0.0.1\",\"remoteIp\":null,\"headers\":{" \
46+
"\"user-agent\":\"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) " \
47+
"AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\"},\"url\":null,\"method\":null}," \
48+
"\"properties\":{\"prop2\":true,\"prop1\":\"CUSTOM_PARAM_VALUE\",\"prop3\":3}}"
49+
50+
responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/track",
51+
json=json.loads(expected), status=200)
52+
event_manager = EventManager(options)
4353
event_manager.start_event_persist()
4454
api_manager = ApiManager(event_manager, options)
4555

4656
try:
47-
res = api_manager.track(self.event_options)
48-
expected = "{\"eventType\":\"sn.user.login\",\"userId\":\"USER_ID\",\"userTraits\":{" \
49-
"\"name\":\"USER_NAME\",\"email\":\"USER_EMAIL\",\"createdAt\":null},\"request\":{" \
50-
"\"cid\":null,\"vid\":null,\"fp\":null,\"ip\":\"127.0.0.1\",\"remoteIp\":null,\"headers\":{" \
51-
"\"user-agent\":\"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) " \
52-
"AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\"},\"url\":null,\"method\":null}," \
53-
"\"properties\":{\"prop2\":true,\"prop1\":\"CUSTOM_PARAM_VALUE\",\"prop3\":3}} "
54-
55-
self.assertEqual(res.body, expected)
57+
api_manager.track(self.event_options)
5658
finally:
5759
event_manager.stop_event_persist()
5860

5961
def test_securenative_invalid_options_exception(self):
6062
options = ConfigurationManager.config_builder(). \
6163
with_api_key("YOUR_API_KEY"). \
6264
with_auto_send(True). \
63-
with_interval(10)
65+
with_interval(10). \
66+
with_api_url("https://api.securenative-stg.com/collector/api/v1")
6467

6568
properties = {}
6669
for i in range(1, 12):
6770
properties[i] = i
6871

69-
client = MockHttpClient() # TODO!
70-
event_manager = EventManager(client, options)
72+
responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/track",
73+
json={}, status=200)
74+
event_manager = EventManager(options)
7175
event_manager.start_event_persist()
7276
api_manager = ApiManager(event_manager, options)
7377

7478
try:
75-
api_manager.track(EventOptionsBuilder(
76-
EventTypes.LOG_IN).with_properties(properties).build())
77-
self.assertRaises(SecureNativeInvalidOptionsException)
78-
finally:
79-
event_manager.stop_event_persist()
80-
81-
def test_automatic_persistent_disable(self):
82-
options = ConfigurationManager.config_builder(). \
83-
with_api_key("YOUR_API_KEY"). \
84-
with_auto_send(True). \
85-
with_interval(10)
86-
87-
client = MockHttpClient() # TODO!
88-
event_manager = EventManager(client, options)
89-
api_manager = ApiManager(event_manager, options)
90-
91-
self.assertIsNotNone(api_manager.track(self.event_options))
92-
93-
def test_unauthorized_track_event(self):
94-
options = ConfigurationManager.config_builder(). \
95-
with_api_key("YOUR_API_KEY"). \
96-
with_auto_send(True). \
97-
with_interval(10)
98-
99-
client = MockHttpClient() # TODO!
100-
event_manager = EventManager(client, options)
101-
event_manager.start_event_persist()
102-
api_manager = ApiManager(event_manager, options)
103-
104-
try:
105-
self.assertIsNotNone(api_manager.track(self.event_options))
79+
with self.assertRaises(SecureNativeInvalidOptionsException):
80+
api_manager.track(EventOptionsBuilder(
81+
EventTypes.LOG_IN).with_properties(properties).build())
10682
finally:
10783
event_manager.stop_event_persist()
10884

10985
def test_verify_event(self):
11086
options = ConfigurationManager.config_builder(). \
111-
with_api_key("YOUR_API_KEY")
87+
with_api_key("YOUR_API_KEY"). \
88+
with_api_url("https://api.securenative-stg.com/collector/api/v1")
11289

113-
verify_result = VerifyResult(RiskLevel.LOW, 0, {})
90+
responses.add(responses.POST, "https://api.securenative-stg.com/collector/api/v1/verify",
91+
json={}, status=200)
92+
verify_result = VerifyResult(RiskLevel.LOW, 0, None)
11493

115-
client = MockHttpClient() # TODO!
116-
event_manager = EventManager(client, options)
94+
event_manager = EventManager(options)
11795
event_manager.start_event_persist()
11896
api_manager = ApiManager(event_manager, options)
11997

120-
result, body = api_manager.verify(self.event_options)
98+
result = api_manager.verify(self.event_options)
12199

122-
self.assertEqual(result.risk_level, verify_result.risk_level)
100+
self.assertIsNotNone(result)
101+
self.assertEqual(result.risk_level, verify_result.risk_level.value)
123102
self.assertEqual(result.score, verify_result.score)
124103
self.assertEqual(result.triggers, verify_result.triggers)
125104

126-
expected = "{\"eventType\":\"sn.user.login\",\"userId\":\"USER_ID\",\"userTraits\":{\"name\":\"USER_NAME\",\"email\":\"USER_EMAIL\",\"createdAt\":null},\"request\":{\"cid\":null,\"vid\":null,\"fp\":null,\"ip\":\"127.0.0.1\",\"remoteIp\":null,\"headers\":{\"user-agent\":\"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\"},\"url\":null,\"method\":null},\"properties\":{\"prop2\":true,\"prop1\":\"CUSTOM_PARAM_VALUE\",\"prop3\":3}}";
127-
self.assertEqual(body, expected)
128-
129-
def test_unauthorized_verify_event(self):
130-
options = ConfigurationManager.config_builder(). \
131-
with_api_key("YOUR_API_KEY")
132-
133-
client = MockHttpClient() # TODO!
134-
event_manager = EventManager(client, options)
135-
event_manager.start_event_persist()
136-
api_manager = ApiManager(event_manager, options)
137-
138-
result, body = api_manager.verify(self.event_options)
139-
140-
self.assertIsNotNone(result)
141-
self.assertEqual(result.risk_level, RiskLevel.LOW)
142-
self.assertEqual(result.score, 0)
143-
self.assertEqual(result.triggers, 0)
144-
145-
expected = "{\"eventType\":\"sn.user.login\",\"userId\":\"USER_ID\",\"userTraits\":{\"name\":\"USER_NAME'\",\"email\":\"USER_EMAIL'\",\"createdAt\":null},\"request\":{\"cid\":null,\"vid\":null,\"fp\":null,\"ip\":\"127.0.0.1\",\"remoteIp\":null,\"headers\":{\"user-agent\":\"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405\"},\"url\":null,\"method\":null},\"properties\":{\"prop2\":true,\"prop1\":\"CUSTOM_PARAM_VALUE\",\"prop3\":3}}";
146-
self.assertIsNotNone(body)
147-
self.assertEqual(body, expected)
148-
149105

150106
if __name__ == '__main__':
151107
unittest.main()

tests/context_builder_test.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
11
import unittest
22

3+
import requests_mock
4+
35
from securenative.context.context_builder import ContextBuilder
46

57

68
class ContextBuilderTest(unittest.TestCase):
79

810
def test_create_context_from_request(self):
9-
request = MockRequest() # TODO!
10-
request.server_name = "www.securenative.com"
11-
request.request_uri = "/login"
12-
request.query_string = "param1=value1&param2=value2"
13-
request.method = "Post"
14-
request.remote_addr = "51.68.201.122"
15-
request.headers = {
16-
"x-securenative": "71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
11+
with requests_mock.Mocker(real_http=True) as request:
12+
request.server_name = "www.securenative.com"
13+
request.request_uri = "/login"
14+
request.query_string = "param1=value1&param2=value2"
15+
request.method = "Post"
16+
request.url = "www.securenative.com"
17+
request.request_uri = "/login/param1=value1&param2=value2"
18+
request.META = {
19+
"REMOTE_ADDR": "51.68.201.122"
20+
}
21+
request.headers = {
22+
"x-securenative": "71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
1723

18-
context = ContextBuilder.from_http_request(request).build()
24+
context = ContextBuilder.from_http_request(request).build()
1925

20-
self.assertEqual(context.client_token,
21-
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
22-
self.assertEqual(context.ip, "51.68.201.122")
23-
self.assertEqual(context.method, "Post")
24-
self.assertEqual(context.url, "/login")
25-
self.assertEqual(context.remote_ip, "51.68.201.122")
26-
self.assertEqual(context.headers, {
27-
"x-securenative": "71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"})
28-
self.assertIsNone(context.body())
26+
self.assertEqual(context.client_token,
27+
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
28+
self.assertEqual(context.ip, "51.68.201.122")
29+
self.assertEqual(context.method, "Post")
30+
self.assertEqual(context.url, "www.securenative.com")
31+
self.assertEqual(context.remote_ip, "")
32+
self.assertEqual(context.headers, {
33+
"x-securenative": "71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"})
34+
self.assertIsNone(context.body)
2935

3036
def test_create_context_from_request_with_cookie(self):
31-
request = MockRequest() # TODO!
32-
request.server_name = "www.securenative.com"
33-
request.request_uri = "/login"
34-
request.query_string = "param1=value1&param2=value2"
35-
request.method = "Post"
36-
request.remote_addr = "51.68.201.122"
37-
# TODO!
38-
request.cookies = Cookie("_sn",
39-
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
37+
with requests_mock.Mocker(real_http=True) as request:
38+
request.server_name = "www.securenative.com"
39+
request.request_uri = "/login"
40+
request.query_string = "param1=value1&param2=value2"
41+
request.method = "Post"
42+
request.url = "www.securenative.com"
43+
request.request_uri = "/login/param1=value1&param2=value2"
44+
request.META = {
45+
"REMOTE_ADDR": "51.68.201.122"
46+
}
47+
request.cookies = {"_sn":
48+
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
4049

41-
context = ContextBuilder.from_http_request(request).build()
50+
context = ContextBuilder.from_http_request(request).build()
4251

43-
self.assertEqual(context.client_token,
44-
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
45-
self.assertEqual(context.ip, "51.68.201.122")
46-
self.assertEqual(context.method, "Post")
47-
self.assertEqual(context.url, "/login")
48-
self.assertEqual(context.remote_ip, "51.68.201.122")
49-
self.assertEqual(context.headers, {
50-
"Cookie": "_sn=71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"})
51-
self.assertIsNone(context.body())
52+
self.assertEqual(context.client_token,
53+
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
54+
self.assertEqual(context.ip, "51.68.201.122")
55+
self.assertEqual(context.method, "Post")
56+
self.assertEqual(context.url, "www.securenative.com")
57+
self.assertEqual(context.remote_ip, "")
58+
self.assertIsNone(context.body)
5259

5360
def test_create_default_context_builder(self):
5461
context = ContextBuilder.default_context_builder().build()
@@ -59,7 +66,7 @@ def test_create_default_context_builder(self):
5966
self.assertIsNone(context.url)
6067
self.assertIsNone(context.remote_ip)
6168
self.assertIsNone(context.headers)
62-
self.assertIsNone(context.body())
69+
self.assertIsNone(context.body)
6370

6471
def test_create_custom_context_with_context_builder(self):
6572
context = ContextBuilder.default_context_builder(). \

0 commit comments

Comments
 (0)