Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

store.mutations.js 8.0 KiB

pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 1 should modify length to 12', t => {
  139. const SET_VERSION = mutations[types.SET_VERSION];
  140. const state = {
  141. password: {length: 16, version: 2},
  142. };
  143. SET_VERSION(state, {version: 1});
  144. t.is(state.password.length, 12);
  145. });
  146. test('SET_VERSION 2 should modify length to 16', t => {
  147. const SET_VERSION = mutations[types.SET_VERSION];
  148. const state = {
  149. password: {length: 12, version: 1},
  150. };
  151. SET_VERSION(state, {version: 2});
  152. t.is(state.password.length, 16);
  153. });
  154. test('LOAD_PASSWORD_FIRST_TIME 30 seconds after last use', t => {
  155. const now = 1485989236000;
  156. const time = new Date(now);
  157. timekeeper.freeze(time);
  158. const thirtySecondBefore = now - 30 * 1000;
  159. const state = {
  160. lastUse: thirtySecondBefore,
  161. password: {
  162. login: 'test@example.org',
  163. length: 30
  164. },
  165. defaultPassword: {
  166. login: '',
  167. length: 16
  168. }
  169. };
  170. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  171. LOAD_PASSWORD_FIRST_TIME(state);
  172. t.is(state.password.login, 'test@example.org');
  173. t.is(state.password.length, 30);
  174. timekeeper.reset();
  175. });
  176. test('LOAD_PASSWORD_FIRST_TIME more than 1 minute after last use', t => {
  177. const now = 1485989236000;
  178. const time = new Date(now);
  179. timekeeper.freeze(time);
  180. const oneMinuteAndOneSecond = now - 61 * 1000;
  181. const state = {
  182. lastUse: oneMinuteAndOneSecond,
  183. password: {
  184. login: 'test@example.org',
  185. length: 30
  186. },
  187. defaultPassword: {
  188. login: '',
  189. length: 16
  190. }
  191. };
  192. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  193. LOAD_PASSWORD_FIRST_TIME(state);
  194. t.is(state.password.login, '');
  195. t.is(state.password.length, 16);
  196. timekeeper.reset();
  197. });
  198. test('LOAD_PASSWORD_FIRST_TIME last use null', t => {
  199. const time = new Date(1485989236000);
  200. timekeeper.freeze(time);
  201. const state = {
  202. lastUse: null,
  203. password: {
  204. site: '',
  205. version: 1
  206. },
  207. defaultPassword: {
  208. site: '',
  209. version: 2
  210. }
  211. };
  212. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  213. LOAD_PASSWORD_FIRST_TIME(state);
  214. t.is(state.password.version, 2);
  215. timekeeper.reset();
  216. });
  217. test('LOAD_PASSWORD_FOR_SITE', 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_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  228. LOAD_PASSWORD_FOR_SITE(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_FOR_SITE no passwords', t => {
  233. const state = {
  234. password: {
  235. site: ''
  236. },
  237. passwords: []
  238. };
  239. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  240. LOAD_PASSWORD_FOR_SITE(state, {site: 'account.google.com'});
  241. t.false('id' in state.password);
  242. t.is(state.password.site, 'account.google.com');
  243. });
  244. test('LOAD_PASSWORD_FOR_SITE multiple accounts matching criteria', t => {
  245. const state = {
  246. password: {
  247. site: ''
  248. },
  249. passwords: [
  250. {id: '1', site: 'www.example.org'},
  251. {id: '2', site: 'www.google.com'},
  252. {id: '3', site: 'account.google.com'},
  253. ]
  254. };
  255. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  256. LOAD_PASSWORD_FOR_SITE(state, {site: 'www.google.com', url: 'https://www.google.com'});
  257. t.is(state.password.id, '2');
  258. t.is(state.password.site, 'www.google.com');
  259. });
  260. test('SET_MESSAGE', t => {
  261. const SET_MESSAGE = mutations[types.SET_MESSAGE];
  262. const state = {};
  263. SET_MESSAGE(state, {message: {text: 'success message', status: 'success'}});
  264. t.is(state.message.text, 'success message');
  265. t.is(state.message.status, 'success');
  266. });
  267. test('CLEAN_MESSAGE', t => {
  268. const CLEAN_MESSAGE = mutations[types.CLEAN_MESSAGE];
  269. const state = {message: {text: 'error message', status: 'error'}};
  270. CLEAN_MESSAGE(state);
  271. t.is(state.message.text, '');
  272. t.is(state.message.status, 'success');
  273. });