solid-logic 3.1.1-da65b2c → 3.1.1-e3eba68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +2 -108
  2. package/eslint.config.js +29 -0
  3. package/jest.config.js +10 -0
  4. package/lib/acl/aclLogic.d.ts +5 -0
  5. package/{dist → lib}/acl/aclLogic.d.ts.map +1 -1
  6. package/lib/acl/aclLogic.js +168 -0
  7. package/lib/acl/aclLogic.js.map +1 -0
  8. package/{dist → lib}/authSession/authSession.js +1 -1
  9. package/{dist → lib}/authSession/authSession.js.map +1 -1
  10. package/{dist → lib}/authn/SolidAuthnLogic.d.ts +3 -3
  11. package/{dist → lib}/authn/SolidAuthnLogic.d.ts.map +1 -1
  12. package/lib/authn/SolidAuthnLogic.js +193 -0
  13. package/lib/authn/SolidAuthnLogic.js.map +1 -0
  14. package/{dist → lib}/authn/authUtil.d.ts +1 -1
  15. package/{dist → lib}/authn/authUtil.js +12 -12
  16. package/lib/authn/authUtil.js.map +1 -0
  17. package/{dist → lib}/chat/chatLogic.d.ts +1 -1
  18. package/lib/chat/chatLogic.js +272 -0
  19. package/lib/chat/chatLogic.js.map +1 -0
  20. package/{dist → lib}/inbox/inboxLogic.d.ts +1 -1
  21. package/{dist → lib}/inbox/inboxLogic.d.ts.map +1 -1
  22. package/lib/inbox/inboxLogic.js +139 -0
  23. package/lib/inbox/inboxLogic.js.map +1 -0
  24. package/{dist → lib}/index.d.ts +1 -1
  25. package/{dist → lib}/index.d.ts.map +1 -1
  26. package/{dist → lib}/index.js +4 -4
  27. package/{dist → lib}/index.js.map +1 -1
  28. package/lib/issuer/issuerLogic.js +52 -0
  29. package/lib/issuer/issuerLogic.js.map +1 -0
  30. package/{dist → lib}/logic/CustomError.d.ts.map +1 -1
  31. package/lib/logic/CustomError.js +89 -0
  32. package/lib/logic/CustomError.js.map +1 -0
  33. package/lib/logic/solidLogic.d.ts +6 -0
  34. package/{dist → lib}/logic/solidLogic.d.ts.map +1 -1
  35. package/{dist → lib}/logic/solidLogic.js +33 -32
  36. package/lib/logic/solidLogic.js.map +1 -0
  37. package/lib/logic/solidLogicSingleton.js +94 -0
  38. package/lib/logic/solidLogicSingleton.js.map +1 -0
  39. package/{dist → lib}/profile/profileLogic.d.ts +1 -1
  40. package/lib/profile/profileLogic.js +259 -0
  41. package/lib/profile/profileLogic.js.map +1 -0
  42. package/lib/typeIndex/typeIndexLogic.js +399 -0
  43. package/lib/typeIndex/typeIndexLogic.js.map +1 -0
  44. package/{dist → lib}/types.d.ts +2 -2
  45. package/{dist → lib}/util/containerLogic.d.ts +1 -1
  46. package/{dist → lib}/util/containerLogic.d.ts.map +1 -1
  47. package/lib/util/containerLogic.js +102 -0
  48. package/lib/util/containerLogic.js.map +1 -0
  49. package/lib/util/debug.d.ts.map +1 -0
  50. package/lib/util/debug.js +40 -0
  51. package/lib/util/debug.js.map +1 -0
  52. package/{dist → lib}/util/ns.js +2 -2
  53. package/{dist → lib}/util/ns.js.map +1 -1
  54. package/{dist → lib}/util/utilityLogic.d.ts +1 -1
  55. package/{dist → lib}/util/utilityLogic.d.ts.map +1 -1
  56. package/lib/util/utilityLogic.js +284 -0
  57. package/lib/util/utilityLogic.js.map +1 -0
  58. package/{dist → lib}/util/utils.d.ts +1 -1
  59. package/{dist → lib}/util/utils.d.ts.map +1 -1
  60. package/lib/util/utils.js +47 -0
  61. package/lib/util/utils.js.map +1 -0
  62. package/package.json +20 -58
  63. package/src/acl/aclLogic.ts +156 -0
  64. package/src/authSession/authSession.ts +7 -0
  65. package/src/authn/SolidAuthnLogic.ts +127 -0
  66. package/src/authn/authUtil.ts +70 -0
  67. package/src/chat/chatLogic.ts +226 -0
  68. package/src/inbox/inboxLogic.ts +59 -0
  69. package/src/index.ts +22 -0
  70. package/src/issuer/issuerLogic.ts +40 -0
  71. package/src/logic/CustomError.ts +29 -0
  72. package/src/logic/solidLogic.ts +76 -0
  73. package/src/logic/solidLogicSingleton.ts +20 -0
  74. package/src/profile/profileLogic.ts +125 -0
  75. package/src/typeIndex/typeIndexLogic.ts +198 -0
  76. package/src/types.ts +122 -0
  77. package/{dist/util/containerLogic.js → src/util/containerLogic.ts} +25 -20
  78. package/src/util/debug.ts +20 -0
  79. package/src/util/ns.ts +5 -0
  80. package/src/util/utilityLogic.ts +156 -0
  81. package/src/util/utils.ts +52 -0
  82. package/test/aclLogic.test.ts +24 -0
  83. package/test/authUtil.test.ts +23 -0
  84. package/test/chatLogic.test.ts +316 -0
  85. package/test/container.test.ts +58 -0
  86. package/test/helpers/dataSetup.ts +134 -0
  87. package/test/helpers/setup.ts +17 -0
  88. package/test/inboxLogic.test.ts +218 -0
  89. package/test/logic.test.ts +29 -0
  90. package/test/profileLogic.test.ts +246 -0
  91. package/test/solidAuthLogic.test.ts +49 -0
  92. package/test/typeIndexLogic.test.ts +255 -0
  93. package/test/utilityLogic.test.ts +179 -0
  94. package/test/utils.test.ts +32 -0
  95. package/tsconfig.json +75 -0
  96. package/dist/789.solid-logic.js +0 -11
  97. package/dist/789.solid-logic.js.map +0 -1
  98. package/dist/789.solid-logic.min.js +0 -1
  99. package/dist/841.solid-logic.js +0 -12133
  100. package/dist/841.solid-logic.js.map +0 -1
  101. package/dist/841.solid-logic.min.js +0 -73
  102. package/dist/841.solid-logic.min.js.map +0 -1
  103. package/dist/acl/aclLogic.d.ts +0 -5
  104. package/dist/acl/aclLogic.js +0 -122
  105. package/dist/acl/aclLogic.js.map +0 -1
  106. package/dist/authn/SolidAuthnLogic.js +0 -147
  107. package/dist/authn/SolidAuthnLogic.js.map +0 -1
  108. package/dist/authn/authUtil.js.map +0 -1
  109. package/dist/chat/chatLogic.js +0 -160
  110. package/dist/chat/chatLogic.js.map +0 -1
  111. package/dist/inbox/inboxLogic.js +0 -55
  112. package/dist/inbox/inboxLogic.js.map +0 -1
  113. package/dist/issuer/issuerLogic.js +0 -40
  114. package/dist/issuer/issuerLogic.js.map +0 -1
  115. package/dist/logic/CustomError.js +0 -38
  116. package/dist/logic/CustomError.js.map +0 -1
  117. package/dist/logic/solidLogic.d.ts +0 -6
  118. package/dist/logic/solidLogic.js.map +0 -1
  119. package/dist/logic/solidLogicSingleton.js +0 -54
  120. package/dist/logic/solidLogicSingleton.js.map +0 -1
  121. package/dist/profile/profileLogic.js +0 -165
  122. package/dist/profile/profileLogic.js.map +0 -1
  123. package/dist/solid-logic.esm.external.js +0 -12725
  124. package/dist/solid-logic.esm.external.js.map +0 -1
  125. package/dist/solid-logic.esm.external.min.js +0 -32
  126. package/dist/solid-logic.esm.external.min.js.map +0 -1
  127. package/dist/solid-logic.external.js +0 -12802
  128. package/dist/solid-logic.external.js.map +0 -1
  129. package/dist/solid-logic.external.min.js +0 -32
  130. package/dist/solid-logic.external.min.js.map +0 -1
  131. package/dist/solid-logic.js +0 -40921
  132. package/dist/solid-logic.js.map +0 -1
  133. package/dist/solid-logic.min.js +0 -41
  134. package/dist/solid-logic.min.js.map +0 -1
  135. package/dist/typeIndex/typeIndexLogic.js +0 -222
  136. package/dist/typeIndex/typeIndexLogic.js.map +0 -1
  137. package/dist/util/containerLogic.js.map +0 -1
  138. package/dist/util/debug.d.ts.map +0 -1
  139. package/dist/util/debug.js +0 -20
  140. package/dist/util/debug.js.map +0 -1
  141. package/dist/util/utilityLogic.js +0 -180
  142. package/dist/util/utilityLogic.js.map +0 -1
  143. package/dist/util/utils.js +0 -47
  144. package/dist/util/utils.js.map +0 -1
  145. package/dist/versionInfo.js +0 -33
  146. package/dist/versionInfo.js.map +0 -1
  147. /package/{dist → lib}/authSession/authSession.d.ts +0 -0
  148. /package/{dist → lib}/authSession/authSession.d.ts.map +0 -0
  149. /package/{dist → lib}/authn/authUtil.d.ts.map +0 -0
  150. /package/{dist → lib}/chat/chatLogic.d.ts.map +0 -0
  151. /package/{dist → lib}/issuer/issuerLogic.d.ts +0 -0
  152. /package/{dist → lib}/issuer/issuerLogic.d.ts.map +0 -0
  153. /package/{dist → lib}/logic/CustomError.d.ts +0 -0
  154. /package/{dist → lib}/logic/solidLogicSingleton.d.ts +0 -0
  155. /package/{dist → lib}/logic/solidLogicSingleton.d.ts.map +0 -0
  156. /package/{dist → lib}/profile/profileLogic.d.ts.map +0 -0
  157. /package/{dist → lib}/typeIndex/typeIndexLogic.d.ts +0 -0
  158. /package/{dist → lib}/typeIndex/typeIndexLogic.d.ts.map +0 -0
  159. /package/{dist → lib}/types.d.ts.map +0 -0
  160. /package/{dist → lib}/types.js +0 -0
  161. /package/{dist → lib}/types.js.map +0 -0
  162. /package/{dist → lib}/util/debug.d.ts +0 -0
  163. /package/{dist → lib}/util/ns.d.ts +0 -0
  164. /package/{dist → lib}/util/ns.d.ts.map +0 -0
@@ -0,0 +1,156 @@
1
+ import { NamedNode, st, sym } from "rdflib";
2
+ import { CrossOriginForbiddenError, FetchError, NotEditableError, SameOriginForbiddenError, UnauthorizedError, WebOperationError } from "../logic/CustomError";
3
+ import * as debug from '../util/debug';
4
+ import { differentOrigin } from "./utils";
5
+
6
+ export function createUtilityLogic(store, aclLogic, containerLogic) {
7
+
8
+ async function recursiveDelete(containerNode: NamedNode) {
9
+ try {
10
+ if (containerLogic.isContainer(containerNode)) {
11
+ const aclDocUrl = await aclLogic.findAclDocUrl(containerNode)
12
+ await store.fetcher._fetch(aclDocUrl, { method: "DELETE" });
13
+ const containerMembers = await containerLogic.getContainerMembers(containerNode);
14
+ await Promise.all(
15
+ containerMembers.map((url) => recursiveDelete(url))
16
+ );
17
+ }
18
+ const nodeToStringHere = containerNode.value;
19
+ return store.fetcher._fetch(nodeToStringHere, { method: "DELETE" })
20
+ } catch (e) {
21
+ debug.log(`Please manually remove ${containerNode.value} from your system.`, e);
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Create a resource if it really does not exist
27
+ * Be absolutely sure something does not exist before creating a new empty file
28
+ * as otherwise existing could be deleted.
29
+ * @param doc {NamedNode} - The resource
30
+ */
31
+ async function loadOrCreateIfNotExists(doc: NamedNode) {
32
+ let response
33
+ try {
34
+ response = await store.fetcher.load(doc)
35
+ } catch (err) {
36
+ if (err.response.status === 404) {
37
+ try {
38
+ await store.fetcher.webOperation('PUT', doc, { data: '', contentType: 'text/turtle' })
39
+ } catch (err) {
40
+ const msg = 'createIfNotExists: PUT FAILED: ' + doc + ': ' + err
41
+ throw new WebOperationError(msg)
42
+ }
43
+ await store.fetcher.load(doc)
44
+ } else {
45
+ if (err.response.status === 401) {
46
+ throw new UnauthorizedError();
47
+ }
48
+ if (err.response.status === 403) {
49
+ if (differentOrigin(doc)) {
50
+ throw new CrossOriginForbiddenError();
51
+ }
52
+ throw new SameOriginForbiddenError();
53
+ }
54
+ const msg = 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err
55
+ throw new FetchError(err.status, err.message + msg)
56
+ }
57
+ }
58
+ return response
59
+ }
60
+
61
+ /* Follow link from this doc to another thing, or else make a new link
62
+ **
63
+ ** @returns existing object, or creates it if non existent
64
+ */
65
+ async function followOrCreateLink(subject: NamedNode, predicate: NamedNode,
66
+ object: NamedNode, doc: NamedNode
67
+ ): Promise<NamedNode | null> {
68
+ await store.fetcher.load(doc)
69
+ const result = store.any(subject, predicate, null, doc)
70
+
71
+ if (result) return result as NamedNode
72
+ if (!store.updater.editable(doc)) {
73
+ const msg = `followOrCreateLink: cannot edit ${doc.value}`
74
+ debug.warn(msg)
75
+ throw new NotEditableError(msg)
76
+ }
77
+ try {
78
+ await store.updater.update([], [st(subject, predicate, object, doc)])
79
+ } catch (err) {
80
+ const msg = `followOrCreateLink: Error making link in ${doc} to ${object}: ${err}`
81
+ debug.warn(msg)
82
+ throw new WebOperationError(err)
83
+ }
84
+
85
+ try {
86
+ await loadOrCreateIfNotExists(object)
87
+ // store.fetcher.webOperation('PUT', object, { data: '', contentType: 'text/turtle'})
88
+ } catch (err) {
89
+ debug.warn(`followOrCreateLink: Error loading or saving new linked document: ${object}: ${err}`)
90
+ throw err;
91
+ }
92
+ return object
93
+ }
94
+
95
+ // Copied from https://github.com/solidos/web-access-control-tests/blob/v3.0.0/test/surface/delete.test.ts#L5
96
+ async function setSinglePeerAccess(options: {
97
+ ownerWebId: string,
98
+ peerWebId: string,
99
+ accessToModes?: string,
100
+ defaultModes?: string,
101
+ target: string
102
+ }) {
103
+ let str = [
104
+ '@prefix acl: <http://www.w3.org/ns/auth/acl#>.',
105
+ '',
106
+ `<#alice> a acl:Authorization;\n acl:agent <${options.ownerWebId}>;`,
107
+ ` acl:accessTo <${options.target}>;`,
108
+ ` acl:default <${options.target}>;`,
109
+ ' acl:mode acl:Read, acl:Write, acl:Control.',
110
+ ''
111
+ ].join('\n')
112
+ if (options.accessToModes) {
113
+ str += [
114
+ '<#bobAccessTo> a acl:Authorization;',
115
+ ` acl:agent <${options.peerWebId}>;`,
116
+ ` acl:accessTo <${options.target}>;`,
117
+ ` acl:mode ${options.accessToModes}.`,
118
+ ''
119
+ ].join('\n')
120
+ }
121
+ if (options.defaultModes) {
122
+ str += [
123
+ '<#bobDefault> a acl:Authorization;',
124
+ ` acl:agent <${options.peerWebId}>;`,
125
+ ` acl:default <${options.target}>;`,
126
+ ` acl:mode ${options.defaultModes}.`,
127
+ ''
128
+ ].join('\n')
129
+ }
130
+ const aclDocUrl = await aclLogic.findAclDocUrl(sym(options.target))
131
+ return store.fetcher._fetch(aclDocUrl, {
132
+ method: 'PUT',
133
+ body: str,
134
+ headers: [
135
+ ['Content-Type', 'text/turtle']
136
+ ]
137
+ });
138
+ }
139
+
140
+ async function createEmptyRdfDoc(doc: NamedNode, comment: string) {
141
+ await store.fetcher.webOperation("PUT", doc.uri, {
142
+ data: `# ${new Date()} ${comment}
143
+ `,
144
+ contentType: "text/turtle",
145
+ });
146
+ }
147
+
148
+ return {
149
+ recursiveDelete,
150
+ setSinglePeerAccess,
151
+ createEmptyRdfDoc,
152
+ followOrCreateLink,
153
+ loadOrCreateIfNotExists
154
+ }
155
+ }
156
+
@@ -0,0 +1,52 @@
1
+ import { NamedNode, sym } from "rdflib";
2
+
3
+ export function newThing(doc: NamedNode): NamedNode {
4
+ return sym(doc.uri + "#" + "id" + ("" + Date.now()));
5
+ }
6
+
7
+ export function uniqueNodes (arr: NamedNode[]): NamedNode[] {
8
+ const uris = arr.map(x => x.uri)
9
+ const set = new Set(uris)
10
+ const uris2 = Array.from(set)
11
+ const arr2 = uris2.map(u => new NamedNode(u))
12
+ return arr2 // Array.from(new Set(arr.map(x => x.uri))).map(u => sym(u))
13
+ }
14
+
15
+ export function getArchiveUrl(baseUrl: string, date: Date) {
16
+ const year = date.getUTCFullYear();
17
+ const month = ('0' + (date.getUTCMonth()+1)).slice(-2);
18
+ const day = ('0' + (date.getUTCDate())).slice(-2);
19
+ const parts = baseUrl.split('/');
20
+ const filename = parts[parts.length -1 ];
21
+ return new URL(`./archive/${year}/${month}/${day}/${filename}`, baseUrl).toString();
22
+ }
23
+
24
+ export function differentOrigin(doc): boolean {
25
+ if (!doc) {
26
+ return true;
27
+ }
28
+ return (
29
+ `${window.location.origin}/` !== new URL(doc.value).origin
30
+ );
31
+ }
32
+
33
+ export function suggestPreferencesFile (me:NamedNode) {
34
+ const stripped = me.uri.replace('/profile/', '/').replace('/public/', '/')
35
+ // const stripped = me.uri.replace(\/[p|P]rofile/\g, '/').replace(\/[p|P]ublic/\g, '/')
36
+ const folderURI = stripped.split('/').slice(0,-1).join('/') + '/Settings/'
37
+ const fileURI = folderURI + 'Preferences.ttl'
38
+ return sym(fileURI)
39
+ }
40
+
41
+ export function determineChatContainer(
42
+ invitee: NamedNode,
43
+ podRoot: NamedNode
44
+ ): NamedNode {
45
+ // Create chat
46
+ // See https://gitter.im/solid/chat-app?at=5f3c800f855be416a23ae74a
47
+ const chatContainerStr = new URL(
48
+ `IndividualChats/${new URL(invitee.value).host}/`,
49
+ podRoot.value
50
+ ).toString();
51
+ return new NamedNode(chatContainerStr);
52
+ }
@@ -0,0 +1,24 @@
1
+ import { Fetcher, Store, sym, UpdateManager } from 'rdflib';
2
+ import { createAclLogic } from '../src/acl/aclLogic';
3
+
4
+ describe('setACLUserPublic', () => {
5
+ let aclLogic
6
+ let store
7
+ beforeAll(() => {
8
+ const options = { fetch: fetch };
9
+ store = new Store()
10
+ store.fetcher = new Fetcher(store, options);
11
+ store.updater = new UpdateManager(store);
12
+ aclLogic = createAclLogic(store)
13
+ })
14
+ it('exists', () => {
15
+ expect(aclLogic.setACLUserPublic).toBeInstanceOf(Function)
16
+ })
17
+ it.skip('runs', async () => {
18
+ expect(await aclLogic.setACLUserPublic(
19
+ 'https://test.test#',
20
+ sym('https://test.test#'),
21
+ {}
22
+ )).toEqual({})
23
+ })
24
+ })
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ *
4
+ */
5
+ import * as authUtil from '../src/authn/authUtil'
6
+
7
+ describe('offlineTestID', () => {
8
+ it('exists', () => {
9
+ expect(authUtil.offlineTestID).toBeInstanceOf(Function)
10
+ })
11
+ it('runs', () => {
12
+ expect(authUtil.offlineTestID()).toEqual(null)
13
+ })
14
+ })
15
+
16
+ describe('appContext', () => {
17
+ it('exists', () => {
18
+ expect(authUtil.appContext).toBeInstanceOf(Function)
19
+ })
20
+ it('runs', () => {
21
+ expect(authUtil.appContext()).toEqual({"viewingNoAuthPage": false,})
22
+ })
23
+ })
@@ -0,0 +1,316 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ *
4
+ */
5
+ import { UpdateManager, Store, Fetcher } from "rdflib";
6
+ import { createAclLogic } from "../src/acl/aclLogic";
7
+ import { createChatLogic } from '../src/chat/chatLogic';
8
+ import { createProfileLogic } from "../src/profile/profileLogic";
9
+ import { createContainerLogic } from "../src/util/containerLogic";
10
+ import { createUtilityLogic } from "../src/util/utilityLogic";
11
+ import { alice, bob } from "./helpers/dataSetup";
12
+
13
+ window.$SolidTestEnvironment = { username: alice.uri }
14
+
15
+ describe("Chat logic", () => {
16
+ let chatLogic;
17
+ let store;
18
+ beforeEach(() => {
19
+ fetchMock.resetMocks();
20
+ fetchMock.mockResponse("Not Found", {
21
+ status: 404,
22
+ });
23
+ store = new Store()
24
+ store.fetcher = new Fetcher(store, { fetch: fetch });
25
+ store.updater = new UpdateManager(store);
26
+ const authn = {
27
+ currentUser: () => {
28
+ return alice;
29
+ },
30
+ };
31
+ const util = createUtilityLogic(store, createAclLogic(store), createContainerLogic(store))
32
+ chatLogic = createChatLogic(store, createProfileLogic(store, authn, util))
33
+ });
34
+
35
+ describe("get chat, without creating", () => {
36
+ describe("when no chat exists yet", () => {
37
+ let result;
38
+ beforeEach(async () => {
39
+ aliceHasValidProfile();
40
+ noChatWithBobExists();
41
+ result = await chatLogic.getChat(bob, false);
42
+ });
43
+ it("does not return a chat", async () => {
44
+ expect(result).toBeNull();
45
+ });
46
+ it("loaded the current user profile", () => {
47
+ expect(fetchMock.mock.calls[0][0]).toBe(
48
+ "https://alice.example.com/profile/card.ttl"
49
+ );
50
+ });
51
+ it("tried to load the chat document", () => {
52
+ expect(fetchMock.mock.calls[1][0]).toBe(
53
+ "https://alice.example.com/IndividualChats/bob.example.com/index.ttl"
54
+ );
55
+ });
56
+ it("has no additional fetch requests", () => {
57
+ expect(fetchMock.mock.calls.length).toBe(2);
58
+ });
59
+ });
60
+ });
61
+
62
+ describe("get chat, create if missing", () => {
63
+ describe("when no chat exists yet", () => {
64
+ let result;
65
+ beforeEach(async () => {
66
+ Date.now = jest.fn(() =>
67
+ new Date(Date.UTC(2021, 1, 6, 10, 11, 12)).valueOf()
68
+ );
69
+ aliceHasValidProfile();
70
+ noChatWithBobExists();
71
+ chatWithBobCanBeCreated();
72
+ bobHasAnInbox();
73
+ invitationCanBeSent();
74
+ chatContainerIsFound();
75
+ chatContainerAclCanBeSet();
76
+ editablePrivateTypeIndexIsFound();
77
+ privateTypeIndexIsUpdated();
78
+ result = await chatLogic.getChat(bob, true);
79
+ });
80
+ it("returns the chat URI based on the invitee's WebID", () => {
81
+ expect(result.uri).toBe(
82
+ "https://alice.example.com/IndividualChats/bob.example.com/index.ttl#this"
83
+ );
84
+ });
85
+ it("created a chat document", () => {
86
+ const request = getRequestTo(
87
+ "PUT",
88
+ "https://alice.example.com/IndividualChats/bob.example.com/index.ttl"
89
+ );
90
+ expect(request.body).toBe(`@prefix : <#>.
91
+ @prefix dc: <http://purl.org/dc/elements/1.1/>.
92
+ @prefix meeting: <http://www.w3.org/ns/pim/meeting#>.
93
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
94
+ @prefix c: </profile/card.ttl#>.
95
+
96
+ :this
97
+ a meeting:LongChat;
98
+ dc:author c:me;
99
+ dc:created "2021-02-06T10:11:12Z"^^xsd:dateTime;
100
+ dc:title "Chat channel".
101
+ `);
102
+ });
103
+ it("allowed Bob to participate in the chat by adding an ACL", () => {
104
+ const request = getRequestTo(
105
+ "PUT",
106
+ "https://alice.example.com/IndividualChats/bob.example.com/.acl"
107
+ );
108
+ expect(request.body).toBe(`
109
+ @prefix acl: <http://www.w3.org/ns/auth/acl#>.
110
+ <#owner>
111
+ a acl:Authorization;
112
+ acl:agent <https://alice.example.com/profile/card.ttl#me>;
113
+ acl:accessTo <.>;
114
+ acl:default <.>;
115
+ acl:mode
116
+ acl:Read, acl:Write, acl:Control.
117
+ <#invitee>
118
+ a acl:Authorization;
119
+ acl:agent <https://bob.example.com/profile/card.ttl#me>;
120
+ acl:accessTo <.>;
121
+ acl:default <.>;
122
+ acl:mode
123
+ acl:Read, acl:Append.
124
+ `);
125
+ });
126
+ it("sent an invitation to invitee inbox", () => {
127
+ const request = getRequestTo("POST", "https://bob.example.com/inbox");
128
+ expect(request.body).toContain(`
129
+ <> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
130
+ <http://www.w3.org/1999/02/22-rdf-syntax-ns#seeAlso> <https://alice.example.com/IndividualChats/bob.example.com/index.ttl#this> .
131
+ `);
132
+ });
133
+ it("added the new chat to private type index", () => {
134
+ const request = getRequestTo(
135
+ "PATCH",
136
+ "https://alice.example.com/settings/privateTypeIndex.ttl"
137
+ );
138
+ expect(request.body)
139
+ .toBe(`INSERT DATA { <https://alice.example.com/settings/privateTypeIndex.ttl#id1612606272000> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/solid/terms#TypeRegistration> .
140
+ <https://alice.example.com/settings/privateTypeIndex.ttl#id1612606272000> <http://www.w3.org/ns/solid/terms#forClass> <http://www.w3.org/ns/pim/meeting#LongChat> .
141
+ <https://alice.example.com/settings/privateTypeIndex.ttl#id1612606272000> <http://www.w3.org/ns/solid/terms#instance> <https://alice.example.com/IndividualChats/bob.example.com/index.ttl#this> .
142
+ }
143
+ `);
144
+ });
145
+ it("has no additional fetch requests", () => {
146
+ expect(fetchMock.mock.calls.length).toBe(9);
147
+ });
148
+ });
149
+ });
150
+
151
+ describe("possible errors", () => {
152
+ it("profile does not link to storage", async () => {
153
+ fetchMock.mockOnceIf("https://alice.example.com/profile/card.ttl", "<><><>.", {
154
+ headers: {
155
+ "Content-Type": "text/turtle",
156
+ },
157
+ });
158
+ const expectedError = new Error("User pod root not found!");
159
+ await expect(chatLogic.getChat(bob, false)).rejects.toEqual(expectedError);
160
+ });
161
+
162
+ it("invitee inbox not found", async () => {
163
+ aliceHasValidProfile();
164
+ noChatWithBobExists();
165
+ chatWithBobCanBeCreated();
166
+ bobDoesNotHaveAnInbox();
167
+ const expectedError = new Error(
168
+ "Invitee inbox not found! https://bob.example.com/profile/card.ttl#me"
169
+ );
170
+ await expect(chatLogic.getChat(bob, true)).rejects.toEqual(expectedError);
171
+ });
172
+ });
173
+
174
+ function aliceHasValidProfile() {
175
+ fetchMock.mockOnceIf(
176
+ "https://alice.example.com/profile/card.ttl",
177
+ `
178
+ <https://alice.example.com/profile/card.ttl#me>
179
+ <http://www.w3.org/ns/pim/space#storage> <https://alice.example.com/> ;
180
+ <http://www.w3.org/ns/solid/terms#privateTypeIndex> <https://alice.example.com/settings/privateTypeIndex.ttl> ;
181
+ .`,
182
+ {
183
+ headers: {
184
+ "Content-Type": "text/turtle",
185
+ },
186
+ }
187
+ );
188
+ }
189
+
190
+ function noChatWithBobExists() {
191
+ return fetchMock.mockOnceIf(
192
+ ({ url, method }) =>
193
+ url === "https://alice.example.com/IndividualChats/bob.example.com/index.ttl" &&
194
+ method === "GET",
195
+ "Not found",
196
+ {
197
+ status: 404,
198
+ }
199
+ );
200
+ }
201
+
202
+ function chatWithBobCanBeCreated() {
203
+ return fetchMock.mockOnceIf(
204
+ ({ url, method }) =>
205
+ url === "https://alice.example.com/IndividualChats/bob.example.com/index.ttl" &&
206
+ method === "PUT",
207
+ "Created",
208
+ {
209
+ status: 201,
210
+ }
211
+ );
212
+ }
213
+
214
+ function bobHasAnInbox() {
215
+ fetchMock.mockOnceIf(
216
+ "https://bob.example.com/profile/card.ttl",
217
+ "<https://bob.example.com/profile/card.ttl#me><http://www.w3.org/ns/ldp#inbox><https://bob.example.com/inbox>.",
218
+ {
219
+ headers: { "Content-Type": "text/turtle" },
220
+ }
221
+ );
222
+ }
223
+
224
+ function bobDoesNotHaveAnInbox() {
225
+ fetchMock.mockOnceIf("https://bob.example.com/profile/card.ttl", "<><><>.", {
226
+ headers: {
227
+ "Content-Type": "text/turtle",
228
+ },
229
+ });
230
+ }
231
+
232
+ function invitationCanBeSent() {
233
+ return fetchMock.mockOnceIf(
234
+ ({ url, method }) =>
235
+ url === "https://bob.example.com/inbox" && method === "POST",
236
+ "Created",
237
+ {
238
+ status: 201,
239
+ headers: {
240
+ location:
241
+ "https://bob.example.com/inbox/22373339-6cc0-49fc-b69e-0402edda6e4e.ttl",
242
+ },
243
+ }
244
+ );
245
+ }
246
+
247
+ function chatContainerIsFound() {
248
+ return fetchMock.mockOnceIf(
249
+ ({ url, method }) =>
250
+ url === "https://alice.example.com/IndividualChats/bob.example.com/" &&
251
+ method === "GET",
252
+ "<><><>.",
253
+ {
254
+ status: 200,
255
+ headers: {
256
+ "Content-Type": "text/turtle",
257
+ Link: '<.acl>; rel="acl"',
258
+ },
259
+ }
260
+ );
261
+ }
262
+
263
+ function chatContainerAclCanBeSet() {
264
+ return fetchMock.mockOnceIf(
265
+ ({ url, method }) =>
266
+ url === "https://alice.example.com/IndividualChats/bob.example.com/.acl" &&
267
+ method === "PUT",
268
+ "Created",
269
+ {
270
+ status: 201,
271
+ }
272
+ );
273
+ }
274
+
275
+ function editablePrivateTypeIndexIsFound() {
276
+ return fetchMock.mockOnceIf(
277
+ ({ url, method }) =>
278
+ url === "https://alice.example.com/settings/privateTypeIndex.ttl" &&
279
+ method === "GET",
280
+ "<><><>.",
281
+ {
282
+ status: 200,
283
+ headers: {
284
+ "Content-Type": "text/turtle",
285
+ "wac-allow": 'user="read write append control",public=""',
286
+ "ms-author-via": "SPARQL",
287
+ },
288
+ }
289
+ );
290
+ }
291
+
292
+ function privateTypeIndexIsUpdated() {
293
+ return fetchMock.mockOnceIf(
294
+ ({ url, method }) =>
295
+ url === "https://alice.example.com/settings/privateTypeIndex.ttl" &&
296
+ method === "PATCH",
297
+ "OK",
298
+ {
299
+ status: 200,
300
+ }
301
+ );
302
+ }
303
+
304
+ function getRequestTo(
305
+ method: "GET" | "PUT" | "POST" | "DELETE" | "PATCH",
306
+ url: string
307
+ ): RequestInit {
308
+ const call = fetchMock.mock.calls.find(
309
+ (it) => it[0] === url && method === it[1]?.method
310
+ );
311
+ expect(call).not.toBeNull();
312
+ const request = call?.[1];
313
+ expect(request).not.toBeNull();
314
+ return request!;
315
+ }
316
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ *
4
+ */
5
+ import { UpdateManager, Store, Fetcher, sym } from "rdflib";
6
+ import { createContainerLogic } from "../src/util/containerLogic";
7
+ import { alice } from "./helpers/dataSetup";
8
+
9
+ window.$SolidTestEnvironment = { username: alice.uri }
10
+
11
+ describe("Container", () => {
12
+ let store
13
+ let containerLogic
14
+ beforeEach(() => {
15
+ fetchMock.resetMocks()
16
+ store = new Store()
17
+ store.fetcher = new Fetcher(store, { fetch: fetch });
18
+ store.updater = new UpdateManager(store);
19
+ containerLogic = createContainerLogic(store)
20
+ })
21
+
22
+ it("getContainerMembers - When container has some containment triples", async () => {
23
+ containerHasSomeContainmentTriples()
24
+ const containerMembers = await containerLogic.getContainerMembers(sym('https://container.com/'));
25
+ const result = containerMembers.map(oneResult => oneResult.value)
26
+ expect(result.sort()).toEqual([
27
+ 'https://container.com/foo.txt',
28
+ 'https://container.com/bar/'
29
+ ].sort());
30
+ });
31
+ it.skip("getContainerMembers- When container is empty - Resolves to an empty array", async () => {
32
+ jest.setTimeout(2000)
33
+ containerIsEmpty();
34
+ const result = await containerLogic.getContainerMembers(sym('https://container.com/'));
35
+ expect(result).toEqual([]);
36
+ });
37
+
38
+ function containerIsEmpty() {
39
+ fetchMock.mockOnceIf(
40
+ "https://com/",
41
+ "", // FIXME: https://github.com/jefflau/jest-fetch-mock/issues/189
42
+ {
43
+ headers: { "Content-Type": "text/turtle" },
44
+ }
45
+ );
46
+ }
47
+
48
+ function containerHasSomeContainmentTriples() {
49
+ fetchMock.mockOnceIf(
50
+ "https://container.com/",
51
+ "<.> <http://www.w3.org/ns/ldp#contains> <./foo.txt>, <./bar/> .",
52
+ {
53
+ headers: { "Content-Type": "text/turtle" },
54
+ }
55
+ );
56
+
57
+ }
58
+ })