-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUserDocumentsFromES.spec.ts
242 lines (227 loc) · 6.47 KB
/
getUserDocumentsFromES.spec.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
import "mocha";
import * as pg from "pg";
import * as es from "@elastic/elasticsearch";
import * as _ from "lodash";
import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import getUserDocumentsFromES from "../getUserDocumentsFromES";
import getTestDBConfig from "./getTestDBConfig";
import runMigrationSql, { deleteAllTables } from "./runMigrationSql";
chai.use(chaiAsPromised);
const { expect } = chai;
const esClient = new es.Client({ node: "http://localhost:9200" });
const idxName = "document_test";
const testDocuments = [
{
name: "doc11",
ownerName: "none",
classificationLevel: 1
},
{
name: "doc21",
ownerName: "none",
classificationLevel: 6
},
{
name: "doc31",
ownerName: "user3",
classificationLevel: 3
},
{
name: "doc32",
ownerName: "user3",
classificationLevel: 4
},
{
name: "doc41",
ownerName: "user4",
classificationLevel: 4
},
{
name: "doc42",
ownerName: "user4",
classificationLevel: 5
},
{
name: "doc51",
ownerName: "none",
classificationLevel: 10
}
];
describe("Test getUserDocumentsFromES", function(this: Mocha.ISuiteCallbackContext) {
this.timeout(10000);
let pool: pg.Pool = null;
const dbConfig = getTestDBConfig();
async function buildInitDb() {
// --- rebuilt the schema
await runMigrationSql(pool, true);
// --- start re-build test es document index
await esClient.indices.delete({
index: idxName,
ignore_unavailable: true
});
await esClient.indices.refresh({
index: "_all"
});
await esClient.indices.create({
index: idxName,
body: {
mappings: {
_doc: {
properties: {
name: { type: "keyword" },
ownerName: { type: "keyword" },
classificationLevel: { type: "integer" }
}
}
}
}
});
await esClient.bulk({
index: idxName,
type: "_doc",
refresh: "wait_for",
body:
testDocuments
.map(item => {
const op = {
index: { _index: idxName, _id: item.name }
};
return JSON.stringify(op) + "\n" + JSON.stringify(item);
})
.join("\n") + "\n"
});
}
before(async function() {
// --- you have to supply a db name to connect to pg
pool = new pg.Pool({ ...dbConfig });
try {
await pool.query("CREATE database test");
} catch (e) {
// --- if database `test` already there
// --- then mute the error
if (e.code !== "42P04") {
throw e;
}
}
// --- end the current one & create a new one
await pool.end();
pool = new pg.Pool({ ...dbConfig, database: "test" });
try {
await buildInitDb();
} catch (e) {
console.log(e);
throw e;
}
});
after(async function() {
if (pool) {
await deleteAllTables(pool);
pool.end();
pool = null;
}
});
it("Should allow user1 to access all documents", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user1"
);
expect(documents.length).to.equal(7);
const documentNames = documents.map(doc => doc.name);
expect(documentNames).to.have.members([
"doc11",
"doc21",
"doc31",
"doc32",
"doc41",
"doc42",
"doc51"
]);
});
it("Should allow user2 to access all documents", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user2"
);
expect(documents.length).to.equal(7);
const documentNames = documents.map(doc => doc.name);
expect(documentNames).to.have.members([
"doc11",
"doc21",
"doc31",
"doc32",
"doc41",
"doc42",
"doc51"
]);
});
it("Should allow user3 to access doc31 Only", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user3"
);
expect(documents.length).to.equal(1);
const documentNames = documents.map(doc => doc.name);
expect(documentNames).to.have.members(["doc31"]);
});
it("Should not allow user4 to any documents", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user4"
);
expect(documents.length).to.equal(0);
});
it("Should not allow user5 to any documents", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user5"
);
expect(documents.length).to.equal(0);
});
it("Should not allow user6 to any documents except doc51", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user6"
);
expect(documents.length).to.equal(6);
const documentNames = documents.map(doc => doc.name);
expect(documentNames).to.have.members([
"doc11",
"doc21",
"doc31",
"doc32",
"doc41",
"doc42"
]);
});
it("Should not allow user7 to any documents except doc51", async () => {
const documents = await getUserDocumentsFromES(
pool,
esClient,
idxName,
"user7"
);
expect(documents.length).to.equal(6);
const documentNames = documents.map(doc => doc.name);
expect(documentNames).to.have.members([
"doc11",
"doc21",
"doc31",
"doc32",
"doc41",
"doc42"
]);
});
});