-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcat-service.test.js
More file actions
147 lines (127 loc) · 6.12 KB
/
Copy pathcat-service.test.js
File metadata and controls
147 lines (127 loc) · 6.12 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
const cds = require('@sap/cds')
describe('CatalogService', () => {
const { GET, axios } = cds.test()
describe('called by reader (cap.Reader policy assigned)', () => {
beforeAll(() => {
axios.defaults.auth = { username: 'reader', password: '' }
})
it('/Books should return all Books', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books`
expect(status).toBe(200)
expect(data.value?.length).toBe(5)
})
it('/ListOfBooks should return all Books', async () => {
const { status, data } = await GET`/odata/v4/catalog/ListOfBooks`
expect(status).toBe(200)
expect(data.value?.length).toBe(5)
})
it('/Books/271/getStockedValue() should return 3300', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books/271/getStockedValue()`
expect(status).toBe(200)
expect(data.value).toBe(3300)
})
it('/Books/getTotalStockedValue() should return 15711.35', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books/getTotalStockedValue()`
expect(status).toBe(200)
expect(data.value).toBe(15711.35)
})
})
describe('called by juniorReader (local.JuniorReader policy assigned)', () => {
beforeAll(() => {
axios.defaults.auth = { username: 'juniorReader', password: '' }
})
/**
* The JuniorReader policy restricts to Fairy Tale, Fantasy, and Mystery genres:
* - access to Catweazle is granted as its genre is Fantasy
* - access to The Raven and Eleonora is granted as their genre is Mystery
* - Fairy Tale books would also be included but there are none in the data
*/
it('/Books should return 3 Books (Catweazle, The Raven, Eleonora)', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books`
expect(status).toBe(200)
expect(data.value?.length).toBe(3)
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Catweazle' }))
expect(data.value).toContainEqual(expect.objectContaining({ title: 'The Raven' }))
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Eleonora' }))
})
// Book 201 = Wuthering Heights (Drama genre - not allowed)
it('/Books/201/getStockedValue() should be forbidden', async () => {
expect.hasAssertions();
return (GET`/odata/v4/catalog/Books/201/getStockedValue()`).catch(error => {
expect(error.response.status).toBe(403)
})
})
// Book 271 = Catweazle (Fantasy genre - allowed)
it('/Books/271/getStockedValue() should return 3300', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books/271/getStockedValue()`
expect(status).toBe(200)
expect(data.value).toBe(3300)
})
// 15711.35 = stocked value over ALL books because instance-based filters are NOT supported by CAP for functions bound to more than one entity
it('/Books/getTotalStockedValue() should return 15711.35', async () => {
expect.hasAssertions();
const { status, data } = await GET`/odata/v4/catalog/Books/getTotalStockedValue`
expect(status).toBe(200)
expect(data.value).toBe(15711.35)
})
})
describe('called by technicalUser (calling ReadCatalog API policy)', () => {
beforeAll(() => {
axios.defaults.auth = { username: 'technicalUser', password: '' }
})
/**
* The ReadCatalog API policy restricts to genres NOT IN (Mystery, Romance, Thriller, Dystopia)
* The remaining list includes Drama (Wuthering Heights, Jane Eyre) and Fantasy (Catweazle)
*/
it('/Books should return 3 Books (Catweazle, Wuthering Heights, Jane Eyre)', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books`
expect(status).toBe(200)
expect(data.value?.length).toBe(3)
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Catweazle' }))
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Wuthering Heights' }))
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Jane Eyre' }))
})
// Book 252 = Eleonora (Mystery genre - forbidden by ReadCatalog policy)
it('/Books/252/getStockedValue() should be forbidden', async () => {
expect.hasAssertions();
return (GET`/odata/v4/catalog/Books/252/getStockedValue()`).catch(error => {
expect(error.response.status).toBe(403)
})
})
// Book 271 = Catweazle (Fantasy genre - allowed by ReadCatalog policy)
it('/Books/271/getStockedValue() should return 3300', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books/271/getStockedValue()`
expect(status).toBe(200)
expect(data.value).toBe(3300)
})
})
describe('called by principalPropagation (local.JuniorReader policy limited to ReadCatalog API policy)', () => {
beforeAll(() => {
axios.defaults.auth = { username: 'principalPropagation', password: '' }
})
/**
* The JuniorReader policy restricts to Fairy Tale, Fantasy, and Mystery genres
* The ReadCatalog API policy restricts to genres NOT IN (Mystery, Romance, Thriller, Dystopia)
* The intersection yields only Fantasy genre books (Mystery is excluded by ReadCatalog), which is Catweazle
*/
it('/Books should return 1 Book (Catweazle)', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books`
expect(status).toBe(200)
expect(data.value?.length).toBe(1)
expect(data.value).toContainEqual(expect.objectContaining({ title: 'Catweazle' }))
})
// Book 252 = Eleonora (Mystery genre - allowed by JuniorReader but forbidden by ReadCatalog API policy)
it('/Books/252/getStockedValue() should be forbidden', async () => {
expect.hasAssertions();
return (GET`/odata/v4/catalog/Books/252/getStockedValue()`).catch(error => {
expect(error.response.status).toBe(403)
})
})
// Book 271 = Catweazle (Fantasy genre - allowed by both policies)
it('/Books/271/getStockedValue() should return 3300', async () => {
const { status, data } = await GET`/odata/v4/catalog/Books/271/getStockedValue()`
expect(status).toBe(200)
expect(data.value).toBe(3300)
})
})
})