Skip to content

Commit

Permalink
fix: remove timestamp restriction on registerFeedback
Browse files Browse the repository at this point in the history
This field is optional.
  • Loading branch information
figueredo committed Jul 18, 2024
1 parent 8dc6718 commit 7552141
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
7 changes: 3 additions & 4 deletions incognia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def register_new_signup(self,

def register_feedback(self,
event: str,
timestamp: dt.datetime,
timestamp: dt.datetime = None,
external_id: Optional[str] = None,
login_id: Optional[str] = None,
payment_id: Optional[str] = None,
Expand All @@ -60,15 +60,12 @@ def register_feedback(self,
session_token: Optional[str] = None) -> None:
if not event:
raise IncogniaError('event is required.')
if not timestamp:
raise IncogniaError('timestamp is required.')

try:
headers = self.__get_authorization_header()
headers.update(JSON_CONTENT_HEADER)
body = {
'event': event,
'timestamp': total_milliseconds_since_epoch(timestamp),
'external_id': external_id,
'login_id': login_id,
'payment_id': payment_id,
Expand All @@ -77,6 +74,8 @@ def register_feedback(self,
'installation_id': installation_id,
'session_token': session_token
}
if timestamp is not None:
body['timestamp'] = total_milliseconds_since_epoch(timestamp)
data = encode(body)
return self.__request.post(Endpoints.FEEDBACKS, headers=headers, data=data)

Expand Down
47 changes: 33 additions & 14 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,25 @@ class TestIncogniaAPI(TestCase):
CLIENT_ERROR_CODE: Final[int] = 400
VALID_EVENT_FEEDBACK_TYPE: Final[str] = 'valid_event_feedback_type'
INVALID_EVENT_FEEDBACK_TYPE: Final[str] = 'invalid_event_feedback_type'
TIMESTAMP: Final[dt.datetime] = dt.datetime.utcfromtimestamp(0)
TIMESTAMP: Final[dt.datetime] = dt.datetime.now()
LOGIN_ID: Final[str] = 'ANY_LOGIN_ID'
PAYMENT_ID: Final[str] = 'ANY_PAYMENT_ID'
REGISTER_VALID_FEEDBACK_DATA: Final[bytes] = encode({
'event': f'{VALID_EVENT_FEEDBACK_TYPE}'
})
REGISTER_VALID_FEEDBACK_DATA_FULL: Final[bytes] = encode({
'event': f'{VALID_EVENT_FEEDBACK_TYPE}',
'timestamp': 0
'external_id': f'{EXTERNAL_ID}',
'login_id': f'{LOGIN_ID}',
'payment_id': f'{PAYMENT_ID}',
'signup_id': f'{SIGNUP_ID}',
'account_id': f'{ACCOUNT_ID}',
'installation_id': f'{INSTALLATION_ID}',
'session_token': f'{SESSION_TOKEN}',
'timestamp': int((TIMESTAMP - dt.datetime.utcfromtimestamp(0)).total_seconds() * 1000.0)
})
REGISTER_INVALID_FEEDBACK_DATA: Final[bytes] = encode({
'event': f'{INVALID_EVENT_FEEDBACK_TYPE}',
'timestamp': 0
'event': f'{INVALID_EVENT_FEEDBACK_TYPE}'
})
REGISTER_VALID_PAYMENT_DATA: Final[bytes] = encode({
'type': 'payment',
Expand Down Expand Up @@ -185,7 +196,7 @@ def test_register_feedback_when_required_fields_are_valid_should_work(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

api.register_feedback(self.VALID_EVENT_FEEDBACK_TYPE, self.TIMESTAMP)
api.register_feedback(self.VALID_EVENT_FEEDBACK_TYPE)

mock_token_manager_get.assert_called()
mock_base_request_post.assert_called_with(Endpoints.FEEDBACKS,
Expand All @@ -194,23 +205,32 @@ def test_register_feedback_when_required_fields_are_valid_should_work(

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_feedback_when_event_is_empty_should_raise_an_IncogniaError(
def test_register_feedback_when_all_fields_are_valid_should_work(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaError, api.register_feedback, event='', timestamp=self.TIMESTAMP)
api.register_feedback(self.VALID_EVENT_FEEDBACK_TYPE,
timestamp=self.TIMESTAMP,
external_id=self.EXTERNAL_ID,
login_id=self.LOGIN_ID,
payment_id=self.PAYMENT_ID,
signup_id=self.SIGNUP_ID,
account_id=self.ACCOUNT_ID,
installation_id=self.INSTALLATION_ID,
session_token=self.SESSION_TOKEN)

mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()
mock_token_manager_get.assert_called()
mock_base_request_post.assert_called_with(Endpoints.FEEDBACKS,
headers=self.AUTH_AND_JSON_CONTENT_HEADERS,
data=self.REGISTER_VALID_FEEDBACK_DATA_FULL)

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_feedback_when_timestamp_is_none_should_raise_an_IncogniaError(
def test_register_feedback_when_event_is_empty_should_raise_an_IncogniaError(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaError, api.register_feedback,
event=self.VALID_EVENT_FEEDBACK_TYPE, timestamp=None)
self.assertRaises(IncogniaError, api.register_feedback, event='')

mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()
Expand All @@ -224,8 +244,7 @@ def test_register_feedback_when_required_fields_are_invalid_should_raise_an_Inco
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaHTTPError, api.register_feedback,
event=self.INVALID_EVENT_FEEDBACK_TYPE,
timestamp=self.TIMESTAMP)
event=self.INVALID_EVENT_FEEDBACK_TYPE)

mock_token_manager_get.assert_called()
mock_base_request_post.assert_called_with(Endpoints.FEEDBACKS,
Expand Down

0 comments on commit 7552141

Please sign in to comment.