From 58546754b8c4d7de3782883cf9a16d85a4dc7609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Mon, 29 Mar 2021 13:16:41 +0200 Subject: [PATCH] adds test for permissions store --- .../builder/src/stores/backend/permissions.js | 14 +--------- .../stores/backend/tests/datasources.spec.js | 5 ++-- .../stores/backend/tests/integration.spec.js | 0 .../stores/backend/tests/permissions.spec.js | 26 +++++++++++++++++++ 4 files changed, 30 insertions(+), 15 deletions(-) delete mode 100644 packages/builder/src/stores/backend/tests/integration.spec.js diff --git a/packages/builder/src/stores/backend/permissions.js b/packages/builder/src/stores/backend/permissions.js index 6669ef3270..f947bcce81 100644 --- a/packages/builder/src/stores/backend/permissions.js +++ b/packages/builder/src/stores/backend/permissions.js @@ -3,27 +3,15 @@ import api from "builderStore/api" export function createPermissionStore() { - const { subscribe, set } = writable([]) + const { subscribe } = writable([]) return { subscribe, - fetchLevels: async () => { - const response = await api.get("/api/permission/levels") - const json = await response.json() - set(json) - }, forResource: async resourceId => { const response = await api.get(`/api/permission/${resourceId}`) const json = await response.json() return json }, - save: async ({ role, resource, level }) => { - const response = await api.post( - `/api/permission/${role}/${resource}/${level}` - ) - const json = await response.json() - return json - }, } } diff --git a/packages/builder/src/stores/backend/tests/datasources.spec.js b/packages/builder/src/stores/backend/tests/datasources.spec.js index 5eb7e8cd05..c70104f74d 100644 --- a/packages/builder/src/stores/backend/tests/datasources.spec.js +++ b/packages/builder/src/stores/backend/tests/datasources.spec.js @@ -32,12 +32,11 @@ const SAVE_DATASOURCE = { import { createDatasourcesStore } from "../datasources" import { queries } from '../queries' -describe("Automation Data Object", () => { +describe("Datasources Store", () => { let store = createDatasourcesStore() beforeEach(() => { api.get.mockReturnValueOnce({ json: () => SOME_DATASOURCE}) - api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'}) store.init() }) @@ -81,6 +80,8 @@ describe("Automation Data Object", () => { expect(value.list).toEqual(expect.arrayContaining([SAVE_DATASOURCE])) }) it("deletes a datasource, updates the store and returns status message", async () => { + api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'}) + await store.fetch() await store.delete(SOME_DATASOURCE[0]) const value = await get(store) diff --git a/packages/builder/src/stores/backend/tests/integration.spec.js b/packages/builder/src/stores/backend/tests/integration.spec.js deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/builder/src/stores/backend/tests/permissions.spec.js b/packages/builder/src/stores/backend/tests/permissions.spec.js index e69de29bb2..54938c3c6f 100644 --- a/packages/builder/src/stores/backend/tests/permissions.spec.js +++ b/packages/builder/src/stores/backend/tests/permissions.spec.js @@ -0,0 +1,26 @@ +import api from 'builderStore/api' + +jest.mock('builderStore/api'); + +const PERMISSIONS_FOR_RESOURCE = { + "write": "BASIC", + "read": "BASIC" +} + +import { createPermissionStore } from "../permissions" + +describe("Permissions Store", () => { + const store = createPermissionStore() + + it("fetches permissions for specific resource", async () => { + api.get.mockReturnValueOnce({ json: () => PERMISSIONS_FOR_RESOURCE}) + + const resourceId = "ta_013657543b4043b89dbb17e9d3a4723a" + + const permissions = await store.forResource(resourceId) + + expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`) + expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE) + + }) +}) \ No newline at end of file