Skip to content
Merged
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
18 changes: 17 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ aliases:
- &with-requests
environment:
REQUESTS: requests
- &lint
steps:
- checkout
- run: make ci-lint
- &job-defaults
steps:
- checkout
- run: pip install $REQUESTS
- run: python setup.py test
- run: make test

jobs:
lint-sdk:
docker:
- image: circleci/python:3.9
<<: *with-requests
<<: *lint
python-3_4:
docker:
- image: circleci/python:3.4
Expand All @@ -36,12 +45,19 @@ jobs:
- image: circleci/python:3.8
<<: *with-requests
<<: *job-defaults
python-3_9:
docker:
- image: circleci/python:3.9
<<: *with-requests
<<: *job-defaults

workflows:
main:
jobs:
- lint-sdk
- python-3_4
- python-3_5
- python-3_6
- python-3_7
- python-3_8
- python-3_9
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.7.7
3.9.0
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PIP = pip3
PYTHON = python3

.PHONY = help ci-lint coverage lint pre-lint setup test
.DEFAULT_GOAL = help

help:
@echo "---------------HELP-----------------"
@echo "To check the project coverage type make coverage"
@echo "To lint the project type make lint"
@echo "To setup the project type make setup"
@echo "To test the project type make test"
@echo "------------------------------------"

coverage:
${PIP} install coverage
coverage run setup.py test

ci-lint: pre-lint lint

pre-lint:
${PIP} install pylint
${PIP} install setuptools-lint
${PIP} install --upgrade pep8
${PIP} install --upgrade autopep8

lint:
${PYTHON} setup.py lint
autopep8 --in-place -r castle

setup:
${PYTHON} setup.py install

test:
${PYTHON} setup.py test
1 change: 1 addition & 0 deletions castle/apis/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

HTTPS_SCHEME = 'https'


class ApisRequest(object):
def __init__(self, headers=None):
self.headers = headers or dict()
Expand Down
12 changes: 6 additions & 6 deletions castle/client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import warnings
from castle.configuration import configuration
from castle.api import Api
from castle.context.default import ContextDefault
from castle.context.merger import ContextMerger
from castle.context.get_default import ContextGetDefault
from castle.context.merge import ContextMerge
from castle.commands.authenticate import CommandsAuthenticate
from castle.commands.identify import CommandsIdentify
from castle.commands.impersonate import CommandsImpersonate
from castle.commands.track import CommandsTrack
from castle.exceptions import InternalServerError, RequestError, ImpersonationFailed
from castle.failover_response import FailoverResponse
from castle.utils import timestamp as generate_timestamp
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp


class Client(object):
Expand All @@ -27,15 +27,15 @@ def from_request(cls, request, options=None):
def to_context(request, options=None):
if options is None:
options = {}
default_context = ContextDefault(
default_context = ContextGetDefault(
request, options.get('cookies')).call()
return ContextMerger.call(default_context, options.get('context', {}))
return ContextMerge.call(default_context, options.get('context', {}))

@staticmethod
def to_options(options=None):
if options is None:
options = {}
options.setdefault('timestamp', generate_timestamp())
options.setdefault('timestamp', generate_timestamp.call())
if 'traits' in options:
warnings.warn('use user_traits instead of traits key', DeprecationWarning)

Expand Down
12 changes: 6 additions & 6 deletions castle/commands/authenticate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from castle.command import Command
from castle.utils import timestamp
from castle.context.merger import ContextMerger
from castle.context.sanitizer import ContextSanitizer
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp
from castle.context.merge import ContextMerge
from castle.context.sanitize import ContextSanitize
from castle.validators.present import ValidatorsPresent


Expand All @@ -11,10 +11,10 @@ def __init__(self, context):

def build(self, options):
ValidatorsPresent.call(options, 'event')
context = ContextMerger.call(self.context, options.get('context'))
context = ContextSanitizer.call(context)
context = ContextMerge.call(self.context, options.get('context'))
context = ContextSanitize.call(context)
if context:
options.update({'context': context})
options.update({'sent_at': timestamp()})
options.update({'sent_at': generate_timestamp.call()})

return Command(method='post', path='authenticate', data=options)
12 changes: 6 additions & 6 deletions castle/commands/identify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from castle.command import Command
from castle.utils import timestamp
from castle.context.merger import ContextMerger
from castle.context.sanitizer import ContextSanitizer
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp
from castle.context.merge import ContextMerge
from castle.context.sanitize import ContextSanitize
from castle.validators.not_supported import ValidatorsNotSupported


Expand All @@ -11,10 +11,10 @@ def __init__(self, context):

def build(self, options):
ValidatorsNotSupported.call(options, 'properties')
context = ContextMerger.call(self.context, options.get('context'))
context = ContextSanitizer.call(context)
context = ContextMerge.call(self.context, options.get('context'))
context = ContextSanitize.call(context)
if context:
options.update({'context': context})
options.update({'sent_at': timestamp()})
options.update({'sent_at': generate_timestamp.call()})

return Command(method='post', path='identify', data=options)
12 changes: 6 additions & 6 deletions castle/commands/impersonate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from castle.command import Command
from castle.utils import timestamp
from castle.context.merger import ContextMerger
from castle.context.sanitizer import ContextSanitizer
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp
from castle.context.merge import ContextMerge
from castle.context.sanitize import ContextSanitize
from castle.validators.present import ValidatorsPresent


Expand All @@ -12,13 +12,13 @@ def __init__(self, context):
def build(self, options):
ValidatorsPresent.call(options, 'user_id')

context = ContextMerger.call(self.context, options.get('context'))
context = ContextSanitizer.call(context)
context = ContextMerge.call(self.context, options.get('context'))
context = ContextSanitize.call(context)
ValidatorsPresent.call(context, 'user_agent', 'ip')

if context:
options.update({'context': context})
options.update({'sent_at': timestamp()})
options.update({'sent_at': generate_timestamp.call()})

method = ('delete' if options.get('reset', False) else 'post')

Expand Down
12 changes: 6 additions & 6 deletions castle/commands/track.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from castle.command import Command
from castle.utils import timestamp
from castle.context.merger import ContextMerger
from castle.context.sanitizer import ContextSanitizer
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp
from castle.context.merge import ContextMerge
from castle.context.sanitize import ContextSanitize
from castle.validators.present import ValidatorsPresent


Expand All @@ -11,10 +11,10 @@ def __init__(self, context):

def build(self, options):
ValidatorsPresent.call(options, 'event')
context = ContextMerger.call(self.context, options.get('context'))
context = ContextSanitizer.call(context)
context = ContextMerge.call(self.context, options.get('context'))
context = ContextSanitize.call(context)
if context:
options.update({'context': context})
options.update({'sent_at': timestamp()})
options.update({'sent_at': generate_timestamp.call()})

return Command(method='post', path='track', data=options)
1 change: 1 addition & 0 deletions castle/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
\Aunix\Z|
\Aunix:"""]


class Configuration(object):
def __init__(self):
self.request_timeout = REQUEST_TIMEOUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__version__ = VERSION


class ContextDefault(object):
class ContextGetDefault(object):
def __init__(self, request, cookies):
self.cookies = self._fetch_cookies(request, cookies)
self.pre_headers = HeadersFilter(request).call()
Expand Down
11 changes: 11 additions & 0 deletions castle/context/merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from castle.utils.merge import UtilsMerge
from castle.utils.clone import UtilsClone


class ContextMerge(object):

@staticmethod
def call(initial_context, request_context):
source_copy = UtilsClone.call(initial_context)
UtilsMerge.call(source_copy, request_context)
return source_copy
10 changes: 0 additions & 10 deletions castle/context/merger.py

This file was deleted.

2 changes: 1 addition & 1 deletion castle/context/sanitizer.py → castle/context/sanitize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ContextSanitizer(object):
class ContextSanitize(object):

@classmethod
def call(cls, context):
Expand Down
10 changes: 6 additions & 4 deletions castle/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
'castle.test.api_test',
'castle.test.client_test',
'castle.test.configuration_test',
'castle.test.context.default_test',
'castle.test.context.merger_test',
'castle.test.context.sanitizer_test',
'castle.test.context.get_default_test',
'castle.test.context.merge_test',
'castle.test.context.sanitize_test',
'castle.test.command_test',
'castle.test.commands.authenticate_test',
'castle.test.commands.identify_test',
Expand All @@ -29,7 +29,9 @@
'castle.test.secure_mode_test',
'castle.test.validators.not_supported_test',
'castle.test.validators.present_test',
'castle.test.utils_test'
'castle.test.utils.clone_test',
'castle.test.utils.merge_test',
'castle.test.utils.timestamp_test',
]

# pylint: disable=redefined-builtin
Expand Down
2 changes: 1 addition & 1 deletion castle/test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def request():
class ClientTestCase(unittest.TestCase):
def setUp(self):
# patch timestamp to return a known value
timestamp_patcher = mock.patch('castle.client.generate_timestamp')
timestamp_patcher = mock.patch('castle.client.generate_timestamp.call')
self.mock_timestamp = timestamp_patcher.start()
self.mock_timestamp.return_value = '2018-01-02T03:04:05.678'
self.addCleanup(timestamp_patcher.stop)
Expand Down
6 changes: 3 additions & 3 deletions castle/test/commands/authenticate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from castle.command import Command
from castle.commands.authenticate import CommandsAuthenticate
from castle.exceptions import InvalidParametersError
from castle.utils import clone
from castle.utils.clone import UtilsClone


def default_options():
Expand Down Expand Up @@ -31,7 +31,7 @@ class CommandsAuthenticateTestCase(unittest.TestCase):
def setUp(self):
# patch timestamp to return a known value
timestamp_patcher = mock.patch(
'castle.commands.authenticate.timestamp')
'castle.commands.authenticate.generate_timestamp.call')
self.mock_timestamp = timestamp_patcher.start()
self.mock_timestamp.return_value = mock.sentinel.timestamp
self.addCleanup(timestamp_patcher.stop)
Expand All @@ -46,7 +46,7 @@ def test_build(self):
options = default_options_plus(context={'spam': True})

# expect the original context to have been merged with the context specified in the options
expected_data = clone(options)
expected_data = UtilsClone.call(options)
expected_data.update(context={'test': '1', 'spam': True})
expected = default_command_with_data(**expected_data)

Expand Down
6 changes: 3 additions & 3 deletions castle/test/commands/identify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from castle.command import Command
from castle.commands.identify import CommandsIdentify
from castle.exceptions import InvalidParametersError
from castle.utils import clone
from castle.utils.clone import UtilsClone


def default_options():
Expand All @@ -29,7 +29,7 @@ def default_command_with_data(**data):
class CommandsIdentifyTestCase(unittest.TestCase):
def setUp(self):
# patch timestamp to return a known value
timestamp_patcher = mock.patch('castle.commands.identify.timestamp')
timestamp_patcher = mock.patch('castle.commands.identify.generate_timestamp.call')
self.mock_timestamp = timestamp_patcher.start()
self.mock_timestamp.return_value = mock.sentinel.timestamp
self.addCleanup(timestamp_patcher.stop)
Expand All @@ -44,7 +44,7 @@ def test_build(self):
options = default_options_plus(context={'color': 'blue'})

# expect the original context to have been merged with the context specified in the options
expected_data = clone(options)
expected_data = UtilsClone.call(options)
expected_data.update(context={'test': '1', 'color': 'blue'})
expected = default_command_with_data(**expected_data)

Expand Down
Loading