Skip to content

Commit

Permalink
Merge pull request #41 from CS3219-AY2425S1/12-be-user-service-implem…
Browse files Browse the repository at this point in the history
…ent-authentication-api

Implement authentication API
  • Loading branch information
lynnlow175 authored Sep 21, 2024
2 parents b09a8b1 + 3c76562 commit 6d18564
Show file tree
Hide file tree
Showing 12 changed files with 587 additions and 14 deletions.
8 changes: 8 additions & 0 deletions backend/user-service/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import request from 'supertest'
import app from '../src/index'

jest.mock('../src/common/config.util', () => ({
NODE_ENV: 'test',
PORT: '3002',
DB_URL: 'placeholder',
ACCESS_TOKEN_PRIVATE_KEY: 'placeholder',
ACCESS_TOKEN_PUBLIC_KEY: 'placeholder',
}))

describe('Index', () => {
describe('GET /', () => {
it('should return 200 OK', async () => {
Expand Down
28 changes: 24 additions & 4 deletions backend/user-service/__tests__/routes/user.routes.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { MongoDBContainer, StartedMongoDBContainer } from '@testcontainers/mongodb'
import { generateKeyPairSync } from 'crypto'
import express, { Express } from 'express'

import { Proficiency } from '../../src/types/Proficiency'
import { Role } from '../../src/types/Role'
import connectToDatabase from '../../src/common/mongodb.util'
import logger from '../../src/common/logger.util'
import mongoose from 'mongoose'
import request from 'supertest'
import config from '../../src/common/config.util'
import logger from '../../src/common/logger.util'
import connectToDatabase from '../../src/common/mongodb.util'
import userRouter from '../../src/routes/user.routes'
import { Proficiency } from '../../src/types/Proficiency'
import { Role } from '../../src/types/Role'

jest.mock('../../src/common/config.util', () => ({
NODE_ENV: 'test',
PORT: '3002',
DB_URL: 'placeholder',
ACCESS_TOKEN_PRIVATE_KEY: 'placeholder',
ACCESS_TOKEN_PUBLIC_KEY: 'placeholder',
}))

describe('User Routes', () => {
let app: Express
Expand Down Expand Up @@ -36,6 +46,16 @@ describe('User Routes', () => {
const connectionString = `${startedContainer.getConnectionString()}?directConnection=true`
logger.info(`[User Routes Test] MongoDB container started on ${connectionString}`)

const { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
})

jest.replaceProperty(config, 'ACCESS_TOKEN_PRIVATE_KEY', Buffer.from(privateKey).toString('base64'))
jest.replaceProperty(config, 'ACCESS_TOKEN_PUBLIC_KEY', Buffer.from(publicKey).toString('base64'))
jest.replaceProperty(config, 'DB_URL', connectionString)

app = express()
app.use(express.json())
app.use('/users', userRouter)
Expand Down
197 changes: 197 additions & 0 deletions backend/user-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions backend/user-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"dotenv": "^16.4.5",
"express": "^4.21.0",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.6.3",
"passport": "^0.7.0",
"passport-local": "^1.0.0",
"winston": "^3.14.2"
},
"devDependencies": {
Expand All @@ -30,7 +33,10 @@
"@types/express": "^4.17.21",
"@types/helmet": "^0.0.48",
"@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.7",
"@types/node": "^22.5.5",
"@types/passport": "^1.0.16",
"@types/passport-local": "^1.0.38",
"@types/supertest": "^6.0.2",
"globals": "^15.9.0",
"jest": "^29.7.0",
Expand Down
13 changes: 13 additions & 0 deletions backend/user-service/src/common/config.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ValidationError } from 'class-validator'
import { Config } from '../types/Config'
import logger from './logger.util'

const config: Config = Config.fromEnv(process.env)
config.validateOrReject().catch((errors: ValidationError[]) => {
logger.error(
`[Config] Invalid or missing configuration: ${errors.map((error: ValidationError) => error.property).join(', ')}`
)
process.exit(1)
})

export default config
Loading

0 comments on commit 6d18564

Please sign in to comment.