Ocelot-Social/src/graphql-schema.test.js
Robert Schäfer ae39c18980 Run all backend tests
I accidently left `describe.only` in the test suite. That usually runs
only one block of tests instead of all tests. My fault, sorry
2019-01-08 14:57:33 +01:00

79 lines
2.2 KiB
JavaScript

import { request } from 'graphql-request'
import createServer from './server'
import mocks from './mocks'
import { create, cleanDatabase } from './seed/factories'
import jwt from 'jsonwebtoken'
let getHost
let app
let port
beforeEach(async () => {
const server = createServer({ mocks })
app = await server.start({ port: 0 })
port = app.address().port
getHost = () => `http://127.0.0.1:${port}`
})
afterEach(async () => {
await app.close()
})
describe('login', () => {
const mutation = (params) => {
const { email, password } = params
return `
mutation {
login(email:"${email}", password:"${password}"){
token
}
}`
}
describe('given an existing user', () => {
beforeEach(async () => {
await create('user', {
email: 'test@example.org',
password: '1234'
})
})
afterEach(async () => {
await cleanDatabase()
})
describe('asking for a `token`', () => {
describe('with valid email/password combination', () => {
it('responds with a JWT token', async () => {
const data = await request(getHost(), mutation({ email: 'test@example.org', password: '1234' }))
const { token } = data.login
jwt.verify(token, process.env.JWT_SECRET, (err, data) => {
expect(data.email).toEqual('test@example.org')
expect(err).toBeNull()
})
})
})
describe('with a valid email but incorrect password', () => {
it('responds with "Incorrect email address or password."', async () => {
try {
await request(getHost(), mutation({ email: 'test@example.org', password: 'wrong' }))
} catch (error) {
expect(error.response.errors[0].message).toEqual('Incorrect email address or password.')
}
})
})
describe('with a non-existing email', () => {
it('responds with "Incorrect email address or password."', async () => {
try {
await request(getHost(), mutation({ email: 'non-existent@example.org', password: 'wrong' }))
} catch (error) {
expect(error.response.errors[0].message).toEqual('Incorrect email address or password.')
}
})
})
})
})
})