feat: usage of ESM modules imports (instead of CommonJS) (#5)
Replace `jest` with `tap`.
This commit is contained in:
@ -1,137 +1,216 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { userExample } from '../../../models/User.js'
|
||||
import { userSettingsExample } from '../../../models/UserSettings.js'
|
||||
import { prismaMock } from '../../../__test__/setup.js'
|
||||
import { OAuthStrategy } from '../OAuthStrategy.js'
|
||||
import prisma from '../../database/prisma.js'
|
||||
import { refreshTokenExample } from '../../../models/RefreshToken.js'
|
||||
|
||||
const oauthStrategy = new OAuthStrategy('discord')
|
||||
|
||||
describe('/tools/utils/OAuthStrategy - callbackSignin', () => {
|
||||
it('should signup the user', async () => {
|
||||
const name = 'Martin'
|
||||
const id = '12345'
|
||||
prismaMock.oAuth.findFirst.mockResolvedValue(null)
|
||||
prismaMock.user.count.mockResolvedValue(0)
|
||||
prismaMock.user.create.mockResolvedValue({
|
||||
...userExample,
|
||||
name
|
||||
await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
await t.test('callbackSignin', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
|
||||
prismaMock.oAuth.create.mockResolvedValue({
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
|
||||
await t.test('should signup the user', async (t) => {
|
||||
const name = 'Martin'
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'user').value({
|
||||
count: async () => {
|
||||
return 0
|
||||
},
|
||||
create: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
name
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
create: async () => {
|
||||
return refreshTokenExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
create: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
create: async () => {
|
||||
return {
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
}
|
||||
}
|
||||
})
|
||||
const oAuthCreateSpy = sinon.spy(prisma.oAuth, 'create')
|
||||
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||
const userCountSpy = sinon.spy(prisma.user, 'count')
|
||||
const userCreateSpy = sinon.spy(prisma.user, 'create')
|
||||
const userSettingCreateSpy = sinon.spy(prisma.userSetting, 'create')
|
||||
await oauthStrategy.callbackSignin({ id, name })
|
||||
t.equal(
|
||||
oAuthCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(userCountSpy.calledWith({ where: { name } }), true)
|
||||
t.equal(userCreateSpy.calledWith({ data: { name } }), true)
|
||||
t.equal(
|
||||
userSettingCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
})
|
||||
await oauthStrategy.callbackSignin({ id, name })
|
||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
})
|
||||
|
||||
await t.test('callbackAddStrategy', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('should add the strategy to the user', async (t) => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
create: async () => {
|
||||
return {
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
}
|
||||
}
|
||||
})
|
||||
const oAuthCreateSpy = sinon.spy(prisma.oAuth, 'create')
|
||||
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
t.equal(result, 'success')
|
||||
t.equal(
|
||||
oAuthCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
await t.test(
|
||||
'should not add the strategy if the account of the provider is already used',
|
||||
async (t) => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
id: 1,
|
||||
userId: 2,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
}
|
||||
}
|
||||
})
|
||||
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
t.equal(result, 'This account is already used by someone else')
|
||||
t.equal(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
}
|
||||
})
|
||||
expect(prismaMock.user.count).toHaveBeenCalledWith({
|
||||
where: { name }
|
||||
})
|
||||
expect(prismaMock.user.create).toHaveBeenCalledWith({
|
||||
data: { name }
|
||||
})
|
||||
expect(prismaMock.userSetting.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
userId: userExample.id
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'should not add the strategy if the user is already connected with it',
|
||||
async (t) => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
}
|
||||
}
|
||||
})
|
||||
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
t.equal(result, 'You are already using this account')
|
||||
t.equal(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
}),
|
||||
true
|
||||
)
|
||||
}
|
||||
})
|
||||
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('/tools/utils/OAuthStrategy - callbackAddStrategy', () => {
|
||||
it('should add the strategy to the user', async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
prismaMock.oAuth.findFirst.mockResolvedValue(null)
|
||||
prismaMock.oAuth.create.mockResolvedValue({
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
})
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
})
|
||||
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
})
|
||||
expect(result).toEqual('success')
|
||||
})
|
||||
|
||||
it('should not add the strategy if the account of the provider is already used', async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
prismaMock.oAuth.findFirst.mockResolvedValue({
|
||||
id: 1,
|
||||
userId: 2,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
})
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
})
|
||||
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
|
||||
expect(result).toEqual('This account is already used by someone else')
|
||||
})
|
||||
|
||||
it('should not add the strategy if the user is already connected with it', async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
prismaMock.oAuth.findFirst.mockResolvedValue({
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
provider: 'discord',
|
||||
providerId: id,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
})
|
||||
const result = await oauthStrategy.callbackAddStrategy(
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||
)
|
||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
||||
where: {
|
||||
provider: 'discord',
|
||||
providerId: id
|
||||
}
|
||||
})
|
||||
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
|
||||
expect(result).toEqual('You are already using this account')
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -1,20 +1,25 @@
|
||||
import tap from 'tap'
|
||||
|
||||
import { buildQueryURL } from '../buildQueryURL.js'
|
||||
|
||||
test('/tools/utils/buildQueryUrl', () => {
|
||||
expect(
|
||||
await tap.test('tools/utils/buildQueryUrl', async (t) => {
|
||||
t.equal(
|
||||
buildQueryURL('http://localhost:8080', {
|
||||
test: 'query'
|
||||
})
|
||||
).toEqual('http://localhost:8080/?test=query')
|
||||
expect(
|
||||
}),
|
||||
'http://localhost:8080/?test=query'
|
||||
)
|
||||
t.equal(
|
||||
buildQueryURL('http://localhost:8080/', {
|
||||
test: 'query'
|
||||
})
|
||||
).toEqual('http://localhost:8080/?test=query')
|
||||
expect(
|
||||
}),
|
||||
'http://localhost:8080/?test=query'
|
||||
)
|
||||
t.equal(
|
||||
buildQueryURL('http://localhost:3000', {
|
||||
test: 'query',
|
||||
code: 'abc'
|
||||
})
|
||||
).toEqual('http://localhost:3000/?test=query&code=abc')
|
||||
}),
|
||||
'http://localhost:3000/?test=query&code=abc'
|
||||
)
|
||||
})
|
||||
|
@ -1,22 +1,27 @@
|
||||
import tap from 'tap'
|
||||
|
||||
import { parseStringNullish } from '../parseStringNullish.js'
|
||||
|
||||
const defaultString = 'defaultString'
|
||||
|
||||
describe('/tools/utils/parseStringNullish', () => {
|
||||
it('returns `defaultString` if `string === undefined`', () => {
|
||||
expect(parseStringNullish(defaultString, undefined)).toEqual(defaultString)
|
||||
await tap.test('tools/utils/parseStringNullish', async (t) => {
|
||||
await t.test(
|
||||
'returns `defaultString` if `string === undefined`',
|
||||
async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, undefined), defaultString)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test('returns `null` if `string === null`', async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, null), null)
|
||||
})
|
||||
|
||||
it('returns `null` if `string === null`', () => {
|
||||
expect(parseStringNullish(defaultString, null)).toEqual(null)
|
||||
await t.test('returns `null` if `string.length === 0`', async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, ''), null)
|
||||
})
|
||||
|
||||
it('returns `null` if `string.length === 0`', () => {
|
||||
expect(parseStringNullish(defaultString, '')).toEqual(null)
|
||||
})
|
||||
|
||||
it('returns `string` if `string.length > 0`', () => {
|
||||
await t.test('returns `string` if `string.length > 0`', async (t) => {
|
||||
const string = 'myString'
|
||||
expect(parseStringNullish(defaultString, string)).toEqual(string)
|
||||
t.equal(parseStringNullish(defaultString, string), string)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user