-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api): ✅ add integration test for Google OAuth
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters