-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestFlows.ts
247 lines (208 loc) · 7.15 KB
/
testFlows.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
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
import { SolidLib } from './SolidLib/Interface/SolidLib';
import { clearStores, setup } from './setup';
import chalk from 'chalk';
const podId = "steve"
// TODO:: add some colours to the successful / failed logging so we can immediately see failed tests
const resourceString = "?webID <https://www.w3.org/2006/vcard/ns#bday> ?bdate ."
const policy = `
<myPolicy> <a> <Policy>;
<subject> <food-store>;
<action> <read>;
<resource> "${resourceString}";
<context> <verification>.`
const purposes = ["verification", "advertisment"]
enum FlowRunnerResult {
Success = "succes",
Fail = "failure"
}
type FlowRunnerOutput = {
result: FlowRunnerResult,
expectedResult: FlowRunnerResult
type: string
error?: string
}
abstract class FlowRunner {
private flowInfo: string;
protected expectedResult: FlowRunnerResult
protected foodSolidLib = new SolidLib("food-store", podId);
protected adminSolidLib = new SolidLib("admin-App", podId);
constructor(info: string, expectedResult: FlowRunnerResult) {
this.flowInfo = info
this.expectedResult = expectedResult
}
protected async login() {
await this.adminSolidLib.login()
await this.foodSolidLib.login()
}
protected async logout() {
await this.adminSolidLib.logout()
await this.foodSolidLib.logout()
}
public async run(): Promise<FlowRunnerOutput> {
let result: FlowRunnerOutput = {
expectedResult: this.expectedResult,
result: FlowRunnerResult.Fail,
type: this.flowInfo
}
try {
await this.runFlow()
result.result = FlowRunnerResult.Success
} catch (e) {
result.error = (e as any).message
}
return result
}
protected abstract runFlow(): Promise<void>
}
class HappyFlow extends FlowRunner {
constructor() {
super('Happy Flow', FlowRunnerResult.Success)
}
protected async runFlow(): Promise<void> {
await this.login()
await this.adminSolidLib.addPolicy(policy)
await this.foodSolidLib.getData(resourceString, purposes)
const agreements: [] = await this.adminSolidLib.getLogEntries()
if (agreements.length === 0) {
throw Error('expected agreement')
}
await this.logout()
}
}
class NotLoggedInAll extends FlowRunner {
constructor() {
super('Not logged in flow (IDP)', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.adminSolidLib.addPolicy(policy)
await this.foodSolidLib.getData(resourceString, purposes)
const agreements: [] = await this.adminSolidLib.getLogEntries()
if (agreements.length === 0) {
throw Error('expected agreement')
}
}
}
class AddPolicyFlow extends FlowRunner {
constructor() {
super('Add Policy Flow', FlowRunnerResult.Success)
}
protected async runFlow(): Promise<void> {
await this.login()
const status = await this.adminSolidLib.addPolicy(policy)
if (!status) {
throw Error("Adding policy did not work")
}
await this.logout()
}
}
class GetDataWithTrust extends FlowRunner {
constructor() {
super('Get Data With Trust Flow', FlowRunnerResult.Success)
}
protected async runFlow(): Promise<void> {
await this.login()
await this.adminSolidLib.addPolicy(policy)
await this.foodSolidLib.getDataWithTrust(resourceString, purposes)
// TODO: do some checking whether data is trusted
// const agreements: [] = await this.adminSolidLib.getLogEntries()
// if (agreements.length === 0) {
// throw Error('expected agreement')
// }
await this.logout()
}
}
class GetDataWithoutPolicy extends FlowRunner {
constructor() {
super('Getting data with policy present flow', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.login()
await this.foodSolidLib.getData(resourceString, purposes)
const agreements: [] = await this.adminSolidLib.getLogEntries()
if (agreements.length === 0) {
throw Error('expected agreement')
}
await this.logout()
}
}
class NotLoggedInFoodStore extends FlowRunner {
constructor() {
super('Getting Data without logged in (food store - IDP)', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.adminSolidLib.login()
await this.adminSolidLib.addPolicy(policy)
await this.foodSolidLib.getData(resourceString, purposes)
const agreements: [] = await this.adminSolidLib.getLogEntries()
if (agreements.length === 0) {
throw Error('expected agreement')
}
await this.logout()
}
}
class NotLoggedInGetAgreements extends FlowRunner {
constructor() {
super('Getting Agreements without logged in (IDP)', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.adminSolidLib.getLogEntries()
}
}
class FoodStoreGetAgreements extends FlowRunner {
constructor() {
super('Getting Agreements logged in as food store(IDP)', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.foodSolidLib.login()
await this.foodSolidLib.getLogEntries()
await this.foodSolidLib.logout()
}
}
class FoodStoreAddPolicy extends FlowRunner {
constructor() {
super('Adding policy logged in as food store(IDP)', FlowRunnerResult.Fail)
}
protected async runFlow(): Promise<void> {
await this.foodSolidLib.login()
const policyAdded = await this.foodSolidLib.addPolicy(policy)
if (policyAdded) {
throw Error("Food store should not be allowed to add a policy.")
}
await this.foodSolidLib.logout()
}
}
async function main() {
const close = await setup(podId)
clearStores()
console.log('')
console.log('######################################')
console.log('Starting multiple flows')
console.log('######################################')
console.log('')
console.log('')
const flows: FlowRunner[] = [
new HappyFlow(),
new NotLoggedInAll(),
new AddPolicyFlow(),
new GetDataWithTrust(),
new GetDataWithoutPolicy(),
new NotLoggedInFoodStore(),
new NotLoggedInGetAgreements(),
new FoodStoreGetAgreements(),
new FoodStoreAddPolicy(),
]
const results: FlowRunnerOutput[] = []
for (const flow of flows) {
results.push(await flow.run())
clearStores()
}
await close();
console.log('');
console.log(`Number of flows implemented: ${results.length}.`);
console.log(`Number of flows working correctly: ${results.filter(output => output.expectedResult === output.result).length}.`);
results.forEach(output => console.log(
`[${output.expectedResult === output.result ? chalk.green.bold('success') : chalk.red.bold('failure')}] ${output.type} ${output.expectedResult === output.result ? '' : `- error: ${output.error}`}`
));
process.exit()
}
main()