You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

342 lines
8.9 KiB

  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 immutable", t => {
  48. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  49. const state = {};
  50. const password = { version: 2 };
  51. SET_PASSWORD(state, { password });
  52. password.version = 1;
  53. t.is(state.password.version, 2);
  54. });
  55. test("SET_DEFAULT_OPTIONS", t => {
  56. const SET_DEFAULT_OPTIONS = mutations[types.SET_DEFAULT_OPTIONS];
  57. const state = {
  58. defaultPassword: {
  59. site: "",
  60. login: "",
  61. uppercase: true,
  62. lowercase: true,
  63. numbers: true,
  64. symbols: true,
  65. length: 16,
  66. counter: 1,
  67. version: 2
  68. }
  69. };
  70. SET_DEFAULT_OPTIONS(state, { options: { symbols: false, length: 30 } });
  71. t.is(state.defaultPassword.length, 30);
  72. t.false(state.defaultPassword.symbols);
  73. });
  74. test("SET_PASSWORDS", t => {
  75. const SET_PASSWORDS = mutations[types.SET_PASSWORDS];
  76. const state = {
  77. passwords: []
  78. };
  79. SET_PASSWORDS(state, { passwords: [{ site: "site1" }, { site: "site2" }] });
  80. t.is(state.passwords[0].site, "site1");
  81. t.is(state.passwords[1].site, "site2");
  82. });
  83. test("DELETE_PASSWORD", t => {
  84. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  85. const state = {
  86. passwords: [{ id: "1", site: "site1" }, { id: "2", site: "site2" }]
  87. };
  88. t.is(state.passwords.length, 2);
  89. DELETE_PASSWORD(state, { id: "1" });
  90. t.is(state.passwords.length, 1);
  91. });
  92. test("DELETE_PASSWORD replace state.password with state.defaultPassword", t => {
  93. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  94. const state = {
  95. passwords: [{ id: "1", length: 30 }, { id: "2", length: 16 }],
  96. password: { id: "1", length: 30 },
  97. defaultPassword: { length: 16 }
  98. };
  99. DELETE_PASSWORD(state, { id: "1" });
  100. t.is(state.password.length, 16);
  101. });
  102. test("SET_BASE_URL", t => {
  103. const SET_BASE_URL = mutations[types.SET_BASE_URL];
  104. const state = {
  105. baseURL: "https://lesspass.com"
  106. };
  107. const baseURL = "https://example.org";
  108. SET_BASE_URL(state, { baseURL: baseURL });
  109. t.is(state.baseURL, baseURL);
  110. });
  111. test("SET_VERSION", t => {
  112. const SET_VERSION = mutations[types.SET_VERSION];
  113. const state = {
  114. password: { version: 2 }
  115. };
  116. SET_VERSION(state, { version: 1 });
  117. t.is(state.password.version, 1);
  118. });
  119. test("SET_VERSION 1 should modify length to 12", t => {
  120. const SET_VERSION = mutations[types.SET_VERSION];
  121. const state = {
  122. password: { length: 16, version: 2 }
  123. };
  124. SET_VERSION(state, { version: 1 });
  125. t.is(state.password.length, 12);
  126. });
  127. test("SET_VERSION 2 should modify length to 16", t => {
  128. const SET_VERSION = mutations[types.SET_VERSION];
  129. const state = {
  130. password: { length: 12, version: 1 }
  131. };
  132. SET_VERSION(state, { version: 2 });
  133. t.is(state.password.length, 16);
  134. });
  135. test("LOAD_PASSWORD_PROFILE", t => {
  136. const state = {
  137. password: {
  138. login: "contact@example.org",
  139. site: "lesspass.com",
  140. uppercase: true,
  141. lowercase: true,
  142. numbers: true,
  143. symbols: true,
  144. length: 12,
  145. counter: 1,
  146. version: 1
  147. },
  148. passwords: [
  149. {
  150. id: "b89fdd8e-8e82-475a-ace4-91bcbd2042dc",
  151. login: "contact@example.org",
  152. site: "subdomaine.example.org",
  153. lowercase: true,
  154. uppercase: true,
  155. symbols: true,
  156. numbers: true,
  157. counter: 1,
  158. length: 16,
  159. version: 2
  160. },
  161. {
  162. id: "7cbadebf-49c8-4136-a579-6ee5beb6de7c",
  163. login: "contact@example.org",
  164. site: "example.org",
  165. lowercase: true,
  166. uppercase: false,
  167. symbols: false,
  168. numbers: true,
  169. counter: 1,
  170. length: 8,
  171. version: 2
  172. },
  173. {
  174. id: "31a50139-4add-4486-b553-5e33b4540640",
  175. login: "contact@example.org",
  176. site: "lesspass.com",
  177. lowercase: true,
  178. uppercase: true,
  179. symbols: true,
  180. numbers: true,
  181. counter: 1,
  182. length: 12,
  183. version: 1
  184. }
  185. ],
  186. defaultPassword: {
  187. login: "",
  188. site: "",
  189. uppercase: true,
  190. lowercase: true,
  191. numbers: true,
  192. symbols: true,
  193. length: 16,
  194. counter: 1,
  195. version: 2
  196. },
  197. lastUse: null
  198. };
  199. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  200. LOAD_PASSWORD_PROFILE(state, { site: "www.example.org" });
  201. t.deepEqual(state.password, state.passwords[1]);
  202. });
  203. test("LOAD_PASSWORD_PROFILE on different site", t => {
  204. const state = {
  205. password: {
  206. site: "example.org",
  207. login: "test@example.org"
  208. },
  209. defaultPassword: {
  210. login: ""
  211. }
  212. };
  213. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  214. LOAD_PASSWORD_PROFILE(state, { site: "lesspass.com" });
  215. t.is(state.password.login, "");
  216. });
  217. test("LOAD_PASSWORD_PROFILE with passwords", t => {
  218. const state = {
  219. password: {
  220. site: ""
  221. },
  222. passwords: [
  223. { id: "1", site: "www.example.org" },
  224. { id: "2", site: "www.google.com" }
  225. ]
  226. };
  227. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  228. LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" });
  229. t.is(state.password.id, "2");
  230. t.is(state.password.site, "www.google.com");
  231. });
  232. test("LOAD_PASSWORD_PROFILE with no site keep password profile", t => {
  233. const state = {
  234. password: {
  235. site: "example.org",
  236. login: "contact@example.org",
  237. length: 8,
  238. version: 1
  239. },
  240. defaultPassword: {
  241. login: "",
  242. length: 16
  243. }
  244. };
  245. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  246. LOAD_PASSWORD_PROFILE(state, { site: "" });
  247. t.is(state.password.site, "example.org");
  248. t.is(state.password.login, "contact@example.org");
  249. t.is(state.password.length, 8);
  250. t.is(state.password.version, 1);
  251. });
  252. test("LOAD_PASSWORD_PROFILE no passwords", t => {
  253. const state = {
  254. password: {
  255. site: ""
  256. },
  257. passwords: []
  258. };
  259. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  260. LOAD_PASSWORD_PROFILE(state, { site: "account.google.com" });
  261. t.is(state.password.site, "");
  262. });
  263. test("LOAD_PASSWORD_PROFILE multiple accounts matching criteria", t => {
  264. const state = {
  265. password: {
  266. site: ""
  267. },
  268. passwords: [
  269. { id: "1", site: "www.example.org" },
  270. { id: "2", site: "www.google.com" },
  271. { id: "3", site: "account.google.com" }
  272. ]
  273. };
  274. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  275. LOAD_PASSWORD_PROFILE(state, { site: "www.google.com" });
  276. t.is(state.password.id, "2");
  277. t.is(state.password.site, "www.google.com");
  278. });
  279. test("LOAD_PASSWORD_PROFILE without www", t => {
  280. const state = {
  281. password: {
  282. site: ""
  283. },
  284. passwords: [{ id: "1", site: "reddit.com" }]
  285. };
  286. const LOAD_PASSWORD_PROFILE = mutations[types.LOAD_PASSWORD_PROFILE];
  287. LOAD_PASSWORD_PROFILE(state, { site: "www.reddit.com" });
  288. t.is(state.password.id, "1");
  289. t.is(state.password.site, "reddit.com");
  290. });
  291. test("SET_SITE default state", t => {
  292. const state = {
  293. password: defaultPassword,
  294. passwords: [],
  295. defaultPassword: defaultPassword,
  296. lastUse: null
  297. };
  298. const SET_SITE = mutations[types.SET_SITE];
  299. SET_SITE(state, { site: "www.example.org" });
  300. t.deepEqual(state.password.site, "www.example.org");
  301. });
  302. test("SET_MESSAGE", t => {
  303. const SET_MESSAGE = mutations[types.SET_MESSAGE];
  304. const state = {};
  305. SET_MESSAGE(state, {
  306. message: { text: "success message", status: "success" }
  307. });
  308. t.is(state.message.text, "success message");
  309. t.is(state.message.status, "success");
  310. });
  311. test("CLEAN_MESSAGE", t => {
  312. const CLEAN_MESSAGE = mutations[types.CLEAN_MESSAGE];
  313. const state = { message: { text: "error message", status: "error" } };
  314. CLEAN_MESSAGE(state);
  315. t.is(state.message.text, "");
  316. t.is(state.message.status, "success");
  317. });