audit_srv_specs.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import helpers from 'test/specs/helpers';
  3. import AuditSrv from '../audit/audit_srv';
  4. import { versions, compare, restore } from 'test/mocks/audit-mocks';
  5. describe('auditSrv', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var versionsResponse = versions();
  8. var compareResponse = compare();
  9. var restoreResponse = restore;
  10. beforeEach(angularMocks.module('grafana.core'));
  11. beforeEach(angularMocks.module('grafana.services'));
  12. beforeEach(angularMocks.inject(function($httpBackend) {
  13. ctx.$httpBackend = $httpBackend;
  14. $httpBackend.whenRoute('GET', 'api/dashboards/db/:id/versions').respond(versionsResponse);
  15. $httpBackend.whenRoute('GET', 'api/dashboards/db/:id/compare/:original...:new').respond(compareResponse);
  16. $httpBackend.whenRoute('POST', 'api/dashboards/db/:id/restore')
  17. .respond(function(method, url, data, headers, params) {
  18. const parsedData = JSON.parse(data);
  19. return [200, restoreResponse(parsedData.version)];
  20. });
  21. }));
  22. beforeEach(ctx.createService('auditSrv'));
  23. describe('getAuditLog', function() {
  24. it('should return a versions array for the given dashboard id', function(done) {
  25. ctx.service.getAuditLog({ id: 1 }).then(function(versions) {
  26. expect(versions).to.eql(versionsResponse);
  27. done();
  28. });
  29. ctx.$httpBackend.flush();
  30. });
  31. it('should return an empty array when not given an id', function(done) {
  32. ctx.service.getAuditLog({ }).then(function(versions) {
  33. expect(versions).to.eql([]);
  34. done();
  35. });
  36. ctx.$httpBackend.flush();
  37. });
  38. it('should return an empty array when not given a dashboard', function(done) {
  39. ctx.service.getAuditLog().then(function(versions) {
  40. expect(versions).to.eql([]);
  41. done();
  42. });
  43. ctx.$httpBackend.flush();
  44. });
  45. });
  46. describe('compareVersions', function() {
  47. it('should return a diff object for the given dashboard revisions', function(done) {
  48. var compare = { original: 6, new: 4 };
  49. ctx.service.compareVersions({ id: 1 }, compare).then(function(response) {
  50. expect(response).to.eql(compareResponse);
  51. done();
  52. });
  53. ctx.$httpBackend.flush();
  54. });
  55. it('should return an empty object when not given an id', function(done) {
  56. var compare = { original: 6, new: 4 };
  57. ctx.service.compareVersions({ }, compare).then(function(response) {
  58. expect(response).to.eql({});
  59. done();
  60. });
  61. ctx.$httpBackend.flush();
  62. });
  63. });
  64. describe('restoreDashboard', function() {
  65. it('should return a success response given valid parameters', function(done) {
  66. var version = 6;
  67. ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) {
  68. expect(response).to.eql(restoreResponse(version));
  69. done();
  70. });
  71. ctx.$httpBackend.flush();
  72. });
  73. it('should return an empty object when not given an id', function(done) {
  74. ctx.service.restoreDashboard({}, 6).then(function(response) {
  75. expect(response).to.eql({});
  76. done();
  77. });
  78. ctx.$httpBackend.flush();
  79. });
  80. });
  81. });