Skip to content

Commit

Permalink
Lint & format
Browse files Browse the repository at this point in the history
  • Loading branch information
Veikkosuhonen committed Mar 21, 2024
1 parent 21680d9 commit e17e099
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 104 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -33,7 +34,9 @@ Response
}
}
```

### Other routes

`GET` `/ping`, `/access-to-all`, `/organisation-data`, `/all-access`, `/iam-groups`, `/:userId`

`POST` `user-organisations`
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 9 additions & 3 deletions src/auth/IAMRights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/db/models/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { STRING, ARRAY, InferAttributes, Model } from 'sequelize'
import { sequelize } from '../connection';
import { sequelize } from '../connection'

class User extends Model<InferAttributes<User>> {
declare id: string;
declare iamGroups: string[];
declare id: string
declare iamGroups: string[]
}

User.init(
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
9 changes: 3 additions & 6 deletions src/organisation/faculties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Faculty, IndependentInstitute } from "./types";
import { Faculty, IndependentInstitute } from './types'

const teologinen: Faculty = {
code: 'H10',
Expand Down Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -1680,5 +1677,5 @@ export const FACULTIES: Readonly<Faculty[]> = [
sockom,
maatalousMetsatieteellinen,
elainlaaketieteellinen,
kielikeskus
kielikeskus,
] as const
36 changes: 18 additions & 18 deletions src/organisation/types.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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<Programme[]>;
readonly code: FacultyCode
readonly name: TranslatedName
readonly programmes: Readonly<Programme[]>
}

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<FacultyKey[]>;
readonly international: boolean;
}
readonly key: string
readonly name: TranslatedName
readonly level: ProgrammeLevel
readonly companionFaculties: Readonly<FacultyKey[]>
readonly international: boolean
}
15 changes: 7 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion src/util/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ export const initializeSentry = () => {
tracesSampleRate: 1.0,
})
}

1 change: 0 additions & 1 deletion tests/basic.spec.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
25 changes: 13 additions & 12 deletions tests/chaos.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ 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++) {
// 50% are random strings
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)
}
}
return randomIams
}

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')
})
}
})
})
})
26 changes: 12 additions & 14 deletions tests/datafile.spec.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')

Expand All @@ -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)
})

Expand All @@ -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 }))
})
})
})
})
7 changes: 3 additions & 4 deletions tests/dekaani.spec.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -51,5 +51,4 @@ describe.concurrent('Dekaani', () => {
expect(access[programme].admin).toBe(true)
})
})

});
})
13 changes: 5 additions & 8 deletions tests/doctoral.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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],
})

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')
Expand Down Expand Up @@ -50,5 +48,4 @@ describe.concurrent('Doctoral schools', () => {
expect(json[programme].admin).toBeFalsy()
})
})

});
})
Loading

0 comments on commit e17e099

Please sign in to comment.