Skip to content

Commit

Permalink
Mock Apple JWT tokens and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OultimoCoder committed Sep 7, 2023
1 parent bf95475 commit 9abbde3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
82 changes: 44 additions & 38 deletions tests/integration/auth/oauth/apple.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { faker } from '@faker-js/faker'
import jwt from '@tsndr/cloudflare-worker-jwt'
import httpStatus from 'http-status'
import { TableReference } from 'kysely/dist/cjs/parser/table-parser'
import { authProviders } from '../../../../src/config/authProviders'
Expand Down Expand Up @@ -49,10 +50,11 @@ describe('Oauth Apple routes', () => {
})
test('should return 200 and successfully register user if request data is ok', async () => {
const fetchMock = getMiniflareFetchMock()
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/apple/auth/token'})
.reply(200, JSON.stringify({access_token: '1234'}))
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))
const providerId = '123456'
const res = await request('/v1/auth/apple/callback', {
method: 'POST',
Expand Down Expand Up @@ -95,7 +97,7 @@ describe('Oauth Apple routes', () => {
.selectAll()
.where('authorisations.provider_type', '=', authProviders.APPLE)
.where('authorisations.user_id', '=', body.user.id)
.where('authorisations.provider_user_id', '=', String(newUser.id))
.where('authorisations.provider_user_id', '=', newUser.sub)
.executeTakeFirst()

expect(oauthUser).toBeDefined()
Expand All @@ -115,14 +117,11 @@ describe('Oauth Apple routes', () => {
newUser.sub = appleUser.provider_user_id

const fetchMock = getMiniflareFetchMock()
const appleApiMock = fetchMock.get('https://apple.com')
appleApiMock
.intercept({method: 'GET', path: '/api/users/@me'})
.reply(200, JSON.stringify(newUser))
const appleMock = fetchMock.get('https://appleapp.com')
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.reply(200, JSON.stringify({access_token: '1234'}))
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))

const providerId = '123456'
const res = await request('/v1/auth/apple/callback', {
Expand Down Expand Up @@ -154,14 +153,11 @@ describe('Oauth Apple routes', () => {
newUser.email = userOne.email

const fetchMock = getMiniflareFetchMock()
const appleApiMock = fetchMock.get('https://apple.com')
appleApiMock
.intercept({method: 'GET', path: '/api/users/@me'})
.reply(200, JSON.stringify(newUser))
const appleMock = fetchMock.get('https://appleapp.com')
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.reply(200, JSON.stringify({access_token: '1234'}))
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))

const providerId = '123456'
const res = await request('/v1/auth/apple/callback', {
Expand All @@ -178,13 +174,29 @@ describe('Oauth Apple routes', () => {
message: 'Cannot signup with apple, user already exists with that email'
})
})


test('should return xxx if no apple email is provided', async () => {
const fetchMock = getMiniflareFetchMock()
delete newUser.email
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))
const providerId = '123456'
const res = await request('/v1/auth/apple/callback', {
method: 'POST',
body: JSON.stringify({code: providerId}),
headers: {
'Content-Type': 'application/json'
}
})
expect(res.status).toBe(httpStatus.UNAUTHORIZED)
})
test('should return 401 if code is invalid', async () => {
const fetchMock = getMiniflareFetchMock()
const appleMock = fetchMock.get('https://appleapp.com')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.intercept({method: 'POST', path: '/auth/token'})
.reply(httpStatus.UNAUTHORIZED, JSON.stringify({error: 'error'}))

const providerId = '123456'
Expand Down Expand Up @@ -225,14 +237,11 @@ describe('Oauth Apple routes', () => {
const userOneAccessToken = await getAccessToken(ids[0], userOne.role, config.jwt)

const fetchMock = getMiniflareFetchMock()
const appleApiMock = fetchMock.get('https://apple.com')
appleApiMock
.intercept({method: 'GET', path: '/api/users/@me'})
.reply(200, JSON.stringify(newUser))
const appleMock = fetchMock.get('https://appleapp.com')
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.reply(200, JSON.stringify({access_token: '1234'}))
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))

const providerId = '123456'
const res = await request(`/v1/auth/apple/${userId}`, {
Expand Down Expand Up @@ -268,7 +277,7 @@ describe('Oauth Apple routes', () => {
.selectAll()
.where('authorisations.provider_type', '=', authProviders.APPLE)
.where('authorisations.user_id', '=', userId)
.where('authorisations.provider_user_id', '=', String(newUser.id))
.where('authorisations.provider_user_id', '=', newUser.sub)
.executeTakeFirst()

expect(oauthUser).toBeDefined()
Expand All @@ -285,14 +294,11 @@ describe('Oauth Apple routes', () => {
.execute()

const fetchMock = getMiniflareFetchMock()
const appleApiMock = fetchMock.get('https://apple.com')
appleApiMock
.intercept({method: 'GET', path: '/api/users/@me'})
.reply(200, JSON.stringify(newUser))
const appleMock = fetchMock.get('https://appleapp.com')
const mockJWT = await jwt.sign(newUser, 'randomSecret')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.reply(200, JSON.stringify({access_token: '1234'}))
.intercept({method: 'POST', path: '/auth/token'})
.reply(200, JSON.stringify({access_token: mockJWT}))

const providerId = '123456'
const res = await request(`/v1/auth/apple/${userId}`, {
Expand Down Expand Up @@ -322,9 +328,9 @@ describe('Oauth Apple routes', () => {
const userOneAccessToken = await getAccessToken(ids[0], userOne.role, config.jwt)

const fetchMock = getMiniflareFetchMock()
const appleMock = fetchMock.get('https://appleapp.com')
const appleMock = fetchMock.get('https://appleid.apple.com')
appleMock
.intercept({method: 'POST', path: '/api/oauth2/token'})
.intercept({method: 'POST', path: '/auth/token'})
.reply(httpStatus.UNAUTHORIZED, JSON.stringify({error: 'error'}))

const providerId = '123456'
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/auth/oauth/discord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Oauth Discord routes', () => {
.selectAll()
.where('authorisations.provider_type', '=', authProviders.DISCORD)
.where('authorisations.user_id', '=', body.user.id)
.where('authorisations.provider_user_id', '=', String(newUser.id))
.where('authorisations.provider_user_id', '=', newUser.id)
.executeTakeFirst()

expect(oauthUser).toBeDefined()
Expand Down Expand Up @@ -216,12 +216,12 @@ describe('Oauth Discord routes', () => {
})

describe('POST /v1/auth/discord/:userId', () => {
let newUser: Omit<OauthUserModel, 'providerType'>
let newUser: DiscordUserType
beforeAll(async () => {
newUser = {
id: faker.number.int(),
name: faker.person.fullName(),
email: faker.internet.email(),
id: faker.number.int().toString(),
username: faker.person.fullName(),
email: faker.internet.email()
}
})
test('should return 200 and successfully link discord account', async () => {
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('Oauth Discord routes', () => {
.selectAll()
.where('authorisations.provider_type', '=', authProviders.DISCORD)
.where('authorisations.user_id', '=', userId)
.where('authorisations.provider_user_id', '=', String(newUser.id))
.where('authorisations.provider_user_id', '=', newUser.id)
.executeTakeFirst()

expect(oauthUser).toBeDefined()
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('Oauth Discord routes', () => {
.selectAll()
.where('authorisations.provider_type', '=', authProviders.DISCORD)
.where('authorisations.user_id', '=', userId)
.where('authorisations.provider_user_id', '=', String(newUser.id))
.where('authorisations.provider_user_id', '=', newUser.id)
.executeTakeFirst()

expect(oauthUser).toBeUndefined()
Expand Down

0 comments on commit 9abbde3

Please sign in to comment.