import type { ServerResponse } from 'http'; import { createResponse } from 'node-mocks-http'; import { RepresentationMetadata } from '../../../../../../src/http/representation/RepresentationMetadata'; import { OwnerMetadataWriter } from '../../../../../../src/identity/interaction/pod/util/OwnerMetadataWriter'; import { PodStore } from '../../../../../../src/identity/interaction/pod/util/PodStore'; import type { StorageLocationStrategy } from '../../../../../../src/server/description/StorageLocationStrategy'; import { joinUrl } from '../../../../../../src/util/PathUtil'; describe('An OwnerMetadataWriter', (): void => { const id = 'id'; const accountId = 'accountId'; const target = { path: 'http://example.com/pod/' }; const webId = 'http://example.com/webId#me'; let metadata: RepresentationMetadata; let response: ServerResponse; let podStore: jest.Mocked; let storageStrategy: jest.Mocked; let writer: OwnerMetadataWriter; beforeEach(async(): Promise => { metadata = new RepresentationMetadata(target); response = createResponse(); podStore = { findByBaseUrl: jest.fn().mockResolvedValue({ id, accountId }), getOwners: jest.fn().mockResolvedValue([{ webId, visible: true }]), } satisfies Partial as any; storageStrategy = { getStorageIdentifier: jest.fn().mockResolvedValue(target), }; writer = new OwnerMetadataWriter(podStore, storageStrategy); }); it('adds the correct link headers.', async(): Promise => { await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({ link: `<${webId}>; rel="http://www.w3.org/ns/solid/terms#owner"` }); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(1); expect(storageStrategy.getStorageIdentifier).toHaveBeenLastCalledWith(target); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(1); expect(podStore.findByBaseUrl).toHaveBeenLastCalledWith(target.path); expect(podStore.getOwners).toHaveBeenCalledTimes(1); expect(podStore.getOwners).toHaveBeenLastCalledWith(id); }); it('adds no headers if the identifier is a blank node.', async(): Promise => { metadata = new RepresentationMetadata(); await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({}); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(0); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(0); expect(podStore.getOwners).toHaveBeenCalledTimes(0); }); it('adds no headers if no root storage could be found.', async(): Promise => { storageStrategy.getStorageIdentifier.mockRejectedValueOnce(new Error('bad identifier')); await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({}); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(1); expect(storageStrategy.getStorageIdentifier).toHaveBeenLastCalledWith(target); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(0); expect(podStore.getOwners).toHaveBeenCalledTimes(0); }); it('adds no headers if the target is not a pod base URL.', async(): Promise => { metadata = new RepresentationMetadata({ path: joinUrl(target.path, 'document') }); await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({}); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(1); expect(storageStrategy.getStorageIdentifier).toHaveBeenLastCalledWith({ path: joinUrl(target.path, 'document') }); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(0); expect(podStore.getOwners).toHaveBeenCalledTimes(0); }); it('adds no headers if there is no matching pod object.', async(): Promise => { podStore.findByBaseUrl.mockResolvedValueOnce(undefined); await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({}); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(1); expect(storageStrategy.getStorageIdentifier).toHaveBeenLastCalledWith(target); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(1); expect(podStore.findByBaseUrl).toHaveBeenLastCalledWith(target.path); expect(podStore.getOwners).toHaveBeenCalledTimes(0); }); it('adds no headers if there are no matching owners.', async(): Promise => { podStore.getOwners.mockResolvedValueOnce(undefined); await expect(writer.handle({ metadata, response })).resolves.toBeUndefined(); expect(response.getHeaders()).toEqual({}); expect(storageStrategy.getStorageIdentifier).toHaveBeenCalledTimes(1); expect(storageStrategy.getStorageIdentifier).toHaveBeenLastCalledWith(target); expect(podStore.findByBaseUrl).toHaveBeenCalledTimes(1); expect(podStore.findByBaseUrl).toHaveBeenLastCalledWith(target.path); expect(podStore.getOwners).toHaveBeenCalledTimes(1); expect(podStore.getOwners).toHaveBeenLastCalledWith(id); }); });