Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

store.mutations.js 12 KiB

před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
před 7 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import test from "ava";
  2. import timekeeper from "timekeeper";
  3. import mutations from "../../src/store/mutations";
  4. import * as types from "../../src/store/mutation-types";
  5. import defaultPassword from "../../src/store/defaultPassword";
  6. test("LOGOUT", t => {
  7. const LOGOUT = mutations[types.LOGOUT];
  8. const state = {
  9. authenticated: true
  10. };
  11. LOGOUT(state);
  12. t.false(state.authenticated);
  13. });
  14. test("LOGOUT clean user personal info", t => {
  15. const LOGOUT = mutations[types.LOGOUT];
  16. const state = {
  17. token: "123456",
  18. password: { counter: 2 },
  19. passwords: [{ id: "1", site: "test@example.org" }],
  20. defaultPassword: { counter: 1 }
  21. };
  22. LOGOUT(state);
  23. t.true(state.token === null);
  24. t.is(state.passwords.length, 0);
  25. t.is(state.password.counter, 1);
  26. });
  27. test("LOGIN", t => {
  28. const LOGIN = mutations[types.LOGIN];
  29. const state = { authenticated: false };
  30. LOGIN(state);
  31. t.true(state.authenticated);
  32. });
  33. test("SET_TOKEN", t => {
  34. const token = "123456";
  35. const SET_TOKEN = mutations[types.SET_TOKEN];
  36. const state = { token: null };
  37. SET_TOKEN(state, { token });
  38. t.is(state.token, token);
  39. });
  40. test("SET_PASSWORD", t => {
  41. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  42. const state = { password: null };
  43. SET_PASSWORD(state, { password: { uppercase: true, version: 2 } });
  44. t.is(state.password.version, 2);
  45. t.true(state.password.uppercase);
  46. });
  47. test("SET_PASSWORD dont change lastUse date", t => {
  48. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  49. const now = 1485989236000;
  50. const time = new Date(now);
  51. timekeeper.freeze(time);
  52. const state = { lastUse: null, password: null };
  53. SET_PASSWORD(state, { password: {} });
  54. t.true(state.lastUse === null);
  55. timekeeper.reset();
  56. });
  57. test("PASSWORD_GENERATED change lastUse date", t => {
  58. const PASSWORD_GENERATED = mutations[types.PASSWORD_GENERATED];
  59. const now = 1485989236000;
  60. const time = new Date(now);
  61. timekeeper.freeze(time);
  62. const state = { lastUse: null };
  63. PASSWORD_GENERATED(state);
  64. t.is(now, state.lastUse);
  65. timekeeper.reset();
  66. });
  67. test("SET_PASSWORD immutable", t => {
  68. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  69. const state = {};
  70. const password = { version: 2 };
  71. SET_PASSWORD(state, { password });
  72. password.version = 1;
  73. t.is(state.password.version, 2);
  74. });
  75. test("SET_DEFAULT_OPTIONS", t => {
  76. const SET_DEFAULT_OPTIONS = mutations[types.SET_DEFAULT_OPTIONS];
  77. const state = {
  78. defaultPassword: {
  79. site: "",
  80. login: "",
  81. uppercase: true,
  82. lowercase: true,
  83. numbers: true,
  84. symbols: true,
  85. length: 16,
  86. counter: 1,
  87. version: 2
  88. }
  89. };
  90. SET_DEFAULT_OPTIONS(state, { options: { symbols: false, length: 30 } });
  91. t.is(state.defaultPassword.length, 30);
  92. t.false(state.defaultPassword.symbols);
  93. });
  94. test("SET_PASSWORDS", t => {
  95. const SET_PASSWORDS = mutations[types.SET_PASSWORDS];
  96. const state = {
  97. passwords: []
  98. };
  99. SET_PASSWORDS(state, { passwords: [{ site: "site1" }, { site: "site2" }] });
  100. t.is(state.passwords[0].site, "site1");
  101. t.is(state.passwords[1].site, "site2");
  102. });
  103. test("DELETE_PASSWORD", t => {
  104. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  105. const state = {
  106. passwords: [{ id: "1", site: "site1" }, { id: "2", site: "site2" }]
  107. };
  108. t.is(state.passwords.length, 2);
  109. DELETE_PASSWORD(state, { id: "1" });
  110. t.is(state.passwords.length, 1);
  111. });
  112. test("DELETE_PASSWORD replace state.password with state.defaultPassword", t => {
  113. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  114. const state = {
  115. passwords: [{ id: "1", length: 30 }, { id: "2", length: 16 }],
  116. password: { id: "1", length: 30 },
  117. defaultPassword: { length: 16 }
  118. };
  119. DELETE_PASSWORD(state, { id: "1" });
  120. t.is(state.password.length, 16);
  121. });
  122. test("SET_BASE_URL", t => {
  123. const SET_BASE_URL = mutations[types.SET_BASE_URL];
  124. const state = {
  125. baseURL: "https://lesspass.com"
  126. };
  127. const baseURL = "https://example.org";
  128. SET_BASE_URL(state, { baseURL: baseURL });
  129. t.is(state.baseURL, baseURL);
  130. });
  131. test("SET_VERSION", t => {
  132. const SET_VERSION = mutations[types.SET_VERSION];
  133. const state = {
  134. password: { version: 2 }
  135. };
  136. SET_VERSION(state, { version: 1 });
  137. t.is(state.password.version, 1);
  138. });
  139. test("SET_VERSION 1 should modify length to 12", t => {
  140. const SET_VERSION = mutations[types.SET_VERSION];
  141. const state = {
  142. password: { length: 16, version: 2 }
  143. };
  144. SET_VERSION(state, { version: 1 });
  145. t.is(state.password.length, 12);
  146. });
  147. test("SET_VERSION 2 should modify length to 16", t => {
  148. const SET_VERSION = mutations[types.SET_VERSION];
  149. const state = {
  150. password: { length: 12, version: 1 }
  151. };
  152. SET_VERSION(state, { version: 2 });
  153. t.is(state.password.length, 16);
  154. });
  155. test("LOAD_PASSWORD_PROFILE", t => {
  156. const state = {
  157. password: {
  158. login: "contact@example.org",
  159. site: "lesspass.com",
  160. uppercase: true,
  161. lowercase: true,
  162. numbers: true,
  163. symbols: true,
  164. length: 12,
  165. counter: 1,
  166. version: 1
  167. },
  168. passwords: [
  169. {
  170. id: "b89fdd8e-8e82-475a-ace4-91bcbd2042dc",
  171. login: "contact@example.org",
  172. site: "subdomaine.example.org",
  173. lowercase: true,
  174. uppercase: true,
  175. symbols: true,
  176. numbers: true,
  177. counter: 1,
  178. length: 16,
  179. version: 2
  180. },
  181. {
  182. id: "7cbadebf-49c8-4136-a579-6ee5beb6de7c",
  183. login: "contact@example.org",
  184. site: "example.org",
  185. lowercase: true,
  186. uppercase: false,
  187. symbols: false,
  188. numbers: true,
  189. counter: 1,
  190. length: 8,
  191. version: 2
  192. },
  193. {
  194. id: "31a50139-4add-4486-b553-5e33b4540640",
  195. login: "contact@example.org",
  196. site: "lesspass.com",
  197. lowercase: true,
  198. uppercase: true,
  199. symbols: true,
  200. numbers: true,
  201. counter: 1,
  202. length: 12,
  203. version: 1
  204. }
  205. ],
  206. defaultPassword: {
  207. login: "",
  208. site: "",
  209. uppercase: true,
  210. lowercase: true,
  211. numbers: true,
  212. symbols: true,
  213. length: 16,
  214. counter: 1,
  215. version: 2
  216. },
  217. lastUse: null
  218. };
  219. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  220. LOAD_PASSWORD_PROFILE(state, { site: "www.example.org" });
  221. t.deepEqual(state.password, state.passwords[1]);
  222. });
  223. test("LOAD_PASSWORD_PROFILE 30 seconds after last use", t => {
  224. const now = 1485989236000;
  225. const time = new Date(now);
  226. timekeeper.freeze(time);
  227. const thirtySecondBefore = now - 30 * 1000;
  228. const state = {
  229. lastUse: thirtySecondBefore,
  230. password: {
  231. site: "example.org",
  232. login: "test@example.org",
  233. length: 30
  234. },
  235. defaultPassword: {
  236. login: "",
  237. length: 16
  238. }
  239. };
  240. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  241. LOAD_PASSWORD_PROFILE(state, { site: "example.org" });
  242. t.is(state.password.login, "test@example.org");
  243. t.is(state.password.length, 30);
  244. timekeeper.reset();
  245. });
  246. test("LOAD_PASSWORD_PROFILE 30 seconds after last use on different site #242", t => {
  247. const now = 1485989236000;
  248. const time = new Date(now);
  249. timekeeper.freeze(time);
  250. const thirtySecondBefore = now - 30 * 1000;
  251. const state = {
  252. lastUse: thirtySecondBefore,
  253. password: {
  254. site: "example.org",
  255. login: "test@example.org",
  256. length: 30
  257. },
  258. defaultPassword: {
  259. login: "",
  260. length: 16
  261. }
  262. };
  263. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  264. LOAD_PASSWORD_PROFILE(state, { site: "lesspass.com" });
  265. t.is(state.password.login, "");
  266. t.is(state.password.length, 16);
  267. timekeeper.reset();
  268. });
  269. test("LOAD_PASSWORD_PROFILE more than 1 minute after last use", t => {
  270. const now = 1485989236000;
  271. const time = new Date(now);
  272. timekeeper.freeze(time);
  273. const oneMinuteAndOneSecond = now - 61 * 1000;
  274. const state = {
  275. lastUse: oneMinuteAndOneSecond,
  276. password: {
  277. site: "example.org",
  278. login: "test@example.org",
  279. length: 30
  280. },
  281. defaultPassword: {
  282. login: "",
  283. length: 16
  284. }
  285. };
  286. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  287. LOAD_PASSWORD_PROFILE(state, { site: "example.org" });
  288. t.is(state.password.login, "");
  289. t.is(state.password.length, 16);
  290. timekeeper.reset();
  291. });
  292. test("LOAD_PASSWORD_PROFILE empty login", t => {
  293. const state = {
  294. lastUse: null,
  295. password: {
  296. site: "",
  297. login: "",
  298. version: 1
  299. },
  300. passwords: [],
  301. defaultPassword: {
  302. site: "",
  303. login: "",
  304. version: 2
  305. }
  306. };
  307. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  308. LOAD_PASSWORD_PROFILE(state, { site: "example.org" });
  309. t.is(state.password.login, "");
  310. t.is(state.password.version, 2);
  311. timekeeper.reset();
  312. });
  313. test("LOAD_PASSWORD_PROFILE with passwords", t => {
  314. const state = {
  315. password: {
  316. site: ""
  317. },
  318. passwords: [
  319. { id: "1", site: "www.example.org" },
  320. { id: "2", site: "www.google.com" }
  321. ]
  322. };
  323. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  324. LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" });
  325. t.is(state.password.id, "2");
  326. t.is(state.password.site, "www.google.com");
  327. });
  328. test("LOAD_PASSWORD_PROFILE with no site reset default", t => {
  329. const state = {
  330. password: {
  331. site: "example.org",
  332. login: "contact@example.org",
  333. length: 8,
  334. version: 1
  335. },
  336. defaultPassword: {
  337. login: "",
  338. length: 16
  339. }
  340. };
  341. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  342. LOAD_PASSWORD_PROFILE(state, { site: "" });
  343. t.is(state.password.login, "");
  344. t.is(state.password.length, 16);
  345. });
  346. test("LOAD_PASSWORD_PROFILE no passwords", t => {
  347. const state = {
  348. password: {
  349. site: ""
  350. },
  351. passwords: []
  352. };
  353. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  354. LOAD_PASSWORD_PROFILE(state, { site: "account.google.com" });
  355. t.is(state.password.site, "");
  356. });
  357. test("LOAD_PASSWORD_PROFILE multiple accounts matching criteria", t => {
  358. const state = {
  359. password: {
  360. site: ""
  361. },
  362. passwords: [
  363. { id: "1", site: "www.example.org" },
  364. { id: "2", site: "www.google.com" },
  365. { id: "3", site: "account.google.com" }
  366. ]
  367. };
  368. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  369. LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" });
  370. t.is(state.password.id, "2");
  371. t.is(state.password.site, "www.google.com");
  372. });
  373. test("LOAD_PASSWORD_PROFILE without www", t => {
  374. const state = {
  375. password: {
  376. site: ""
  377. },
  378. passwords: [{ id: "1", site: "reddit.com" }]
  379. };
  380. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  381. LOAD_PASSWORD_PROFILE(state, { site: "www.reddit.com" });
  382. t.is(state.password.id, "1");
  383. t.is(state.password.site, "reddit.com");
  384. });
  385. test("SET_SITE default state", t => {
  386. const state = {
  387. password: defaultPassword,
  388. passwords: [],
  389. defaultPassword: defaultPassword,
  390. lastUse: null
  391. };
  392. const SET_SITE = mutations[types.SET_SITE];
  393. SET_SITE(state, { site: "www.example.org" });
  394. t.deepEqual(state.password.site, "www.example.org");
  395. });
  396. test("SET_MESSAGE", t => {
  397. const SET_MESSAGE = mutations[types.SET_MESSAGE];
  398. const state = {};
  399. SET_MESSAGE(state, {
  400. message: { text: "success message", status: "success" }
  401. });
  402. t.is(state.message.text, "success message");
  403. t.is(state.message.status, "success");
  404. });
  405. test("CLEAN_MESSAGE", t => {
  406. const CLEAN_MESSAGE = mutations[types.CLEAN_MESSAGE];
  407. const state = { message: { text: "error message", status: "error" } };
  408. CLEAN_MESSAGE(state);
  409. t.is(state.message.text, "");
  410. t.is(state.message.status, "success");
  411. });