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.

пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 {LOCAL_STORAGE_KEY} from '../src/api/storage';
  6. import Storage from '../src/api/storage';
  7. import {LocalStorageMock} from './_helpers';
  8. const storage = new Storage(new LocalStorageMock());
  9. const passwords = new HTTP('passwords', storage);
  10. const token = 'ZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFt';
  11. storage.save({baseURL: 'https://lesspass.com', [TOKEN_KEY]: token});
  12. /* eslint camelcase: 0 */
  13. const foo = {
  14. name: 'foo'
  15. };
  16. test('should send requests with Authorization header', t => {
  17. const headers = {reqheaders: {Authorization: `JWT ${token}`}};
  18. nock('https://lesspass.com', headers).get('/api/passwords/').query(true).reply(200, {});
  19. return passwords.all().then(response => {
  20. t.is(response.status, 200);
  21. });
  22. });
  23. test('should create a foo', t => {
  24. nock('https://lesspass.com').post('/api/passwords/', foo).reply(201, foo);
  25. return passwords.create(foo).then(response => {
  26. const newIncident = response.data;
  27. t.is(foo.login, newIncident.login);
  28. });
  29. });
  30. test('should send requests with Authorization header updated', t => {
  31. const newToken = 'WV9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRyd';
  32. storage.save({baseURL: 'https://lesspass.com', [TOKEN_KEY]: newToken});
  33. const headers = {reqheaders: {Authorization: `JWT ${newToken}`}};
  34. nock('https://lesspass.com', headers).get('/api/passwords/').query(true).reply(200, {});
  35. return passwords.all().then(response => {
  36. t.is(response.status, 200);
  37. });
  38. });
  39. test('should get all foo with parameters', t => {
  40. nock('https://lesspass.com').get('/api/passwords/?limit=100&offset=0&search=query&ordering=-created')
  41. .reply(200, {});
  42. const params = {limit: 100, offset: 0, search: 'query', ordering: '-created'};
  43. return passwords.all(params).then(response => {
  44. t.is(response.status, 200);
  45. });
  46. });