Skip to content

Commit 20f65ec

Browse files
Jeyfeljflbr
authored andcommitted
Add comprehensive test suite for extension
- Implement 11 tests covering JWT decoding functionality - Add tests for setHoverContent function - Add tests for extension activation and command registration - Include edge case testing for invalid tokens, Bearer prefix, and special characters
1 parent a5e298c commit 20f65ec

1 file changed

Lines changed: 130 additions & 8 deletions

File tree

src/test/suite/extension.test.ts

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,140 @@
11
import * as assert from 'assert';
22
import { before } from 'mocha';
3-
4-
// You can import and use all API from the 'vscode' module
5-
// as well as import your extension to test it
63
import * as vscode from 'vscode';
7-
// import * as myExtension from '../extension';
4+
import { decodeToken, setHoverContent } from '../../jwt';
85

9-
suite('Extension Test Suite', () => {
6+
suite('JWT Decoder Extension Test Suite', () => {
107
before(() => {
118
vscode.window.showInformationMessage('Start all tests.');
129
});
1310

14-
test('Sample test', () => {
15-
assert.equal(-1, [1, 2, 3].indexOf(5));
16-
assert.equal(-1, [1, 2, 3].indexOf(0));
11+
suite('decodeToken', () => {
12+
test('Should decode a valid JWT token', () => {
13+
// Valid JWT token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
14+
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
15+
const decoded = decodeToken(token);
16+
17+
assert.strictEqual(decoded.headers.alg, 'HS256');
18+
assert.strictEqual(decoded.headers.typ, 'JWT');
19+
assert.strictEqual(decoded.payload.sub, '1234567890');
20+
assert.strictEqual(decoded.payload.name, 'John Doe');
21+
assert.strictEqual(decoded.payload.iat, 1516239022);
22+
});
23+
24+
test('Should decode JWT with only header', () => {
25+
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9';
26+
const decoded = decodeToken(token);
27+
28+
assert.strictEqual(decoded.headers.alg, 'HS256');
29+
assert.strictEqual(decoded.headers.typ, 'JWT');
30+
assert.strictEqual(decoded.payload, '');
31+
});
32+
33+
test('Should decode JWT with Bearer prefix', () => {
34+
const token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ';
35+
const decoded = decodeToken(token);
36+
37+
assert.strictEqual(decoded.headers.alg, 'HS256');
38+
assert.strictEqual(decoded.headers.typ, 'JWT');
39+
assert.strictEqual(decoded.payload.sub, '1234567890');
40+
});
41+
42+
test('Should decode JWT with different algorithms', () => {
43+
// RS256 token header
44+
const token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI0MjEiLCJyb2xlIjoiYWRtaW4ifQ';
45+
const decoded = decodeToken(token);
46+
47+
assert.strictEqual(decoded.headers.alg, 'RS256');
48+
assert.strictEqual(decoded.headers.typ, 'JWT');
49+
assert.strictEqual(decoded.payload.userId, '421');
50+
assert.strictEqual(decoded.payload.role, 'admin');
51+
});
52+
53+
test('Should handle JWT with special characters in payload', () => {
54+
// Token with email and special characters: {"email":"user@example.com","role":"user"}
55+
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJyb2xlIjoidXNlciJ9';
56+
const decoded = decodeToken(token);
57+
58+
assert.strictEqual(decoded.payload.email, 'user@example.com');
59+
assert.strictEqual(decoded.payload.role, 'user');
60+
});
61+
62+
test('Should handle empty token', () => {
63+
const token = '';
64+
assert.throws(() => {
65+
decodeToken(token);
66+
});
67+
});
68+
69+
test('Should handle token with quotes', () => {
70+
const token = '"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0"';
71+
const decoded = decodeToken(token);
72+
73+
assert.strictEqual(decoded.headers.alg, 'HS256');
74+
assert.strictEqual(decoded.payload.name, 'John Doe');
75+
});
76+
});
77+
78+
suite('setHoverContent', () => {
79+
test('Should set hover content with headers and payload', () => {
80+
const hover = new vscode.Hover(new vscode.MarkdownString());
81+
const headers = JSON.stringify({ alg: 'HS256', typ: 'JWT' }, null, 4);
82+
const payload = JSON.stringify({ sub: '1234567890', name: 'John Doe' }, null, 4);
83+
84+
setHoverContent(hover, headers, payload);
85+
86+
assert.strictEqual(hover.contents.length, 1);
87+
const content = hover.contents[0] as vscode.MarkdownString;
88+
assert.ok(content.value.includes('## Headers'));
89+
assert.ok(content.value.includes('## Payload'));
90+
assert.ok(content.value.includes('HS256'));
91+
assert.ok(content.value.includes('John Doe'));
92+
});
93+
94+
test('Should replace existing hover content', () => {
95+
const initialContent = new vscode.MarkdownString('Initial content');
96+
const hover = new vscode.Hover(initialContent);
97+
const headers = JSON.stringify({ alg: 'RS256' }, null, 4);
98+
const payload = JSON.stringify({ userId: '123' }, null, 4);
99+
100+
setHoverContent(hover, headers, payload);
101+
102+
assert.strictEqual(hover.contents.length, 1);
103+
const content = hover.contents[0] as vscode.MarkdownString;
104+
assert.ok(!content.value.includes('Initial content'));
105+
assert.ok(content.value.includes('RS256'));
106+
assert.ok(content.value.includes('userId'));
107+
});
108+
109+
test('Should format content as markdown code blocks', () => {
110+
const hover = new vscode.Hover(new vscode.MarkdownString());
111+
const headers = JSON.stringify({ alg: 'HS256' }, null, 4);
112+
const payload = JSON.stringify({ test: 'data' }, null, 4);
113+
114+
setHoverContent(hover, headers, payload);
115+
116+
const content = hover.contents[0] as vscode.MarkdownString;
117+
assert.ok(content.value.includes('```javascript'));
118+
assert.ok(content.value.match(/```/g)!.length >= 2);
119+
});
120+
});
121+
122+
suite('Extension Activation', () => {
123+
test('Extension should be present', () => {
124+
assert.ok(vscode.extensions.getExtension('jflbr.jwt-decoder'));
125+
});
126+
127+
test('Extension should activate', async () => {
128+
const extension = vscode.extensions.getExtension('jflbr.jwt-decoder');
129+
if (extension) {
130+
await extension.activate();
131+
assert.ok(extension.isActive);
132+
}
133+
});
134+
135+
test('Command should be registered', async () => {
136+
const commands = await vscode.commands.getCommands(true);
137+
assert.ok(commands.includes('extension.jwt-decoder'));
138+
});
17139
});
18140
});

0 commit comments

Comments
 (0)