From 2a54ace005ae47fd9a17d5df15495024336570ea Mon Sep 17 00:00:00 2001 From: sjaanus Date: Mon, 26 Sep 2022 14:42:39 +0200 Subject: [PATCH] Add description to PAT (#2090) * Add description to PAT * Add tests --- src/lib/db/pat-store.ts | 11 +++++++---- src/lib/types/models/pat.ts | 13 ++++++++++++- src/migrations/20220912165344-pat-tokens.js | 1 + src/test/e2e/api/user/pat.e2e.test.ts | 3 +++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/lib/db/pat-store.ts b/src/lib/db/pat-store.ts index 04ab6b1336..c5a0909897 100644 --- a/src/lib/db/pat-store.ts +++ b/src/lib/db/pat-store.ts @@ -8,6 +8,7 @@ const TABLE = 'personal_access_tokens'; const PAT_COLUMNS = [ 'secret', + 'description', 'user_id', 'expires_at', 'created_at', @@ -21,16 +22,18 @@ const fromRow = (row) => { return new Pat({ secret: row.secret, userId: row.user_id, + description: row.description, createdAt: row.created_at, seenAt: row.seen_at, expiresAt: row.expires_at, }); }; -const toRow = (user: IPat) => ({ - secret: user.secret, - user_id: user.userId, - expires_at: user.expiresAt, +const toRow = (pat: IPat) => ({ + secret: pat.secret, + description: pat.description, + user_id: pat.userId, + expires_at: pat.expiresAt, }); export default class PatStore implements IPatStore { diff --git a/src/lib/types/models/pat.ts b/src/lib/types/models/pat.ts index 0458404223..cd6b93b390 100644 --- a/src/lib/types/models/pat.ts +++ b/src/lib/types/models/pat.ts @@ -1,5 +1,6 @@ export interface IPat { secret: string; + description: string; userId: number; expiresAt?: Date; createdAt?: Date; @@ -9,6 +10,8 @@ export interface IPat { export default class Pat implements IPat { secret: string; + description: string; + userId: number; expiresAt: Date; @@ -17,11 +20,19 @@ export default class Pat implements IPat { createdAt: Date; - constructor({ secret, userId, expiresAt, seenAt, createdAt }: IPat) { + constructor({ + secret, + userId, + expiresAt, + seenAt, + createdAt, + description, + }: IPat) { this.secret = secret; this.userId = userId; this.expiresAt = expiresAt; this.seenAt = seenAt; this.createdAt = createdAt; + this.description = description; } } diff --git a/src/migrations/20220912165344-pat-tokens.js b/src/migrations/20220912165344-pat-tokens.js index 4cd6602a68..99a29f87d4 100644 --- a/src/migrations/20220912165344-pat-tokens.js +++ b/src/migrations/20220912165344-pat-tokens.js @@ -5,6 +5,7 @@ exports.up = function (db, cb) { ` CREATE TABLE personal_access_tokens ( secret text not null primary key, + description text, user_id integer not null references users (id) ON DELETE CASCADE, expires_at timestamp with time zone NOT NULL, seen_at timestamp with time zone, diff --git a/src/test/e2e/api/user/pat.e2e.test.ts b/src/test/e2e/api/user/pat.e2e.test.ts index 966467386c..faa362d663 100644 --- a/src/test/e2e/api/user/pat.e2e.test.ts +++ b/src/test/e2e/api/user/pat.e2e.test.ts @@ -26,17 +26,20 @@ afterAll(async () => { }); test('should create a PAT', async () => { + const description = 'expected description'; const { request } = app; const { body } = await request .post('/api/admin/user/tokens') .send({ expiresAt: tomorrow, + description: description, } as IPat) .set('Content-Type', 'application/json') .expect(201); expect(new Date(body.expiresAt)).toEqual(tomorrow); + expect(body.description).toEqual(description); }); test('should delete the PAT', async () => {