Skip to content

Commit 3fe2e55

Browse files
committed
feat(tests): ✨ add tests for TokenOrigin handling
- Implement tests for creating tokens with specific origins. - Ensure serialization includes `TokenOrigin` in payloads.
1 parent d18c677 commit 3fe2e55

File tree

2 files changed

+75
-20
lines changed

2 files changed

+75
-20
lines changed

tests/EnqueueToken.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
22

3-
import {expect} from "chai";
4-
import {Payload} from "../src";
5-
import {Utils} from "../src/QueueITHelpers";
6-
import {EnqueueToken, Token} from "../src/Token";
7-
import {TokenVersion} from "../src/model/TokenVersion";
8-
import {EncryptionType} from "../src/model/EncryptionType";
9-
import {HeaderDto} from "../src/model/HeaderDto";
3+
import { expect } from "chai";
4+
import { Payload } from "../src";
5+
import { Utils } from "../src/QueueITHelpers";
6+
import { EnqueueToken, Token } from "../src/Token";
7+
import { TokenVersion } from "../src/model/TokenVersion";
8+
import { EncryptionType } from "../src/model/EncryptionType";
9+
import { HeaderDto } from "../src/model/HeaderDto";
10+
import { TokenOrigin } from "../src/model/TokenOrigin";
1011

1112
describe('Enqueue Token', () => {
1213
it('should create a simple token', () => {
@@ -122,7 +123,7 @@ describe('Enqueue Token', () => {
122123

123124
it('should sign a token with payload and custom data', () => {
124125
const expectedSignedToken =
125-
"eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50In0.0rDlI69F1Dx4Twps5qD4cQrbXbCRiezBd6fH1PVm6CnVY456FALkAhN3rgVrh_PGCJHcEXN5zoqFg65MH8WZc_CQdD63hJre3Sedu0-9zIs.aZgzkJm57etFaXjjME_-9LjOgPNTTqkp1aJ057HuEiU";
126+
"eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50In0.0rDlI69F1Dx4Twps5qD4cQrbXbCRiezBd6fH1PVm6CnVY456FALkAhN3rgVrh_PGCJHcEXN5zoqFg65MH8WZcxl-G7_FAsZgEyBPRqsoJoylWJjVe-e1HI-voBaV7x6Q.bRyvSKj3Dn0w_HPer62N0mVQP4dNPl5OIcXw7DrTH5g";
126127

127128
const payload = Payload
128129
.Enqueue()
@@ -282,4 +283,35 @@ describe('Enqueue Token', () => {
282283
expect(enqueueToken.TokenVersion).to.be.equal(TokenVersion.QT1);
283284
expect(enqueueToken.Payload).to.be.undefined;
284285
});
286+
287+
it('should create token with AkamaiBotManagerHeaderValidator origin and parse it back correctly', () => {
288+
const expectedCustomerId = "testId";
289+
const expectedKey = "testkey";
290+
const expectedOrigin = TokenOrigin.AkamaiBotManagerHeaderValidator;
291+
const secretKey = "testKey";
292+
293+
const payload = Payload
294+
.Enqueue()
295+
.WithKey(expectedKey)
296+
.WithOrigin(expectedOrigin)
297+
.Generate();
298+
299+
const token = Token
300+
.Enqueue(expectedCustomerId)
301+
.WithPayload(payload)
302+
.Generate(secretKey);
303+
304+
// Verify the payload before parsing
305+
expect(token.Payload.TokenOrigin).to.be.equal(expectedOrigin);
306+
expect(token.Payload.Key).to.be.equal(expectedKey);
307+
308+
// Parse the token back
309+
const parsedToken = Token.Parse(token.Token, secretKey);
310+
311+
// Verify all properties are preserved
312+
expect(parsedToken.CustomerId).to.be.equal(expectedCustomerId);
313+
expect(parsedToken.Payload).to.not.be.undefined;
314+
expect(parsedToken.Payload.Key).to.be.equal(expectedKey);
315+
expect(parsedToken.Payload.TokenOrigin).to.be.equal(expectedOrigin);
316+
});
285317
});

tests/EnqueueTokenPayload.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {expect} from "chai";
2-
import {IEnqueueTokenPayload, Payload} from "../src/Payload";
3-
import {Utils} from "../src/QueueITHelpers";
1+
import { expect } from "chai";
2+
import { IEnqueueTokenPayload, Payload } from "../src/Payload";
3+
import { Utils } from "../src/QueueITHelpers";
4+
import { TokenOrigin } from "../src/model/TokenOrigin";
45

56
describe('Enqueue token payload', function () {
67
it('should be able to generate a simple payload', () => {
@@ -111,8 +112,30 @@ describe('Enqueue token payload', function () {
111112
expect(actualCustomData).to.be.equal(expectedCustomDataValue);
112113
});
113114

115+
it('Should be able to generate a payload only with Token Origin', () => {
116+
const instance = Payload.Enqueue()
117+
.WithOrigin(TokenOrigin.Connector)
118+
.Generate();
119+
120+
const actualOrigin = instance.TokenOrigin;
121+
expect(actualOrigin).to.be.equal(TokenOrigin.Connector);
122+
});
123+
124+
it('should serialize payload with InviteOnly origin', () => {
125+
const expectedJson = "{\"k\":\"myKey\",\"o\":\"InviteOnly\"}";
126+
127+
const instance = Payload
128+
.Enqueue()
129+
.WithKey("myKey")
130+
.WithOrigin(TokenOrigin.InviteOnly)
131+
.Generate();
132+
const actualJson = Utils.uint8ArrayToString(instance.Serialize());
133+
134+
expect(actualJson).to.be.equal(expectedJson);
135+
});
136+
114137
it('should serialize key with relative quality and multiple custom data', () => {
115-
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\",\"cd\":{\"key1\":\"Value1\",\"key2\":\"Value2\",\"key3\":\"Value3\"}}";
138+
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\",\"cd\":{\"key1\":\"Value1\",\"key2\":\"Value2\",\"key3\":\"Value3\"},\"o\":\"Connector\"}";
116139

117140
const instance = Payload
118141
.Enqueue()
@@ -128,7 +151,7 @@ describe('Enqueue token payload', function () {
128151
});
129152

130153
it('should serialize key relative quality and one custom data', () => {
131-
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\",\"cd\":{\"key1\":\"Value1\"}}";
154+
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\",\"cd\":{\"key1\":\"Value1\"},\"o\":\"Connector\"}";
132155

133156
const instance = Payload
134157
.Enqueue()
@@ -142,7 +165,7 @@ describe('Enqueue token payload', function () {
142165
});
143166

144167
it('should serialize key and relative quality', () => {
145-
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\"}";
168+
const expectedJson = "{\"r\":0.456,\"k\":\"myKey\",\"o\":\"Connector\"}";
146169

147170
const instance = Payload
148171
.Enqueue()
@@ -155,7 +178,7 @@ describe('Enqueue token payload', function () {
155178
});
156179

157180
it('should serialize key only', () => {
158-
const expectedJson = "{\"k\":\"myKey\"}";
181+
const expectedJson = "{\"k\":\"myKey\",\"o\":\"Connector\"}";
159182

160183
const instance = Payload
161184
.Enqueue()
@@ -167,7 +190,7 @@ describe('Enqueue token payload', function () {
167190
});
168191

169192
it('should serialize key only escaped', () => {
170-
const expectedJson = "{\"k\":\"my\\\"Key\"}";
193+
const expectedJson = "{\"k\":\"my\\\"Key\",\"o\":\"Connector\"}";
171194

172195
const instance = Payload
173196
.Enqueue()
@@ -179,7 +202,7 @@ describe('Enqueue token payload', function () {
179202
});
180203

181204
it('should serialize relative quality only', () => {
182-
const expectedJson = "{\"r\":0.456}";
205+
const expectedJson = "{\"r\":0.456,\"o\":\"Connector\"}";
183206

184207
const instance = Payload
185208
.Enqueue()
@@ -191,7 +214,7 @@ describe('Enqueue token payload', function () {
191214
});
192215

193216
it('should serialize custom data only', () => {
194-
const expectedJson = "{\"cd\":{\"key1\":\"Value1\"}}";
217+
const expectedJson = "{\"cd\":{\"key1\":\"Value1\"},\"o\":\"Connector\"}";
195218

196219
const instance = Payload
197220
.Enqueue()
@@ -203,7 +226,7 @@ describe('Enqueue token payload', function () {
203226
});
204227

205228
it('should serialize custom data escaped', () => {
206-
const expectedJson = "{\"cd\":{\"ke\\\"y1\":\"Va\\\"lue1\"}}";
229+
const expectedJson = "{\"cd\":{\"ke\\\"y1\":\"Va\\\"lue1\"},\"o\":\"Connector\"}";
207230

208231
const instance = Payload
209232
.Enqueue()
@@ -215,7 +238,7 @@ describe('Enqueue token payload', function () {
215238
});
216239

217240
it('should be encrypted correctly', () => {
218-
const expectedEncryptedPayload = "0rDlI69F1Dx4Twps5qD4cQrbXbCRiezBd6fH1PVm6CnVY456FALkAhN3rgVrh_PGCJHcEXN5zoqFg65MH8WZc_CQdD63hJre3Sedu0-9zIs";
241+
const expectedEncryptedPayload = "0rDlI69F1Dx4Twps5qD4cQrbXbCRiezBd6fH1PVm6CnVY456FALkAhN3rgVrh_PGCJHcEXN5zoqFg65MH8WZcxl-G7_FAsZgEyBPRqsoJoylWJjVe-e1HI-voBaV7x6Q";
219242
const payload = Payload
220243
.Enqueue()
221244
.WithKey("somekey")

0 commit comments

Comments
 (0)