Skip to content

Commit

Permalink
close #114 - save pass entries in the local storage for guests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eoksni committed Nov 22, 2019
1 parent a74cf72 commit 76546a8
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 145 deletions.
238 changes: 140 additions & 98 deletions e2e/test/main.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ describe('main', function() {
const PASS = 'mypassmypass';
const EMAIL = '[email protected]';

const uniqueUser = (function() {
let counter = 1;

return () => `${USER}${counter++}`;
})();

const uniqueEmail = () => `${uniqueUser()}@myemail.ru`;

describe('login', function() {
const Page = require('./pageobjects/page.js');
const page = new Page();
Expand Down Expand Up @@ -74,104 +82,138 @@ describe('main', function() {
describe('home', function() {
const page = require('./pageobjects/home.js');

beforeEach(function() {
page.open();
page.login(USER, PASS);
page.waitForSuccessLogin();
page.removeAllPasses();
page.waitForNoPasses();
});

afterEach(function() {
page.waitForNoPasses();
page.logout();
page.waitForSuccessLogout();
});

it('can add and remove pass', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

page.addPass(pass);
page.waitForPass(pass);
page.removeLastPass();
page.waitForPass(pass, undefined, true);
});
it('can add and remove pass with refresh', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

page.addPass(pass);
page.waitForPass(pass);
page.removeLastPass();
page.waitForPass(pass, undefined, true);

page.refreshAllPasses();
page.waitForPass(pass, undefined, true);
});
it('bug with delete', function() {
let pass1 = {
title: 'first title',
user: 'first user',
password: 'first password'
};
let pass2 = {
title: 'second title',
user: 'second user',
password: 'second password'
};

page.addPass(pass1);
page.addPass(pass2);
page.waitForPass(pass1);
page.removePass(pass1);
page.refreshAllPasses();
page.waitForPass(pass1, undefined, true);
page.waitForPass(pass2);
page.removeLastPass();
});
it('can edit', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

let updatedPass = {
title: 'updated title',
user: 'updated user',
password: 'updated password'
};

page.addPass(pass);
page.editLastPass(updatedPass);
page.waitForPass(updatedPass);
page.waitForPass(pass, undefined, true);
page.removeLastPass();
});
it('can search', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};
let passing_query = "tit";
let failing_query = "lalala";

page.addPass(pass);
page.searchPass(passing_query);
page.waitForPass(pass);
page.searchPass(failing_query);
page.waitForNoPasses();
page.searchPass("");
page.waitForPass(pass);
page.removeLastPass();
describe('logged in user', function() {
beforeEach(function() {
page.open();
page.login(USER, PASS);
page.waitForSuccessLogin();
page.removeAllPasses();
page.waitForNoPasses();
});

afterEach(function() {
page.waitForNoPasses();
page.logout();
page.waitForSuccessLogout();
});

it('can add and remove pass', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

page.addPass(pass);
page.waitForPass(pass);
page.removeLastPass();
page.waitForPass(pass, undefined, true);
});
it('can add and remove pass with refresh', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

page.addPass(pass);
page.waitForPass(pass);
page.removeLastPass();
page.waitForPass(pass, undefined, true);

page.refreshAllPasses();
page.waitForPass(pass, undefined, true);
});
it('bug with delete', function() {
let pass1 = {
title: 'first title',
user: 'first user',
password: 'first password'
};
let pass2 = {
title: 'second title',
user: 'second user',
password: 'second password'
};

page.addPass(pass1);
page.addPass(pass2);
page.waitForPass(pass1);
page.removePass(pass1);
page.refreshAllPasses();
page.waitForPass(pass1, undefined, true);
page.waitForPass(pass2);
page.removeLastPass();
});
it('can edit', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

let updatedPass = {
title: 'updated title',
user: 'updated user',
password: 'updated password'
};

page.addPass(pass);
page.editLastPass(updatedPass);
page.waitForPass(updatedPass);
page.waitForPass(pass, undefined, true);
page.removeLastPass();
});
it('can search', function() {
let pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};
let passing_query = "tit";
let failing_query = "lalala";

page.addPass(pass);
page.searchPass(passing_query);
page.waitForPass(pass);
page.searchPass(failing_query);
page.waitForNoPasses();
page.searchPass("");
page.waitForPass(pass);
page.removeLastPass();
});
});

describe('guest user', function() {
beforeEach(function() {
page.open();
});

it('should keep passwords after signup', function() {
const pass = {
title: 'test title',
user: 'test user',
password: 'test password'
};

page.addPass(pass);
page.waitForPass(pass);

page.signup(uniqueUser(), PASS, uniqueEmail());
page.waitForSuccessSignup();
page.waitForSuccessLogin();

page.waitForPass(pass);

page.removeAllPasses();
page.waitForNoPasses();

page.logout();
page.waitForSuccessLogout();

page.removeAllPasses();
page.waitForNoPasses();
});
});
});
});
11 changes: 9 additions & 2 deletions frontend/src/client-store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import * as logger from 'src/services/logger';

export class ClientStore {
/**
Expand All @@ -12,12 +13,18 @@ export class ClientStore {
get() {
let val = window.localStorage.getItem(this.key);
if (val === null) return undefined;
return val;
try {
const { value } = JSON.parse(val);
return value;
} catch (err) {
logger.error(err);
return undefined;
}
}

set(val) {
if (val === undefined) this.remove(this.key);
else window.localStorage.setItem(this.key, val);
else window.localStorage.setItem(this.key, JSON.stringify({ value: val }));
}

remove() {
Expand Down
26 changes: 15 additions & 11 deletions frontend/src/entry-command.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import assert from 'assert';
import { Command, execute, can_execute } from 'command-decorator'; // eslint-disable-line no-unused-vars
import { can_execute, Command, execute } from 'command-decorator'; // eslint-disable-line no-unused-vars
import { http, parse_location } from 'src/plugins/http.js';
import { notifier_error } from 'src/services/loader.js';
import * as i18n from 'src/plugins/i18n.js';
import * as auth from 'src/services/auth.js';
import { notifier_error } from 'src/services/loader.js';

// TODO: move it somewhere
const API_ENTRIES_URL = 'api/entries';

export class EntryCommand extends Command {
/**
* @param options.entry Must be reactive
*/
/**
* @param options.entry Must be reactive
*/
constructor(options) {
super();

Expand All @@ -21,15 +21,19 @@ export class EntryCommand extends Command {
assert(this.entry._id);
}

dirty() {
this.entry.synced = false;
}

update(newitem) {
this.entry.synced = false;
this.entry.title = newitem.title;
this.entry.user = newitem.user;
this.entry.password = newitem.password;
}

@notifier_error(i18n.terror)
@execute
@notifier_error(i18n.terror)
@execute
async save() {
const dto = {
title: this.entry.title,
Expand All @@ -50,7 +54,7 @@ export class EntryCommand extends Command {
return response;
}

@can_execute
@can_execute
can_save() {
if (!this.entry.synced) {
return { canExecute: true };
Expand All @@ -61,8 +65,8 @@ export class EntryCommand extends Command {
};
}

@notifier_error(i18n.terror)
@execute
@notifier_error(i18n.terror)
@execute
async delete() {
if (auth.is_authenticated()) {
if (this.entry.id !== undefined) {
Expand All @@ -72,7 +76,7 @@ export class EntryCommand extends Command {
(this.ondelete || function() { })();
}

@can_execute
@can_execute
can_delete() {
return { canExecute: true };
}
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/i18ns/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export default {
itemremoved_unknown: "Couldn't remove item.",
itemsfetched: "Items fetched.",
itemsfetched_timeout: "Request timed-out when fetching items.",
itemsfetched_unknown: "Couldn't fetch all items."
itemsfetched_unknown: "Couldn't fetch all items.",
itemssynced: "Items synced.",
itemssynced_timeout: "Request timed-out when syncing items.",
itemssynced_unknown: "Couldn't sync all items."
},
confirm_text: "Are you sure?",
confirm_title: "Confirm",
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/i18ns/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export default {
itemremoved_unknown: "Запись не была удалена.",
itemsfetched: "Записи получены.",
itemsfetched_timeout: "Время запроса истекло при попытке получить записи.",
itemsfetched_unknown: "Записи не были получены."
itemsfetched_unknown: "Записи не были получены.",
itemssynced: "Записи синхронизированы.",
itemssynced_timeout: "Время запроса истекло при попытке синхронизировать записи.",
itemssynced_unknown: "Записи не были синхронизированы."
},
confirm_text: "Вы уверены?",
confirm_title: "Подтвердить",
Expand Down
Loading

0 comments on commit 76546a8

Please sign in to comment.