Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

entries.service.js 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import test from 'ava';
  2. import nock from 'nock';
  3. import entries from '../src/services/entries';
  4. import {entriesGetAll, entriesGetOne, storageMock} from './_helpers';
  5. entries.localStorage = storageMock();
  6. const token = 'ZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFt';
  7. entries.localStorage.setItem('token', token);
  8. const entry = {
  9. site: 'twitter.com',
  10. password: {
  11. counter: 1,
  12. settings: [
  13. 'lowercase',
  14. 'uppercase',
  15. 'numbers',
  16. 'symbols'
  17. ],
  18. length: 12
  19. },
  20. login: 'guillaume@lesspass.com'
  21. };
  22. test('should send requests with Authorization header', t => {
  23. const headers = {reqheaders: {Authorization: `JWT ${token}`}};
  24. nock('http://localhost/', headers).get('/api/entries/').query(true).reply(200, {entries: []});
  25. return entries.all().then(response => {
  26. t.is(response.status, 200);
  27. });
  28. });
  29. test('should create an entry', t => {
  30. nock('http://localhost/').post('/api/entries/', entry).reply(201, entry);
  31. return entries.create(entry)
  32. .then(newEntry => {
  33. t.is(entry.login, newEntry.login);
  34. });
  35. });
  36. test('should send requests with Authorization header updated', t => {
  37. const newToken = 'WV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRyd';
  38. entries.localStorage.setItem('token', newToken);
  39. const headers = {reqheaders: {Authorization: `JWT ${newToken}`}};
  40. nock('http://localhost/', headers).get('/api/entries/').query(true).reply(200, {entries: []});
  41. return entries.all().then(response => {
  42. t.is(response.status, 200);
  43. });
  44. });
  45. test('should get all entries with offset', t => {
  46. nock('http://localhost/').get('/api/entries/').query(true).reply(200, {entries: entriesGetAll});
  47. return entries.all().then(response => {
  48. t.is(response.status, 200);
  49. t.is(response.data.entries.results.length, entriesGetAll.count);
  50. });
  51. });
  52. test('should get all entries with parameters', t => {
  53. nock('http://localhost/').get('/api/entries/?limit=100&offset=0&search=query&ordering=-created')
  54. .reply(200, {entries: []});
  55. return entries.all(100, 0, 'query', '-created')
  56. .then(response => {
  57. t.is(response.status, 200);
  58. });
  59. });
  60. test('should get an entry', t => {
  61. nock('http://localhost/').get('/api/entries/d1ff1ae9-bb29-469d-8e5e-8a387f529de0/').reply(200, entriesGetOne);
  62. return entries.get('d1ff1ae9-bb29-469d-8e5e-8a387f529de0')
  63. .then(entry => {
  64. t.is(entriesGetOne.email, entry.email);
  65. });
  66. });
  67. test('should update an entry', t => {
  68. const updatedEntry = JSON.parse(JSON.stringify(entriesGetOne));
  69. updatedEntry.email = 'test2@lesspass.com';
  70. nock('http://localhost/').put(`/api/entries/${updatedEntry.id}/`).reply(200, updatedEntry);
  71. return entries.update(updatedEntry)
  72. .then(entry => {
  73. t.is(updatedEntry.email, entry.email);
  74. });
  75. });
  76. test('should delete an entry', t => {
  77. nock('http://localhost/').delete(`/api/entries/${entriesGetOne.id}/`).reply(200);
  78. return entries.delete(entriesGetOne)
  79. .then(response => {
  80. t.is(response, '');
  81. });
  82. });