Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Ooth local user verification and password reset #20

Merged
merged 2 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/ooth-local/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ Object {
}
`;

exports[`ooth-local after registration after login can generate verification token 1`] = `
Object {
"_id": "<obfuscated>",
"email": "test@example.com",
"verificationToken": "<obfuscated>",
}
`;

exports[`ooth-local after registration after login can verify user with token 1`] = `
Object {
"message": "Email verified",
"user": Object {
"_id": "<obfuscated>",
"local": Object {
"email": "test@example.com",
"verified": true,
},
},
}
`;

exports[`ooth-local after registration after login can set email 1`] = `
Object {
"message": "Email updated.",
Expand Down Expand Up @@ -58,6 +79,22 @@ Object {
}
`;

exports[`ooth-local after registration can login 2`] = `
Object {
"user": Object {
"local": Object {
"email": "test@example.com",
},
},
}
`;

exports[`ooth-local after registration can reset password 1`] = `
Object {
"message": "Password has been reset.",
}
`;

exports[`ooth-local after registration login fails with wrong password 1`] = `
Object {
"message": "Incorrect password.",
Expand Down
4 changes: 2 additions & 2 deletions packages/ooth-local/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ module.exports = function({
throw new Error('No email to verify')
}

updateUser(req.user._id, {
return updateUser(req.user._id, {
verificationToken
}).then(() => {
if (onGenerateVerificationToken) {
Expand Down Expand Up @@ -328,7 +328,7 @@ module.exports = function({
throw new Error('Invalid password.')
}

testValue('password', password)
testValue('password', newPassword)

return getUserByFields({
passwordResetToken: token
Expand Down
103 changes: 98 additions & 5 deletions packages/ooth-local/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ const obfuscate = (obj, ...keys) => {
return res
}


describe('ooth-local', () => {

let onForgotPasswordListener;
let onRequestVerifyListener;
let resetToken;
let verificationToken;

beforeAll(async () => {
db = await MongoClient.connect(mongoUrl)
Expand All @@ -55,8 +57,18 @@ describe('ooth-local', () => {
oothLocalConfig = {
onForgotPassword(data) {
if (onForgotPasswordListener) {
//Store reset token for follow on test
resetToken = data.passwordResetToken
onForgotPasswordListener(data)
}
},

onGenerateVerificationToken(data) {
if (onRequestVerifyListener) {
//Store verification token for follow on test
verificationToken = data.verificationToken
onRequestVerifyListener(data)
}
}
}
app = express()
Expand Down Expand Up @@ -175,17 +187,18 @@ describe('ooth-local', () => {
}
})

test('can forget password', async () => {
onForgotPasswordListener = jest.fn()
test('can login', async () => {
const res = await request({
method: 'POST',
uri: 'http://localhost:8080/local/forgot-password',
uri: 'http://localhost:8080/local/login',
body: {
username: 'test@example.com',
password: 'Asdflba09',
},
json: true,
})
expect(obfuscate(onForgotPasswordListener.mock.calls[0][0], '_id', 'passwordResetToken')).toMatchSnapshot()
delete(res.user._id)
expect(res).toMatchSnapshot()
})

test('can login', async () => {
Expand All @@ -202,6 +215,43 @@ describe('ooth-local', () => {
expect(res).toMatchSnapshot()
})

test('can forget password', async () => {
onForgotPasswordListener = jest.fn()
const res = await request({
method: 'POST',
uri: 'http://localhost:8080/local/forgot-password',
body: {
username: 'test@example.com',
},
json: true,
})
expect(obfuscate(onForgotPasswordListener.mock.calls[0][0], '_id', 'passwordResetToken')).toMatchSnapshot()
})

test('can reset password', async () =>{
onForgotPasswordListener = jest.fn()

//Store the password reset token so we can use it later
await request({
method: 'POST',
uri: 'http://localhost:8080/local/forgot-password',
body: {
username: 'test@example.com',
},
json: true,
})
const res = await request({
method: 'POST',
uri: 'http://localhost:8080/local/reset-password',
body: {
token: resetToken,
newPassword: 'Asdflba10',
},
json: true,
})
expect(res).toMatchSnapshot()
})

describe('after login', () => {
beforeEach(async () => {
const res = await request({
Expand All @@ -217,6 +267,49 @@ describe('ooth-local', () => {
cookies = res.headers['set-cookie']
})

test('can generate verification token', async () => {
onRequestVerifyListener = jest.fn()
const res = await request({
method: 'POST',
uri: 'http://localhost:8080/local/generate-verification-token',
json: true,
headers: {
Cookie: cookies
}
})
expect(obfuscate(onRequestVerifyListener.mock.calls[0][0], '_id', 'verificationToken')).toMatchSnapshot()
})

test('can verify user with token', async () => {
onRequestVerifyListener = jest.fn()
//Store verification token for later use
await request({
method: 'POST',
uri: 'http://localhost:8080/local/generate-verification-token',
json: true,
headers: {
Cookie: cookies
}
})
const res = await request({
method: 'POST',
uri: 'http://localhost:8080/local/verify',
json: true,
body: {
token: verificationToken
},
headers: {
Cookie: cookies
}
})
//This is a bit messy, probably need a nestedObfuscate
const obfuscatedRes = {
message: (res.message ? res.message : null),
user: obfuscate(res.user, '_id')
}
expect(obfuscatedRes).toMatchSnapshot()
})

test('can check status', async () => {
const res = await request({
uri: 'http://localhost:8080/status',
Expand Down