Skip to content

Commit 27b35f4

Browse files
committed
acl refactor and tests
1 parent 70090f2 commit 27b35f4

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/utils/keyHelpers/acl.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ export async function setAcl (keyDoc: string, aclBody: string) {
1919
throw new Error('Key ACL doc not found!')
2020
}
2121

22-
// delete READ only keyAclDoc. This is possible if the webId is an owner
2322
try {
24-
const response = await store.fetcher.webOperation('DELETE', keyAclDoc.value) // this may fail if webId is not an owner
25-
debug.log('delete ' + keyAclDoc.value + ' ' + response.status) // should test 404 and 2xx
23+
await store.fetcher.webOperation('PUT', keyAclDoc.value, {
24+
data: aclBody,
25+
contentType: 'text/turtle'
26+
})
2627
} catch (err) {
27-
if (err.response.status !== 404) { throw new Error(err) }
28+
if (err?.response?.status !== 404) { throw new Error(err) }
2829
debug.log('delete ' + keyAclDoc.value + ' ' + err.response.status) // should test 404 and 2xx
2930
}
30-
31-
const aclResponse = await store.fetcher.webOperation('PUT', keyAclDoc.value, {
32-
data: aclBody,
33-
contentType: 'text/turtle'
34-
})
3531
}
3632

3733
/**

test/unit/utils/keyHelpers/acl.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@ import { setAcl, keyAclBody } from '../../../../src/utils/keyHelpers/acl'
33

44
store.fetcher.load = jest.fn().mockImplementation(() => {})
55
store.any = jest.fn()
6+
store.fetcher.webOperation = jest.fn()
7+
8+
const keyDoc = 'https://alice.solidcommunity.net/profile/keys/publicKey.ttl'
9+
const keyAclDoc = 'https://alice.solidcommunity.net/profile/keys/publicKey.ttl'
10+
const error404 = {
11+
response: {
12+
status: 404
13+
}
14+
}
615

716
describe('ACL Helpers', () => {
817
describe('setAcl', () => {
918
it('throws an error if an ACL document is NOT found', async () => {
1019
/* @ts-ignore */
11-
store.any.mockReturnValue('')
12-
const keyDoc = 'https://alice.solidcommunity.net/profile/keys/publicKey.ttl'
20+
store.any.mockReturnValueOnce('')
1321
await expect(setAcl(keyDoc, keyAclBody(keyDoc, ''))).rejects.toThrowError('Key ACL doc not found!')
1422
})
15-
it.skip('want to find out why we delete and then re-add', () => {
16-
23+
it('throws error if acl can not be written', async () => {
24+
/* @ts-ignore */
25+
store.any.mockReturnValueOnce(keyAclDoc)
26+
/* @ts-ignore */
27+
store.fetcher.webOperation.mockRejectedValueOnce()
28+
await expect(setAcl(keyDoc, keyAclBody(keyDoc, ''))).rejects.toThrowError('')
29+
})
30+
it('continues if error is because acl doc can not be found.', async () => {
31+
/* @ts-ignore */
32+
store.any.mockReturnValueOnce(keyAclDoc)
33+
/* @ts-ignore */
34+
store.fetcher.webOperation.mockRejectedValueOnce(error404)
35+
await expect(setAcl(keyDoc, keyAclBody(keyDoc, ''))).resolves.toBeUndefined
1736
})
1837
})
1938
})

0 commit comments

Comments
 (0)