-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathauthentication.test.js
119 lines (105 loc) · 3.96 KB
/
authentication.test.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import path from 'node:path';
import process from 'node:process';
import dotenv from 'dotenv';
import {back as nockBack} from 'nock';
import {beforeAll, describe, expect, it} from 'vitest';
import {initializeClient} from './setup.js';
dotenv.config();
const username = process.env.ZENDESK_USERNAME;
const password = process.env.ZENDESK_PASSWORD;
const token = process.env.ZENDESK_TOKEN;
const oauthAccessToken = process.env.ZENDESK_OAUTH_ACCESS_TOKEN;
const TEST_USER = process.env.ZENDESK_FULL_NAME;
const endpointUri = process.env.ZENDESK_ENDPOINT_URI;
describe('Zendesk Client Authentication', () => {
beforeAll(async () => {
nockBack.setMode('record');
nockBack.fixtures = path.join(__dirname, '/fixtures');
});
const setupClient = initializeClient;
/**
* Verifies the authenticated user's name.
* @param {import('../src/index.js').ZendeskClient} client - The Zendesk client.
* @param {string} expectedName - The expected name of the authenticated user.
* @returns {Promise<void>}
*/
const verifyUser = async (client, expectedName) => {
const {result: user} = await client.users.me();
expect(user.name).toBe(expectedName);
};
it('should authenticate an anonymous user without any credentials(fail condition)', async () => {
const {nockDone} = await nockBack('authentication_test_no_creds.json');
const client = setupClient({});
await verifyUser(client, 'Anonymous user');
nockDone();
});
it('should fail authentication with an incorrect username and token combination', async () => {
const {nockDone} = await nockBack(
'authentication_test_incorrect_token.json',
);
const client = setupClient({token: 'incorrectToken'});
await verifyUser(client, 'Anonymous user');
nockDone();
});
it('should fail authentication with an incorrect username and password combination', async () => {
const {nockDone} = await nockBack('authentication_test_incorrect_u_p.json');
const client = setupClient({password: 'incorrectPassword'});
await verifyUser(client, 'Anonymous user');
nockDone();
});
it('should throw an error for an invalid subdomain', async () => {
const {nockDone} = await nockBack(
'authentication_test_incorrect_subdomain.json',
);
const client = initializeClient({
username,
token,
subdomain: 'invalidSubdomain',
});
await expect(() => client.users.me()).rejects.toThrowError(
'Item not found',
);
nockDone();
});
it('should authenticate a user using a provided username and token', async () => {
const {nockDone} = await nockBack(
'authentication_test_correct_u_token.json',
);
const client = setupClient({token});
await verifyUser(client, TEST_USER);
nockDone();
});
it('should authenticate a user using a provided username and password', async () => {
const {nockDone} = await nockBack('authentication_test_user_pass.json');
const client = setupClient({password});
await verifyUser(client, TEST_USER);
nockDone();
});
it('should throw an error when trying OAuth authentication without a token', async () => {
const {nockDone} = await nockBack(
'authentication_test_incorrect_sans_token.json',
);
const client = setupClient({useOAuth: true});
await expect(() => client.users.me()).rejects.toThrowError(
'token is missing',
);
nockDone();
});
it('should authenticate a user using OAuth with a valid token', async () => {
const {nockDone} = await nockBack(
'authentication_test_correct_oauthtoken.json',
);
const client = setupClient({
token: oauthAccessToken,
useOAuth: true,
});
await verifyUser(client, TEST_USER);
nockDone();
});
it('should authenticate a user with a valid endpoint', async () => {
const {nockDone} = await nockBack('authentication_test_endpoint_uri.json');
const client = setupClient({endpointUri, token, subdomain: undefined});
await verifyUser(client, TEST_USER);
nockDone();
});
});