From c64173e05be723eb4c99ae2cbd092a5ff8779e49 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Mon, 16 Oct 2017 21:31:25 +0200 Subject: [PATCH] fix selecting profile on two sub domains Fixes https://github.com/lesspass/lesspass/issues/285 --- src/store/mutations.js | 14 ++++++++++---- test/unit/store.mutations.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/store/mutations.js b/src/store/mutations.js index 987af11..962a9f4 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -39,17 +39,23 @@ export default { state.password.site = site; }, [types.LOAD_PASSWORD_PROFILE](state, { site }) { - const siteDontMatch = site && !site.endsWith(state.password.site); - if (siteDontMatch) { - state.password = { ...state.defaultPassword }; - } + let siteMatch = false; const passwords = state.passwords || []; + const siteWithoutWWW = site.replace(/^www./g, ""); for (let i = 0; i < passwords.length; i++) { const password = passwords[i]; if (site.endsWith(password.site)) { state.password = { ...password }; + siteMatch = true; + break; + } else if (password.site.endsWith(siteWithoutWWW)) { + state.password = { ...password }; + siteMatch = true; } } + if (site && !siteMatch) { + state.password = { ...state.defaultPassword }; + } }, [types.SET_MESSAGE](state, { message }) { state.message = message; diff --git a/test/unit/store.mutations.js b/test/unit/store.mutations.js index 7809826..456f778 100644 --- a/test/unit/store.mutations.js +++ b/test/unit/store.mutations.js @@ -256,7 +256,10 @@ test("LOAD_PASSWORD_PROFILE no passwords", t => { password: { site: "" }, - passwords: [] + passwords: [], + defaultPassword: { + site: "" + } }; const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE]; LOAD_PASSWORD_PROFILE(state, { site: "account.google.com" }); @@ -280,6 +283,36 @@ test("LOAD_PASSWORD_PROFILE multiple accounts matching criteria", t => { t.is(state.password.site, "www.google.com"); }); +test("LOAD_PASSWORD_PROFILE multiple accounts matching criteria order doesn't matter", t => { + const state = { + password: { + site: "" + }, + passwords: [ + { id: "1", site: "www.example.org" }, + { id: "2", site: "account.google.com" }, + { id: "3", site: "www.google.com" } + ] + }; + const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE]; + LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" }); + t.is(state.password.id, "3"); + t.is(state.password.site, "www.google.com"); +}); + +test("LOAD_PASSWORD_PROFILE ends matching criteria nrt #285", t => { + const state = { + password: { + site: "" + }, + passwords: [{ id: "1", site: "account.google.com" }] + }; + const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE]; + LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" }); + t.is(state.password.id, "1"); + t.is(state.password.site, "account.google.com"); +}); + test("LOAD_PASSWORD_PROFILE without www", t => { const state = { password: {