Skip to content

Commit

Permalink
test(api): ✅ add integration test for Google OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
rasouza committed Apr 28, 2024
1 parent 33cb851 commit 384281d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/api/.test.env
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
DATABASE_URL="postgresql://test:test@localhost:5433/tests"

OAUTH_GOOGLE_CLIENT_ID="google-client-id"
OAUTH_GOOGLE_CLIENT_SECRET="google-client-secret"
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"eslint-plugin-prettier": "^5.0.0",
"fishery": "^2.2.2",
"jest": "^29.7.0",
"nock": "^13.5.4",
"prettier": "^3.0.0",
"prisma": "^5.11.0",
"source-map-support": "^0.5.21",
Expand Down
69 changes: 69 additions & 0 deletions apps/api/test/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Server } from 'net'

import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication } from '@nestjs/common'
import request from 'supertest'
import nock from 'nock'
import { JwtService } from '@nestjs/jwt'

import { AppModule } from '../src/app/app.module'

describe('AuthController (e2e)', () => {
let app: INestApplication<Server>

// Mock Google OAuth API
nock('https://www.googleapis.com')
.post('/oauth2/v4/token')
.reply(200, {
access_token: 'your_access_token' // Access token for API access
})

nock('https://www.googleapis.com')
.get('/oauth2/v3/userinfo')
.query({ access_token: 'your_access_token' })
.reply(200, {
sub: '1234567890',
name: 'John Doe',
given_name: 'John',
family_name: 'Doe',
picture: 'image-url',
email: '[email protected]'
})

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile()

app = moduleFixture.createNestApplication()
await app.init()
})

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

it('/auth/google (GET)', async () => {
const response = await request(app.getHttpServer()).get('/auth/google')

expect(response.status).toBe(302)
expect(response.headers.location).toContain('accounts.google.com')
})

it('/auth/google/callback (GET)', async () => {
const jwt = app.get(JwtService)

const response = await request(app.getHttpServer()).get('/auth/google/callback?code=your_code')
const cookies = response.headers['set-cookie'] as unknown as string[]

// Parse access token from cookie
const tokenCookie = cookies.find(cookie => cookie.startsWith('access_token='))
const accessToken = tokenCookie?.split(';')[0].split('=')[1]

if (!accessToken) {
throw new Error('Access token not found')
}

expect(jwt.decode(accessToken).email).toBe('[email protected]')
})
})
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,7 @@ __metadata:
jest: "npm:^29.7.0"
nest-winston: "npm:^1.9.4"
nestjs-zod: "npm:^3.0.0"
nock: "npm:^13.5.4"
passport: "npm:^0.7.0"
passport-google-oauth20: "npm:^2.0.0"
passport-jwt: "npm:^4.0.1"
Expand Down Expand Up @@ -15475,6 +15476,13 @@ __metadata:
languageName: node
linkType: hard

"json-stringify-safe@npm:^5.0.1":
version: 5.0.1
resolution: "json-stringify-safe@npm:5.0.1"
checksum: 10c0/7dbf35cd0411d1d648dceb6d59ce5857ec939e52e4afc37601aa3da611f0987d5cee5b38d58329ceddf3ed48bd7215229c8d52059ab01f2444a338bf24ed0f37
languageName: node
linkType: hard

"json5@npm:^1.0.1, json5@npm:^1.0.2":
version: 1.0.2
resolution: "json5@npm:1.0.2"
Expand Down Expand Up @@ -17393,6 +17401,17 @@ __metadata:
languageName: node
linkType: hard

"nock@npm:^13.5.4":
version: 13.5.4
resolution: "nock@npm:13.5.4"
dependencies:
debug: "npm:^4.1.0"
json-stringify-safe: "npm:^5.0.1"
propagate: "npm:^2.0.0"
checksum: 10c0/9ca47d9d7e4b1f4adf871d7ca12722f8ef1dc7d2b9610b2568f5d9264eae9f424baa24fd9d91da9920b360d641b4243e89de198bd22c061813254a99cc6252af
languageName: node
linkType: hard

"node-abort-controller@npm:^3.0.1":
version: 3.1.1
resolution: "node-abort-controller@npm:3.1.1"
Expand Down Expand Up @@ -19493,6 +19512,13 @@ __metadata:
languageName: node
linkType: hard

"propagate@npm:^2.0.0":
version: 2.0.1
resolution: "propagate@npm:2.0.1"
checksum: 10c0/01e1023b60ae4050d1a2783f976d7db702022dbdb70dba797cceedad8cfc01b3939c41e77032f8c32aa9d93192fe937ebba1345e8604e5ce61fd3b62ee3003b8
languageName: node
linkType: hard

"proxy-addr@npm:~2.0.7":
version: 2.0.7
resolution: "proxy-addr@npm:2.0.7"
Expand Down

0 comments on commit 384281d

Please sign in to comment.