|
1 | 1 | import * as assert from 'assert'; |
2 | 2 | import { before } from 'mocha'; |
3 | 3 | import * as vscode from 'vscode'; |
4 | | -import { decodeToken, setHoverContent } from '../../jwt'; |
| 4 | +import { decodeToken, setHoverContent, isValidJWT } from '../../jwt'; |
5 | 5 |
|
6 | 6 | suite('JWT Decoder Extension Test Suite', () => { |
7 | 7 | before(() => { |
@@ -119,6 +119,58 @@ suite('JWT Decoder Extension Test Suite', () => { |
119 | 119 | }); |
120 | 120 | }); |
121 | 121 |
|
| 122 | + suite('isValidJWT', () => { |
| 123 | + test('Should validate a valid JWT token', () => { |
| 124 | + const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; |
| 125 | + assert.strictEqual(isValidJWT(validToken), true); |
| 126 | + }); |
| 127 | + |
| 128 | + test('Should validate JWT with Bearer prefix', () => { |
| 129 | + const tokenWithBearer = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; |
| 130 | + assert.strictEqual(isValidJWT(tokenWithBearer), true); |
| 131 | + }); |
| 132 | + |
| 133 | + test('Should validate JWT with whitespace', () => { |
| 134 | + const tokenWithWhitespace = ' eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c '; |
| 135 | + assert.strictEqual(isValidJWT(tokenWithWhitespace), true); |
| 136 | + }); |
| 137 | + |
| 138 | + test('Should reject token with only 2 parts', () => { |
| 139 | + const invalidToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ'; |
| 140 | + assert.strictEqual(isValidJWT(invalidToken), false); |
| 141 | + }); |
| 142 | + |
| 143 | + test('Should reject token with more than 3 parts', () => { |
| 144 | + const invalidToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c.extra'; |
| 145 | + assert.strictEqual(isValidJWT(invalidToken), false); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Should reject empty string', () => { |
| 149 | + assert.strictEqual(isValidJWT(''), false); |
| 150 | + }); |
| 151 | + |
| 152 | + test('Should reject null or undefined', () => { |
| 153 | + assert.strictEqual(isValidJWT(null as any), false); |
| 154 | + assert.strictEqual(isValidJWT(undefined as any), false); |
| 155 | + }); |
| 156 | + |
| 157 | + test('Should reject token with invalid characters', () => { |
| 158 | + const invalidToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWI@invalid!.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; |
| 159 | + assert.strictEqual(isValidJWT(invalidToken), false); |
| 160 | + }); |
| 161 | + |
| 162 | + test('Should reject token with empty parts', () => { |
| 163 | + const invalidToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; |
| 164 | + assert.strictEqual(isValidJWT(invalidToken), false); |
| 165 | + }); |
| 166 | + |
| 167 | + test('Should reject non-JWT strings', () => { |
| 168 | + assert.strictEqual(isValidJWT('This is not a JWT'), false); |
| 169 | + assert.strictEqual(isValidJWT('random.text.here'), false); |
| 170 | + assert.strictEqual(isValidJWT('123.456.789'), false); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
122 | 174 | suite('Extension Activation', () => { |
123 | 175 | test('Extension should be present', () => { |
124 | 176 | assert.ok(vscode.extensions.getExtension('jflbr.jwt-decoder')); |
|
0 commit comments