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.

store.mutations.js 7.7 KiB

há 7 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. test('LOGOUT', t => {
  6. const LOGOUT = mutations[types.LOGOUT];
  7. const state = {
  8. authenticated: true
  9. };
  10. LOGOUT(state);
  11. t.false(state.authenticated);
  12. });
  13. test('LOGOUT clean user personal info', t => {
  14. const LOGOUT = mutations[types.LOGOUT];
  15. const state = {
  16. token: '123456',
  17. password: {counter: 2},
  18. passwords: [{id: '1', site: 'test@example.org'}],
  19. defaultPassword: {counter: 1},
  20. };
  21. LOGOUT(state);
  22. t.true(state.token === null);
  23. t.is(state.passwords.length, 0);
  24. t.is(state.password.counter, 1);
  25. });
  26. test('LOGIN', t => {
  27. const LOGIN = mutations[types.LOGIN];
  28. const state = {authenticated: false};
  29. LOGIN(state);
  30. t.true(state.authenticated);
  31. });
  32. test('SET_TOKEN', t => {
  33. const token = '123456';
  34. const SET_TOKEN = mutations[types.SET_TOKEN];
  35. const state = {token: null};
  36. SET_TOKEN(state, {token});
  37. t.is(state.token, token);
  38. });
  39. test('SET_PASSWORD', t => {
  40. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  41. const state = {password: null};
  42. SET_PASSWORD(state, {password: {uppercase: true, version: 2}});
  43. t.is(state.password.version, 2);
  44. t.true(state.password.uppercase);
  45. });
  46. test('SET_PASSWORD dont change lastUse date', t => {
  47. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  48. const now = 1485989236000;
  49. const time = new Date(now);
  50. timekeeper.freeze(time);
  51. const state = {lastUse: null, password: null};
  52. SET_PASSWORD(state, {password: {}});
  53. t.true(state.lastUse === null);
  54. timekeeper.reset();
  55. });
  56. test('PASSWORD_GENERATED change lastUse date', t => {
  57. const PASSWORD_GENERATED = mutations[types.PASSWORD_GENERATED];
  58. const now = 1485989236000;
  59. const time = new Date(now);
  60. timekeeper.freeze(time);
  61. const state = {lastUse: null};
  62. PASSWORD_GENERATED(state);
  63. t.is(now, state.lastUse);
  64. timekeeper.reset();
  65. });
  66. test('SET_PASSWORD immutable', t => {
  67. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  68. const state = {};
  69. const password = {version: 2};
  70. SET_PASSWORD(state, {password});
  71. password.version = 1;
  72. t.is(state.password.version, 2);
  73. });
  74. test('SET_DEFAULT_PASSWORD', t => {
  75. const SET_DEFAULT_PASSWORD = mutations[types.SET_DEFAULT_PASSWORD];
  76. const state = {
  77. defaultPassword: {
  78. site: '',
  79. login: '',
  80. uppercase: true,
  81. lowercase: true,
  82. numbers: true,
  83. symbols: true,
  84. length: 16,
  85. counter: 1,
  86. version: 2
  87. }
  88. };
  89. SET_DEFAULT_PASSWORD(state, {password: {symbols: false, length: 30}});
  90. t.is(state.defaultPassword.length, 30);
  91. t.false(state.defaultPassword.symbols);
  92. });
  93. test('SET_PASSWORDS', t => {
  94. const SET_PASSWORDS = mutations[types.SET_PASSWORDS];
  95. const state = {
  96. passwords: []
  97. };
  98. SET_PASSWORDS(state, {passwords: [{site: 'site1'}, {site: 'site2'}]});
  99. t.is(state.passwords[0].site, 'site1');
  100. t.is(state.passwords[1].site, 'site2');
  101. });
  102. test('DELETE_PASSWORD', t => {
  103. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  104. const state = {
  105. passwords: [{id: '1', site: 'site1'}, {id: '2', site: 'site2'}]
  106. };
  107. t.is(state.passwords.length, 2);
  108. DELETE_PASSWORD(state, {id: '1'});
  109. t.is(state.passwords.length, 1);
  110. });
  111. test('DELETE_PASSWORD clean password with default password if same id', t => {
  112. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  113. const state = {
  114. passwords: [{id: '1', length: 30}, {id: '2', length: 16}],
  115. password: {id: '1', length: 30},
  116. defaultPassword: {length: 16}
  117. };
  118. DELETE_PASSWORD(state, {id: '1'});
  119. t.is(state.password.length, 16);
  120. });
  121. test('SET_BASE_URL', t => {
  122. const SET_BASE_URL = mutations[types.SET_BASE_URL];
  123. const state = {
  124. baseURL: 'https://lesspass.com'
  125. };
  126. const baseURL = 'https://example.org';
  127. SET_BASE_URL(state, {baseURL: baseURL});
  128. t.is(state.baseURL, baseURL);
  129. });
  130. test('SET_VERSION', t => {
  131. const SET_VERSION = mutations[types.SET_VERSION];
  132. const state = {
  133. password: {version: 2},
  134. };
  135. SET_VERSION(state, {version: 1});
  136. t.is(state.password.version, 1);
  137. });
  138. test('SET_VERSION password null', t => {
  139. const SET_VERSION = mutations[types.SET_VERSION];
  140. const state = {
  141. password: null,
  142. };
  143. SET_VERSION(state, {version: 2});
  144. t.is(state.password.version, 2);
  145. });
  146. test('LOAD_PASSWORD_FIRST_TIME 5 minutes after last use', t => {
  147. const now = 1485989236000;
  148. const time = new Date(now);
  149. timekeeper.freeze(time);
  150. const fiveMinutesBefore = now - 5 * 60 * 1000;
  151. const state = {
  152. lastUse: fiveMinutesBefore,
  153. password: {
  154. login: 'test@example.org',
  155. length: 30
  156. },
  157. defaultPassword: {
  158. login: '',
  159. length: 16
  160. }
  161. };
  162. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  163. LOAD_PASSWORD_FIRST_TIME(state);
  164. t.is(state.password.login, 'test@example.org');
  165. t.is(state.password.length, 30);
  166. timekeeper.reset();
  167. });
  168. test('LOAD_PASSWORD_FIRST_TIME more than 10 minutes after last use', t => {
  169. const now = 1485989236000;
  170. const time = new Date(now);
  171. timekeeper.freeze(time);
  172. const twentyMinutesBefore = now - 20 * 60 * 1000;
  173. const state = {
  174. lastUse: twentyMinutesBefore,
  175. password: {
  176. login: 'test@example.org',
  177. length: 30
  178. },
  179. defaultPassword: {
  180. login: '',
  181. length: 16
  182. }
  183. };
  184. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  185. LOAD_PASSWORD_FIRST_TIME(state);
  186. t.is(state.password.login, '');
  187. t.is(state.password.length, 16);
  188. timekeeper.reset();
  189. });
  190. test('LOAD_PASSWORD_FIRST_TIME last use null', t => {
  191. const time = new Date(1485989236000);
  192. timekeeper.freeze(time);
  193. const state = {
  194. lastUse: null,
  195. password: {
  196. site: '',
  197. version: 1
  198. },
  199. defaultPassword: {
  200. site: '',
  201. version: 2
  202. }
  203. };
  204. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  205. LOAD_PASSWORD_FIRST_TIME(state);
  206. t.is(state.password.version, 2);
  207. timekeeper.reset();
  208. });
  209. test('LOAD_PASSWORD_FOR_SITE', t => {
  210. const state = {
  211. password: {
  212. site: ''
  213. },
  214. passwords: [
  215. {id: '1', site: 'www.example.org'},
  216. {id: '2', site: 'www.google.com'}
  217. ]
  218. };
  219. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  220. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'});
  221. t.is(state.password.id, '2');
  222. t.is(state.password.site, 'www.google.com');
  223. });
  224. test('LOAD_PASSWORD_FOR_SITE no passwords', t => {
  225. const state = {
  226. password: {
  227. site: ''
  228. },
  229. passwords: []
  230. };
  231. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  232. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'});
  233. t.false('id' in state.password);
  234. t.is(state.password.site, 'google.com');
  235. });
  236. test('LOAD_PASSWORD_FOR_SITE multiple accounts matching criteria', t => {
  237. const state = {
  238. password: {
  239. site: ''
  240. },
  241. passwords: [
  242. {id: '1', site: 'www.example.org'},
  243. {id: '2', site: 'www.google.com'},
  244. {id: '3', site: 'account.google.com'},
  245. ]
  246. };
  247. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  248. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com', url: 'https://www.google.com'});
  249. t.is(state.password.id, '2');
  250. t.is(state.password.site, 'www.google.com');
  251. });