Test error path of #login procedure

This commit is contained in:
Robert Schäfer 2018-12-19 09:37:36 +01:00
parent bf8f2f8877
commit b17686656b
2 changed files with 105 additions and 19 deletions

View File

@ -104,8 +104,9 @@ export const actions = {
if (res && res.token) {
await this.app.$apolloHelpers.onLogin(res.token)
commit('SET_TOKEN', res.token)
delete res.token
commit('SET_USER', res)
const userData = Object.assign({}, res)
delete userData.token
commit('SET_USER', userData)
commit('SET_PENDING', false)
return true
} else {

View File

@ -2,7 +2,41 @@ import { getters, mutations, actions } from './auth.js'
let state
let commit
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InUxIiwic2x1ZyI6InBldGVyLWx1c3RpZyIsIm5hbWUiOiJQZXRlciBMdXN0aWciLCJhdmF0YXIiOiJodHRwczovL3MzLmFtYXpvbmF3cy5jb20vdWlmYWNlcy9mYWNlcy90d2l0dGVyL2FudG9ueXpvdG92LzEyOC5qcGciLCJlbWFpbCI6ImFkbWluQGV4YW1wbGUub3JnIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNTQ1MTQwMTQ5LCJleHAiOjE2MzE1NDAxNDksImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDAwMCIsInN1YiI6InUxIn0.t1nDgdRPNxXGbNzHHN6uSt5fmS4ofFNLjk_k5XnCoCs"
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InUzIiwic2x1ZyI6Implbm55LXJvc3RvY2siLCJuYW1lIjoiSmVubnkgUm9zdG9jayIsImF2YXRhciI6Imh0dHBzOi8vczMuYW1hem9uYXdzLmNvbS91aWZhY2VzL2ZhY2VzL3R3aXR0ZXIvbXV0dV9rcmlzaC8xMjguanBnIiwiZW1haWwiOiJ1c2VyQGV4YW1wbGUub3JnIiwicm9sZSI6InVzZXIiLCJpYXQiOjE1NDUxNDQ2ODgsImV4cCI6MTYzMTU0NDY4OCwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDozMDAwIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo0MDAwIiwic3ViIjoidTMifQ.s5_JeQN9TaUPfymAXPOpbMAwhmTIg9cnOvNEcj4z75k"
const successfulLoginResponse = {
data: {
login: {
id: "u3",
name: "Jenny Rostock",
slug: "jenny-rostock",
email: "user@example.org",
avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg",
role: "user",
token
}
}
}
const incorrectPasswordResponse = {
data: {
login: null
},
errors: [
{
message: "Incorrect password.",
locations: [
{
"line": 2,
"column": 3
}
],
path: [
"login"
]
}
]
}
beforeEach(() => {
commit = jest.fn()
@ -23,13 +57,59 @@ describe('actions', () => {
let action
describe('login', () => {
describe('given a successful response', () => {
let mutate
let onLogin
describe('given valid credentials and a successful response', () => {
beforeEach(async () => {
const response = Object.assign({}, successfulLoginResponse)
const mutate = jest.fn(() => Promise.resolve(response))
const onLogin = jest.fn(() => Promise.resolve())
const module = {
app: {
apolloProvider: { defaultClient: { mutate } },
$apolloHelpers: { onLogin }
}
}
action = actions.login.bind(module)
await action({commit}, {email: 'user@example.org', password: '1234'})
})
afterEach(() => {
action = null
})
it('saves the JWT Bearer token', () => {
expect(commit.mock.calls).toEqual(
expect.arrayContaining([['SET_TOKEN', token]])
)
})
it('saves user data without token', () => {
expect(commit.mock.calls).toEqual(
expect.arrayContaining([['SET_USER', {
id: "u3",
name: "Jenny Rostock",
slug: "jenny-rostock",
email: "user@example.org",
avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg",
role: "user"
}]])
)
})
it('saves pending flags in order', () => {
expect(commit.mock.calls).toEqual(
expect.arrayContaining([
['SET_PENDING', true],
['SET_PENDING', false]
])
)
})
})
describe('given invalid credentials and incorrect password response', () => {
beforeEach(() => {
mutate = jest.fn(() => Promise.resolve( { data: { login: { token } } }))
onLogin = jest.fn(() => Promise.resolve())
const response = Object.assign({}, incorrectPasswordResponse)
const mutate = jest.fn(() => Promise.resolve(response))
const onLogin = jest.fn(() => Promise.resolve())
const module = {
app: {
apolloProvider: { defaultClient: { mutate } },
@ -43,17 +123,22 @@ describe('actions', () => {
action = null
})
it('saves the JWT Bearer token', async () => {
await action({commit}, {email: 'doesnot@matter.org', password: '1234'})
const expected = [
['SET_PENDING', true],
['SET_USER', null],
['SET_TOKEN', null],
['SET_TOKEN', token],
['SET_USER', { }],
['SET_PENDING', false]
]
expect(commit.mock.calls).toEqual(expected)
xit('shows a user friendly error message', async () => {
await action({commit}, {email: 'user@example.org', password: 'wrong'})
})
it('saves pending flags in order', async () => {
try {
await action({commit}, {email: 'user@example.org', password: 'wrong'})
} catch(err) {
console.log(err)
}
expect(commit.mock.calls).toEqual(
expect.arrayContaining([
['SET_PENDING', true],
['SET_PENDING', false]
])
)
})
})
})