Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check #45

Merged
merged 7 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions __tests__/config/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import Config from '../../src/config/config'

beforeEach(() => {
jest.resetModules()
process.env = {
NOTION_TOKEN: 'NOTION_TOKEN',
DATABASE_ID: 'DATABASE_ID',
PORT: '3000',
GOOGLE_CLOUD_PROJECT: 'GOOGLE_CLOUD_PROJECT',
ADMIN_EMAIL: '[email protected]',
TMDB_READ_KEY: 'TMDB_READ_KEY',
CALENDAR_URL: 'https://CALENDAR_URL',
}
})


describe('env variables all present', () => {
it('returns the config', () => {
const config = new Config()

expect(config.notionToken).toBe('NOTION_TOKEN')
expect(config.notionDatabaseId).toBe('DATABASE_ID')
expect(config.port).toBe(3000)
expect(config.googleCloudProject).toBe('GOOGLE_CLOUD_PROJECT')
expect(config.adminEmail).toBe('[email protected]')
expect(config.tmdbApiKey).toBe('TMDB_READ_KEY')
expect(config.calendarUrl).toBe('https://CALENDAR_URL')
})
})

describe('env missing NOTION_TOKEN', () => {
beforeEach(() => {
delete process.env.NOTION_TOKEN
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('NOTION_TOKEN is missing')
})
})

describe('env missing DATABASE_ID', () => {
beforeEach(() => {
delete process.env.DATABASE_ID
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('DATABASE_ID is missing')
})
})

describe('env missing PORT', () => {
beforeEach(() => {
delete process.env.PORT
})

it('defaults to 8080', () => {
expect(new Config().port).toBe(8080)
})
})

describe('env missing GOOGLE_CLOUD_PROJECT', () => {
beforeEach(() => {
delete process.env.GOOGLE_CLOUD_PROJECT
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('GOOGLE_CLOUD_PROJECT is missing')
})
})


describe('env missing ADMIN_EMAIL', () => {
beforeEach(() => {
delete process.env.ADMIN_EMAIL
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('ADMIN_EMAIL is missing')
})
})

describe('env missing TMDB_READ_KEY', () => {
beforeEach(() => {
delete process.env.TMDB_READ_KEY
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('TMDB_READ_KEY is missing')
})
})

describe('env missing CALENDAR_URL', () => {
beforeEach(() => {
delete process.env.CALENDAR_URL
})

it('throws an error', () => {
expect(() => new Config()).toThrowError('CALENDAR_URL is missing')
})
})
3 changes: 2 additions & 1 deletion __tests__/config/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { initializeApp } from 'firebase/app'
import { applicationDefault } from 'firebase-admin/app'
import { getFirestore } from 'firebase/firestore'
import setupFirestore from '../../src/config/firestore'
import { mockConfig } from '../support/mockConfig'

describe('setupFirestore', () => {
it('initializes the firestore', () => {
const firestore = setupFirestore()
const firestore = setupFirestore(mockConfig())

expect (applicationDefault).toHaveBeenCalledTimes(1)
expect (initializeApp).toHaveBeenCalledTimes(1)
Expand Down
8 changes: 5 additions & 3 deletions __tests__/controllers/cacheController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import { TmdbMock } from '../support/tmdbMock'
import { mockFetch } from '../support/fetchMock'
import Movie from '../../src/models/movie'
import TmdbAdapter from '../../src/data/tmdb/tmdbAdapter'
import { mockConfig } from '../support/mockConfig'

let notionMock: NotionMock

const { res, mockClear } = getMockRes()

const newCacheController = () => {
const firestore = new FirestoreAdapter()
const notion = new NotionAdapter()
const tmdbAdapter = new TmdbAdapter()
const config = mockConfig()
const firestore = new FirestoreAdapter(config)
const notion = new NotionAdapter(config)
const tmdbAdapter = new TmdbAdapter(config)
return new CacheController(firestore, notion, tmdbAdapter)
}

Expand Down
21 changes: 21 additions & 0 deletions __tests__/controllers/healthCheckController.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { beforeEach, describe, expect, it } from '@jest/globals'
import { getMockReq, getMockRes } from '@jest-mock/express'
import HealthCheckController from '../../src/controllers/healthCheckController'

const { res, mockClear } = getMockRes()

beforeEach(() => {
mockClear()
})

describe('index', () => {
it('should respond ok', async () => {
const req = getMockReq()

await HealthCheckController.index(req, res)

expect(res.status).toHaveBeenCalledWith(200)
expect(res.send).toHaveBeenCalledWith('ok')
})
})
6 changes: 3 additions & 3 deletions __tests__/controllers/rsvpController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { getMockReq, getMockRes } from '@jest-mock/express'
import { Timestamp, addDoc } from 'firebase/firestore'
import { FirebaseMock } from '../support/firebaseMock'
import FirestoreAdapter from '../../src/data/firestore/firestoreAdapter'
import { mockConfig } from '../support/mockConfig'

const { res, mockClear } = getMockRes()

beforeEach(() => {
process.env.ADMIN_EMAIL = '[email protected]'
jest.clearAllMocks()
mockClear()
})
Expand All @@ -23,7 +23,7 @@ describe('store', () => {
let firestoreAdapter: FirestoreAdapter

beforeEach(() => {
firestoreAdapter = new FirestoreAdapter()
firestoreAdapter = new FirestoreAdapter(mockConfig())
})

describe('has correct week', () => {
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('store', () => {
expect(addDoc).toHaveBeenCalledWith(
FirebaseMock.mockCollection('mail'),
{
to: 'admin@example.com',
to: 'ADMIN_EMAIL@example.com',
message: {
subject: 'TNMC RSVP: test name',
// eslint-disable-next-line max-len
Expand Down
5 changes: 3 additions & 2 deletions __tests__/controllers/weekController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Request } from 'express'
import { getMockReq, getMockRes } from '@jest-mock/express'
import { FirebaseMock } from '../support/firebaseMock'
import FirestoreAdapter from '../../src/data/firestore/firestoreAdapter'
import { mockConfig } from '../support/mockConfig'

const { res, mockClear } = getMockRes()

Expand All @@ -31,7 +32,7 @@ describe('index', () => {
let req: Request

beforeEach(() => {
firestore = new FirestoreAdapter()
firestore = new FirestoreAdapter(mockConfig())
FirebaseMock.mockWeeks([
{
date: new Date('2021-01-01'),
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('index', () => {
let req: Request

beforeEach(() => {
firestore = new FirestoreAdapter()
firestore = new FirestoreAdapter(mockConfig())
FirebaseMock.mockWeeks([
{
date: new Date('2021-01-01'),
Expand Down
5 changes: 3 additions & 2 deletions __tests__/data/firestore/firestoreAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { transaction } from '../../../__mocks__/firebase/firestore'
import { FirebaseMock } from '../../support/firebaseMock'
import Week from '../../../src/models/week'
import { mockConfig } from '../../support/mockConfig'

let firestore: FirestoreAdapter

Expand All @@ -28,13 +29,13 @@ beforeAll(() => {
})

beforeEach(() => {
firestore = new FirestoreAdapter()
firestore = new FirestoreAdapter(mockConfig())
jest.clearAllMocks()
})

describe('constructor', () => {
it('initializes the firestore', () => {
firestore = new FirestoreAdapter()
firestore = new FirestoreAdapter(mockConfig())

expect (applicationDefault).toHaveBeenCalledTimes(1)
expect (initializeApp).toHaveBeenCalledTimes(1)
Expand Down
59 changes: 9 additions & 50 deletions __tests__/data/notion/notionAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import NotionAdapter from '../../../src/data/notion/notionAdapter'
import { NotionMock } from '../../support/notionMock'
import Movie from '../../../src/models/movie'
import { mockConfig } from '../../support/mockConfig'

let notionMock: NotionMock

Expand All @@ -21,48 +22,6 @@ beforeEach(() => {
jest.clearAllMocks()
})

describe('constructor', () => {
describe('when NOTION_TOKEN and DATABASE_ID are set', () => {
beforeEach(() => {
process.env = {
NOTION_TOKEN: 'NOTION_TOKEN',
DATABASE_ID: 'DATABASE_ID',
}
})

it('should be created successfully', () => {
expect(() => new NotionAdapter()).not.toThrow()
})
})


describe('when NOTION_TOKEN is not set', () => {
beforeEach(() => {
process.env = {
DATABASE_ID: 'DATABASE_ID',
}
})

it('should throw an error', () => {
expect(() => new NotionAdapter())
.toThrowError('Missing NOTION_TOKEN environment variable')
})
})

describe('when DATABASE_ID is not set', () => {
beforeEach(() => {
process.env = {
NOTION_TOKEN: 'NOTION_TOKEN',
}
})

it('should throw an error', () => {
expect(() => new NotionAdapter())
.toThrowError('Missing DATABASE_ID environment variable')
})
})
})

describe('getMovie', () => {
beforeEach(() => {
process.env = {
Expand All @@ -78,7 +37,7 @@ describe('getMovie', () => {
})

it('should return the movie', async () => {
const movie = await new NotionAdapter().getMovie('movieId')
const movie = await new NotionAdapter(mockConfig()).getMovie('movieId')

expect(movie).toEqual({
notionId: 'movieId',
Expand All @@ -95,7 +54,7 @@ describe('getMovie', () => {
})

it ('calls the retrieve method with page_id', async () => {
await new NotionAdapter().getMovie('movieId')
await new NotionAdapter(mockConfig()).getMovie('movieId')

expect(notionMock.retrieve).toHaveBeenCalledWith({ 'page_id': 'movieId' })
})
Expand All @@ -107,7 +66,7 @@ describe('getMovie', () => {
})

it('should throw an error', async () => {
await expect(new NotionAdapter().getMovie('movieId'))
await expect(new NotionAdapter(mockConfig()).getMovie('movieId'))
.rejects.toThrowError('Page was not successfully retrieved')
})
})
Expand All @@ -130,7 +89,7 @@ describe('getWeek', () => {
})

it('should return the week', async () => {
const notion = new NotionAdapter()
const notion = new NotionAdapter(mockConfig())
const week = await notion.getWeek('2021-01-01')

expect(week).toEqual({
Expand All @@ -149,7 +108,7 @@ describe('getWeek', () => {
})

it('should throw an error', async () => {
const notion = new NotionAdapter()
const notion = new NotionAdapter(mockConfig())

expect(notion.getWeek('2021-01-01'))
.rejects.toThrowError('Page was not successfully retrieved')
Expand All @@ -168,7 +127,7 @@ describe('getWeeks', () => {
})

it('should return the weeks', async () => {
const notion = new NotionAdapter()
const notion = new NotionAdapter(mockConfig())
const weeks = await notion.getWeeks()

expect(weeks).toEqual([
Expand All @@ -195,7 +154,7 @@ describe('getWeeks', () => {
})

it('should call query with the correct parameters', async () => {
const notion = new NotionAdapter()
const notion = new NotionAdapter(mockConfig())
await notion.getWeeks()

expect(notionMock.query).toHaveBeenCalledWith({
Expand Down Expand Up @@ -227,7 +186,7 @@ describe('setMovie', () => {
'Theater',
'Showing Url',
)
const notion = new NotionAdapter()
const notion = new NotionAdapter(mockConfig())
await notion.setMovie(movie)

expect(notionMock.update).toHaveBeenCalledWith(movie.toNotion())
Expand Down
3 changes: 2 additions & 1 deletion __tests__/data/tmdb/tmdbAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import TmdbAdapter from '../../../src/data/tmdb/tmdbAdapter'
import Movie from '../../../src/models/movie'
import { TmdbMock } from '../../support/tmdbMock'
import { mockFetch } from '../../support/fetchMock'
import { mockConfig } from '../../support/mockConfig'

let tmdbMock: TmdbMock

Expand All @@ -14,7 +15,7 @@ describe('getMovie', () => {
let tmdbAdapter: TmdbAdapter

beforeEach(() => {
tmdbAdapter = new TmdbAdapter()
tmdbAdapter = new TmdbAdapter(mockConfig())
})

it('should return a movie', async () => {
Expand Down
Loading
Loading