Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
rafnarnason committed Oct 28, 2024
1 parent 37b7104 commit e54c78e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 226 deletions.

This file was deleted.

43 changes: 0 additions & 43 deletions apps/services/endorsements/api/seeders/20210505212921-e2e-tests.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Endorsement extends Model {

@ApiProperty()
@Column({
type: DataType.NUMBER,
type: DataType.INTEGER,
allowNull: true,
})
counter!: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,127 +1,79 @@
import { Type } from '@nestjs/common'
import { getConnectionToken } from '@nestjs/sequelize'
import { Sequelize } from 'sequelize-typescript'
import request from 'supertest'
import {
TestApp,
setupApp,
setupAppWithoutAuth,
truncate,
} from '@island.is/testing/nest'
import { createCurrentUser } from '@island.is/testing/fixtures'
import { EndorsementsScope, AdminPortalScope } from '@island.is/auth/scopes'
// import { FixtureFactory } from '../../../test/fixtureFactory'
import { AppModule } from '../../app.module'
import { SequelizeConfigService } from '../../sequelizeConfig.service'


import { Injectable } from '@nestjs/common'
import { getModelToken } from '@nestjs/sequelize'
import { Test, TestingModule } from '@nestjs/testing'
import { EndorsementListController } from './endorsementList.controller'
import { EndorsementListService } from './endorsementList.service'
import { EndorsementList } from './endorsementList.model'
import { v4 as uuid } from 'uuid'

@Injectable()
export class FixtureFactory {
private endorsementListModel: typeof EndorsementList

constructor(private app: TestApp) {
this.endorsementListModel = this.app.get<typeof EndorsementList>(
getModelToken(EndorsementList),
)
}

async createEndorsementList(input: Partial<EndorsementList> = {}) {
const defaults = {
id: uuid(),
title: 'Test Endorsement List',
description: 'Test Description',
owner: '0101303099',
openedDate: new Date(),
closedDate: new Date(Date.now() + 24 * 60 * 60 * 1000), // tomorrow
tags: ['test-tag'],
meta: {},
adminLock: false,
endorsementCount: 0,
}
import { EndorsementTag } from './constants'

return await this.endorsementListModel.create({
...defaults,
...input,
})
describe('EndorsementListController', () => {
let controller: EndorsementListController
let service: EndorsementListService

const mockEndorsementList = {
id: '123e4567-e89b-12d3-a456-426614174000',
title: 'Test Petition',
description: 'This is a test petition',
owner: '1234567890',
endorsementCount: 0,
closed: false,
closedDate: new Date('2024-12-31'),
created: new Date(),
modified: new Date(),
endorsementMeta: {},
locked: false,
tags: [EndorsementTag.GENERAL_PETITION],
validationRules: {}
}

async createEndorsementLists(count: number, input: Partial<EndorsementList> = {}) {
const lists = []
for (let i = 0; i < count; i++) {
lists.push(await this.createEndorsementList(input))
beforeEach(async () => {
const mockEndorsementListService = {
findSingleList: jest.fn()
}
return lists
}
}

describe('EndorsementListController', () => {
let app: TestApp
let server: request.SuperTest<request.Test>
let fixtureFactory: FixtureFactory
let sequelize: Sequelize

beforeAll(async () => {
// We set this up in the general beforeAll but will override user per describe block
app = await setupApp({
AppModule,
SequelizeConfigService,
dbType: 'postgres', // Specify postgres as the db type for tests

})

fixtureFactory = new FixtureFactory(app)
sequelize = await app.resolve(getConnectionToken() as Type<Sequelize>)
server = request(app.getHttpServer())
const module: TestingModule = await Test.createTestingModule({
controllers: [EndorsementListController],
providers: [
{
provide: EndorsementListService,
useValue: mockEndorsementListService
}
]
}).compile()

controller = module.get<EndorsementListController>(EndorsementListController)
service = module.get<EndorsementListService>(EndorsementListService)
})

afterEach(async () => {
await truncate(sequelize)
afterEach(() => {
jest.clearAllMocks()
})

afterAll(async () => {
await app.cleanUp()
})

describe('GET /endorsement-list - No Auth', () => {
it('should return 401 when user is not authenticated', async () => {
const app = await setupAppWithoutAuth({
AppModule,
SequelizeConfigService,
dbType: 'postgres', // Specify postgres as the db type for tests

})
const server = request(app.getHttpServer())
describe('getGeneralPetitionList', () => {
it('should return a single general petition list by id', async () => {
// Arrange
const listId = '123e4567-e89b-12d3-a456-426614174000'
jest.spyOn(service, 'findSingleList').mockResolvedValue(mockEndorsementList)

const res = await server.get('/endorsement-list')
// Act
const result = await controller.getGeneralPetitionList(listId)

expect(res.status).toBe(401)

await app.cleanUp()
// Assert
expect(result).toEqual(mockEndorsementList)
expect(service.findSingleList).toHaveBeenCalledWith(listId)
expect(service.findSingleList).toHaveBeenCalledTimes(1)
})
})

describe('GET /endorsement-list - With Auth No Scope', () => {
it('should return 403 when user lacks required scope', async () => {
const app = await setupApp({
AppModule,
SequelizeConfigService,
dbType: 'postgres', // Specify postgres as the db type for tests

user: createCurrentUser({}), // No scopes
})
const server = request(app.getHttpServer())
it('should return null when list is not found', async () => {
// Arrange
const listId = 'non-existent-id'
jest.spyOn(service, 'findSingleList').mockResolvedValue(null)

const res = await server.get('/endorsement-list')
// Act
const result = await controller.getGeneralPetitionList(listId)

expect(res.status).toBe(403)

await app.cleanUp()
// Assert
expect(result).toBeNull()
expect(service.findSingleList).toHaveBeenCalledWith(listId)
expect(service.findSingleList).toHaveBeenCalledTimes(1)
})
})

})
})

0 comments on commit e54c78e

Please sign in to comment.