-
Notifications
You must be signed in to change notification settings - Fork 14
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
Alexey Zorkaltsev
authored and
Alexey Zorkaltsev
committed
Jul 25, 2023
1 parent
7ac6ff1
commit f9df471
Showing
2 changed files
with
203 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import axios from 'axios' | ||
import {MetadataTokenService} from './metadata-token-service' | ||
import Mock = jest.Mock; | ||
|
||
describe('metadata-token-service', () => { | ||
|
||
let oldGet = axios.get; | ||
|
||
beforeEach(() => { | ||
axios.get = jest.fn(); | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
axios.get = oldGet; | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('simple scenario', async () => { | ||
|
||
let metadataTokenService = new MetadataTokenService(); | ||
|
||
// set token | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 200, | ||
data: { | ||
access_token: '123' | ||
} | ||
}); | ||
|
||
// first time | ||
const t1 = await metadataTokenService.getToken(); | ||
|
||
expect(t1).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(1); | ||
|
||
// second time - no extra token request | ||
const t2 = await metadataTokenService.getToken(); | ||
|
||
expect(t2).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(1); | ||
}); | ||
|
||
it('provider works over few hours, so token expected to get updated every hour', async () => { | ||
|
||
let metadataTokenService = new MetadataTokenService(); | ||
|
||
// set token | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 200, | ||
data: { | ||
access_token: '123' | ||
} | ||
}); | ||
|
||
// first time | ||
let t = await metadataTokenService.getToken(); | ||
expect(t).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(1); | ||
|
||
// change token | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 200, | ||
data: { | ||
access_token: '456' | ||
} | ||
}); | ||
|
||
// after 30 minutes | ||
jest.advanceTimersByTime(30 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(1); | ||
|
||
// after 1 hour | ||
jest.advanceTimersByTime(30 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('456'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(2); | ||
|
||
// change token | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 200, | ||
data: { | ||
access_token: '789' | ||
} | ||
}); | ||
|
||
// after 1 hour 30 minutes | ||
jest.advanceTimersByTime(30 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('456'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(2); | ||
|
||
// after 2 hours | ||
jest.advanceTimersByTime(30 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('789'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(3); | ||
|
||
// after 2 hours 30 minutes | ||
jest.advanceTimersByTime(30 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('789'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(3); | ||
|
||
// after 2 hours 59 minutes | ||
jest.advanceTimersByTime(29 * 60 * 1000); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('789'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(3); | ||
}); | ||
|
||
it('Iam always returns an error', async () => { | ||
|
||
let metadataTokenService = new MetadataTokenService(); | ||
|
||
// return an error | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 400, | ||
}); | ||
|
||
await expect(() => metadataTokenService.getToken()).rejects.toThrow(); | ||
}); | ||
|
||
it('Iam occasionally returns an error', async () => { | ||
|
||
let metadataTokenService = new MetadataTokenService(); | ||
|
||
// return token on 4th attempt - tests initialize() | ||
const nextResp = (function*() { | ||
|
||
for (let i = 0; i < 3; i++) { | ||
yield { | ||
status: 400, | ||
}; | ||
} | ||
|
||
return { | ||
status: 200, | ||
data: { | ||
access_token: '123' | ||
} | ||
}; | ||
})(); | ||
|
||
(axios.get as Mock).mockImplementation(() => nextResp.next().value); | ||
|
||
// first time - return token, even if it was returned only on 4th attempt | ||
let t = await metadataTokenService.getToken(); | ||
expect(t).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(4); | ||
|
||
// after 1 hour, return on an error use old token and make only one attempt to get token | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 400, | ||
}); | ||
|
||
// return an error | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 400, | ||
}); | ||
|
||
// after 1 hour, | ||
jest.advanceTimersByTime(60 * 60 * 1000); | ||
|
||
// Iam returns an error on 1st attempt, so we use the old token | ||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('123'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(5); | ||
|
||
// on next attempt we receive new token, and use this one | ||
(axios.get as Mock).mockReturnValue({ | ||
status: 200, | ||
data: { | ||
access_token: '456' | ||
} | ||
}); | ||
|
||
t = await metadataTokenService.getToken(); | ||
expect(t).toBe('456'); | ||
expect((axios.get as Mock).mock.calls).toHaveLength(6); | ||
}); | ||
}); |
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