-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (140 loc) · 3.25 KB
/
index.js
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
const fs = require('fs');
var clc = require("cli-color");
const api = require('firebase-tools/lib/api');
const requireAuth = require('firebase-tools/lib/requireAuth');
function log(logger, msg, shouldLog) {
if (shouldLog) logger(msg);
}
function buildRequest(suite) {
return {
auth: true,
origin: api.rulesOrigin,
data: {
source: {
files: {
name: suite.ruleName,
content: suite.ruleContent,
}
},
testSuite: {
testCases: suite.testCases.map(testCase => {
return {
request: testCase.request,
expectation: testCase.expectation
}
})
}
}
}
}
function logDebugMessages(result, logging) {
if (result.debugMessages) {
result.debugMessages.forEach(msg => {
log(
console.error,
clc.redBright(`\t\t${msg}`),
logging
)
});
}
}
function assertResults(suite, results, options) {
let exitError = false;
const description = (index) => suite.testCases[index].description || '';
log(
console.log,
suite.description,
options.logging && suite.description
);
results.map((result, index) => {
if (result.state === "SUCCESS") {
log(
console.log,
`\t${index}. ${clc.green("[SUCCESS]")} ${description(index)}`,
options.logging
);
} else {
log(
console.error,
clc.red(`\t${index}. [FAILURE] ${description(index)}`),
options.logging
)
exitError = true;
}
logDebugMessages(result, options.logging);
});
if (exitError) {
throw new Error('Some tests failed');
}
}
function assertIssues(res) {
if (res.body && res.body.issues) {
res.body.issues.forEach(issue => {
log(
console.error,
clc.redBright(`${issue.sourcePosition.fileName}:${issue.sourcePosition.line}:${issue.sourcePosition.column}:\n\t${issue.severity} ${issue.description}`),
true
)
});
return Promise.reject(new Error('Compile error'));
}
return res;
}
function validateRuleSuite(suite, options = {}) {
if (typeof options.exitOnFailure === 'undefined') {
options.exitOnFailure = true;
}
return requireAuth({}).then(() =>
api.request(
"POST",
`/v1/projects/${suite.project}:test`,
buildRequest(suite)
)).then(assertIssues)
.then(res => {
return res.body.testResults;
}).then(results => {
try {
assertResults(suite, results, options);
} catch (err) {
return Promise.reject(err);
}
}).catch(err => {
log(console.error, clc.red(err.message), options.logging);
if (options.exitOnFailure) {
process.exit(1);
}
return Promise.reject(err);
});
}
function TestCase(description, options) {
this.request = {
...options,
auth: options.auth || null,
time: options.time || new Date().toISOString(),
}
this.description = description;
this.shouldFail = () => {
this.expectation = "DENY";
}
this.shouldSucceed = () => {
this.expectation = "ALLOW";
}
return this;
}
function RuleTestSuite({ project, rulePath, description }) {
this.testCases = [];
this.ruleName = rulePath;
this.ruleContent = fs.readFileSync(rulePath).toString();
this.project = project;
this.description = description;
this.test = function (description, options) {
var testCase = new TestCase(description, options);
this.testCases.push(testCase);
return testCase;
}
return this;
}
module.exports = {
validateRuleSuite,
RuleTestSuite
};