@oneuptime/common 12.0.29 → 12.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Models/DatabaseModels/Index.ts +11 -0
- package/Models/DatabaseModels/OnCallDutyPolicySchedule.ts +43 -0
- package/Models/DatabaseModels/OnCallDutyPolicyScheduleCalendarFeed.ts +662 -0
- package/Models/DatabaseModels/ProjectOnCallCalendarFeed.ts +584 -0
- package/Models/DatabaseModels/UserOnCallCalendarFeed.ts +546 -0
- package/Models/DatabaseModels/UserOnCallShiftReminder.ts +231 -0
- package/Models/DatabaseModels/UserOnCallShiftReminderLog.ts +331 -0
- package/Server/API/OnCallCalendarAPI.ts +2194 -0
- package/Server/EnvironmentConfig.ts +138 -0
- package/Server/Infrastructure/OnCallCalendarFeedCache.ts +1050 -0
- package/Server/Infrastructure/Postgres/SchemaMigrations/1790400000000-AddOnCallCalendarFeeds.ts +253 -0
- package/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +2 -0
- package/Server/Middleware/OnCallCalendarFeedRateLimit.ts +487 -0
- package/Server/Services/Index.ts +12 -0
- package/Server/Services/OnCallDutyPolicyEscalationRuleScheduleService.ts +32 -0
- package/Server/Services/OnCallDutyPolicyScheduleCalendarFeedService.ts +260 -0
- package/Server/Services/OnCallDutyPolicyScheduleLayerService.ts +70 -3
- package/Server/Services/OnCallDutyPolicyScheduleLayerUserService.ts +138 -3
- package/Server/Services/OnCallDutyPolicyScheduleService.ts +1465 -134
- package/Server/Services/OnCallDutyPolicyUserOverrideService.ts +167 -31
- package/Server/Services/ProjectOnCallCalendarFeedService.ts +192 -0
- package/Server/Services/TeamMemberService.ts +489 -1
- package/Server/Services/TeamService.ts +50 -0
- package/Server/Services/UserNotificationSettingService.ts +44 -1
- package/Server/Services/UserOnCallCalendarFeedService.ts +159 -0
- package/Server/Services/UserOnCallShiftReminderLogService.ts +151 -0
- package/Server/Services/UserOnCallShiftReminderService.ts +112 -0
- package/Server/Utils/OnCall/CalendarFeedToken.ts +241 -0
- package/Server/Utils/OnCall/OnCallCalendarFeedRenderer.ts +1488 -0
- package/Server/Utils/OnCall/OnCallCalendarFeedUrls.ts +230 -0
- package/Server/Utils/OnCall/OnCallShiftChangeListeners.ts +227 -0
- package/Server/Utils/OnCall/OnCallShiftMaterializer.ts +1127 -0
- package/Server/Utils/OnCall/OnCallShiftReminderListener.ts +174 -0
- package/Server/Utils/OnCall/OnCallShiftReminderRunner.ts +2584 -0
- package/Server/Utils/Response.ts +134 -0
- package/Server/Utils/StartServer.ts +10 -0
- package/Server/Utils/Telemetry/AppMetrics.ts +51 -0
- package/Tests/App/Dashboard/OnCallCalendarFeedLinks.test.tsx +229 -0
- package/Tests/App/Dashboard/OnCallCalendarFeedPlanGate.test.tsx +489 -0
- package/Tests/App/Dashboard/OnCallCalendarFeedSideMenus.test.tsx +149 -0
- package/Tests/App/Dashboard/OnCallCalendarFeedUtil.test.ts +855 -0
- package/Tests/App/Dashboard/OnCallPersonalCalendarFeed.test.tsx +1209 -0
- package/Tests/App/Dashboard/OnCallSharedCalendarFeedCard.test.tsx +1019 -0
- package/Tests/App/Dashboard/OnCallShiftRemindersAndUpcomingShifts.test.tsx +655 -0
- package/Tests/App/Dashboard/UserSettingsSetupChecklistModel.test.ts +73 -0
- package/Tests/Models/OnCallCalendarFeedModels.test.ts +642 -0
- package/Tests/Models/OnCallDutyPolicyScheduleCalendarFeed.test.ts +288 -0
- package/Tests/Models/OnCallDutyPolicyScheduleShiftConfigVersion.test.ts +88 -0
- package/Tests/Models/UserOnCallShiftReminderModels.test.ts +368 -0
- package/Tests/Server/API/Helpers.ts +8 -0
- package/Tests/Server/API/OnCallCalendarAPI.test.ts +4461 -0
- package/Tests/Server/EnvironmentConfigOnCallCalendarFeed.test.ts +357 -0
- package/Tests/Server/Infrastructure/OnCallCalendarFeedCache.test.ts +1589 -0
- package/Tests/Server/Infrastructure/Postgres/AddOnCallCalendarFeedsMigration.test.ts +369 -0
- package/Tests/Server/Middleware/OnCallCalendarFeedRateLimit.test.ts +1365 -0
- package/Tests/Server/Services/FeedRetentionConsistency.test.ts +70 -0
- package/Tests/Server/Services/OnCallCalendarFeedServices.test.ts +1103 -0
- package/Tests/Server/Services/OnCallDutyPolicyScheduleResolver.test.ts +1037 -0
- package/Tests/Server/Services/OnCallShiftConfigPropagation.test.ts +1270 -0
- package/Tests/Server/Services/TeamDeleteMemberCleanup.test.ts +156 -0
- package/Tests/Server/Services/TeamMemberOnCallCleanup.test.ts +755 -0
- package/Tests/Server/Services/UserNotificationSettingShiftReminderDefaults.test.ts +267 -0
- package/Tests/Server/Services/UserOnCallShiftReminderServices.test.ts +583 -0
- package/Tests/Server/Types/Database/Permissions/OnCallCalendarFeedPermissions.test.ts +254 -0
- package/Tests/Server/Utils/OnCall/CalendarFeedToken.test.ts +467 -0
- package/Tests/Server/Utils/OnCall/OnCallCalendarFeedRenderer.test.ts +2142 -0
- package/Tests/Server/Utils/OnCall/OnCallCalendarFeedUrls.test.ts +278 -0
- package/Tests/Server/Utils/OnCall/OnCallResolverTestHarness.ts +440 -0
- package/Tests/Server/Utils/OnCall/OnCallShiftChangeListeners.test.ts +314 -0
- package/Tests/Server/Utils/OnCall/OnCallShiftMaterializer.test.ts +1502 -0
- package/Tests/Server/Utils/OnCall/OnCallShiftReminderListener.test.ts +491 -0
- package/Tests/Server/Utils/OnCall/OnCallShiftReminderRunner.test.ts +3088 -0
- package/Tests/Server/Utils/OnCall/OnCallShiftReminderTestHarness.ts +910 -0
- package/Tests/Server/Utils/ResponseSendCalendarResponse.test.ts +561 -0
- package/Tests/Server/Utils/StartServerEncryptionSecretWarning.test.ts +64 -0
- package/Tests/Server/Utils/Telemetry/AppMetrics.test.ts +29 -0
- package/Tests/Types/Calendar/ICalendar.test.ts +488 -0
- package/Tests/Types/NotificationSetting/NotificationSettingEventType.test.ts +53 -0
- package/Tests/Types/OnCallDutyPolicy/CalendarFeedTestFixtures.ts +310 -0
- package/Tests/Types/OnCallDutyPolicy/CalendarFeedWindow.test.ts +205 -0
- package/Tests/Types/OnCallDutyPolicy/LayerUtilLayerMetaAndIterationCap.test.ts +458 -0
- package/Tests/Types/OnCallDutyPolicy/MaterializedShift.test.ts +197 -0
- package/Tests/Types/OnCallDutyPolicy/OnCallCalendarFeedUtil.test.ts +1986 -0
- package/Tests/Types/OnCallDutyPolicy/ScheduleShiftUtilGroupKey.test.ts +678 -0
- package/Tests/Types/OnCallDutyPolicy/ShiftSeamUtil.test.ts +417 -0
- package/Tests/UI/Utils/Breadcrumb/fixtures/RealBreadcrumbTrails.ts +10 -0
- package/Tests/UI/Utils/Breadcrumb/fixtures/RealRoutePatterns.ts +2 -0
- package/Types/Calendar/ICalendar.ts +442 -0
- package/Types/Email/EmailTemplateType.ts +2 -0
- package/Types/NotificationSetting/NotificationSettingEventType.ts +4 -0
- package/Types/OnCallDutyPolicy/CalendarFeedWindow.ts +140 -0
- package/Types/OnCallDutyPolicy/Layer.ts +190 -35
- package/Types/OnCallDutyPolicy/MaterializedShift.ts +274 -0
- package/Types/OnCallDutyPolicy/OnCallCalendarFeedUtil.ts +1415 -0
- package/Types/OnCallDutyPolicy/ScheduleShiftUtil.ts +159 -4
- package/Types/OnCallDutyPolicy/ShiftSeamUtil.ts +106 -0
- package/build/dist/Models/DatabaseModels/Index.js +11 -0
- package/build/dist/Models/DatabaseModels/Index.js.map +1 -1
- package/build/dist/Models/DatabaseModels/OnCallDutyPolicySchedule.js +44 -0
- package/build/dist/Models/DatabaseModels/OnCallDutyPolicySchedule.js.map +1 -1
- package/build/dist/Models/DatabaseModels/OnCallDutyPolicyScheduleCalendarFeed.js +699 -0
- package/build/dist/Models/DatabaseModels/OnCallDutyPolicyScheduleCalendarFeed.js.map +1 -0
- package/build/dist/Models/DatabaseModels/ProjectOnCallCalendarFeed.js +620 -0
- package/build/dist/Models/DatabaseModels/ProjectOnCallCalendarFeed.js.map +1 -0
- package/build/dist/Models/DatabaseModels/UserOnCallCalendarFeed.js +585 -0
- package/build/dist/Models/DatabaseModels/UserOnCallCalendarFeed.js.map +1 -0
- package/build/dist/Models/DatabaseModels/UserOnCallShiftReminder.js +247 -0
- package/build/dist/Models/DatabaseModels/UserOnCallShiftReminder.js.map +1 -0
- package/build/dist/Models/DatabaseModels/UserOnCallShiftReminderLog.js +355 -0
- package/build/dist/Models/DatabaseModels/UserOnCallShiftReminderLog.js.map +1 -0
- package/build/dist/Server/API/OnCallCalendarAPI.js +1371 -0
- package/build/dist/Server/API/OnCallCalendarAPI.js.map +1 -0
- package/build/dist/Server/EnvironmentConfig.js +102 -0
- package/build/dist/Server/EnvironmentConfig.js.map +1 -1
- package/build/dist/Server/Infrastructure/OnCallCalendarFeedCache.js +663 -0
- package/build/dist/Server/Infrastructure/OnCallCalendarFeedCache.js.map +1 -0
- package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1790400000000-AddOnCallCalendarFeeds.js +103 -0
- package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1790400000000-AddOnCallCalendarFeeds.js.map +1 -0
- package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js +2 -0
- package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js.map +1 -1
- package/build/dist/Server/Middleware/OnCallCalendarFeedRateLimit.js +342 -0
- package/build/dist/Server/Middleware/OnCallCalendarFeedRateLimit.js.map +1 -0
- package/build/dist/Server/Services/Index.js +12 -0
- package/build/dist/Server/Services/Index.js.map +1 -1
- package/build/dist/Server/Services/OnCallDutyPolicyEscalationRuleScheduleService.js +20 -0
- package/build/dist/Server/Services/OnCallDutyPolicyEscalationRuleScheduleService.js.map +1 -1
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleCalendarFeedService.js +219 -0
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleCalendarFeedService.js.map +1 -0
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleLayerService.js +47 -3
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleLayerService.js.map +1 -1
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleLayerUserService.js +112 -3
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleLayerUserService.js.map +1 -1
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleService.js +955 -9
- package/build/dist/Server/Services/OnCallDutyPolicyScheduleService.js.map +1 -1
- package/build/dist/Server/Services/OnCallDutyPolicyUserOverrideService.js +93 -6
- package/build/dist/Server/Services/OnCallDutyPolicyUserOverrideService.js.map +1 -1
- package/build/dist/Server/Services/ProjectOnCallCalendarFeedService.js +157 -0
- package/build/dist/Server/Services/ProjectOnCallCalendarFeedService.js.map +1 -0
- package/build/dist/Server/Services/TeamMemberService.js +396 -1
- package/build/dist/Server/Services/TeamMemberService.js.map +1 -1
- package/build/dist/Server/Services/TeamService.js +43 -0
- package/build/dist/Server/Services/TeamService.js.map +1 -1
- package/build/dist/Server/Services/UserNotificationSettingService.js +26 -1
- package/build/dist/Server/Services/UserNotificationSettingService.js.map +1 -1
- package/build/dist/Server/Services/UserOnCallCalendarFeedService.js +140 -0
- package/build/dist/Server/Services/UserOnCallCalendarFeedService.js.map +1 -0
- package/build/dist/Server/Services/UserOnCallShiftReminderLogService.js +116 -0
- package/build/dist/Server/Services/UserOnCallShiftReminderLogService.js.map +1 -0
- package/build/dist/Server/Services/UserOnCallShiftReminderService.js +92 -0
- package/build/dist/Server/Services/UserOnCallShiftReminderService.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/CalendarFeedToken.js +161 -0
- package/build/dist/Server/Utils/OnCall/CalendarFeedToken.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallCalendarFeedRenderer.js +983 -0
- package/build/dist/Server/Utils/OnCall/OnCallCalendarFeedRenderer.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallCalendarFeedUrls.js +155 -0
- package/build/dist/Server/Utils/OnCall/OnCallCalendarFeedUrls.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftChangeListeners.js +145 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftChangeListeners.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftMaterializer.js +752 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftMaterializer.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftReminderListener.js +92 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftReminderListener.js.map +1 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftReminderRunner.js +1786 -0
- package/build/dist/Server/Utils/OnCall/OnCallShiftReminderRunner.js.map +1 -0
- package/build/dist/Server/Utils/Response.js +97 -0
- package/build/dist/Server/Utils/Response.js.map +1 -1
- package/build/dist/Server/Utils/StartServer.js +9 -1
- package/build/dist/Server/Utils/StartServer.js.map +1 -1
- package/build/dist/Server/Utils/Telemetry/AppMetrics.js +41 -0
- package/build/dist/Server/Utils/Telemetry/AppMetrics.js.map +1 -1
- package/build/dist/Types/Calendar/ICalendar.js +290 -0
- package/build/dist/Types/Calendar/ICalendar.js.map +1 -0
- package/build/dist/Types/Email/EmailTemplateType.js +2 -0
- package/build/dist/Types/Email/EmailTemplateType.js.map +1 -1
- package/build/dist/Types/NotificationSetting/NotificationSettingEventType.js +3 -0
- package/build/dist/Types/NotificationSetting/NotificationSettingEventType.js.map +1 -1
- package/build/dist/Types/OnCallDutyPolicy/CalendarFeedWindow.js +97 -0
- package/build/dist/Types/OnCallDutyPolicy/CalendarFeedWindow.js.map +1 -0
- package/build/dist/Types/OnCallDutyPolicy/Layer.js +113 -19
- package/build/dist/Types/OnCallDutyPolicy/Layer.js.map +1 -1
- package/build/dist/Types/OnCallDutyPolicy/MaterializedShift.js +134 -0
- package/build/dist/Types/OnCallDutyPolicy/MaterializedShift.js.map +1 -0
- package/build/dist/Types/OnCallDutyPolicy/OnCallCalendarFeedUtil.js +800 -0
- package/build/dist/Types/OnCallDutyPolicy/OnCallCalendarFeedUtil.js.map +1 -0
- package/build/dist/Types/OnCallDutyPolicy/ScheduleShiftUtil.js +97 -3
- package/build/dist/Types/OnCallDutyPolicy/ScheduleShiftUtil.js.map +1 -1
- package/build/dist/Types/OnCallDutyPolicy/ShiftSeamUtil.js +77 -0
- package/build/dist/Types/OnCallDutyPolicy/ShiftSeamUtil.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import CalendarFeedToken, {
|
|
2
|
+
CALENDAR_FEED_TOKEN_HINT_LENGTH,
|
|
3
|
+
CALENDAR_FEED_TOKEN_LENGTH,
|
|
4
|
+
CALENDAR_FEED_TOKEN_REGEX,
|
|
5
|
+
CalendarFeedRotation,
|
|
6
|
+
CalendarFeedRotationUpdateData,
|
|
7
|
+
CalendarFeedTokenColumns,
|
|
8
|
+
MintedCalendarFeedToken,
|
|
9
|
+
} from "../../../../Server/Utils/OnCall/CalendarFeedToken";
|
|
10
|
+
import { PREVIOUS_TOKEN_GRACE_DAYS } from "../../../../Types/OnCallDutyPolicy/CalendarFeedWindow";
|
|
11
|
+
import crypto from "crypto";
|
|
12
|
+
import { describe, expect, test } from "@jest/globals";
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* The token is the only secret in the whole calendar-feed feature: it is the
|
|
16
|
+
* feed URL. These tests pin its shape (what the public route's regex guard
|
|
17
|
+
* accepts), its entropy (what makes guessing hopeless), its hash (what the
|
|
18
|
+
* database indexes) and its hint (what the UI may show).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const SAMPLE_COUNT: number = 200;
|
|
22
|
+
|
|
23
|
+
describe("CalendarFeedToken.mint", () => {
|
|
24
|
+
test("mints a 43-character base64url token", () => {
|
|
25
|
+
for (let i: number = 0; i < SAMPLE_COUNT; i++) {
|
|
26
|
+
const token: string = CalendarFeedToken.mint();
|
|
27
|
+
|
|
28
|
+
expect(token).toHaveLength(CALENDAR_FEED_TOKEN_LENGTH);
|
|
29
|
+
expect(token).toHaveLength(43);
|
|
30
|
+
expect(token).toMatch(/^[A-Za-z0-9_-]+$/);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("never carries base64 padding or the standard-alphabet symbols", () => {
|
|
35
|
+
for (let i: number = 0; i < SAMPLE_COUNT; i++) {
|
|
36
|
+
const token: string = CalendarFeedToken.mint();
|
|
37
|
+
|
|
38
|
+
expect(token).not.toContain("=");
|
|
39
|
+
expect(token).not.toContain("+");
|
|
40
|
+
expect(token).not.toContain("/");
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("decodes back to exactly 32 random bytes", () => {
|
|
45
|
+
const token: string = CalendarFeedToken.mint();
|
|
46
|
+
|
|
47
|
+
expect(Buffer.from(token, "base64url")).toHaveLength(32);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("is unique across many mints", () => {
|
|
51
|
+
const tokens: Set<string> = new Set<string>();
|
|
52
|
+
|
|
53
|
+
for (let i: number = 0; i < SAMPLE_COUNT; i++) {
|
|
54
|
+
tokens.add(CalendarFeedToken.mint());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
expect(tokens.size).toBe(SAMPLE_COUNT);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("uses both halves of the base64url alphabet (not hex)", () => {
|
|
61
|
+
/*
|
|
62
|
+
* A regression guard against somebody "simplifying" the mint to hex: hex
|
|
63
|
+
* would still pass a length check if the byte count were adjusted, but
|
|
64
|
+
* it would never contain an uppercase letter beyond F, nor '-' or '_'.
|
|
65
|
+
*/
|
|
66
|
+
const joined: string = Array.from({ length: SAMPLE_COUNT }, () => {
|
|
67
|
+
return CalendarFeedToken.mint();
|
|
68
|
+
}).join("");
|
|
69
|
+
|
|
70
|
+
expect(joined).toMatch(/[G-Zg-z]/);
|
|
71
|
+
expect(joined).toMatch(/[-_]/);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("CALENDAR_FEED_TOKEN_REGEX", () => {
|
|
76
|
+
test("is the documented shape guard", () => {
|
|
77
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.source).toBe("^[A-Za-z0-9_-]{43}$");
|
|
78
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.flags).toBe("");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("accepts every minted token", () => {
|
|
82
|
+
for (let i: number = 0; i < SAMPLE_COUNT; i++) {
|
|
83
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(CalendarFeedToken.mint())).toBe(
|
|
84
|
+
true,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("rejects the wrong length, padding, and foreign characters", () => {
|
|
90
|
+
const good: string = CalendarFeedToken.mint();
|
|
91
|
+
|
|
92
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42))).toBe(false);
|
|
93
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good + "a")).toBe(false);
|
|
94
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42) + "=")).toBe(false);
|
|
95
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42) + "+")).toBe(false);
|
|
96
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42) + "/")).toBe(false);
|
|
97
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42) + " ")).toBe(false);
|
|
98
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(good.slice(0, 42) + "\n")).toBe(
|
|
99
|
+
false,
|
|
100
|
+
);
|
|
101
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test("")).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("rejects a UUID and a 64-hex digest (other token shapes in the codebase)", () => {
|
|
105
|
+
expect(
|
|
106
|
+
CALENDAR_FEED_TOKEN_REGEX.test("11111111-1111-4111-8111-111111111111"),
|
|
107
|
+
).toBe(false);
|
|
108
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test("a".repeat(64))).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("is anchored: a valid token embedded in junk does not match", () => {
|
|
112
|
+
const good: string = CalendarFeedToken.mint();
|
|
113
|
+
|
|
114
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(`x${good}`)).toBe(false);
|
|
115
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(`${good}x`)).toBe(false);
|
|
116
|
+
expect(CALENDAR_FEED_TOKEN_REGEX.test(`${good}\n${good}`)).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("CalendarFeedToken.isValidShape", () => {
|
|
121
|
+
test("agrees with the regex and refuses non-strings", () => {
|
|
122
|
+
const good: string = CalendarFeedToken.mint();
|
|
123
|
+
|
|
124
|
+
expect(CalendarFeedToken.isValidShape(good)).toBe(true);
|
|
125
|
+
expect(CalendarFeedToken.isValidShape(good.slice(1))).toBe(false);
|
|
126
|
+
expect(CalendarFeedToken.isValidShape(undefined)).toBe(false);
|
|
127
|
+
expect(CalendarFeedToken.isValidShape(null)).toBe(false);
|
|
128
|
+
expect(CalendarFeedToken.isValidShape(42)).toBe(false);
|
|
129
|
+
expect(CalendarFeedToken.isValidShape([good])).toBe(false);
|
|
130
|
+
expect(CalendarFeedToken.isValidShape({ token: good })).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("CalendarFeedToken.hash", () => {
|
|
135
|
+
test("is the unkeyed SHA-256 hex digest of the token", () => {
|
|
136
|
+
const token: string = CalendarFeedToken.mint();
|
|
137
|
+
|
|
138
|
+
expect(CalendarFeedToken.hash(token)).toBe(
|
|
139
|
+
crypto.createHash("sha256").update(token, "utf8").digest("hex"),
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("is 64 lowercase hex characters", () => {
|
|
144
|
+
for (let i: number = 0; i < 20; i++) {
|
|
145
|
+
expect(CalendarFeedToken.hash(CalendarFeedToken.mint())).toMatch(
|
|
146
|
+
/^[0-9a-f]{64}$/,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("matches a known SHA-256 test vector", () => {
|
|
152
|
+
// sha256("abc") from FIPS 180-4.
|
|
153
|
+
expect(CalendarFeedToken.hash("abc")).toBe(
|
|
154
|
+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
|
155
|
+
);
|
|
156
|
+
// sha256("") — the empty message.
|
|
157
|
+
expect(CalendarFeedToken.hash("")).toBe(
|
|
158
|
+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("is deterministic and differs between tokens", () => {
|
|
163
|
+
const a: string = CalendarFeedToken.mint();
|
|
164
|
+
const b: string = CalendarFeedToken.mint();
|
|
165
|
+
|
|
166
|
+
expect(CalendarFeedToken.hash(a)).toBe(CalendarFeedToken.hash(a));
|
|
167
|
+
expect(CalendarFeedToken.hash(a)).not.toBe(CalendarFeedToken.hash(b));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("does not depend on any instance secret", () => {
|
|
171
|
+
/*
|
|
172
|
+
* The token is looked up by hash on the public route; if the hash were
|
|
173
|
+
* keyed by ENCRYPTION_SECRET, rotating that secret would orphan every
|
|
174
|
+
* subscribed calendar. Pin that the digest is a function of the token
|
|
175
|
+
* alone by comparing with the plain library call under a changed env.
|
|
176
|
+
*/
|
|
177
|
+
const token: string = CalendarFeedToken.mint();
|
|
178
|
+
const before: string = CalendarFeedToken.hash(token);
|
|
179
|
+
|
|
180
|
+
const previous: string | undefined = process.env["ENCRYPTION_SECRET"];
|
|
181
|
+
process.env["ENCRYPTION_SECRET"] = "a-completely-different-secret";
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
expect(CalendarFeedToken.hash(token)).toBe(before);
|
|
185
|
+
} finally {
|
|
186
|
+
if (previous === undefined) {
|
|
187
|
+
delete process.env["ENCRYPTION_SECRET"];
|
|
188
|
+
} else {
|
|
189
|
+
process.env["ENCRYPTION_SECRET"] = previous;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("fits the ShortText column it is stored in", () => {
|
|
195
|
+
// ColumnLength.ShortText is 100; a hex SHA-256 is 64.
|
|
196
|
+
expect(CalendarFeedToken.hash(CalendarFeedToken.mint()).length).toBe(64);
|
|
197
|
+
expect(64).toBeLessThanOrEqual(100);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("CalendarFeedToken.hint", () => {
|
|
202
|
+
test("is the last four characters of the token", () => {
|
|
203
|
+
const token: string = CalendarFeedToken.mint();
|
|
204
|
+
|
|
205
|
+
expect(CalendarFeedToken.hint(token)).toBe(token.slice(-4));
|
|
206
|
+
expect(CalendarFeedToken.hint(token)).toHaveLength(
|
|
207
|
+
CALENDAR_FEED_TOKEN_HINT_LENGTH,
|
|
208
|
+
);
|
|
209
|
+
expect(token.endsWith(CalendarFeedToken.hint(token))).toBe(true);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("reveals nothing but the tail", () => {
|
|
213
|
+
const token: string = CalendarFeedToken.mint();
|
|
214
|
+
const hint: string = CalendarFeedToken.hint(token);
|
|
215
|
+
|
|
216
|
+
// 43 - 4 = 39 characters remain unknown from the hint alone.
|
|
217
|
+
expect(token.length - hint.length).toBe(39);
|
|
218
|
+
expect(token.startsWith(hint)).toBe(false);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test("copes with short inputs without throwing", () => {
|
|
222
|
+
expect(CalendarFeedToken.hint("ab")).toBe("ab");
|
|
223
|
+
expect(CalendarFeedToken.hint("")).toBe("");
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("CalendarFeedToken.mintSet", () => {
|
|
228
|
+
test("returns a consistent token/hash/hint triple", () => {
|
|
229
|
+
const set: MintedCalendarFeedToken = CalendarFeedToken.mintSet();
|
|
230
|
+
|
|
231
|
+
expect(CalendarFeedToken.isValidShape(set.token)).toBe(true);
|
|
232
|
+
expect(set.tokenHash).toBe(CalendarFeedToken.hash(set.token));
|
|
233
|
+
expect(set.tokenHint).toBe(CalendarFeedToken.hint(set.token));
|
|
234
|
+
expect(Object.keys(set).sort()).toEqual([
|
|
235
|
+
"token",
|
|
236
|
+
"tokenHash",
|
|
237
|
+
"tokenHint",
|
|
238
|
+
]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("mints a different token every call", () => {
|
|
242
|
+
const a: MintedCalendarFeedToken = CalendarFeedToken.mintSet();
|
|
243
|
+
const b: MintedCalendarFeedToken = CalendarFeedToken.mintSet();
|
|
244
|
+
|
|
245
|
+
expect(a.token).not.toBe(b.token);
|
|
246
|
+
expect(a.tokenHash).not.toBe(b.tokenHash);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe("CalendarFeedToken.isHashEqual", () => {
|
|
251
|
+
test("is true for equal digests and false otherwise", () => {
|
|
252
|
+
const token: string = CalendarFeedToken.mint();
|
|
253
|
+
const digest: string = CalendarFeedToken.hash(token);
|
|
254
|
+
|
|
255
|
+
expect(CalendarFeedToken.isHashEqual(digest, digest)).toBe(true);
|
|
256
|
+
expect(
|
|
257
|
+
CalendarFeedToken.isHashEqual(
|
|
258
|
+
digest,
|
|
259
|
+
CalendarFeedToken.hash(CalendarFeedToken.mint()),
|
|
260
|
+
),
|
|
261
|
+
).toBe(false);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("answers false on a length mismatch instead of throwing", () => {
|
|
265
|
+
const digest: string = CalendarFeedToken.hash(CalendarFeedToken.mint());
|
|
266
|
+
|
|
267
|
+
expect(CalendarFeedToken.isHashEqual(digest, digest.slice(0, 63))).toBe(
|
|
268
|
+
false,
|
|
269
|
+
);
|
|
270
|
+
expect(CalendarFeedToken.isHashEqual("", digest)).toBe(false);
|
|
271
|
+
expect(CalendarFeedToken.isHashEqual("", "")).toBe(true);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe("CalendarFeedToken.applyTokenColumnsOnCreate", () => {
|
|
276
|
+
function expectConsistent(data: CalendarFeedTokenColumns): void {
|
|
277
|
+
expect(CalendarFeedToken.isValidShape(data.token)).toBe(true);
|
|
278
|
+
expect(data.tokenHash).toBe(CalendarFeedToken.hash(data.token as string));
|
|
279
|
+
expect(data.tokenHint).toBe(CalendarFeedToken.hint(data.token as string));
|
|
280
|
+
expect(data.rotatedAt).toBeInstanceOf(Date);
|
|
281
|
+
expect(data.previousTokenHash).toBeUndefined();
|
|
282
|
+
expect(data.previousTokenExpiresAt).toBeUndefined();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
test("mints a consistent set onto an empty row", () => {
|
|
286
|
+
const data: CalendarFeedTokenColumns = {};
|
|
287
|
+
const minted: MintedCalendarFeedToken =
|
|
288
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
289
|
+
trustSuppliedToken: true,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
expectConsistent(data);
|
|
293
|
+
expect(minted).toEqual({
|
|
294
|
+
token: data.token,
|
|
295
|
+
tokenHash: data.tokenHash,
|
|
296
|
+
tokenHint: data.tokenHint,
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test("keeps a trusted, well-formed token and derives hash and hint from it", () => {
|
|
301
|
+
const token: string = CalendarFeedToken.mint();
|
|
302
|
+
const data: CalendarFeedTokenColumns = {
|
|
303
|
+
token,
|
|
304
|
+
tokenHash: "chosen",
|
|
305
|
+
tokenHint: "zzzz",
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
309
|
+
trustSuppliedToken: true,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
expect(data.token).toBe(token);
|
|
313
|
+
expect(data.tokenHash).toBe(CalendarFeedToken.hash(token));
|
|
314
|
+
expect(data.tokenHint).toBe(CalendarFeedToken.hint(token));
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("discards a supplied token when it is not trusted", () => {
|
|
318
|
+
const token: string = CalendarFeedToken.mint();
|
|
319
|
+
const data: CalendarFeedTokenColumns = {
|
|
320
|
+
token,
|
|
321
|
+
tokenHash: CalendarFeedToken.hash(token),
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
325
|
+
trustSuppliedToken: false,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
expect(data.token).not.toBe(token);
|
|
329
|
+
expect(data.tokenHash).not.toBe(CalendarFeedToken.hash(token));
|
|
330
|
+
expectConsistent(data);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("replaces a malformed token even when trusted", () => {
|
|
334
|
+
const data: CalendarFeedTokenColumns = { token: "nope" };
|
|
335
|
+
|
|
336
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
337
|
+
trustSuppliedToken: true,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
expect(data.token).not.toBe("nope");
|
|
341
|
+
expectConsistent(data);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("never trusts a bare hash without its token", () => {
|
|
345
|
+
const data: CalendarFeedTokenColumns = { tokenHash: "chosen-hash" };
|
|
346
|
+
|
|
347
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
348
|
+
trustSuppliedToken: true,
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
expect(data.tokenHash).not.toBe("chosen-hash");
|
|
352
|
+
expectConsistent(data);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("clears any grace-period columns the caller set and stamps rotatedAt now", () => {
|
|
356
|
+
const before: number = Date.now();
|
|
357
|
+
const data: CalendarFeedTokenColumns = {
|
|
358
|
+
previousTokenHash: "stale",
|
|
359
|
+
previousTokenExpiresAt: new Date(0),
|
|
360
|
+
rotatedAt: new Date(0),
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
CalendarFeedToken.applyTokenColumnsOnCreate(data, {
|
|
364
|
+
trustSuppliedToken: false,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
expect(data.previousTokenHash).toBeUndefined();
|
|
368
|
+
expect(data.previousTokenExpiresAt).toBeUndefined();
|
|
369
|
+
expect(data.rotatedAt!.getTime()).toBeGreaterThanOrEqual(before);
|
|
370
|
+
expect(data.rotatedAt!.getTime()).toBeLessThanOrEqual(Date.now());
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
describe("CalendarFeedToken.buildRotation", () => {
|
|
375
|
+
test("mints a new consistent token and parks the old hash for the grace period", () => {
|
|
376
|
+
const now: Date = new Date("2026-09-01T12:00:00.000Z");
|
|
377
|
+
const rotation: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
378
|
+
currentTokenHash: "old-hash",
|
|
379
|
+
now,
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
expect(CalendarFeedToken.isValidShape(rotation.token)).toBe(true);
|
|
383
|
+
expect(rotation.tokenHash).toBe(CalendarFeedToken.hash(rotation.token));
|
|
384
|
+
expect(rotation.tokenHint).toBe(CalendarFeedToken.hint(rotation.token));
|
|
385
|
+
expect(rotation.tokenHash).not.toBe("old-hash");
|
|
386
|
+
expect(rotation.rotatedAt).toBe(now);
|
|
387
|
+
expect(rotation.previousTokenHash).toBe("old-hash");
|
|
388
|
+
|
|
389
|
+
const graceMs: number = PREVIOUS_TOKEN_GRACE_DAYS * 24 * 60 * 60 * 1000;
|
|
390
|
+
const expiresAt: number = rotation.previousTokenExpiresAt!.getTime();
|
|
391
|
+
// moment.add(days) is DST-aware; allow an hour either side.
|
|
392
|
+
expect(Math.abs(expiresAt - (now.getTime() + graceMs))).toBeLessThanOrEqual(
|
|
393
|
+
60 * 60 * 1000,
|
|
394
|
+
);
|
|
395
|
+
expect(PREVIOUS_TOKEN_GRACE_DAYS).toBe(30);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test("a first mint (no current hash) parks nothing", () => {
|
|
399
|
+
for (const current of [undefined, null, ""]) {
|
|
400
|
+
const rotation: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
401
|
+
currentTokenHash: current,
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
expect(rotation.previousTokenHash).toBeNull();
|
|
405
|
+
expect(rotation.previousTokenExpiresAt).toBeNull();
|
|
406
|
+
expect(rotation.rotatedAt).toBeInstanceOf(Date);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test("defaults `now` to the current time", () => {
|
|
411
|
+
const before: number = Date.now();
|
|
412
|
+
const rotation: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
413
|
+
currentTokenHash: "old-hash",
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
expect(rotation.rotatedAt.getTime()).toBeGreaterThanOrEqual(before);
|
|
417
|
+
expect(rotation.rotatedAt.getTime()).toBeLessThanOrEqual(Date.now());
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("two rotations never share a token", () => {
|
|
421
|
+
const a: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
422
|
+
currentTokenHash: "x",
|
|
423
|
+
});
|
|
424
|
+
const b: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
425
|
+
currentTokenHash: "x",
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
expect(a.token).not.toBe(b.token);
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
describe("CalendarFeedToken.toRotationUpdateData", () => {
|
|
433
|
+
test("carries exactly the six token columns, nulls included", () => {
|
|
434
|
+
const rotation: CalendarFeedRotation = CalendarFeedToken.buildRotation({
|
|
435
|
+
currentTokenHash: "old-hash",
|
|
436
|
+
now: new Date("2026-09-01T12:00:00.000Z"),
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
const data: CalendarFeedRotationUpdateData =
|
|
440
|
+
CalendarFeedToken.toRotationUpdateData(rotation);
|
|
441
|
+
|
|
442
|
+
expect(Object.keys(data).sort()).toEqual(
|
|
443
|
+
[
|
|
444
|
+
"token",
|
|
445
|
+
"tokenHash",
|
|
446
|
+
"tokenHint",
|
|
447
|
+
"rotatedAt",
|
|
448
|
+
"previousTokenHash",
|
|
449
|
+
"previousTokenExpiresAt",
|
|
450
|
+
].sort(),
|
|
451
|
+
);
|
|
452
|
+
expect(data.token).toBe(rotation.token);
|
|
453
|
+
expect(data.tokenHash).toBe(rotation.tokenHash);
|
|
454
|
+
expect(data.tokenHint).toBe(rotation.tokenHint);
|
|
455
|
+
expect(data.rotatedAt).toBe(rotation.rotatedAt);
|
|
456
|
+
expect(data.previousTokenHash).toBe("old-hash");
|
|
457
|
+
expect(data.previousTokenExpiresAt).toBe(rotation.previousTokenExpiresAt);
|
|
458
|
+
|
|
459
|
+
const first: CalendarFeedRotationUpdateData =
|
|
460
|
+
CalendarFeedToken.toRotationUpdateData(
|
|
461
|
+
CalendarFeedToken.buildRotation({ currentTokenHash: null }),
|
|
462
|
+
);
|
|
463
|
+
|
|
464
|
+
expect(first.previousTokenHash).toBeNull();
|
|
465
|
+
expect(first.previousTokenExpiresAt).toBeNull();
|
|
466
|
+
});
|
|
467
|
+
});
|