Skip to content

Commit

Permalink
uses nock to mock API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kondanna committed Jul 17, 2021
1 parent 6c63090 commit ff3e1fa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
27 changes: 27 additions & 0 deletions __dummy__/discordOAuthData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default {
tokenResponse: {
access_token: '6qrZcUqja7812RVdnEKjpzOL4CvHBFG',
token_type: 'Bearer',
expires_in: 604800,
refresh_token: 'D43f5y0ahjqew82jZ4NViEr2YafMKhue',
scope: 'email guilds.join gdm.join identify'
},
userInfoResponse: {
id: '756944741073027119',
username: 'charolastra',
avatar: 'ea8f5f59aff14450e892321ba128745d',
discriminator: '3886',
public_flags: 0,
flags: 0,
locale: 'en-US',
mfa_enabled: true,
email: '[email protected]',
verified: true
},
userInfo: {
userId: 1,
username: 'charolastra',
avatarUrl: `https://cdn.discordapp.com/avatars/756944741073027119/ea8f5f59aff14450e892321ba128745d.png`,
refreshToken: 'D43f5y0ahjqew82jZ4NViEr2YafMKhue'
}
}
42 changes: 10 additions & 32 deletions helpers/discordAuth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,16 @@ import {
getUserInfoFromRefreshToken
} from './discordAuth'
import { prisma } from '../prisma'
import fetch, {Response} from 'node-fetch'
import { tokenResponse, userInfoResponse, userInfo } from '../__dummy__/discordOAuthData'

const mockTokenResponse = {
access_token: '6qrZcUqja7812RVdnEKjpzOL4CvHBFG',
token_type: 'Bearer',
expires_in: 604800,
refresh_token: 'D43f5y0ahjqew82jZ4NViEr2YafMKhue',
scope: 'email guilds.join gdm.join identify'
}

const mockUserInfoResponse = {
id: '756944741073027119',
username: 'charolastra',
avatar: 'ea8f5f59aff14450e892321ba128745d',
discriminator: '3886',
public_flags: 0,
flags: 0,
locale: 'en-US',
mfa_enabled: true,
email: '[email protected]',
verified: true
}

const mockUserInfo = {
userId: 1,
username: 'charolastra',
avatarUrl: `https://cdn.discordapp.com/avatars/756944741073027119/ea8f5f59aff14450e892321ba128745d.png`,
refreshToken: 'D43f5y0ahjqew82jZ4NViEr2YafMKhue'
}
const nock = require('nock')

describe('getUserInfoFromRefreshToken function', () => {
jest.mock('node-fetch')
const fetch = require('node-fetch')
const { Response } = jest.requireActual('node-fetch')
beforeEach(() => {
fetch.mockClear()
jest.clearAllMocks()
})
const userUpdateFn = (prisma.user.update = jest.fn())
Expand All @@ -49,12 +24,15 @@ describe('getUserInfoFromRefreshToken function', () => {
})

it('should return user info if refresh token valid', async () => {
fetch
.mockReturnValueOnce(Promise.resolve(new Response(mockTokenResponse)))
.mockReturnValueOnce(Promise.resolve(new Response(mockUserInfoResponse)))
const discordAPI = nock('https://discordapp.com/api')
discordAPI.post('/oauth2/token').reply(201, tokenResponse)
discordAPI.get('/users/@me').reply(200, userInfoResponse)
// fetch
// .mockReturnValueOnce(new Response(JSON.stringify(tokenResponse)))
// .mockReturnValueOnce(new Response(JSON.stringify(userInfoResponse)))
await expect(
getUserInfoFromRefreshToken(1, 'mockRefreshToken')
).resolves.toEqual(mockUserInfo)
).resolves.toEqual(userInfo)
expect(userUpdateFn).toBeCalled()
expect(fetch.mock.calls.length).toBe(2)
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"nanoid": "^3.1.23",
"next": "^11.0.1",
"next-connect": "^0.10.0",
"nock": "^13.1.1",
"nodemailer": "^6.6.2",
"posthog-js": "^1.11.4",
"prism-react-renderer": "^1.2.1",
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10382,6 +10382,11 @@ json-stable-stringify@^1.0.1:
dependencies:
jsonify "~0.0.0"

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=

json-to-pretty-yaml@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/json-to-pretty-yaml/-/json-to-pretty-yaml-1.2.2.tgz#f4cd0bd0a5e8fe1df25aaf5ba118b099fd992d5b"
Expand Down Expand Up @@ -10804,6 +10809,11 @@ lodash.once@^4.0.0:
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -11681,6 +11691,16 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"

nock@^13.1.1:
version "13.1.1"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.1.1.tgz#3c830129d4560957f59b6f480a41ddbaf9cf57af"
integrity sha512-YKTR9MjfK3kS9/l4nuTxyYm30cgOExRHzkLNhL8nhEUyU4f8Za/dRxOqjhVT1vGs0svWo3dDnJTUX1qxYeWy5w==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
lodash.set "^4.3.2"
propagate "^2.0.0"

node-addon-api@^3.0.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
Expand Down Expand Up @@ -12800,6 +12820,11 @@ [email protected], prop-types@^15.0.0, prop-types@^15.6.0, prop-types@^15.6.1, p
object-assign "^4.1.1"
react-is "^16.8.1"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

property-expr@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.4.tgz#37b925478e58965031bb612ec5b3260f8241e910"
Expand Down

0 comments on commit ff3e1fa

Please sign in to comment.