chore: password tests

This commit is contained in:
Mo
2023-08-02 06:54:18 -05:00
parent 4b6bd452f7
commit 846e5b3ca8
2 changed files with 91 additions and 74 deletions

View File

@@ -1,7 +1,6 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import { BaseItemCounts } from './lib/BaseItemCounts.js' import { BaseItemCounts } from './lib/BaseItemCounts.js'
import * as Factory from './lib/factory.js' import * as Factory from './lib/factory.js'
chai.use(chaiAsPromised) chai.use(chaiAsPromised)
const expect = chai.expect const expect = chai.expect
@@ -23,7 +22,7 @@ describe('basic auth', function () {
beforeEach(async function () { beforeEach(async function () {
localStorage.clear() localStorage.clear()
context = await Factory.createAppContextWithRealCrypto() context = await Factory.createAppContextWithFakeCrypto()
await context.launch() await context.launch()
@@ -70,7 +69,14 @@ describe('basic auth', function () {
await context.signout() await context.signout()
const response = await context.application.signIn(context.email, context.password, undefined, undefined, undefined, true) const response = await context.application.signIn(
context.email,
context.password,
undefined,
undefined,
undefined,
true,
)
expect(response).to.be.ok expect(response).to.be.ok
expect(response.data.error).to.not.be.ok expect(response.data.error).to.not.be.ok
expect(await context.application.encryption.getRootKey()).to.be.ok expect(await context.application.encryption.getRootKey()).to.be.ok
@@ -81,7 +87,14 @@ describe('basic auth', function () {
await Factory.createSyncedNote(context.application) await Factory.createSyncedNote(context.application)
await context.signout() await context.signout()
const response = await context.application.signIn(context.email, context.password, undefined, undefined, undefined, true) const response = await context.application.signIn(
context.email,
context.password,
undefined,
undefined,
undefined,
true,
)
expect(response).to.be.ok expect(response).to.be.ok
expect(response.data.error).to.not.be.ok expect(response.data.error).to.not.be.ok
expect(await context.application.encryption.getRootKey()).to.be.ok expect(await context.application.encryption.getRootKey()).to.be.ok
@@ -113,7 +126,14 @@ describe('basic auth', function () {
await Promise.all([ await Promise.all([
(async () => { (async () => {
const response = await context.application.signIn(context.email, context.password, undefined, undefined, undefined, true) const response = await context.application.signIn(
context.email,
context.password,
undefined,
undefined,
undefined,
true,
)
expect(response).to.be.ok expect(response).to.be.ok
expect(response.data.error).to.not.be.ok expect(response.data.error).to.not.be.ok
expect(await context.application.encryption.getRootKey()).to.be.ok expect(await context.application.encryption.getRootKey()).to.be.ok
@@ -160,7 +180,14 @@ describe('basic auth', function () {
await context.register() await context.register()
await context.signout() await context.signout()
let response = await context.application.signIn(context.email, 'wrong password', undefined, undefined, undefined, true) let response = await context.application.signIn(
context.email,
'wrong password',
undefined,
undefined,
undefined,
true,
)
expect(response).to.have.property('status', 401) expect(response).to.have.property('status', 401)
expect(response.data.error).to.be.ok expect(response.data.error).to.be.ok
@@ -246,7 +273,14 @@ describe('basic auth', function () {
it('fails login with wrong password', async function () { it('fails login with wrong password', async function () {
await context.register() await context.register()
context.application = await Factory.signOutApplicationAndReturnNew(context.application) context.application = await Factory.signOutApplicationAndReturnNew(context.application)
const response = await context.application.signIn(context.email, 'wrongpassword', undefined, undefined, undefined, true) const response = await context.application.signIn(
context.email,
'wrongpassword',
undefined,
undefined,
undefined,
true,
)
expect(response).to.be.ok expect(response).to.be.ok
expect(response.data.error).to.be.ok expect(response.data.error).to.be.ok
expect(await context.application.encryption.getRootKey()).to.not.be.ok expect(await context.application.encryption.getRootKey()).to.not.be.ok
@@ -300,102 +334,77 @@ describe('basic auth', function () {
expect(outOfSync).to.equal(false) expect(outOfSync).to.equal(false)
}) })
async function changePassword() { it('successfully changes password', async function () {
await context.register() await context.register()
const noteCount = 10 const noteCount = 5
await Factory.createManyMappedNotes(context.application, noteCount) await Factory.createManyMappedNotes(context.application, noteCount)
this.expectedItemCount += noteCount this.expectedItemCount += noteCount
await context.application.sync.sync(syncOptions) await context.sync()
expect(context.application.items.items.length).to.equal(this.expectedItemCount) expect(context.application.items.items.length).to.equal(this.expectedItemCount)
const newPassword = 'newpassword' const newPassword = 'newpassword'
const response = await context.application.changePassword(context.password, newPassword) const response = await context.application.changePassword(context.password, newPassword)
/** New items key */
this.expectedItemCount++
expect(context.application.items.items.length).to.equal(this.expectedItemCount)
expect(response.error).to.not.be.ok expect(response.error).to.not.be.ok
this.expectedItemCount += ['new items key'].length
expect(context.application.items.items.length).to.equal(this.expectedItemCount) expect(context.application.items.items.length).to.equal(this.expectedItemCount)
expect(context.application.payloads.invalidPayloads.length).to.equal(0) expect(context.application.payloads.invalidPayloads.length).to.equal(0)
await context.application.sync.markAllItemsAsNeedingSyncAndPersist() await context.application.sync.markAllItemsAsNeedingSyncAndPersist()
await context.application.sync.sync(syncOptions) await context.sync(syncOptions)
expect(context.application.items.items.length).to.equal(this.expectedItemCount) expect(context.application.items.items.length).to.equal(this.expectedItemCount)
}).timeout(40000)
const note = context.application.items.getDisplayableNotes()[0] it('should sign into account after changing password', async function () {
await context.register()
/** const newPassword = 'newpassword'
* Create conflict for a note. First modify the item without saving so that const response = await context.application.changePassword(context.password, newPassword)
* our local contents digress from the server's expect(response.error).to.not.be.ok
*/
await context.application.mutator.changeItem(note, (mutator) => {
mutator.title = `${Math.random()}`
})
await Factory.changePayloadTimeStampAndSync( this.expectedItemCount += ['new items key'].length
context.application,
note.payload,
Factory.dateToMicroseconds(Factory.yesterday()),
{
title: `${Math.random()}`,
},
syncOptions,
)
this.expectedItemCount++
await context.signout() await context.signout()
/** Should login with new password */ const signinResponse = await context.application.signIn(
const signinResponse = await context.application.signIn(context.email, newPassword, undefined, undefined, undefined, true) context.email,
newPassword,
undefined,
undefined,
undefined,
true,
)
expect(signinResponse).to.be.ok expect(signinResponse).to.be.ok
expect(signinResponse.data.error).to.not.be.ok expect(signinResponse.data.error).to.not.be.ok
expect(await context.application.encryption.getRootKey()).to.be.ok expect(await context.application.encryption.getRootKey()).to.be.ok
expect(context.application.items.items.length).to.equal(this.expectedItemCount) expect(context.application.items.items.length).to.equal(this.expectedItemCount)
expect(context.application.payloads.invalidPayloads.length).to.equal(0) expect(context.application.payloads.invalidPayloads.length).to.equal(0)
} })
it('successfully changes password', changePassword).timeout(40000)
it('successfully changes password when passcode is set', async function () { it('successfully changes password when passcode is set', async function () {
const passcode = 'passcode' const passcode = 'passcode'
const promptValueReply = (prompts) => { await context.addPasscode(passcode)
const values = [] await context.register()
for (const prompt of prompts) {
if (prompt.validation === ChallengeValidation.LocalPasscode) { const noteCount = 3
values.push(CreateChallengeValue(prompt, passcode)) await Factory.createManyMappedNotes(context.application, noteCount)
} else { this.expectedItemCount += noteCount
values.push(CreateChallengeValue(prompt, context.password))
} await context.sync()
}
return values const newPassword = 'newpassword'
} const response = await context.application.changePassword(context.password, newPassword)
context.application.setLaunchCallback({ expect(response.error).to.not.be.ok
receiveChallenge: (challenge) => {
context.application.addChallengeObserver(challenge, { this.expectedItemCount += ['new items key'].length
onInvalidValue: (value) => {
const values = promptValueReply([value.prompt]) expect(context.application.items.items.length).to.equal(this.expectedItemCount)
context.application.submitValuesForChallenge(challenge, values) })
numPasscodeAttempts++
},
})
const initialValues = promptValueReply(challenge.prompts)
context.application.submitValuesForChallenge(challenge, initialValues)
},
})
await context.application.addPasscode(passcode)
await changePassword.bind(this)()
}).timeout(20000)
it('changes password many times', async function () { it('changes password many times', async function () {
await context.register() await context.register()
@@ -541,12 +550,14 @@ describe('basic auth', function () {
expect(signOutSpy.callCount).to.equal(1) expect(signOutSpy.callCount).to.equal(1)
}).timeout(Factory.TenSecondTimeout) }).timeout(Factory.TenSecondTimeout)
it('should not allow to delete someone else\'s account', async function () { it("should not allow to delete someone else's account", async function () {
const secondContext = await Factory.createAppContextWithRealCrypto() const secondContext = await Factory.createAppContextWithRealCrypto()
await secondContext.launch() await secondContext.launch()
const registerResponse = await secondContext.register() const registerResponse = await secondContext.register()
const response = await context.application.dependencies.get(TYPES.UserApiService).deleteAccount(registerResponse.user.uuid) const response = await context.application.dependencies
.get(TYPES.UserApiService)
.deleteAccount(registerResponse.user.uuid)
expect(response.status).to.equal(401) expect(response.status).to.equal(401)
expect(response.data.error.message).to.equal('Operation not allowed.') expect(response.data.error.message).to.equal('Operation not allowed.')

View File

@@ -226,6 +226,12 @@ export class AppContext {
return this.application.register(this.email, this.password) return this.application.register(this.email, this.password)
} }
async addPasscode(passcode) {
this.passcode = passcode
await this.application.addPasscode(passcode)
}
receiveServerResponse({ retrievedItems }) { receiveServerResponse({ retrievedItems }) {
const response = new ServerSyncResponse({ const response = new ServerSyncResponse({
data: { data: {