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.
 
 
 
 
 
 

237 rivejä
6.8 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 = 1485989236000;
  116. const time = new Date(now);
  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 = 1485989236000;
  138. const time = new Date(now);
  139. timekeeper.freeze(time);
  140. const twentyMinutesBefore = now - 20 * 60 * 1000;
  141. const state = {
  142. lastUse: twentyMinutesBefore,
  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. });
  177. test('LOAD_PASSWORD_FOR_SITE', t => {
  178. const state = {
  179. password: {
  180. site: ''
  181. },
  182. passwords: [
  183. {id: '1', site: 'www.example.org'},
  184. {id: '2', site: 'www.google.com'}
  185. ]
  186. };
  187. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  188. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'});
  189. t.is(state.password.id, '2');
  190. t.is(state.password.site, 'www.google.com');
  191. });
  192. test('LOAD_PASSWORD_FOR_SITE no passwords', t => {
  193. const state = {
  194. password: {
  195. site: ''
  196. },
  197. passwords: []
  198. };
  199. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  200. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'});
  201. t.false('id' in state.password);
  202. t.is(state.password.site, 'google.com');
  203. });
  204. test('LOAD_PASSWORD_FOR_SITE multiple accounts matching criteria', t => {
  205. const state = {
  206. password: {
  207. site: ''
  208. },
  209. passwords: [
  210. {id: '1', site: 'www.example.org'},
  211. {id: '2', site: 'www.google.com'},
  212. {id: '3', site: 'account.google.com'},
  213. ]
  214. };
  215. const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE];
  216. LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com', url: 'https://www.google.com'});
  217. t.is(state.password.id, '2');
  218. t.is(state.password.site, 'www.google.com');
  219. });