adds test for permissions store

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 13:16:41 +02:00
parent f8475bfdc2
commit 58546754b8
4 changed files with 30 additions and 15 deletions

View File

@ -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
},
}
}

View File

@ -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)

View File

@ -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)
})
})