@vida-global/core 2.5.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.js +4 -0
- package/lib/activeRecord/baseRecord.js +2 -2
- package/lib/search/README.md +232 -0
- package/lib/search/abstractSearchClient.js +188 -0
- package/lib/search/errors.js +31 -0
- package/lib/search/index.js +16 -0
- package/lib/search/openSearchClient.js +319 -0
- package/lib/search/query.js +163 -0
- package/lib/server/authorizer.js +42 -0
- package/lib/server/index.js +4 -2
- package/lib/server/serverController.js +37 -3
- package/package.json +2 -1
- package/test/activeRecord/baseRecord.test.js +22 -0
- package/test/activeRecord/db/schema.test.js +15 -0
- package/test/search/abstractSearchClient.test.js +354 -0
- package/test/search/errors.test.js +41 -0
- package/test/search/helpers/searchClient.js +137 -0
- package/test/search/openSearchClient.test.js +514 -0
- package/test/search/query.test.js +286 -0
- package/test/search/serializer.test.js +202 -0
- package/test/server/authorizer.test.js +105 -0
- package/test/server/helpers/authorizer.js +19 -0
- package/test/server/serverController.test.js +77 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
const { Authorizer } = require('../../lib/server/authorizer');
|
|
1
2
|
const { FooController } = require('./helpers/controllers/fooController');
|
|
2
3
|
const Errors = require('../../lib/server/errors');
|
|
3
4
|
const { randomPayload } = require('./helpers/serverController');
|
|
5
|
+
const { TestAuthorizer } = require('./helpers/authorizer');
|
|
4
6
|
const { buildResponseMock } = require('./helpers/response');
|
|
5
7
|
const TestHelpers = require('@vida-global/test-helpers');
|
|
6
8
|
const {
|
|
@@ -775,6 +777,81 @@ describe('VidaServerController', () => {
|
|
|
775
777
|
});
|
|
776
778
|
|
|
777
779
|
|
|
780
|
+
describe('Authorization', () => {
|
|
781
|
+
let adminUser;
|
|
782
|
+
let controller;
|
|
783
|
+
let currentUser;
|
|
784
|
+
|
|
785
|
+
class AuthorizedController extends VidaServerController {
|
|
786
|
+
get currentUser() { return currentUser; }
|
|
787
|
+
get adminUser() { return adminUser; }
|
|
788
|
+
get authorizerClass() { return TestAuthorizer; }
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
beforeEach(() => {
|
|
792
|
+
adminUser = {id: TestHelpers.Faker.Text.randomString()};
|
|
793
|
+
currentUser = {id: TestHelpers.Faker.Text.randomString()};
|
|
794
|
+
controller = new AuthorizedController({}, {});
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
describe('#authorizerClass', () => {
|
|
798
|
+
it ('returns Authorizer when a subclass does not override it', () => {
|
|
799
|
+
expect(new FooController({}, {}).authorizerClass).toBe(Authorizer);
|
|
800
|
+
});
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
describe('#authorizerParameters', () => {
|
|
805
|
+
it ('returns the current user and the admin user', () => {
|
|
806
|
+
expect(controller.authorizerParameters).toEqual([currentUser, adminUser]);
|
|
807
|
+
});
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
describe('#authorizer', () => {
|
|
812
|
+
it ('builds an instance of authorizerClass from authorizerParameters', () => {
|
|
813
|
+
expect(controller.authorizer).toBeInstanceOf(TestAuthorizer);
|
|
814
|
+
expect(controller.authorizer.currentUser).toBe(currentUser);
|
|
815
|
+
expect(controller.authorizer.adminUser).toBe(adminUser);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
it ('returns the same instance on every read', () => {
|
|
819
|
+
expect(controller.authorizer).toBe(controller.authorizer);
|
|
820
|
+
});
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
describe('#can', () => {
|
|
825
|
+
it ('returns the decision made by the authorizer', async () => {
|
|
826
|
+
const action = TestHelpers.Faker.Text.randomString();
|
|
827
|
+
const scope = {};
|
|
828
|
+
const canSpy = jest.spyOn(controller.authorizer, 'can').mockResolvedValue(true);
|
|
829
|
+
|
|
830
|
+
await expect(controller.can(action, scope)).resolves.toBe(true);
|
|
831
|
+
|
|
832
|
+
expect(canSpy).toHaveBeenCalledTimes(1);
|
|
833
|
+
expect(canSpy).toHaveBeenCalledWith(action, scope);
|
|
834
|
+
});
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
describe('#authorize', () => {
|
|
839
|
+
it ('resolves when the action is allowed', async () => {
|
|
840
|
+
jest.spyOn(controller, 'can').mockResolvedValue(true);
|
|
841
|
+
|
|
842
|
+
await expect(controller.authorize('read', {})).resolves.toBeUndefined();
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it ('throws a Forbidden error when the action is denied', async () => {
|
|
846
|
+
jest.spyOn(controller, 'can').mockResolvedValue(false);
|
|
847
|
+
|
|
848
|
+
await expect(controller.authorize('read', {}))
|
|
849
|
+
.rejects.toBeInstanceOf(Errors.ForbiddenError);
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
|
|
778
855
|
describe('#statusText', () => {
|
|
779
856
|
const cases = [
|
|
780
857
|
[200, 'ok'],
|