Skip to content

Commit 539c802

Browse files
committed
key tests
1 parent 1a4db38 commit 539c802

2 files changed

Lines changed: 45 additions & 97 deletions

File tree

src/utils/keyHelpers/accessData.ts

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,35 @@ import * as debug from '../../debug'
22
import { store } from 'solid-logic'
33
import * as ns from '../../ns'
44
import { NamedNode } from 'rdflib'
5+
import { getRootIfPreferencesExist } from './otherHelpers'
56

6-
/* export const getPodRoot = async (webId: NamedNode) => {
7-
const webIdURL = new URL(webId.uri)
8-
// find storages in webId document
9-
await store.fetcher.load(webId.uri)
10-
const storages = store.each(webId, ns.space('storage'), null, webId.doc())
11-
var podRoot: NamedNode | undefined
12-
if (!storages?.length) {
13-
// find storage recursively in webId URL
14-
let path = webIdURL.pathname
15-
while (path.length) {
16-
path = path.substring(0, path.lastIndexOf('/'))
17-
podRoot = store.sym(webIdURL.origin + path + '/')
18-
const res = await store.fetcher.webOperation('HEAD', podRoot.uri)
19-
if (res.headers.get('link')?.includes(ns.space('Storage').value)) break
20-
if (!path) debug.warn(`Current user storage not found for\n${webId}`)
21-
}
22-
} else {
23-
// give preference to storage in webId root
24-
podRoot = storages.find((storage) => webIdURL.origin === new URL(storage.value).origin) as NamedNode
25-
if (!podRoot) podRoot = storages[0] as NamedNode
7+
export const pubKeyUrl = (webId: NamedNode) => {
8+
let url
9+
try {
10+
const root = getRootIfPreferencesExist(webId)
11+
url = `${root}/profile/keys/publicKey.ttl`
12+
} catch (err) {
13+
debug.error(err)
2614
}
15+
return url
16+
}
2717

28-
return podRoot as NamedNode
29-
} */
30-
31-
export const pubKeyUrl = async (webId: NamedNode) => {
32-
let parentSettings = store.any(webId, ns.space('preferencesFile'), null, webId.doc())?.value
33-
parentSettings = parentSettings?.split('/').slice(0, -2).join('/')
34-
if (!parentSettings) throw new Error(`prefererencesFile is expected to exist in ${webId.doc}`)
35-
return `${parentSettings}/profile/keys/publicKey.ttl`
36-
/* try {
37-
return (await getPodRoot(webId)).value + 'profile/keys/publicKey.ttl'
38-
} catch (err) { throw new Error(err) } */
18+
export const privKeyUrl = (webId: NamedNode) => {
19+
let url
20+
try {
21+
const root = getRootIfPreferencesExist(webId)
22+
url = `${root}/keys/privateKey.ttl`
23+
} catch (err) {
24+
debug.error(err)
25+
}
26+
return url
3927
}
4028

4129
export async function getExistingPublicKey (webId: NamedNode, publicKeyUrl: string) {
4230
// find publickey
4331
return await getKeyIfExists(webId, publicKeyUrl, 'publicKey')
4432
}
4533

46-
export const privKeyUrl = async (webId: NamedNode) => {
47-
let settings = store.any(webId, ns.space('preferencesFile'), null, webId.doc())?.value
48-
settings = settings?.split('/').slice(0, -1).join('/')
49-
if (!settings) throw new Error(`prefererencesFile is expected to exist in ${webId.doc}`)
50-
return `${settings}/keys/privateKey.ttl`
51-
/* try {
52-
const podRoot = await getPodRoot(webId)
53-
if (!settings?.startsWith(podRoot.value)) throw new Error(`/settings/ is expected to be in ${podRoot.value}`)
54-
return `${settings}/keys/privateKey.ttl`
55-
} catch (err) { throw new Error(err) } */
56-
}
57-
5834
export async function getExistingPrivateKey (webId: NamedNode, privateKeyUrl: string) {
5935
// find privateKey
6036
return await getKeyIfExists(webId, privateKeyUrl, 'privateKey')
Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { store } from 'solid-logic'
2-
import Fetcher from 'rdflib/lib/fetcher'
32
import { getKeyIfExists, pubKeyUrl, privKeyUrl, getExistingPublicKey, getExistingPrivateKey } from '../../../../src/utils/keyHelpers/accessData'
43
import { NamedNode } from 'rdflib'
54

@@ -13,64 +12,37 @@ store.fetcher.webOperation = jest.fn()
1312
store.each = jest.fn()
1413
store.any = jest.fn()
1514

16-
describe('cryptoKeyHelpers', () => {
17-
describe.skip('pubKeyUrl', () => {
18-
// want to check a success and an error
19-
it('returns the url for the public key', () => {
20-
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
21-
const result = pubKeyUrl(webId)
22-
expect(result).toEqual('https://alice.solid.example/profile/keys/publicKey.ttl')
23-
})
24-
})
25-
26-
describe('getExistingPrivateKey', () => {
27-
// want to make sure that getKeyIfExists is called with correct parameters
28-
it('returns...', () => {
29-
30-
})
31-
})
32-
describe('privateKeyExists', () => {
33-
// want to make sure that getKeyIfExists is called with correct parameters
34-
it('returns...', () => {
15+
jest.mock('../../../../src/utils/keyHelpers/otherHelpers', () => {
16+
return {
17+
getRootIfPreferencesExist: jest.fn().mockImplementationOnce(() => {
18+
throw new Error()
19+
})
20+
.mockImplementationOnce(() => 'https://alice.example.net')
21+
.mockImplementationOnce(() => {
22+
throw new Error()
23+
})
24+
.mockImplementationOnce(() => 'https://alice.example.net')
25+
}
26+
})
3527

28+
describe('accessData', () => {
29+
const webId = new NamedNode('https://alice.solid.example.net/profile/card#me')
30+
describe('pubKeyUrl', () => {
31+
it('to return undefined if there is not preference file', () => {
32+
expect(pubKeyUrl(webId)).toEqual(undefined)
3633
})
37-
})
38-
describe.skip('privKeyUrl', () => {
39-
it('returns...', () => {
40-
const result = privKeyUrl(new NamedNode('https://alice.solid.example/profile/card#me'))
41-
expect(result).toEqual('https://alice.solid.example/settings/keys/privateKey.ttl')
34+
it('returns the url for the public key', () => {
35+
const result = pubKeyUrl(webId)
36+
expect(result).toEqual('https://alice.example.net/profile/keys/publicKey.ttl')
4237
})
4338
})
44-
45-
/* describe('getPodRoot', () => {
46-
/* @ts-ignore */
47-
// store.fetcher.webOperation.mockImplementationOnce(() => {})
48-
/* @ts-ignore */
49-
/* store.each.mockImplementationOnce(() => [{ value: 'https://alice.solid.example' }])
50-
it('returns...', async () => {
51-
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
52-
const result = await getPodRoot(webId)
53-
expect(result.value).toBe('https://alice.solid.example')
54-
})
55-
}) */
56-
describe('getKeyIfExists', () => {
57-
/* @ts-ignore */
58-
store.any.mockReturnValue({ value: PUB_KEY })
59-
it('returns a key if it exists', async () => {
60-
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
61-
const result = await getKeyIfExists(webId, PUB_KEY, 'publicKey')
62-
expect(result).toBe(PUB_KEY)
63-
})
64-
it.skip('throws an error when load fails', async () => {
65-
// need to check if 'any' can throw an error
66-
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
67-
const result = await getKeyIfExists(webId, 'testing', 'publicKey')
68-
expect(result).toBe('testing')
39+
describe('privKeyUrl', () => {
40+
it('to return undefined if there is not preference file', () => {
41+
expect(privKeyUrl(webId)).toEqual(undefined)
6942
})
70-
it.skip('returns undefined if key not found', async () => {
71-
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
72-
const result = await getKeyIfExists(webId, PUB_KEY, 'publicKey')
73-
expect(result).toBe(PUB_KEY)
43+
it('returns the url for the private key', () => {
44+
const result = privKeyUrl(webId)
45+
expect(result).toEqual('https://alice.example.net/keys/privateKey.ttl')
7446
})
7547
})
7648
})

0 commit comments

Comments
 (0)