Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

8 роки тому
8 роки тому
8 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import test from 'ava';
  2. import nock from 'nock';
  3. import HTTP from '../src/api/http';
  4. import {TOKEN_KEY} from '../src/api/token';
  5. import Storage from '../src/api/storage';
  6. import {LocalStorageMock} from './_helpers';
  7. const storage = new Storage(new LocalStorageMock());
  8. const passwords = new HTTP('passwords', storage);
  9. const token = 'ZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFt';
  10. storage.save({baseURL: 'https://lesspass.com', [TOKEN_KEY]: token});
  11. /* eslint camelcase: 0 */
  12. const foo = {
  13. name: 'foo'
  14. };
  15. test('should send requests with Authorization header', t => {
  16. const headers = {reqheaders: {Authorization: `JWT ${token}`}};
  17. nock('https://lesspass.com', headers).get('/api/passwords/').query(true).reply(200, {});
  18. return passwords.all().then(response => {
  19. t.is(response.status, 200);
  20. });
  21. });
  22. test('should create a foo', t => {
  23. nock('https://lesspass.com').post('/api/passwords/', foo).reply(201, foo);
  24. return passwords.create(foo).then(response => {
  25. const newIncident = response.data;
  26. t.is(foo.login, newIncident.login);
  27. });
  28. });
  29. test('should send requests with Authorization header updated', t => {
  30. const newToken = 'WV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRyd';
  31. storage.save({baseURL: 'https://lesspass.com', [TOKEN_KEY]: newToken});
  32. const headers = {reqheaders: {Authorization: `JWT ${newToken}`}};
  33. nock('https://lesspass.com', headers).get('/api/passwords/').query(true).reply(200, {});
  34. return passwords.all().then(response => {
  35. t.is(response.status, 200);
  36. });
  37. });
  38. test('should get all foo with parameters', t => {
  39. nock('https://lesspass.com').get('/api/passwords/?limit=100&offset=0&search=query&ordering=-created').reply(200, {});
  40. return passwords.all({params: {limit: 100, offset: 0, search: 'query', ordering: '-created'}}).then(response => {
  41. t.is(response.status, 200);
  42. });
  43. });
  44. const clients = new HTTP('clients', storage);
  45. test('should get all clients', t => {
  46. nock('https://lesspass.com').get('/api/clients/').reply(200, {});
  47. return clients.all().then(response => {
  48. t.is(response.status, 200);
  49. });
  50. });
  51. test('should get all clients with parameters', t => {
  52. nock('https://lesspass.com').get('/api/clients/?param1=10&param2=-created').reply(200, {});
  53. return clients.all({params: {param1: 10, param2: '-created'}}).then(response => {
  54. t.is(response.status, 200);
  55. });
  56. });
  57. test('should get one resource', t => {
  58. nock('https://lesspass.com').get('/api/clients/c8e4f983-8ffe-b705-4064-d3b7aa4a4782/').reply(200, {});
  59. return clients.get({id: 'c8e4f983-8ffe-b705-4064-d3b7aa4a4782'}).then(response => {
  60. t.is(response.status, 200);
  61. });
  62. });
  63. test('should get one resource with parameters', t => {
  64. nock('https://lesspass.com').get('/api/clients/c8e4f983-8ffe-b705-4064-d3b7aa4a4782/?param1=10&param2=-created').reply(200, {});
  65. return clients.get({id: 'c8e4f983-8ffe-b705-4064-d3b7aa4a4782'}, {params: {param1: 10, param2: '-created'}})
  66. .then(response => {
  67. t.is(response.status, 200);
  68. });
  69. });
  70. test('should create one resource', t => {
  71. nock('https://lesspass.com').post('/api/clients/', {name: 'resource'}).reply(201, {});
  72. return clients.create({name: 'resource'}).then(response => {
  73. t.is(response.status, 201);
  74. });
  75. });
  76. test('should create one resource with parameters', t => {
  77. nock('https://lesspass.com').post('/api/clients/?param1=10&param2=-created', {name: 'resource'}).reply(201, {});
  78. return clients.create({name: 'resource'}, {params: {param1: 10, param2: '-created'}}).then(response => {
  79. t.is(response.status, 201);
  80. });
  81. });
  82. test('should update one resource', t => {
  83. nock('https://lesspass.com').put('/api/clients/c8e4f983-4064-8ffe-b705-d3b7aa4a4782/', {}).reply(200, {});
  84. return clients.update({id: 'c8e4f983-4064-8ffe-b705-d3b7aa4a4782'}, {}).then(response => {
  85. t.is(response.status, 200);
  86. });
  87. });
  88. test('should update one resource with parameters', t => {
  89. nock('https://lesspass.com').put('/api/clients/2/?param1=10&param2=-created', {id: '2'}).reply(200, {});
  90. return clients.update({id: '2'}, {params: {param1: 10, param2: '-created'}}).then(response => {
  91. t.is(response.status, 200);
  92. });
  93. });
  94. test('should remove one resource', t => {
  95. nock('https://lesspass.com').delete('/api/clients/c8e4f983-8ffe-4064-b705-d3b7aa4a4782/').reply(204);
  96. return clients.remove({id: 'c8e4f983-8ffe-4064-b705-d3b7aa4a4782'}).then(response => {
  97. t.is(response.status, 204);
  98. });
  99. });
  100. test('should remove one resource with parameters', t => {
  101. nock('https://lesspass.com').delete('/api/clients/8/?param1=10&param2=-created').reply(204);
  102. return clients.remove({id: '8'}, {params: {param1: 10, param2: '-created'}}).then(response => {
  103. t.is(response.status, 204);
  104. });
  105. });
  106. test('should send requests with headers', t => {
  107. const headers = {Accept: 'application/json, text/javascript, *!/!*;'};
  108. nock('https://lesspass.com', headers).get('/api/clients/').query(true).reply(200, {});
  109. return clients.all({headers}).then(response => {
  110. t.is(response.status, 200);
  111. });
  112. });