Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dabc79a
commit 5a9c8b2
Showing
3 changed files
with
156 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,125 @@ | ||
import * as dotenv from 'dotenv' | ||
import axios from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
dotenv.config() | ||
import { TillhubClient, v1 } 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' | ||
|
||
describe('v1: Balances: can get count number of all balances', () => { | ||
const mock = new MockAdapter(axios) | ||
afterEach(() => { | ||
mock.reset() | ||
}) | ||
it("Tillhub's balances 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/v1/balances/${legacyId}/meta`) | ||
.reply(function (config) { | ||
return [ | ||
200, | ||
{ | ||
count: 50, | ||
results: [{ count: 50 }] | ||
} | ||
] | ||
}) | ||
} | ||
|
||
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 balances = th.balances() | ||
|
||
expect(balances).toBeInstanceOf(v1.Balances) | ||
|
||
const { data } = await balances.meta() | ||
|
||
expect(data).toEqual({ count: 50 }) | ||
}) | ||
|
||
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/v1/balances/${legacyId}/meta`) | ||
.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.balances().meta() | ||
} catch (err) { | ||
expect(err.name).toBe('BalancesMetaFailed') | ||
} | ||
}) | ||
}) |