Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

191 строка
5.4 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. test('LOGOUT', t => {
  6. const LOGOUT = mutations[types.LOGOUT];
  7. const state = {authenticated: true};
  8. LOGOUT(state);
  9. t.false(state.authenticated);
  10. });
  11. test('LOGIN', t => {
  12. const LOGIN = mutations[types.LOGIN];
  13. const state = {authenticated: false};
  14. LOGIN(state);
  15. t.true(state.authenticated);
  16. });
  17. test('SET_PASSWORD', t => {
  18. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  19. const state = {password: null};
  20. SET_PASSWORD(state, {password: {uppercase: true, version: 2}});
  21. t.is(state.password.version, 2);
  22. t.true(state.password.uppercase);
  23. });
  24. test('SET_PASSWORD change lastUse date', t => {
  25. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  26. const now = 1485989236000;
  27. const time = new Date(now);
  28. timekeeper.freeze(time);
  29. const state = {lastUse: null, password: null};
  30. SET_PASSWORD(state, {password: {}});
  31. t.is(now, state.lastUse);
  32. timekeeper.reset();
  33. });
  34. test('SET_PASSWORD immutable', t => {
  35. const SET_PASSWORD = mutations[types.SET_PASSWORD];
  36. const state = {};
  37. const password = {version: 2};
  38. SET_PASSWORD(state, {password});
  39. password.version = 1;
  40. t.is(state.password.version, 2);
  41. });
  42. test('SET_DEFAULT_PASSWORD', t => {
  43. const SET_DEFAULT_PASSWORD = mutations[types.SET_DEFAULT_PASSWORD];
  44. const state = {
  45. defaultPassword: {
  46. site: '',
  47. login: '',
  48. uppercase: true,
  49. lowercase: true,
  50. numbers: true,
  51. symbols: true,
  52. length: 16,
  53. counter: 1,
  54. version: 2
  55. }
  56. };
  57. SET_DEFAULT_PASSWORD(state, {password: {symbols: false, length: 30}});
  58. t.is(state.defaultPassword.length, 30);
  59. t.false(state.defaultPassword.symbols);
  60. });
  61. test('SET_PASSWORDS', t => {
  62. const SET_PASSWORDS = mutations[types.SET_PASSWORDS];
  63. const state = {
  64. passwords: []
  65. };
  66. SET_PASSWORDS(state, {passwords: [{site: 'site1'}, {site: 'site2'}]});
  67. t.is(state.passwords[0].site, 'site1');
  68. t.is(state.passwords[1].site, 'site2');
  69. });
  70. test('DELETE_PASSWORD', t => {
  71. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  72. const state = {
  73. passwords: [{id: '1', site: 'site1'}, {id: '2', site: 'site2'}]
  74. };
  75. t.is(state.passwords.length, 2);
  76. DELETE_PASSWORD(state, {id: '1'});
  77. t.is(state.passwords.length, 1);
  78. });
  79. test('DELETE_PASSWORD clean password with default password if same id', t => {
  80. const DELETE_PASSWORD = mutations[types.DELETE_PASSWORD];
  81. const state = {
  82. passwords: [{id: '1', length: 30}, {id: '2', length: 16}],
  83. password: {id: '1', length: 30},
  84. defaultPassword: {length: 16}
  85. };
  86. DELETE_PASSWORD(state, {id: '1'});
  87. t.is(state.password.length, 16);
  88. });
  89. test('SET_BASE_URL', t => {
  90. const SET_BASE_URL = mutations[types.SET_BASE_URL];
  91. const state = {
  92. baseURL: 'https://lesspass.com'
  93. };
  94. const baseURL = 'https://example.org';
  95. SET_BASE_URL(state, {baseURL: baseURL});
  96. t.is(state.baseURL, baseURL);
  97. });
  98. test('SET_VERSION', t => {
  99. const SET_VERSION = mutations[types.SET_VERSION];
  100. const state = {
  101. password: {version: 2},
  102. };
  103. SET_VERSION(state, {version: 1});
  104. t.is(state.password.version, 1);
  105. });
  106. test('SET_VERSION password null', t => {
  107. const SET_VERSION = mutations[types.SET_VERSION];
  108. const state = {
  109. password: null,
  110. };
  111. SET_VERSION(state, {version: 2});
  112. t.is(state.password.version, 2);
  113. });
  114. test('LOAD_PASSWORD_FIRST_TIME 5 minutes after last use', t => {
  115. const now = 1485989236;
  116. const time = new Date(now * 1000);
  117. timekeeper.freeze(time);
  118. const fiveMinutesBefore = (now - 5 * 60) * 1000;
  119. const state = {
  120. lastUse: fiveMinutesBefore,
  121. password: {
  122. login: 'test@example.org',
  123. length: 30
  124. },
  125. defaultPassword: {
  126. login: '',
  127. length: 16
  128. }
  129. };
  130. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  131. LOAD_PASSWORD_FIRST_TIME(state);
  132. t.is(state.password.login, 'test@example.org');
  133. t.is(state.password.length, 30);
  134. timekeeper.reset();
  135. });
  136. test('LOAD_PASSWORD_FIRST_TIME more than 10 minutes after last use', t => {
  137. const now = 1485989236;
  138. const time = new Date(now * 1000);
  139. timekeeper.freeze(time);
  140. const fifteenMinutesBefore = (now - 15 * 60) * 1000;
  141. const state = {
  142. lastUse: fifteenMinutesBefore,
  143. password: {
  144. login: 'test@example.org',
  145. length: 30
  146. },
  147. defaultPassword: {
  148. login: '',
  149. length: 16
  150. }
  151. };
  152. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  153. LOAD_PASSWORD_FIRST_TIME(state);
  154. t.is(state.password.login, '');
  155. t.is(state.password.length, 16);
  156. timekeeper.reset();
  157. });
  158. test('LOAD_PASSWORD_FIRST_TIME last use null', t => {
  159. const time = new Date(1485989236000);
  160. timekeeper.freeze(time);
  161. const state = {
  162. lastUse: null,
  163. password: {
  164. site: '',
  165. version: 1
  166. },
  167. defaultPassword: {
  168. site: '',
  169. version: 2
  170. }
  171. };
  172. const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME];
  173. LOAD_PASSWORD_FIRST_TIME(state);
  174. t.is(state.password.version,2);
  175. timekeeper.reset();
  176. });