Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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