Skip to content

Commit

Permalink
feat!: remove timestamp from register_feedback
Browse files Browse the repository at this point in the history
This is a deprecated field, should use occurred_at instead.
  • Loading branch information
figueredo authored and aninhalbuquerque committed Sep 20, 2024
1 parent 6233669 commit fcf0d5a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 47 deletions.
7 changes: 1 addition & 6 deletions incognia/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime as dt
from typing import Optional, List

from .datetime_util import total_milliseconds_since_epoch, has_timezone
from .datetime_util import has_timezone
from .endpoints import Endpoints
from .exceptions import IncogniaHTTPError, IncogniaError
from .json_util import encode
Expand Down Expand Up @@ -51,7 +51,6 @@ def register_new_signup(self,

def register_feedback(self,
event: str,
timestamp: dt.datetime = None,
external_id: Optional[str] = None,
login_id: Optional[str] = None,
payment_id: Optional[str] = None,
Expand All @@ -63,8 +62,6 @@ def register_feedback(self,
expires_at: dt.datetime = None) -> None:
if not event:
raise IncogniaError('event is required.')
if timestamp is not None and not has_timezone(timestamp):
raise IncogniaError('timestamp must have timezone')
if occurred_at is not None and not has_timezone(occurred_at):
raise IncogniaError('occurred_at must have timezone')
if expires_at is not None and not has_timezone(expires_at):
Expand All @@ -83,8 +80,6 @@ def register_feedback(self,
'installation_id': installation_id,
'request_token': request_token
}
if timestamp is not None:
body['timestamp'] = total_milliseconds_since_epoch(timestamp)
if occurred_at is not None:
body['occurred_at'] = occurred_at.isoformat()
if expires_at is not None:
Expand Down
4 changes: 0 additions & 4 deletions incognia/datetime_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import datetime as dt


def total_milliseconds_since_epoch(t: dt.datetime) -> int:
return int((t - dt.datetime.fromtimestamp(0, dt.timezone.utc)).total_seconds() * 1000.0)


def has_timezone(d: dt.datetime) -> bool:
return d.tzinfo is not None and d.tzinfo.utcoffset(d) is not None
37 changes: 0 additions & 37 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class TestIncogniaAPI(TestCase):
OK_STATUS_CODE: Final[int] = 200
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.now(dt.timezone.utc)
TIMESTAMP_WITHOUT_TIMEZONE: Final[dt.datetime] = dt.datetime.now()
LOGIN_ID: Final[str] = 'ANY_LOGIN_ID'
Expand All @@ -89,14 +88,9 @@ class TestIncogniaAPI(TestCase):
'account_id': f'{ACCOUNT_ID}',
'installation_id': f'{INSTALLATION_ID}',
'request_token': f'{REQUEST_TOKEN}',
'timestamp': int((
TIMESTAMP - dt.datetime.fromtimestamp(0, dt.timezone.utc)).total_seconds() * 1000.0),
'occurred_at': TIMESTAMP.isoformat(),
'expires_at': TIMESTAMP.isoformat(),
})
REGISTER_INVALID_FEEDBACK_DATA: Final[bytes] = encode({
'event': f'{INVALID_EVENT_FEEDBACK_TYPE}'
})
REGISTER_VALID_PAYMENT_DATA: Final[bytes] = encode({
'type': 'payment',
'request_token': f'{REQUEST_TOKEN}',
Expand Down Expand Up @@ -222,7 +216,6 @@ def test_register_feedback_when_all_fields_are_valid_should_work(
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

api.register_feedback(self.VALID_EVENT_FEEDBACK_TYPE,
timestamp=self.TIMESTAMP,
occurred_at=self.TIMESTAMP,
expires_at=self.TIMESTAMP,
external_id=self.EXTERNAL_ID,
Expand All @@ -249,20 +242,6 @@ def test_register_feedback_when_event_is_empty_should_raise_an_IncogniaError(
mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_feedback_when_timestamp_does_not_have_timezone_should_raise_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=self.TIMESTAMP_WITHOUT_TIMEZONE)

mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_feedback_when_occurred_at_does_not_have_timezone_should_raise_IncogniaError(
Expand Down Expand Up @@ -291,22 +270,6 @@ def test_register_feedback_when_expires_at_does_not_have_timezone_should_raise_I
mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_feedback_when_required_fields_are_invalid_should_raise_an_IncogniaHTTPError(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
mock_base_request_post.configure_mock(side_effect=IncogniaHTTPError)

api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

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

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_INVALID_FEEDBACK_DATA)

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_payment_when_required_fields_are_valid_should_work(
Expand Down

0 comments on commit fcf0d5a

Please sign in to comment.