-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhttpAuthenticator.test.ts
65 lines (58 loc) · 2.14 KB
/
httpAuthenticator.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import axios, { AxiosError, HttpStatusCode } from 'axios';
import HttpAuthenticator from '../src/httpAuthenticator';
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
describe('Testing HTTP Authenticator', () => {
function makeAxiosError(code: HttpStatusCode): AxiosError {
return {
message: 'Test message',
code: '',
config: { headers: {} },
request: undefined,
response: { status: code },
} as AxiosError;
}
test('reauthenticates for 401 requests', async () => {
mockedAxios.post.mockResolvedValueOnce({
headers: {
// MindsDB Cloud login always returns a session cookie in response headers.
'set-cookie': ['session=new-session; Domain=.mindsdb.com; Path=/'],
},
});
const authenticator = new HttpAuthenticator();
authenticator.session = 'old-session';
const unauthError = makeAxiosError(HttpStatusCode.Unauthorized);
const reauthenticated = await authenticator.handleReauthentication(
mockedAxios,
unauthError
);
expect(reauthenticated).toBe(true);
});
test('reauthenticates for 403 requests', async () => {
mockedAxios.post.mockResolvedValueOnce({
headers: {
// MindsDB Cloud login always returns a session cookie in response headers.
'set-cookie': ['session=new-session; Domain=.mindsdb.com; Path=/'],
},
});
const authenticator = new HttpAuthenticator();
authenticator.session = 'old-session';
const unauthError = makeAxiosError(HttpStatusCode.Forbidden);
const reauthenticated = await authenticator.handleReauthentication(
mockedAxios,
unauthError
);
expect(reauthenticated).toBe(true);
expect(authenticator.session).toEqual('new-session');
});
test('does not reauthenticate for irrelevant errors', async () => {
const authenticator = new HttpAuthenticator();
authenticator.session = 'old-session';
const internalError = makeAxiosError(HttpStatusCode.InternalServerError);
const reauthenticated = await authenticator.handleReauthentication(
mockedAxios,
internalError
);
expect(reauthenticated).toBe(false);
});
});