mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-03-01 12:44:28 +00:00
This is a side quest of #8558. The motivation is to be able to do dependency injection in the tests without overwriting global data. I saw the first merge conflict from #8551 and voila: It seems @Mogge could have used this already. refactor: follow @Mogge's review See: https://github.com/Ocelot-Social-Community/Ocelot-Social/pull/8603#pullrequestreview-2880714796 refactor: better test helper methods wip: continue refactoring wip: continue posts continue wip: continue groups continue registration registration continue messages continue observeposts continue categories continue posts in groups continue invite codes refactor: continue notificationsMiddleware continue statistics spec followed-users online-status mentions-in-groups posts-in-groups email spec finish all tests improve typescript missed one test remove one more reference of CONFIG eliminate one more global import of CONFIG fix language spec test fix two more test suites refactor: completely mock out 3rd part API request refactor test fixed user_management spec fixed more locatoin specs install types for jsonwebtoken one more fetchmock fixed one more suite fix one more spec yet another spec fix spec delete whitespaces remove beforeAll that the same as the default fix merge conflict fix e2e test refactor: use single callback function for `context` setup refactor: display logs from backend during CI Because why not? fix seeds fix login refactor: one unnecessary naming refactor: better editor support refactor: fail early Interestingly, I've had to destructure `context.user` in order to make typescript happy. Weird. refactor: undo changes to workflows - no effect We're running in `--detached` mode on CI, so I guess we won't be able to see the logs anyways. refactor: remove fetch from context after review See: refactor: found an easier way for required props Co-authored-by: Max <maxharz@gmail.com> Co-authored-by: Ulf Gebhardt <ulf.gebhardt@webcraft-media.de>
154 lines
4.8 KiB
TypeScript
154 lines
4.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
import Factory, { cleanDatabase } from '@db/factories'
|
|
import User from '@db/models/User'
|
|
import { getDriver, getNeode } from '@db/neo4j'
|
|
import { TEST_CONFIG } from '@root/test/helpers'
|
|
|
|
import { decode } from './decode'
|
|
import { encode } from './encode'
|
|
|
|
const driver = getDriver()
|
|
const neode = getNeode()
|
|
const config = {
|
|
JWT_SECRET: 'supersecret',
|
|
JWT_EXPIRES: TEST_CONFIG.JWT_EXPIRES,
|
|
CLIENT_URI: TEST_CONFIG.CLIENT_URI,
|
|
GRAPHQL_URI: TEST_CONFIG.GRAPHQL_URI,
|
|
}
|
|
const context = { driver, config }
|
|
|
|
beforeAll(async () => {
|
|
await cleanDatabase()
|
|
})
|
|
|
|
afterAll(async () => {
|
|
await cleanDatabase()
|
|
await driver.close()
|
|
})
|
|
|
|
// TODO: avoid database clean after each test in the future if possible for performance and flakyness reasons by filling the database step by step, see issue https://github.com/Ocelot-Social-Community/Ocelot-Social/issues/4543
|
|
afterEach(async () => {
|
|
await cleanDatabase()
|
|
})
|
|
|
|
describe('decode', () => {
|
|
let authorizationHeader: string | undefined | null
|
|
const returnsNull = async () => {
|
|
await expect(decode(context)(authorizationHeader)).resolves.toBeNull()
|
|
}
|
|
|
|
describe('given `null` as JWT Bearer token', () => {
|
|
beforeEach(() => {
|
|
authorizationHeader = null
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
|
|
describe('given no JWT Bearer token', () => {
|
|
beforeEach(() => {
|
|
authorizationHeader = undefined
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
|
|
describe('given malformed JWT Bearer token', () => {
|
|
beforeEach(() => {
|
|
authorizationHeader = 'blah'
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
|
|
describe('given valid JWT Bearer token', () => {
|
|
describe('and corresponding user in the database', () => {
|
|
let user
|
|
let validAuthorizationHeader: string
|
|
beforeEach(async () => {
|
|
user = await Factory.build(
|
|
'user',
|
|
{
|
|
role: 'user',
|
|
name: 'Jenny Rostock',
|
|
id: 'u3',
|
|
slug: 'jenny-rostock',
|
|
},
|
|
{
|
|
image: Factory.build('image', {
|
|
url: 'https://s3.amazonaws.com/uifaces/faces/twitter/sasha_shestakov/128.jpg',
|
|
}),
|
|
email: 'user@example.org',
|
|
},
|
|
)
|
|
validAuthorizationHeader = encode(context)(await user.toJson())
|
|
})
|
|
|
|
it('returns user object without email', async () => {
|
|
await expect(decode(context)(validAuthorizationHeader)).resolves.toMatchObject({
|
|
role: 'user',
|
|
name: 'Jenny Rostock',
|
|
id: 'u3',
|
|
slug: 'jenny-rostock',
|
|
})
|
|
})
|
|
|
|
it('sets `lastActiveAt`', async () => {
|
|
let user = await neode.first<typeof User>('User', { id: 'u3' }, undefined)
|
|
await expect(user.toJson()).resolves.not.toHaveProperty('lastActiveAt')
|
|
await decode(context)(validAuthorizationHeader)
|
|
user = await neode.first<typeof User>('User', { id: 'u3' }, undefined)
|
|
await expect(user.toJson()).resolves.toMatchObject({
|
|
lastActiveAt: expect.any(String),
|
|
})
|
|
})
|
|
|
|
it('updates `lastActiveAt` for every authenticated request', async () => {
|
|
let user = await neode.first('User', { id: 'u3' }, undefined)
|
|
await user.update({
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
updatedAt: new Date().toISOString() as any,
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
lastActiveAt: '2019-10-03T23:33:08.598Z' as any,
|
|
})
|
|
await expect(user.toJson()).resolves.toMatchObject({
|
|
lastActiveAt: '2019-10-03T23:33:08.598Z',
|
|
})
|
|
await decode(context)(validAuthorizationHeader)
|
|
user = await neode.first<typeof User>('User', { id: 'u3' }, undefined)
|
|
await expect(user.toJson()).resolves.toMatchObject({
|
|
// should be a different time by now ;)
|
|
lastActiveAt: expect.not.stringContaining('2019-10-03T23:33'),
|
|
})
|
|
})
|
|
|
|
describe('but user is deleted', () => {
|
|
beforeEach(async () => {
|
|
await user.update({ updatedAt: new Date().toISOString(), deleted: true })
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
|
|
describe('but user is disabled', () => {
|
|
beforeEach(async () => {
|
|
await user.update({ updatedAt: new Date().toISOString(), disabled: true })
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
|
|
describe('and NO corresponding user in the database', () => {
|
|
beforeEach(async () => {
|
|
await cleanDatabase()
|
|
authorizationHeader = validAuthorizationHeader
|
|
})
|
|
|
|
it('returns null', returnsNull)
|
|
})
|
|
})
|
|
})
|
|
})
|