Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(points): add required request headers for trackPointsEvent call #5378

Merged
merged 7 commits into from
May 16, 2024
30 changes: 28 additions & 2 deletions src/points/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { FetchMock } from 'jest-fetch-mock/types'
import { expectSaga } from 'redux-saga-test-plan'
import * as matchers from 'redux-saga-test-plan/matchers'
import { throwError } from 'redux-saga-test-plan/providers'
import { select } from 'redux-saga/effects'
import { call, select } from 'redux-saga/effects'
import { Actions as AppActions } from 'src/app/actions'
import { retrieveSignedMessage } from 'src/pincode/authentication'
import * as pointsSaga from 'src/points/saga'
import {
fetchHistory,
Expand Down Expand Up @@ -33,10 +34,11 @@ import pointsReducer, {
sendPointsEventStarted,
trackPointsEvent,
} from 'src/points/slice'
import { ClaimHistory, GetHistoryResponse } from 'src/points/types'
import { ClaimHistory, GetHistoryResponse, PointsEvent } from 'src/points/types'
import Logger from 'src/utils/Logger'
import * as fetchWithTimeout from 'src/utils/fetchWithTimeout'
import networkConfig from 'src/web3/networkConfig'
import { walletAddressSelector } from 'src/web3/selectors'
import { createMockStore } from 'test/utils'
import { mockAccount } from 'test/values'
import { v4 as uuidv4 } from 'uuid'
Expand All @@ -45,6 +47,7 @@ jest.mock('src/statsig')

jest.mock('uuid')
jest.mock('src/utils/Logger')
jest.unmock('src/pincode/authentication')

const MOCK_HISTORY_RESPONSE: GetHistoryResponse = {
data: [
Expand Down Expand Up @@ -567,3 +570,26 @@ describe('watchAppMounted', () => {
expect(mockSendPendingPointsEvents).toHaveBeenCalledTimes(1)
})
})

describe('fetchTrackPointsEventsEndpoint', () => {
it('should call fetch with correct params', async () => {
const mockEvent: PointsEvent = { activityId: 'create-wallet' }
mockFetch.mockResponseOnce(JSON.stringify({ ok: true }))

await expectSaga(fetchTrackPointsEventsEndpoint, mockEvent)
.provide([
[select(walletAddressSelector), mockAccount],
[call(retrieveSignedMessage), 'someSignedMessage'],
])
.run()

expect(fetchWithTimeoutSpy).toHaveBeenCalledWith(networkConfig.trackPointsEventUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Valora ${mockAccount}:someSignedMessage`,
},
body: JSON.stringify(mockEvent),
})
})
})
12 changes: 10 additions & 2 deletions src/points/saga.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { differenceInDays } from 'date-fns'
import { Actions as AppActions } from 'src/app/actions'
import { Actions as HomeActions } from 'src/home/actions'
import { retrieveSignedMessage } from 'src/pincode/authentication'
import {
nextPageUrlSelector,
pendingPointsEvents,
Expand Down Expand Up @@ -51,8 +52,8 @@

const address = yield* select(walletAddressSelector)
if (!address) {
Logger.error(TAG, 'No wallet address found when fetching points balance')
return

Check warning on line 56 in src/points/saga.ts

View check run for this annotation

Codecov / codecov/patch

src/points/saga.ts#L55-L56

Added lines #L55 - L56 were not covered by tests
}

try {
Expand Down Expand Up @@ -171,9 +172,16 @@
}
}

export async function fetchTrackPointsEventsEndpoint(event: PointsEvent) {
return fetchWithTimeout(networkConfig.trackPointsEventUrl, {
export function* fetchTrackPointsEventsEndpoint(event: PointsEvent) {
const address = yield* select(walletAddressSelector)
const signedMessage = yield* call(retrieveSignedMessage)

return yield* call(fetchWithTimeout, networkConfig.trackPointsEventUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Valora ${address}:${signedMessage}`,
},
body: JSON.stringify(event),
})
}
Expand Down Expand Up @@ -243,7 +251,7 @@

function* watchGetHistory() {
yield* takeLeading(getHistoryStarted.type, safely(getHistory))
yield* takeLeading(getHistoryStarted.type, safely(getPointsBalance))

Check warning on line 254 in src/points/saga.ts

View check run for this annotation

Codecov / codecov/patch

src/points/saga.ts#L254

Added line #L254 was not covered by tests
}

function* watchGetConfig() {
Expand All @@ -260,10 +268,10 @@
}

export function* pointsSaga() {
const showPoints = getFeatureGate(StatsigFeatureGates.SHOW_POINTS)

Check warning on line 271 in src/points/saga.ts

View check run for this annotation

Codecov / codecov/patch

src/points/saga.ts#L271

Added line #L271 was not covered by tests
if (!showPoints) {
Logger.info(TAG, 'Points feature is disabled, not spawning points sagas')
return

Check warning on line 274 in src/points/saga.ts

View check run for this annotation

Codecov / codecov/patch

src/points/saga.ts#L273-L274

Added lines #L273 - L274 were not covered by tests
}

yield* spawn(watchGetHistory)
Expand Down
Loading