-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathobject.spec.js
More file actions
336 lines (335 loc) · 12.5 KB
/
object.spec.js
File metadata and controls
336 lines (335 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* global describe, beforeAll, beforeEach, it, expect */
const express = require('express')
const request = require('supertest')
const nock = require('nock')
describe('resources', function () {
let testUser
let app
let apex
let client
beforeAll(async function () {
const init = await global.initApex()
testUser = init.testUser
app = init.app
apex = init.apex
client = init.client
const router = express.Router()
router.get('/u/:actor', apex.net.actor.get)
router.get('/o/:id', apex.net.object.get)
router.get('/s/:id', apex.net.activityStream.get)
router.post('/proxy', apex.net.proxy.post)
app.use(router)
app.use('/authorized', (req, res, next) => {
req.user = { username: 'test' }
next()
}, router)
})
beforeEach(function () {
return global.resetDb(apex, client, testUser)
})
describe('get actor', function () {
it('returns actor object', function (done) {
request(app)
.get('/u/test')
.set('Accept', 'application/activity+json')
.expect(200)
.end(function (err, res) {
if (err) done.fail(err)
const standard = {
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: 'https://localhost/u/test',
type: 'Person',
name: 'test',
preferredUsername: 'test',
summary: 'test user',
inbox: 'https://localhost/inbox/test',
outbox: 'https://localhost/outbox/test',
followers: 'https://localhost/followers/test',
following: 'https://localhost/following/test',
liked: 'https://localhost/liked/test',
publicKey: {
id: 'https://localhost/u/test#main-key',
owner: 'https://localhost/u/test',
publicKeyPem: testUser.publicKey[0].publicKeyPem[0]
},
endpoints: {
id: 'https://localhost/u/test#endpoints',
uploadMedia: 'https://localhost/upload',
oauthAuthorizationEndpoint: 'https://localhost/auth/authorize',
proxyUrl: 'https://localhost/proxy'
}
}
expect(res.body).toEqual(standard)
done()
})
})
})
describe('get object', function () {
it('returns public object', async function () {
const oid = apex.utils.objectIdToIRI()
let obj = {
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
obj = await apex.fromJSONLD(obj)
await apex.store.saveObject(obj)
const res = await request(app)
.get(oid.replace('https://localhost', ''))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
const standard = {
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
expect(res.get('content-type')?.includes('application/ld+json')).toBeTrue()
expect(res.body).toEqual(standard)
})
it('denies non-public object without authorization', async function () {
const oid = apex.utils.objectIdToIRI()
let obj = {
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored'
}
obj = await apex.fromJSONLD(obj)
await apex.store.saveObject(obj)
const res = await request(app)
.get(oid.replace('https://localhost', ''))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(403)
expect(res.body).toEqual({})
})
it('returns non-public object with authorization', async function () {
const oid = apex.utils.objectIdToIRI()
let obj = {
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored'
}
obj = await apex.fromJSONLD(obj)
await apex.store.saveObject(obj)
const res = await request(app)
.get(oid.replace('https://localhost', '/authorized'))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
const standard = {
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored'
}
expect(res.get('content-type')?.includes('application/ld+json')).toBeTrue()
expect(res.body).toEqual(standard)
})
it('handles fully qualified activitypub mime', async function () {
const oid = apex.utils.objectIdToIRI()
let obj = {
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
obj = await apex.fromJSONLD(obj)
await apex.store.saveObject(obj)
const res = await request(app)
.get(oid.replace('https://localhost', ''))
.set('Accept', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"')
.expect(200)
const standard = {
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
// don't match the whole string because express injects other params like charset
expect(res.get('content-type').includes('application/ld+json')).toBeTrue()
expect(res.get('content-type').includes('profile="https://www.w3.org/ns/activitystreams"')).toBeTrue()
expect(res.body).toEqual(standard)
})
it('handles fully shorthand activitypub mime', async function () {
const oid = apex.utils.objectIdToIRI()
let obj = {
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
obj = await apex.fromJSONLD(obj)
await apex.store.saveObject(obj)
const res = await request(app)
.get(oid.replace('https://localhost', ''))
.set('Accept', 'application/activity+json')
.expect(200)
const standard = {
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: oid,
type: 'Note',
content: 'Hello.',
attributedTo: 'https://localhost/u/test',
to: ['https://ignore.com/u/ignored', apex.consts.publicAddress]
}
// don't match the whole string because express injects other params like charset
expect(res.get('content-type').includes('application/activity+json')).toBeTrue()
expect(res.body).toEqual(standard)
})
})
describe('get activity', function () {
it('returns public activity', async function () {
const aid = apex.utils.activityIdToIRI()
const activityInput = {
id: aid,
type: 'Create',
to: 'https://ignore.com/u/ignored',
cc: 'https://www.w3.org/ns/activitystreams#Public',
actor: 'https://localhost/u/test',
object: {
id: apex.utils.objectIdToIRI(),
type: 'Note',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored',
cc: 'https://www.w3.org/ns/activitystreams#Public',
content: 'Say, did you finish reading that book I lent you?'
}
}
const activity = await apex.fromJSONLD(activityInput)
activity._meta = { collection: [] }
await apex.store.saveActivity(activity)
const res = await request(app)
.get(aid.replace('https://localhost', ''))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
activityInput['@context'] = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1']
expect(res.get('content-type')?.includes('application/ld+json')).toBeTrue()
// converted format during jsonld processing
activityInput.cc = apex.consts.publicAddress
activityInput.object.cc = apex.consts.publicAddress
expect(res.body).toEqual(activityInput)
})
it('denies non-public activity without authorization', async function () {
const aid = apex.utils.activityIdToIRI()
const activityInput = {
id: aid,
type: 'Create',
to: 'https://ignore.com/u/ignored',
actor: 'https://localhost/u/test',
object: {
id: apex.utils.objectIdToIRI(),
type: 'Note',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored',
content: 'Say, did you finish reading that book I lent you?'
}
}
const activity = await apex.fromJSONLD(activityInput)
activity._meta = { collection: [] }
await apex.store.saveActivity(activity)
const res = await request(app)
.get(aid.replace('https://localhost', ''))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(403)
expect(res.body).toEqual({})
})
it('returns non-public activity with authorization', async function () {
const aid = apex.utils.activityIdToIRI()
const activityInput = {
id: aid,
type: 'Create',
to: 'https://ignore.com/u/ignored',
actor: 'https://localhost/u/test',
object: {
id: apex.utils.objectIdToIRI(),
type: 'Note',
attributedTo: 'https://localhost/u/test',
to: 'https://ignore.com/u/ignored',
content: 'Say, did you finish reading that book I lent you?'
}
}
const activity = await apex.fromJSONLD(activityInput)
activity._meta = { collection: [] }
await apex.store.saveActivity(activity)
const res = await request(app)
.get(aid.replace('https://localhost', '/authorized'))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
activityInput['@context'] = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1']
expect(res.get('content-type')?.includes('application/ld+json')).toBeTrue()
expect(res.body).toEqual(activityInput)
})
it('returns activity with embedded collections', async function () {
const activity = await apex.buildActivity('Create', testUser.id, ['https://ignore.com/u/ignored', apex.consts.publicAddress], {
object: {
id: apex.utils.objectIdToIRI(),
type: 'Note',
attributedTo: 'https://localhost/u/test',
content: 'Say, did you finish reading that book I lent you?'
}
})
await apex.store.saveActivity(activity)
const res = await request(app)
.get(activity.id.replace('https://localhost', ''))
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
expect(res.body.shares).toEqual({
id: `${activity.id}/shares`,
type: 'OrderedCollection',
totalItems: 0,
first: `${activity.id}/shares?page=true`
})
expect(res.body.likes).toEqual({
id: `${activity.id}/likes`,
type: 'OrderedCollection',
totalItems: 0,
first: `${activity.id}/likes?page=true`
})
})
})
describe('proxy remote objects', function () {
it('fetches the remote resouce', function (done) {
nock('https://mocked.com')
.get('/abc123')
.reply(200, { id: 'https://mocked.com/abc123', type: 'Note', summary: 'I am a remote resource' })
request(app)
.post('/proxy')
.type('form')
.send({ id: 'https://mocked.com/abc123' })
.set('Accept', apex.consts.jsonldTypes[0])
.expect(200)
.end(function (err, res) {
if (err) done.fail(err)
expect(res.body).toEqual({
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
id: 'https://mocked.com/abc123',
type: 'Note',
summary: 'I am a remote resource'
})
done()
})
})
it('handles invalid request', function () {
return request(app)
.post('/proxy')
.type('form')
// property should be id
.send({ foo: 'https://mocked.com/abc123' })
.set('Accept', apex.consts.jsonldTypes[0])
.expect(400)
})
})
})