-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ner-cookies Add hashed userId to whats-new-banner cookie key
- Loading branch information
Showing
4 changed files
with
24 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
import { Request, Response } from 'express' | ||
import NotificationCookieService from './whatsNewCookieService' | ||
import md5 from 'md5' | ||
import WhatsNewCookieService from './whatsNewCookieService' | ||
|
||
describe('WhatsNew cookie', () => { | ||
let res: Response | ||
let req: Request | ||
|
||
const id = '123' | ||
const hashedKey = md5(`whats-new-banner-${id}`) | ||
|
||
beforeEach(() => { | ||
res = { cookie: jest.fn() } as unknown as Response | ||
req = { cookies: { 'whats-new-banner': 1 } } as unknown as Request | ||
req = { cookies: { [hashedKey]: 1 } } as unknown as Request | ||
}) | ||
|
||
it('should store cookie correctly', () => { | ||
NotificationCookieService.persistDismissedVersion(res, 1) | ||
expect(res.cookie).toHaveBeenCalledWith('whats-new-banner', 1, { | ||
WhatsNewCookieService.persistDismissedVersion(res, id, 1) | ||
expect(res.cookie).toHaveBeenCalledWith(hashedKey, 1, { | ||
httpOnly: true, | ||
maxAge: 4492800000, | ||
}) | ||
}) | ||
|
||
it('should find matching cookie', () => { | ||
expect(NotificationCookieService.getDismissedVersion(req)).toEqual(1) | ||
expect(WhatsNewCookieService.getDismissedVersion(req, id)).toEqual(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { Request, Response } from 'express' | ||
import md5 from 'md5' | ||
|
||
export default class WhatsNewCookieService { | ||
static COOKIE_NAME = 'whats-new-banner' | ||
|
||
static persistDismissedVersion = (res: Response, version: number) => { | ||
static key = (id: string) => md5(`${this.COOKIE_NAME}-${id}`) | ||
|
||
static persistDismissedVersion = (res: Response, id: string, version: number) => { | ||
const oneYear = 52 * 24 * 3600000 | ||
return res.cookie(this.COOKIE_NAME, version, { maxAge: oneYear, httpOnly: true }) | ||
return res.cookie(this.key(id), version, { maxAge: oneYear, httpOnly: true }) | ||
} | ||
|
||
static getDismissedVersion = (req: Request): number => Number(req.cookies[this.COOKIE_NAME]) | ||
static getDismissedVersion = (req: Request, id: string): number => Number(req.cookies[this.key(id)]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters