Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics): added vouchers reports handler in analytics
- Loading branch information
1 parent
2095f5c
commit 3afabd0
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import * as dotenv from 'dotenv' | ||
import axios from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
dotenv.config() | ||
import { TillhubClient, v0 } from '../../src/tillhub-js' | ||
|
||
let user = { | ||
username: 'test@example.com', | ||
password: '12345678', | ||
clientAccount: 'someuuid', | ||
apiKey: '12345678' | ||
} | ||
|
||
if (process.env.SYSTEM_TEST) { | ||
user.username = process.env.SYSTEM_TEST_USERNAME || user.username | ||
user.password = process.env.SYSTEM_TEST_PASSWORD || user.password | ||
user.clientAccount = process.env.SYSTEM_TEST_CLIENT_ACCOUNT_ID || user.clientAccount | ||
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey | ||
} | ||
|
||
const legacyId = '4564' | ||
|
||
const mock = new MockAdapter(axios) | ||
afterEach(() => { | ||
mock.reset() | ||
}) | ||
|
||
describe('v0: Analytics: reports gets all vouchers', () => { | ||
it("Tillhub's Analytics are instantiable", async () => { | ||
if (process.env.SYSTEM_TEST !== 'true') { | ||
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function(config) { | ||
return [ | ||
200, | ||
{ | ||
token: '', | ||
user: { | ||
id: '123', | ||
legacy_id: legacyId | ||
} | ||
} | ||
] | ||
}) | ||
|
||
mock | ||
.onGet(`https://api.tillhub.com/api/v0/analytics/${legacyId}/reports/vouchers`) | ||
.reply(function(config) { | ||
return [ | ||
200, | ||
{ | ||
count: 1, | ||
results: [{}] | ||
} | ||
] | ||
}) | ||
} | ||
|
||
const options = { | ||
credentials: { | ||
username: user.username, | ||
password: user.password | ||
}, | ||
base: process.env.TILLHUB_BASE | ||
} | ||
|
||
const th = new TillhubClient() | ||
|
||
th.init(options) | ||
await th.auth.loginUsername({ | ||
username: user.username, | ||
password: user.password | ||
}) | ||
|
||
const analytics = th.analytics() | ||
|
||
expect(analytics).toBeInstanceOf(v0.Analytics) | ||
|
||
const { data } = await analytics.getVouchersReports() | ||
|
||
expect(Array.isArray(data)).toBe(true) | ||
}) | ||
|
||
it('rejects on status codes that are not 200', async () => { | ||
if (process.env.SYSTEM_TEST !== 'true') { | ||
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function(config) { | ||
return [ | ||
200, | ||
{ | ||
token: '', | ||
user: { | ||
id: '123', | ||
legacy_id: legacyId | ||
} | ||
} | ||
] | ||
}) | ||
|
||
mock | ||
.onGet(`https://api.tillhub.com/api/v0/analytics/${legacyId}/reports/vouchers`) | ||
.reply(function(config) { | ||
return [205] | ||
}) | ||
} | ||
|
||
const options = { | ||
credentials: { | ||
username: user.username, | ||
password: user.password | ||
}, | ||
base: process.env.TILLHUB_BASE | ||
} | ||
|
||
const th = new TillhubClient() | ||
|
||
th.init(options) | ||
await th.auth.loginUsername({ | ||
username: user.username, | ||
password: user.password | ||
}) | ||
|
||
try { | ||
await th.analytics().getVouchersReports() | ||
} catch (err) { | ||
expect(err.name).toBe('VouchersReportFetchFailed') | ||
} | ||
}) | ||
}) |