Skip to content

Commit b8feb7e

Browse files
committed
Fix PR #755: Restore missing Query type and add comprehensive tests
- Restore accidentally removed Query type definition - Fix linting errors with lexical declarations in switch cases - Add comprehensive unit tests for BasicAuth and OAuth2Auth classes - Update package.json to include unit test script - All tests now pass successfully
1 parent 40fccb8 commit b8feb7e

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
},
4949
"scripts": {
5050
"build": "tsc --project tsconfig.build.json",
51-
"test:it": "jest --testPathPatterns tests/it",
51+
"test:it": "jest --testPathPattern tests/it",
52+
"test:unit": "jest --testPathPattern tests/unit",
53+
"test": "jest",
5254
"test:lint": "eslint . --flag unstable_ts_config",
5355
"publish": "yarn build && yarn npm publish"
5456
}

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ export type QueryInfo = {
159159
failureInfo?: QueryFailureInfo;
160160
};
161161

162+
export type Query = {
163+
query: string;
164+
catalog?: string;
165+
schema?: string;
166+
user?: string;
167+
session?: Session;
168+
extraCredential?: ExtraCredential;
169+
extraHeaders?: RequestHeaders;
170+
};
171+
162172
/**
163173
* It takes a Headers object and returns a new object with the same keys, but only the values that are
164174
* truthy
@@ -204,15 +214,16 @@ class Client {
204214

205215
if (options.auth) {
206216
switch (options.auth.type) {
207-
case 'basic':
217+
case 'basic': {
208218
const basic: BasicAuth = <BasicAuth>options.auth;
209219
clientConfig.auth = {
210220
username: basic.username,
211221
password: basic.password ?? '',
212222
};
213223
headers[TRINO_USER_HEADER] = basic.username;
214224
break;
215-
case 'oauth2':
225+
}
226+
case 'oauth2': {
216227
const oauth2: OAuth2Auth = <OAuth2Auth>options.auth;
217228
headers['Authorization'] = `Bearer ${oauth2.token}`;
218229
if (oauth2.clientId) {
@@ -243,6 +254,7 @@ class Client {
243254
headers['Grant-Type'] = oauth2.grantType;
244255
}
245256
break;
257+
}
246258
default:
247259
throw new Error(`Unsupported auth type: ${options.auth.type}`);
248260
}

tests/unit/auth.spec.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {BasicAuth, OAuth2Auth} from '../../src';
2+
3+
describe('Auth Classes', () => {
4+
describe('BasicAuth', () => {
5+
test('should create BasicAuth with username only', () => {
6+
const auth = new BasicAuth('testuser');
7+
expect(auth.username).toBe('testuser');
8+
expect(auth.password).toBeUndefined();
9+
expect(auth.type).toBe('basic');
10+
});
11+
12+
test('should create BasicAuth with username and password', () => {
13+
const auth = new BasicAuth('testuser', 'testpass');
14+
expect(auth.username).toBe('testuser');
15+
expect(auth.password).toBe('testpass');
16+
expect(auth.type).toBe('basic');
17+
});
18+
});
19+
20+
describe('OAuth2Auth', () => {
21+
test('should create OAuth2Auth with token only', () => {
22+
const auth = new OAuth2Auth('test-token');
23+
expect(auth.token).toBe('test-token');
24+
expect(auth.type).toBe('oauth2');
25+
expect(auth.clientId).toBeUndefined();
26+
expect(auth.clientSecret).toBeUndefined();
27+
expect(auth.refreshToken).toBeUndefined();
28+
expect(auth.tokenEndpoint).toBeUndefined();
29+
expect(auth.scopes).toBeUndefined();
30+
expect(auth.tokenType).toBeUndefined();
31+
expect(auth.expiresIn).toBeUndefined();
32+
expect(auth.redirectUri).toBeUndefined();
33+
expect(auth.grantType).toBeUndefined();
34+
});
35+
36+
test('should create OAuth2Auth with all optional parameters', () => {
37+
const auth = new OAuth2Auth(
38+
'test-token',
39+
'client-id',
40+
'client-secret',
41+
'refresh-token',
42+
'https://example.com/oauth2/token',
43+
['read', 'write'],
44+
'Bearer',
45+
3600,
46+
'https://example.com/callback',
47+
'authorization_code'
48+
);
49+
50+
expect(auth.token).toBe('test-token');
51+
expect(auth.clientId).toBe('client-id');
52+
expect(auth.clientSecret).toBe('client-secret');
53+
expect(auth.refreshToken).toBe('refresh-token');
54+
expect(auth.tokenEndpoint).toBe('https://example.com/oauth2/token');
55+
expect(auth.scopes).toEqual(['read', 'write']);
56+
expect(auth.tokenType).toBe('Bearer');
57+
expect(auth.expiresIn).toBe(3600);
58+
expect(auth.redirectUri).toBe('https://example.com/callback');
59+
expect(auth.grantType).toBe('authorization_code');
60+
expect(auth.type).toBe('oauth2');
61+
});
62+
63+
test('should create OAuth2Auth with some optional parameters', () => {
64+
const auth = new OAuth2Auth(
65+
'test-token',
66+
'client-id',
67+
undefined,
68+
'refresh-token',
69+
undefined,
70+
['read']
71+
);
72+
73+
expect(auth.token).toBe('test-token');
74+
expect(auth.clientId).toBe('client-id');
75+
expect(auth.clientSecret).toBeUndefined();
76+
expect(auth.refreshToken).toBe('refresh-token');
77+
expect(auth.tokenEndpoint).toBeUndefined();
78+
expect(auth.scopes).toEqual(['read']);
79+
});
80+
});
81+
});

0 commit comments

Comments
 (0)