@frozencrow/create-api 0.6.1 → 0.6.2
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.
package/lib/generate-service.js
CHANGED
|
@@ -74,8 +74,11 @@ export const ${n.camel}DataResolver = resolve<${n.Pascal}, HookContext>({
|
|
|
74
74
|
updatedAt: async () => Date.now()
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
// Patchable fields: the client's own fields, plus updatedAt (set by the patch
|
|
78
|
+
// resolver, which runs before validation). organizationId and createdAt are
|
|
79
|
+
// deliberately left out: they are set once on create and must never change.
|
|
77
80
|
export const ${n.camel}PatchSchema = Type.Partial(
|
|
78
|
-
Type.Pick(${n.camel}Schema, ['name', '
|
|
81
|
+
Type.Pick(${n.camel}Schema, ['name', 'updatedAt']),
|
|
79
82
|
{ $id: '${n.Pascal}Patch' }
|
|
80
83
|
)
|
|
81
84
|
export type ${n.Pascal}Patch = Static<typeof ${n.camel}PatchSchema>
|
package/package.json
CHANGED
package/template/package.json
CHANGED
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"test": "npm run mocha"
|
|
17
17
|
},
|
|
18
18
|
"overrides": {
|
|
19
|
-
"config": "3.3.
|
|
19
|
+
"config": "3.3.12"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@frozencrow/api-core": "^0.6.
|
|
22
|
+
"@frozencrow/api-core": "^0.6.2",
|
|
23
23
|
"@feathersjs/configuration": "^5.0.37",
|
|
24
24
|
"@feathersjs/feathers": "^5.0.37",
|
|
25
25
|
"@feathersjs/mongodb": "^5.0.37",
|
|
@@ -46,8 +46,11 @@ export const widgetDataResolver = resolve<Widget, HookContext>({
|
|
|
46
46
|
updatedAt: async () => Date.now()
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
+
// Patchable fields: the client's own fields, plus updatedAt (set by the patch
|
|
50
|
+
// resolver, which runs before validation). organizationId and createdAt are
|
|
51
|
+
// deliberately left out: they are set once on create and must never change.
|
|
49
52
|
export const widgetPatchSchema = Type.Partial(
|
|
50
|
-
Type.Pick(widgetSchema, ['name', '
|
|
53
|
+
Type.Pick(widgetSchema, ['name', 'updatedAt']),
|
|
51
54
|
{ $id: 'WidgetPatch' }
|
|
52
55
|
)
|
|
53
56
|
export type WidgetPatch = Static<typeof widgetPatchSchema>
|
|
@@ -57,6 +57,61 @@ describe('widgets service', () => {
|
|
|
57
57
|
assert.ok(widget.organizationId, 'widget is scoped to an organization')
|
|
58
58
|
})
|
|
59
59
|
|
|
60
|
+
it('keeps a widget in its organization when a patch names another one', async () => {
|
|
61
|
+
const authHeaders = (token: string) => ({ 'Content-Type': 'application/json', Authorization: `Bearer ${token}` })
|
|
62
|
+
|
|
63
|
+
// A second user, with their own organization.
|
|
64
|
+
const creds = { email: `w2-${Date.now()}@example.com`, password: 'supersecret' }
|
|
65
|
+
await fetch(`${appUrl}/users`, {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
headers: { 'Content-Type': 'application/json' },
|
|
68
|
+
body: JSON.stringify(creds)
|
|
69
|
+
})
|
|
70
|
+
const other = await asJson(
|
|
71
|
+
await fetch(`${appUrl}/authentication`, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: { 'Content-Type': 'application/json' },
|
|
74
|
+
body: JSON.stringify({ strategy: 'local', ...creds })
|
|
75
|
+
})
|
|
76
|
+
)
|
|
77
|
+
const otherOrg = String(other.user.activeOrganization)
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const widget = await asJson(
|
|
81
|
+
await fetch(`${appUrl}/widgets`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: authHeaders(accessToken),
|
|
84
|
+
body: JSON.stringify({ name: 'Mine' })
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
const ownOrg = String(widget.organizationId)
|
|
88
|
+
assert.notStrictEqual(ownOrg, otherOrg)
|
|
89
|
+
|
|
90
|
+
for (const data of [{ organizationId: otherOrg }, { createdAt: 1 }]) {
|
|
91
|
+
const res = await fetch(`${appUrl}/widgets/${widget._id}`, {
|
|
92
|
+
method: 'PATCH',
|
|
93
|
+
headers: authHeaders(accessToken),
|
|
94
|
+
body: JSON.stringify(data)
|
|
95
|
+
})
|
|
96
|
+
assert.ok(res.status === 400 || res.status === 403, `${JSON.stringify(data)}: expected 400/403, got ${res.status}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const stored: any = await app.service('widgets').get(widget._id)
|
|
100
|
+
assert.strictEqual(String(stored.organizationId), ownOrg, 'widget stayed in its organization')
|
|
101
|
+
assert.strictEqual(stored.createdAt, widget.createdAt, 'createdAt unchanged')
|
|
102
|
+
|
|
103
|
+
const otherList = await asJson(await fetch(`${appUrl}/widgets`, { headers: authHeaders(other.accessToken) }))
|
|
104
|
+
const otherIds = (otherList.data || otherList).map((w: any) => String(w._id))
|
|
105
|
+
assert.ok(!otherIds.includes(String(widget._id)), 'not visible to the other organization')
|
|
106
|
+
} finally {
|
|
107
|
+
try {
|
|
108
|
+
await app.service('users').remove(other.user._id)
|
|
109
|
+
} catch {
|
|
110
|
+
/* ignore cleanup errors */
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
60
115
|
it('rejects unauthenticated widget access', async () => {
|
|
61
116
|
const res = await fetch(`${appUrl}/widgets`)
|
|
62
117
|
assert.ok(res.status === 401 || res.status === 403, `expected auth error, got ${res.status}`)
|