Skip to content

Commit

Permalink
refactor: use RFC3339 fields in requests (#53)
Browse files Browse the repository at this point in the history
Fields that received timestamp as Epoch milliseconds have been
replaced with other fields that receives date-time as RFC3339
strings. This commits makes use of the new fields.
  • Loading branch information
figueredo authored Jul 24, 2024
1 parent 82663a0 commit 384cde2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,11 @@ assessment, err := client.RegisterPayment(&incognia.Payment{
This method registers a feedback event for the given identifiers (represented in `FeedbackIdentifiers`) related to a signup, login or payment.
```go
timestamp := time.Now()
feedbackEvent := incognia.SignupAccepted
err := client.RegisterFeedback(feedbackEvent, &timestamp, &incognia.FeedbackIdentifiers{
InstallationID: "some-installation-id",
LoginID: "some-login-id",
PaymentID: "some-payment-id",
SignupID: "some-signup-id",
AccountID: "some-account-id",
ExternalID: "some-external-id",
occurred_at := time.Parse(time.RFC3339, "2024-07-22T15:20:00Z")
feedbackEvent := incognia.AccountTakeover
err := client.RegisterFeedback(feedbackEvent, &occurred_at, &incognia.FeedbackIdentifiers{
InstallationID: "some-installation-id",
AccountID: "some-account-id",
})
```
Expand Down
12 changes: 6 additions & 6 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,27 @@ func (c *Client) registerSignup(params *Signup) (ret *SignupAssessment, err erro
return &signupAssessment, nil
}

func (c *Client) RegisterFeedback(feedbackEvent FeedbackType, timestamp *time.Time, feedbackIdentifiers *FeedbackIdentifiers) (err error) {
func (c *Client) RegisterFeedback(feedbackEvent FeedbackType, occurredAt *time.Time, feedbackIdentifiers *FeedbackIdentifiers) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()

return c.registerFeedback(feedbackEvent, timestamp, feedbackIdentifiers)
return c.registerFeedback(feedbackEvent, occurredAt, feedbackIdentifiers)
}

func (c *Client) registerFeedback(feedbackEvent FeedbackType, timestamp *time.Time, feedbackIdentifiers *FeedbackIdentifiers) (err error) {
func (c *Client) registerFeedback(feedbackEvent FeedbackType, occurredAt *time.Time, feedbackIdentifiers *FeedbackIdentifiers) (err error) {
if !isValidFeedbackType(feedbackEvent) {
return ErrInvalidFeedbackType
}
if timestamp == nil {
if occurredAt == nil {
return ErrMissingTimestamp
}

requestBody := postFeedbackRequestBody{
Event: feedbackEvent,
Timestamp: timestamp.UnixNano() / 1000000,
Event: feedbackEvent,
OccurredAt: occurredAt,
}
if feedbackIdentifiers != nil {
requestBody.InstallationID = feedbackIdentifiers.InstallationID
Expand Down
57 changes: 31 additions & 26 deletions incognia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
)

var (
now = time.Now()
installationId = "installation-id"
sessionToken = "session-token"
shouldEval bool = true
Expand Down Expand Up @@ -108,7 +109,7 @@ var (
}
postFeedbackRequestBodyFixture = &postFeedbackRequestBody{
Event: SignupAccepted,
Timestamp: time.Now().UnixNano() / 1000000,
OccurredAt: &now,
InstallationID: "some-installation-id",
LoginID: "some-login-id",
PaymentID: "some-payment-id",
Expand All @@ -117,8 +118,8 @@ var (
ExternalID: "some-external-id",
}
postFeedbackRequestBodyRequiredFieldsFixture = &postFeedbackRequestBody{
Event: SignupAccepted,
Timestamp: time.Now().UnixNano() / 1000000,
Event: SignupAccepted,
OccurredAt: &now,
}
feedbackIdentifiersFixture = &FeedbackIdentifiers{
InstallationID: "some-installation-id",
Expand Down Expand Up @@ -457,8 +458,7 @@ func (suite *IncogniaTestSuite) TestManualRefreshTokenProviderErrorTokenNotFound
_, err = client.RegisterPayment(paymentFixture)
suite.EqualError(err, ErrTokenNotFound.Error())

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err = client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err = client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.EqualError(err, ErrTokenNotFound.Error())
}

Expand Down Expand Up @@ -492,8 +492,7 @@ func (suite *IncogniaTestSuite) TestManualRefreshTokenProviderSuccess() {

feedbackServer := suite.mockFeedbackEndpoint(token, postFeedbackRequestBodyFixture)
defer feedbackServer.Close()
timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err = client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err = client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.NoError(err)
}

Expand Down Expand Up @@ -630,50 +629,45 @@ func (suite *IncogniaTestSuite) TestSuccessRegisterFeedback() {
feedbackServer := suite.mockFeedbackEndpoint(token, postFeedbackRequestBodyFixture)
defer feedbackServer.Close()

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.NoError(err)
}

func (suite *IncogniaTestSuite) TestSuccessRegisterFeedbackNilOptional() {
feedbackServer := suite.mockFeedbackEndpoint(token, postFeedbackRequestBodyRequiredFieldsFixture)
defer feedbackServer.Close()

timestamp := time.Unix(0, postFeedbackRequestBodyRequiredFieldsFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback(postFeedbackRequestBodyRequiredFieldsFixture.Event, &timestamp, nil)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyRequiredFieldsFixture.Event, postFeedbackRequestBodyRequiredFieldsFixture.OccurredAt, nil)
suite.NoError(err)
}

func (suite *IncogniaTestSuite) TestSuccessRegisterFeedbackAfterTokenExpiration() {
feedbackServer := suite.mockFeedbackEndpoint(token, postFeedbackRequestBodyFixture)
defer feedbackServer.Close()

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.NoError(err)

token, _ := suite.client.tokenProvider.GetToken()
token.(*accessToken).ExpiresIn = 0

err = suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err = suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.NoError(err)
}

func (suite *IncogniaTestSuite) TestForbiddenRegisterFeedback() {
feedbackServer := suite.mockFeedbackEndpoint("some-other-token", postFeedbackRequestBodyFixture)
defer feedbackServer.Close()

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.EqualError(err, "403 Forbidden")
}

func (suite *IncogniaTestSuite) TestErrorRegisterFeedbackInvalidFeedbackType() {
feedbackServer := suite.mockFeedbackEndpoint(token, postFeedbackRequestBodyFixture)
defer feedbackServer.Close()

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback("invalid-type", &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback("invalid-type", postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.EqualError(err, ErrInvalidFeedbackType.Error())
}

Expand All @@ -686,14 +680,12 @@ func (suite *IncogniaTestSuite) TestErrorRegisterFeedbackNilTimestamp() {
}

func (suite *IncogniaTestSuite) TestErrorsRegisterFeedback() {
timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))

errors := []int{http.StatusBadRequest, http.StatusInternalServerError}
for _, status := range errors {
statusServer := mockStatusServer(status)
suite.client.endpoints.Feedback = statusServer.URL

err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.Contains(err.Error(), strconv.Itoa(status))
}
}
Expand Down Expand Up @@ -897,8 +889,7 @@ func (suite *IncogniaTestSuite) TestUnauthorizedTokenGeneration() {
suite.Nil(responseSignUp)
suite.EqualError(err, ErrInvalidCredentials.Error())

timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err = suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err = suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.EqualError(err, ErrInvalidCredentials.Error())
}

Expand All @@ -921,8 +912,7 @@ func (suite *IncogniaTestSuite) TestPanic() {
suite.client.tokenProvider = &PanickingTokenProvider{panicString: panicString}

suite.client.RegisterLogin(loginFixture)
timestamp := time.Unix(0, postFeedbackRequestBodyFixture.Timestamp*int64(1000000))
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, &timestamp, feedbackIdentifiersFixture)
err := suite.client.RegisterFeedback(postFeedbackRequestBodyFixture.Event, postFeedbackRequestBodyFixture.OccurredAt, feedbackIdentifiersFixture)
suite.Equal(err.Error(), panicString)
_, err = suite.client.RegisterSignup("some-installationId", addressFixture)
suite.Equal(err.Error(), panicString)
Expand All @@ -948,7 +938,7 @@ func (suite *IncogniaTestSuite) mockFeedbackEndpoint(expectedToken string, expec
var requestBody postFeedbackRequestBody
json.NewDecoder(r.Body).Decode(&requestBody)

if reflect.DeepEqual(&requestBody, expectedBody) {
if postFeedbackRequestBodyEqual(&requestBody, expectedBody) {
w.WriteHeader(http.StatusOK)
return
}
Expand Down Expand Up @@ -1108,3 +1098,18 @@ func mockTokenEndpoint(expectedToken string, expiresIn string) *httptest.Server

return tokenServer
}

func postFeedbackRequestBodyEqual(a, b *postFeedbackRequestBody) bool {
if a == nil || b == nil {
return a == b
}
aOccurredAt := a.OccurredAt
bOccurredAt := b.OccurredAt
aCopy := *a
aCopy.OccurredAt = nil
bCopy := *b
bCopy.OccurredAt = nil
return reflect.DeepEqual(aCopy, bCopy) &&
(aOccurredAt == nil && bOccurredAt == nil) ||
(aOccurredAt != nil && bOccurredAt != nil && aOccurredAt.Equal(*bOccurredAt))
}
4 changes: 3 additions & 1 deletion request_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package incognia

import "time"

type Coordinates struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
Expand Down Expand Up @@ -57,7 +59,7 @@ const (

type postFeedbackRequestBody struct {
Event FeedbackType `json:"event"`
Timestamp int64 `json:"timestamp"`
OccurredAt *time.Time `json:"occurred_at,omitempty"`
InstallationID string `json:"installation_id,omitempty"`
LoginID string `json:"login_id,omitempty"`
PaymentID string `json:"payment_id,omitempty"`
Expand Down

0 comments on commit 384cde2

Please sign in to comment.