Skip to content

Commit d36520f

Browse files
committed
feat!: refactor server/auth context
1 parent 128d058 commit d36520f

File tree

8 files changed

+329
-70
lines changed

8 files changed

+329
-70
lines changed

typescript/packages/cf-router/src/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
122122
const authHeader = request.headers.get('authorization');
123123
if (authHeader !== null && this.authInterceptor !== undefined) {
124124
const authContext = await this.authInterceptor.intercept(context, authHeader);
125-
context.setAuthContext(authContext);
125+
context.authContext = authContext;
126126
}
127127
}
128128

@@ -174,7 +174,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
174174
}
175175
return record;
176176
},
177-
context.clientDeadline(),
177+
context.clientDeadline,
178178
);
179179
};
180180
return await method.invoke(generator, context);
@@ -231,7 +231,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
231231
}
232232
return record;
233233
},
234-
context.clientDeadline(),
234+
context.clientDeadline,
235235
);
236236
};
237237
if (!TempoUtil.isAsyncGeneratorFunction(method.invoke)) {
@@ -355,7 +355,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
355355
}
356356
responseHeaders.set('content-type', contentType.raw);
357357

358-
const outgoingCredential = context.getOutgoingCredential();
358+
const outgoingCredential = context.outgoingCredential;
359359
if (outgoingCredential) {
360360
responseHeaders.set('tempo-credential', stringifyCredential(outgoingCredential));
361361
}
@@ -384,7 +384,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
384384
}
385385
return data;
386386
},
387-
context.clientDeadline(),
387+
context.clientDeadline,
388388
);
389389
} else {
390390
if (record === undefined) {

typescript/packages/node-http/src/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class TempoRouter<TEnv> extends BaseRouter<IncomingMessage, TEnv, ServerR
107107
const authHeader = request.headers.authorization;
108108
if (authHeader !== undefined && this.authInterceptor !== undefined) {
109109
const authContext = await this.authInterceptor.intercept(context, authHeader);
110-
context.setAuthContext(authContext);
110+
context.authContext = authContext;
111111
}
112112
}
113113

@@ -165,7 +165,7 @@ export class TempoRouter<TEnv> extends BaseRouter<IncomingMessage, TEnv, ServerR
165165
}
166166
return record;
167167
},
168-
context.clientDeadline(),
168+
context.clientDeadline,
169169
);
170170
};
171171
return await method.invoke(generator, context);
@@ -228,7 +228,7 @@ export class TempoRouter<TEnv> extends BaseRouter<IncomingMessage, TEnv, ServerR
228228
}
229229
return record;
230230
},
231-
context.clientDeadline(),
231+
context.clientDeadline,
232232
);
233233
};
234234
if (!TempoUtil.isAsyncGeneratorFunction(method.invoke)) {
@@ -343,7 +343,7 @@ export class TempoRouter<TEnv> extends BaseRouter<IncomingMessage, TEnv, ServerR
343343
}
344344
response.setHeader('content-type', contentType.raw);
345345

346-
const outgoingCredential = context.getOutgoingCredential();
346+
const outgoingCredential = context.outgoingCredential;
347347
if (outgoingCredential) {
348348
response.setHeader('tempo-credential', stringifyCredential(outgoingCredential));
349349
}
@@ -368,7 +368,7 @@ export class TempoRouter<TEnv> extends BaseRouter<IncomingMessage, TEnv, ServerR
368368
}
369369
return data;
370370
},
371-
context.clientDeadline(),
371+
context.clientDeadline,
372372
);
373373
} else {
374374
if (record === undefined) {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { AuthContext } from './auth';
3+
import { TempoError } from '@tempojs/common';
4+
5+
describe('AuthContext', () => {
6+
let authContext: AuthContext;
7+
let key: string;
8+
let propertyName: string;
9+
let propertyValue: unknown;
10+
11+
beforeEach(() => {
12+
authContext = new AuthContext();
13+
key = 'key1';
14+
propertyName = 'prop1';
15+
propertyValue = 'value1';
16+
authContext.addProperty(key, propertyName, propertyValue);
17+
});
18+
19+
it('should add a new property', () => {
20+
const prop = authContext.findPropertyByName(key, propertyName);
21+
expect(prop).toBeDefined();
22+
expect(prop?.name).toEqual(propertyName);
23+
expect(prop?.getValue()).toEqual(propertyValue);
24+
});
25+
26+
it('should return peer identity if authenticated', () => {
27+
authContext.peerIdentityKey = key;
28+
const peerIdentity = authContext.peerIdentity;
29+
expect(peerIdentity).toBeDefined();
30+
expect(peerIdentity?.[0]?.name).toEqual(propertyName);
31+
expect(peerIdentity?.[0]?.getValue()).toEqual(propertyValue);
32+
});
33+
34+
it('should throw TempoError when setting undefined key as peerIdentityKey', () => {
35+
expect(() => {
36+
authContext.peerIdentityKey = undefined;
37+
}).toThrow(TempoError);
38+
});
39+
40+
it('should throw TempoError when setting a non-existent key as peerIdentityKey', () => {
41+
expect(() => {
42+
authContext.peerIdentityKey = 'nonExistentKey';
43+
}).toThrow(TempoError);
44+
});
45+
46+
it('should return undefined for non-authenticated peer', () => {
47+
const nonAuthenticatedContext = new AuthContext();
48+
const peerIdentity = nonAuthenticatedContext.peerIdentity;
49+
expect(peerIdentity).toBeUndefined();
50+
});
51+
52+
it('should return undefined for non-existing property', () => {
53+
const prop = authContext.findPropertyByName(key, 'nonExistingProperty');
54+
expect(prop).toBeUndefined();
55+
});
56+
57+
it('should return undefined for properties of non-existing key', () => {
58+
const properties = authContext.getProperties('nonExistingKey');
59+
expect(properties).toBeUndefined();
60+
});
61+
});
62+
63+
describe('AuthContext - Edge Cases', () => {
64+
let authContext: AuthContext;
65+
66+
beforeEach(() => {
67+
authContext = new AuthContext();
68+
});
69+
70+
it('should handle adding a property with null or undefined key', () => {
71+
expect(() => {
72+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
73+
//@ts-ignore
74+
authContext.addProperty(null, 'propertyName', 'value');
75+
}).toThrow(TempoError);
76+
expect(() => {
77+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
78+
//@ts-ignore
79+
authContext.addProperty(undefined, 'propertyName', 'value');
80+
}).toThrow(TempoError);
81+
});
82+
83+
it('should handle adding a property with null or undefined name', () => {
84+
expect(() => {
85+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
86+
//@ts-ignore
87+
authContext.addProperty('key', null, 'value');
88+
}).toThrow(TempoError);
89+
expect(() => {
90+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
91+
//@ts-ignore
92+
authContext.addProperty('key', undefined, 'value');
93+
}).toThrow(TempoError);
94+
});
95+
96+
it('should handle adding a property with undefined value', () => {
97+
expect(() => authContext.addProperty('key', 'propertyName', undefined)).toThrow(TempoError);
98+
});
99+
100+
it('should handle multiple properties with the same key and name', () => {
101+
const value1 = 'value1';
102+
const value2 = 'value2';
103+
authContext.addProperty('key', 'propertyName', value1);
104+
authContext.addProperty('key', 'propertyName', value2);
105+
const properties = authContext.findPropertiesByName('key', 'propertyName');
106+
expect(properties).toBeDefined();
107+
expect(properties?.length).toEqual(2);
108+
expect(properties?.[0]?.getValue()).toEqual(value1);
109+
expect(properties?.[1]?.getValue()).toEqual(value2);
110+
});
111+
112+
it('should throw error when trying to set peer identity with non-existing key', () => {
113+
expect(() => {
114+
authContext.peerIdentityKey = 'nonExistentKey';
115+
}).toThrow(TempoError);
116+
});
117+
118+
it('should handle setting peer identity with a key that exists but has no properties', () => {
119+
const emptyKey = 'emptyKey';
120+
authContext.addProperty(emptyKey, 'propertyName', 'value');
121+
authContext.getProperties(emptyKey)?.pop();
122+
expect(() => {
123+
authContext.peerIdentityKey = emptyKey;
124+
}).not.toThrow();
125+
expect(authContext.peerIdentity).length(0);
126+
});
127+
});

0 commit comments

Comments
 (0)