-
Notifications
You must be signed in to change notification settings - Fork 394
/
templatePermissions.test.ts
115 lines (109 loc) · 3.29 KB
/
templatePermissions.test.ts
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
/* eslint-disable camelcase */
import {first} from 'rxjs/operators'
import {SanityClient} from '@sanity/client'
import {prepareTemplates, defaultTemplatesForSchema} from '../../../templates'
import {createMockSanityClient} from '../../../../test/mocks/mockSanityClient'
import {createSchema} from '../../../schema'
import {requiresApproval} from '../debug/exampleGrants'
import {createGrantsStore} from '../grantsStore'
import {getTemplatePermissions} from '../templatePermissions'
const schema = createSchema({
name: 'test',
types: [
{
name: 'author',
title: 'Author',
type: 'document',
fields: [
{name: 'name', type: 'string'},
{name: 'role', type: 'string'},
{name: 'locked', type: 'boolean'},
],
},
],
})
const templates = prepareTemplates(schema, [
...defaultTemplatesForSchema(schema),
{
id: 'author-developer-locked',
title: 'Developer',
schemaType: 'author',
value: {role: 'developer', locked: true},
},
{
id: 'author-developer-unlocked',
title: 'Developer',
schemaType: 'author',
value: {role: 'developer', locked: false},
},
])
describe('getTemplatePermissions', () => {
it('takes in a list of `InitialValueTemplateItem`s and returns an observable of `TemplatePermissionsResult` in a record', async () => {
const client = createMockSanityClient({requests: {'/acl': requiresApproval}})
const grantsStore = createGrantsStore({
client: client as unknown as SanityClient,
currentUser: null,
})
const permissions = getTemplatePermissions({
grantsStore,
schema,
templates,
templateItems: [
{
id: 'author-developer-locked',
templateId: 'author-developer-locked',
type: 'initialValueTemplateItem',
schemaType: 'author',
},
{
id: 'author-developer-unlocked',
templateId: 'author-developer-unlocked',
type: 'initialValueTemplateItem',
schemaType: 'author',
},
],
})
.pipe(first())
.toPromise()
await expect(permissions).resolves.toEqual([
{
description: undefined,
granted: false,
icon: undefined,
schemaType: 'author',
id: 'author-developer-locked',
reason: 'No matching grants found',
resolvedInitialValue: {locked: true, role: 'developer'},
subtitle: 'Author',
template: {
id: 'author-developer-locked',
schemaType: 'author',
title: 'Developer',
value: {locked: true, role: 'developer'},
},
templateId: 'author-developer-locked',
title: 'Developer',
type: 'initialValueTemplateItem',
},
{
description: undefined,
granted: true,
icon: undefined,
schemaType: 'author',
id: 'author-developer-unlocked',
reason: 'Matching grant',
resolvedInitialValue: {locked: false, role: 'developer'},
subtitle: 'Author',
template: {
id: 'author-developer-unlocked',
schemaType: 'author',
title: 'Developer',
value: {locked: false, role: 'developer'},
},
templateId: 'author-developer-unlocked',
title: 'Developer',
type: 'initialValueTemplateItem',
},
])
})
})