From e17e0992c84946e4fcb8b6ab0b923b80806afb99 Mon Sep 17 00:00:00 2001 From: Veikkosuhonen Date: Thu, 21 Mar 2024 14:37:39 +0200 Subject: [PATCH] Lint & format --- README.md | 13 ++++++++----- package.json | 3 +++ src/auth/IAMRights.ts | 12 +++++++++--- src/db/models/user.ts | 6 +++--- src/index.ts | 6 +++++- src/organisation/faculties.ts | 9 +++------ src/organisation/types.ts | 36 +++++++++++++++++------------------ src/types.ts | 15 +++++++-------- src/util/sentry.ts | 1 - tests/basic.spec.js | 1 - tests/chaos.spec.js | 25 ++++++++++++------------ tests/datafile.spec.js | 26 ++++++++++++------------- tests/dekaani.spec.js | 7 +++---- tests/doctoral.spec.js | 13 +++++-------- tests/jory.spec.js | 16 +++++++--------- tests/kosu.spec.js | 7 +++---- tests/lomake.spec.js | 6 +++--- tsconfig.json | 4 ++-- vitest.config.ts | 4 ++-- 19 files changed, 106 insertions(+), 104 deletions(-) diff --git a/README.md b/README.md index d24042a..d045d0d 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,18 @@ Jämpti autorisaatio mankeli IAM:eille ## Usage + e.g. `POST /` + ```json { "userId": "hy-hlo-12345678", - "iamGroups": [ - "hy-employees", - "grp-toska" - ] + "iamGroups": ["hy-employees", "grp-toska"] } ``` -Response + +Response + ```json { "500-M009": { @@ -33,7 +34,9 @@ Response } } ``` + ### Other routes + `GET` `/ping`, `/access-to-all`, `/organisation-data`, `/all-access`, `/iam-groups`, `/:userId` `POST` `user-organisations` diff --git a/package.json b/package.json index 845835d..4a937ae 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "start:prod": "node build/index.js", "build": "tsc", "test": "vitest", + "lint": "eslint . --ext .ts --ext .js", + "lint:fix": "eslint . --ext .ts --ext .js --fix", + "format": "prettier --write .", "coverage": "vitest run --coverage" }, "repository": { diff --git a/src/auth/IAMRights.ts b/src/auth/IAMRights.ts index f8a24d8..8ead6e7 100644 --- a/src/auth/IAMRights.ts +++ b/src/auth/IAMRights.ts @@ -14,20 +14,26 @@ import { opetusVaradekaani, isStudyLeaderGroup, isKatselmusViewer, - dekaaniIamToFaculty + dekaaniIamToFaculty, } from './IAMConfig' import { FACULTIES } from '../organisation/faculties' import { mapToDegreeCode } from './common' import { OrganisationAccess } from '../types' import { Programme } from '../organisation/types' -type AccessSpecialGroupFunction = (hyGroups: string[]) => { access?: { [programmeCode: string]: OrganisationAccess }, specialGroup?: { [key: string]: boolean } } +type AccessSpecialGroupFunction = (hyGroups: string[]) => { + access?: { [programmeCode: string]: OrganisationAccess } + specialGroup?: { [key: string]: boolean } +} /** * Return given access to all programmes where predicate is true * (all if no predicate defined) */ -const getAllProgrammeAccess = (accessLevel: OrganisationAccess, where?: (program: Programme) => boolean): { [key: string]: OrganisationAccess } => { +const getAllProgrammeAccess = ( + accessLevel: OrganisationAccess, + where?: (program: Programme) => boolean, +): { [key: string]: OrganisationAccess } => { const access = {} FACULTIES.forEach((faculty) => { faculty.programmes.forEach((program) => { diff --git a/src/db/models/user.ts b/src/db/models/user.ts index 7db2797..cc329f4 100644 --- a/src/db/models/user.ts +++ b/src/db/models/user.ts @@ -1,9 +1,9 @@ import { STRING, ARRAY, InferAttributes, Model } from 'sequelize' -import { sequelize } from '../connection'; +import { sequelize } from '../connection' class User extends Model> { - declare id: string; - declare iamGroups: string[]; + declare id: string + declare iamGroups: string[] } User.init( diff --git a/src/index.ts b/src/index.ts index 190aa70..f5067c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,11 @@ import logger from './util/logger' import errorHandler from './middleware/errors' import accessLogger from './middleware/accessLogger' -import { relevantIAMs, relevantOrganisations, iamToFaculty } from './auth/IAMConfig' +import { + relevantIAMs, + relevantOrganisations, + iamToFaculty, +} from './auth/IAMConfig' import getIAMRights from './auth/IAMRights' import { FACULTIES } from './organisation/faculties' diff --git a/src/organisation/faculties.ts b/src/organisation/faculties.ts index e260175..5e7e37b 100644 --- a/src/organisation/faculties.ts +++ b/src/organisation/faculties.ts @@ -1,4 +1,4 @@ -import { Faculty, IndependentInstitute } from "./types"; +import { Faculty, IndependentInstitute } from './types' const teologinen: Faculty = { code: 'H10', @@ -1049,10 +1049,7 @@ const bioYmparistotieteellinen: Faculty = { sv: 'Magisterprogrammet i miljöförändringar och global hållbarhet', }, level: 'master', - companionFaculties: [ - 'maatalous-metsätieteellinen', - 'valtiotieteellinen', - ], + companionFaculties: ['maatalous-metsätieteellinen', 'valtiotieteellinen'], international: true, }, { @@ -1680,5 +1677,5 @@ export const FACULTIES: Readonly = [ sockom, maatalousMetsatieteellinen, elainlaaketieteellinen, - kielikeskus + kielikeskus, ] as const diff --git a/src/organisation/types.ts b/src/organisation/types.ts index e1d35f3..40cd6ce 100644 --- a/src/organisation/types.ts +++ b/src/organisation/types.ts @@ -1,5 +1,5 @@ -import { TranslatedName } from "../types"; -import { FACULTIES } from "./faculties"; +import { TranslatedName } from '../types' +import { FACULTIES } from './faculties' export const FACULTY_MAP = { teologinen: 'H10', @@ -15,30 +15,30 @@ export const FACULTY_MAP = { 'maatalous-metsätieteellinen': 'H80', eläinlääketieteellinen: 'H90', kielikeskus: 'H906', -} as const; +} as const -export type FacultyCode = typeof FACULTY_MAP[keyof typeof FACULTY_MAP]; +export type FacultyCode = typeof FACULTY_MAP[keyof typeof FACULTY_MAP] -export type FacultyKey = keyof typeof FACULTY_MAP; +export type FacultyKey = keyof typeof FACULTY_MAP export interface Faculty { - readonly code: FacultyCode; - readonly name: TranslatedName; - readonly programmes: Readonly; + readonly code: FacultyCode + readonly name: TranslatedName + readonly programmes: Readonly } export interface IndependentInstitute extends Faculty { - readonly code: FacultyCode; - readonly name: TranslatedName; - readonly independentInstitute: true; + readonly code: FacultyCode + readonly name: TranslatedName + readonly independentInstitute: true } -export type ProgrammeLevel = 'bachelor' | 'master' | 'doctoral'; +export type ProgrammeLevel = 'bachelor' | 'master' | 'doctoral' export type Programme = { - readonly key: string; - readonly name: TranslatedName; - readonly level: ProgrammeLevel; - readonly companionFaculties: Readonly; - readonly international: boolean; -} \ No newline at end of file + readonly key: string + readonly name: TranslatedName + readonly level: ProgrammeLevel + readonly companionFaculties: Readonly + readonly international: boolean +} diff --git a/src/types.ts b/src/types.ts index 427fd63..004b71d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,12 +1,11 @@ - export type TranslatedName = { - fi: string; - en: string; - sv: string; -}; + fi: string + en: string + sv: string +} export type OrganisationAccess = { - read?: boolean; - write?: boolean; - admin?: boolean; + read?: boolean + write?: boolean + admin?: boolean } diff --git a/src/util/sentry.ts b/src/util/sentry.ts index 312b7f3..2973a57 100644 --- a/src/util/sentry.ts +++ b/src/util/sentry.ts @@ -10,4 +10,3 @@ export const initializeSentry = () => { tracesSampleRate: 1.0, }) } - diff --git a/tests/basic.spec.js b/tests/basic.spec.js index bec3773..18d0c28 100644 --- a/tests/basic.spec.js +++ b/tests/basic.spec.js @@ -1,7 +1,6 @@ import { expect, test, beforeAll } from 'vitest' import { api } from './util/utils' import { seed } from './util/seed' -import { mapToDegreeCode } from '../src/auth/common' beforeAll(async () => { await seed() diff --git a/tests/chaos.spec.js b/tests/chaos.spec.js index 2533f4a..596fde2 100644 --- a/tests/chaos.spec.js +++ b/tests/chaos.spec.js @@ -3,9 +3,7 @@ import { relevantIAMs } from '../src/auth/IAMConfig' import { api } from './util/utils' describe('Chaos test', () => { - it('User IAM endpoint should not crash', async () => { - const getRandomIams = () => { const randomIams = [] for (let i = 0; i < 5 + Math.ceil(Math.random() * 10); i++) { @@ -13,7 +11,8 @@ describe('Chaos test', () => { if (Math.random() < 0.5) { randomIams.push(Math.random().toString(36).substring(2, 15)) } else { - const randomIam = relevantIAMs[Math.floor(Math.random() * relevantIAMs.length)] + const randomIam = + relevantIAMs[Math.floor(Math.random() * relevantIAMs.length)] randomIams.push(randomIam) } } @@ -21,14 +20,16 @@ describe('Chaos test', () => { } for (let i = 0; i < 100; i++) { - api.post('', { - userId: 'user-1', - iamGroups: getRandomIams(), - }).then(async res => { - expect(res.status).toBe(200) - const json = await res.json() - expect(json).toHaveProperty('specialGroup') - }) + api + .post('', { + userId: 'user-1', + iamGroups: getRandomIams(), + }) + .then(async (res) => { + expect(res.status).toBe(200) + const json = await res.json() + expect(json).toHaveProperty('specialGroup') + }) } }) -}) \ No newline at end of file +}) diff --git a/tests/datafile.spec.js b/tests/datafile.spec.js index 12d6b88..b5ea5ff 100644 --- a/tests/datafile.spec.js +++ b/tests/datafile.spec.js @@ -1,21 +1,19 @@ -import { describe, expect, it } from "vitest"; -import { FACULTIES as data } from "../src/organisation/faculties"; -import { FACULTY_MAP } from "../src/organisation/types"; +import { describe, expect, it } from 'vitest' +import { FACULTIES as data } from '../src/organisation/faculties' +import { FACULTY_MAP } from '../src/organisation/types' describe('Organisation data', () => { - it('Has faculties in a valid format', () => { const faculties = data - + // There should be 13 faculties expect(faculties.length).toBe(13) // Each faculty code should be unique - const facultyCodes = faculties.map(faculty => faculty.code) + const facultyCodes = faculties.map((faculty) => faculty.code) expect(facultyCodes.length).toBe(new Set(facultyCodes).size) - faculties.forEach(faculty => { - + faculties.forEach((faculty) => { // Faculty should have a name in fi, en and sv expect(faculty).toHaveProperty('name') expect(faculty.name).toHaveProperty('fi') @@ -30,13 +28,13 @@ describe('Organisation data', () => { }) it('Has programmes in valid format', () => { - const programmes = data.flatMap(faculty => faculty.programmes) + const programmes = data.flatMap((faculty) => faculty.programmes) // Each programme key should be unique - const programmeKeys = programmes.map(programme => programme.key) + const programmeKeys = programmes.map((programme) => programme.key) expect(programmeKeys.length).toBe(new Set(programmeKeys).size) - programmes.forEach(programme => { + programmes.forEach((programme) => { // Programme should have a key expect(programme).toHaveProperty('key') @@ -52,7 +50,7 @@ describe('Organisation data', () => { // Programme should have valid companion faculties expect(programme).toHaveProperty('companionFaculties') - programme.companionFaculties.forEach(facultyId => { + programme.companionFaculties.forEach((facultyId) => { expect(FACULTY_MAP).toHaveProperty(facultyId) }) @@ -63,9 +61,9 @@ describe('Organisation data', () => { }) it('Facultymap should map to actual faculty in data', () => { - Object.keys(FACULTY_MAP).forEach(facultyId => { + Object.keys(FACULTY_MAP).forEach((facultyId) => { const code = FACULTY_MAP[facultyId] expect(data).toContainEqual(expect.objectContaining({ code })) }) }) -}) \ No newline at end of file +}) diff --git a/tests/dekaani.spec.js b/tests/dekaani.spec.js index e46757e..87ef497 100644 --- a/tests/dekaani.spec.js +++ b/tests/dekaani.spec.js @@ -1,5 +1,5 @@ -import { describe, expect, it } from "vitest"; -import { api } from "./util/utils"; +import { describe, expect, it } from 'vitest' +import { api } from './util/utils' describe.concurrent('Dekaani', () => { it('gets READ access to faculty and each programme of faculty', async () => { @@ -51,5 +51,4 @@ describe.concurrent('Dekaani', () => { expect(access[programme].admin).toBe(true) }) }) - -}); +}) diff --git a/tests/doctoral.spec.js b/tests/doctoral.spec.js index 6d8ab68..6800382 100644 --- a/tests/doctoral.spec.js +++ b/tests/doctoral.spec.js @@ -1,11 +1,9 @@ -import { describe, expect, it } from "vitest"; -import { api } from "./util/utils"; +import { describe, expect, it } from 'vitest' +import { api } from './util/utils' describe.concurrent('Doctoral schools', () => { it('Doctoral iam gives access to all doctoral schools & gives "doctoral" special group', async () => { - [ - 'hy-tohtorikoulutus-johtoryhma', 'hy-tine' - ].forEach(async (iam) => { + ;['hy-tohtorikoulutus-johtoryhma', 'hy-tine'].forEach(async (iam) => { const res = await api.post('', { userId: 'doctoralschools-user', iamGroups: [iam], @@ -13,7 +11,7 @@ describe.concurrent('Doctoral schools', () => { expect(res.status).toBe(200) const json = await res.json() - + const numberOfDoctoralSchools = 33 // Go to data.ts and CTR+F "level: 'doctoral'" expect(Object.keys(json).length).toBe(numberOfDoctoralSchools + 1) // +1 because 'specialGroup' field. expect(json).toHaveProperty('specialGroup') @@ -50,5 +48,4 @@ describe.concurrent('Doctoral schools', () => { expect(json[programme].admin).toBeFalsy() }) }) - -}); +}) diff --git a/tests/jory.spec.js b/tests/jory.spec.js index 5832dc1..f8a5c4d 100644 --- a/tests/jory.spec.js +++ b/tests/jory.spec.js @@ -1,17 +1,16 @@ -import { describe, expect, it } from "vitest"; -import { api } from "./util/utils"; -import { mapToDegreeCode } from "../src/auth/common"; +import { describe, expect, it } from 'vitest' +import { api } from './util/utils' +import { mapToDegreeCode } from '../src/auth/common' const degreeCode = mapToDegreeCode('200-K001') describe.concurrent('Jory', () => { - it('Gets read access to program', async () => { const res = await api.post('', { userId: 'oiktdk-jory-member', iamGroups: ['hy-oiktdk-on-jory'], }) - + expect(res.status).toBe(200) const json = await res.json() expect(json).toHaveProperty(degreeCode) @@ -25,7 +24,7 @@ describe.concurrent('Jory', () => { userId: 'oiktdk-jory-member', iamGroups: ['hy-oiktdk-on-jory'], }) - + expect(res.status).toBe(200) const json = await res.json() expect(json).toHaveProperty('specialGroup') @@ -38,7 +37,7 @@ describe.concurrent('Jory', () => { userId: 'oikis-kojo', iamGroups: ['hy-oiktdk-on-jory', 'hy-oiktdk-kandi-kojot'], }) - + expect(res.status).toBe(200) expect(await res.json()).toEqual({ [degreeCode]: { @@ -51,5 +50,4 @@ describe.concurrent('Jory', () => { }, }) }) - -}) \ No newline at end of file +}) diff --git a/tests/kosu.spec.js b/tests/kosu.spec.js index 33d2600..4439183 100644 --- a/tests/kosu.spec.js +++ b/tests/kosu.spec.js @@ -1,5 +1,5 @@ -import { describe, expect, it } from "vitest"; -import { api } from "./util/utils"; +import { describe, expect, it } from 'vitest' +import { api } from './util/utils' describe.concurrent('Kosu', () => { it('gets READ access to each faculty and programme of location', async () => { @@ -100,5 +100,4 @@ describe.concurrent('Kosu', () => { expect(access.specialGroup).toHaveProperty('kosu') expect(access.specialGroup.kosu).toBe(true) }) - -}); +}) diff --git a/tests/lomake.spec.js b/tests/lomake.spec.js index ffd8496..0640db2 100644 --- a/tests/lomake.spec.js +++ b/tests/lomake.spec.js @@ -1,5 +1,5 @@ -import { describe, expect, it } from "vitest"; -import { api } from "./util/utils"; +import { describe, expect, it } from 'vitest' +import { api } from './util/utils' describe.concurrent('Lomake', () => { it('Katselmus iam gives the katselmus special group', async () => { @@ -9,7 +9,7 @@ describe.concurrent('Lomake', () => { userId: 'katselmus', iamGroups: [someKatselmusIam], }) - + expect(res.status).toBe(200) const json = await res.json() expect(json).toHaveProperty('specialGroup') diff --git a/tsconfig.json b/tsconfig.json index 8e5820f..7df59dd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,8 +15,8 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "moduleResolution": "node16", - "allowUnreachableCode": false, + "allowUnreachableCode": false }, "include": ["src/**/*"], "exclude": ["node_modules", "build", "tests", "scripts", "prettier.config.js"] -} \ No newline at end of file +} diff --git a/vitest.config.ts b/vitest.config.ts index 9929318..afdd4b5 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,6 +6,6 @@ export default defineConfig({ provider: 'v8', include: ['src/**/*'], exclude: ['src/db/migrations/**/*'], - } + }, }, -}) \ No newline at end of file +})