Repository navigation
Expand file tree
/
Copy pathEngineAccessGuardUnitTests.java
More file actions
638 lines (526 loc) · 23.5 KB
/
Copy pathEngineAccessGuardUnitTests.java
File metadata and controls
638 lines (526 loc) · 23.5 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.engine.impl.pipeline;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import prerna.auth.AccessPermissionEnum;
import prerna.auth.User;
import prerna.auth.access.EngineGrantee;
import prerna.auth.utils.SecurityEngineAccessUtils;
import prerna.engine.api.IEngine;
import prerna.engine.api.IModelEngine;
import prerna.engine.api.IRDBMSEngine;
import prerna.engine.api.IVenvEngine;
import prerna.om.Insight;
import prerna.om.ThreadStore;
import prerna.project.api.IProject;
import prerna.reactor.AbstractReactor;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.execptions.SemossPixelException;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.SandboxProbe;
import prerna.util.Utility;
/**
* The access check the engine proxy runs on every call, for calls the
* platform's own code makes. Each test is one rule of
* docs/platform_services/engine_access.md. The calls a project's custom code
* makes are in prerna.util.SandboxEngineAccessUnitTests.
*/
class EngineAccessGuardUnitTests {
private static final String ENGINE = "engine-1";
private static final String OTHER_ENGINE = "engine-2";
private static final String PROJECT = "project-1";
private final User user = new User();
private MockedStatic<SecurityEngineAccessUtils> access;
@BeforeEach
void aServerWithOneSignedInUserWhoHoldsNothing() {
this.access = Mockito.mockStatic(SecurityEngineAccessUtils.class);
this.access.when(SecurityEngineAccessUtils::isSecurityDatabaseLoaded).thenReturn(true);
this.access.when(() -> SecurityEngineAccessUtils.isListed(anyString(), anyBoolean())).thenReturn(true);
ThreadStore.setUser(this.user);
}
@AfterEach
void clear() {
this.access.close();
ThreadStore.remove();
}
@ParameterizedTest
@CsvSource({ "READ_ONLY,true,false,false", "EDIT,true,true,false", "OWNER,true,true,true" })
void aUserCallsTheMethodsTheirLevelCovers(AccessPermissionEnum held, boolean reads, boolean writes,
boolean deletes) {
userHolds(ENGINE, false, held);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertEquals(reads, allowed(engine::getEngineId));
assertEquals(writes, allowed(() -> engine.insertData("insert into t values (1)")));
assertEquals(deletes, allowed(engine::delete));
}
@Test
void aUserWithNoPermissionIsRefusedEveryMethod() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, engine::getEngineId);
assertEquals("Engine " + ENGINE + " does not exist or user does not have READ_ONLY permission on it",
refusal.getMessage());
assertThrows(SemossPixelException.class, engine::getConnection);
assertThrows(SemossPixelException.class, engine::getSmssProp);
assertThrows(SemossPixelException.class, engine::delete);
}
@Test
void aRefusalNamesTheLevelTheMethodNeeds() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine engine = StubEngines.database(ENGINE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, engine::commit);
assertEquals("Engine " + ENGINE + " does not exist or user does not have EDIT permission on it",
refusal.getMessage());
}
@Test
void thePlatformGetsAConnectionAndOneSettingAtATimeForAUserWhoCanView() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(engine::getConnection);
assertDoesNotThrow(() -> engine.getPreparedStatement("select 1"));
assertDoesNotThrow(engine::getQueryUtil);
assertDoesNotThrow(() -> engine.getCatalogSubType());
assertDoesNotThrow(engine::isBigData);
assertDoesNotThrow(engine::getPipelineFile);
assertDoesNotThrow(engine::close);
}
@ParameterizedTest
@CsvSource({ "READ_ONLY", "EDIT" })
void onlyAnOwnerReadsTheSettingsOrPicksTheFileTheEngineOpensFrom(AccessPermissionEnum held) {
userHolds(ENGINE, false, held);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertThrows(SemossPixelException.class, engine::getSmssProp);
assertThrows(SemossPixelException.class, engine::getOrigSmssProp);
assertThrows(SemossPixelException.class, engine::getSmssFilePath);
assertThrows(SemossPixelException.class, () -> engine.setSmssProp(new Properties()));
assertThrows(SemossPixelException.class, () -> engine.open("other.smss"));
assertThrows(SemossPixelException.class, () -> engine.open(new Properties()));
assertThrows(SemossPixelException.class, () -> engine.getProperty("PASSWORD"));
assertThrows(SemossPixelException.class, engine::getConnectionUrl);
assertThrows(SemossPixelException.class, engine::getDataSource);
}
@Test
void anOwnerReadsTheSettingsAndPicksTheFileTheEngineOpensFrom() {
userHolds(ENGINE, false, AccessPermissionEnum.OWNER);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertNotNull(engine.getSmssProp());
assertDoesNotThrow(engine::getSmssFilePath);
assertDoesNotThrow(() -> engine.open("other.smss"));
assertDoesNotThrow(engine::getConnectionUrl);
}
@Test
void anEditorReloadsTheEngineAndChangesOnlyTheSettingsEditorsChange() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
assertThrows(SemossPixelException.class, engine::reload);
assertThrows(SemossPixelException.class, () -> engine.updateSetting(IEngine.PIPELINE, "pipeline.json"));
userHolds(ENGINE, false, AccessPermissionEnum.EDIT);
assertDoesNotThrow(engine::reload);
assertDoesNotThrow(() -> engine.updateSetting(IEngine.PIPELINE, "pipeline.json"));
}
@Test
void permissionOnOneEngineGivesNothingOnAnother() {
userHolds(ENGINE, false, AccessPermissionEnum.OWNER);
assertThrows(SemossPixelException.class, StubEngines.database(OTHER_ENGINE)::getEngineId);
}
@Test
void anAdminCallsAnyMethodThroughAnAdminClass() {
this.access.when(() -> SecurityEngineAccessUtils.isAdmin(this.user)).thenReturn(true);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(() -> AdminEngineCalls.run(engine::getEngineId));
assertDoesNotThrow(() -> AdminEngineCalls.run(engine::delete));
}
@Test
void anAdminHasOnlyTheirOwnPermissionsOutsideAnAdminClass() {
this.access.when(() -> SecurityEngineAccessUtils.isAdmin(this.user)).thenReturn(true);
IRDBMSEngine engine = StubEngines.database(ENGINE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, engine::getEngineId);
assertEquals("Engine " + ENGINE + " does not exist or user does not have READ_ONLY permission on it",
refusal.getMessage());
assertThrows(SemossPixelException.class, engine::delete);
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
assertDoesNotThrow(engine::getEngineId);
assertThrows(SemossPixelException.class, engine::delete);
}
@Test
void aUserWhoIsNotAnAdminGainsNothingFromAnAdminClass() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertThrows(SemossPixelException.class, () -> AdminEngineCalls.run(engine::getEngineId));
}
@Test
void anAdminClassWithNoSignedInUserIsRefused() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertThrows(SemossPixelException.class, () -> AdminEngineCalls.run(engine::getEngineId));
}
@Test
void aCallWithNoSignedInUserIsRefused() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
SemossPixelException refusal = assertThrows(SemossPixelException.class, engine::getEngineId);
assertEquals("Engine " + ENGINE + " does not exist or user does not have READ_ONLY permission on it",
refusal.getMessage());
assertThrows(SemossPixelException.class, engine::delete);
}
@Test
void aCallWithNoSignedInUserIsRefusedOnAnEngineTheSecurityDatabaseDoesNotList() {
ThreadStore.remove();
this.access.when(() -> SecurityEngineAccessUtils.isListed(ENGINE, false)).thenReturn(false);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertThrows(SemossPixelException.class, engine::getEngineId);
}
@Test
void aPlatformOperationGoesThroughWithNoSignedInUser() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
try (PlatformOperation operation = PlatformOperation.begin("cluster sync")) {
assertEquals("cluster sync", operation.getName());
assertDoesNotThrow(engine::getEngineId);
assertDoesNotThrow(engine::delete);
}
}
@Test
void aPlatformOperationGoesThroughForAUserWithNoPermission() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertThrows(SemossPixelException.class, engine::getEngineId);
try (PlatformOperation operation = PlatformOperation.begin("cluster sync")) {
assertDoesNotThrow(engine::getEngineId);
}
}
@Test
void aPlatformOperationEndsWhenItIsClosed() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
PlatformOperation operation = PlatformOperation.begin("cluster sync");
assertDoesNotThrow(engine::getEngineId);
operation.close();
assertThrows(SemossPixelException.class, engine::getEngineId);
assertDoesNotThrow(operation::close);
}
@Test
void aPlatformOperationBegunInsideAnotherLeavesTheOuterOneRunning() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
try (PlatformOperation outer = PlatformOperation.begin("cluster sync")) {
try (PlatformOperation inner = PlatformOperation.begin("git sync")) {
assertDoesNotThrow(engine::getEngineId);
}
assertDoesNotThrow(engine::getEngineId);
}
assertThrows(SemossPixelException.class, engine::getEngineId);
}
@Test
void anOuterPlatformOperationClosedFirstIsNotPutBack() {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
PlatformOperation outer = PlatformOperation.begin("cluster sync");
PlatformOperation inner = PlatformOperation.begin("git sync");
outer.close();
assertDoesNotThrow(engine::getEngineId);
inner.close();
assertThrows(SemossPixelException.class, engine::getEngineId);
}
@Test
void aPlatformOperationIsNotCarriedToAThreadTheWorkStarts() throws Exception {
AtomicReference<String> onAnotherThread = new AtomicReference<>("not read");
try (PlatformOperation operation = PlatformOperation.begin("cluster sync")) {
assertEquals("cluster sync", PlatformOperation.covering());
Thread worker = new Thread(() -> onAnotherThread.set(PlatformOperation.covering()));
worker.start();
worker.join();
}
assertNull(onAnotherThread.get());
assertNull(PlatformOperation.covering());
}
@Test
void aPlatformOperationClosedOnAnotherThreadKeepsRunning() throws Exception {
ThreadStore.remove();
IRDBMSEngine engine = StubEngines.database(ENGINE);
try (PlatformOperation operation = PlatformOperation.begin("cluster sync")) {
Thread worker = new Thread(operation::close);
worker.start();
worker.join();
assertDoesNotThrow(engine::getEngineId);
}
}
@Test
void aPlatformOperationNeedsAName() {
assertThrows(IllegalArgumentException.class, () -> PlatformOperation.begin(" "));
assertThrows(IllegalArgumentException.class, () -> PlatformOperation.begin(null));
}
@Test
void aUserOffTheThreadIsReadFromTheInsightOfTheCall() {
ThreadStore.remove();
Insight insight = new Insight();
insight.setUser(this.user);
IModelEngine model = StubEngines.model(ENGINE);
Map<String, Object> parameters = new HashMap<>();
assertThrows(SemossPixelException.class, () -> model.ask("question", null, insight, parameters));
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
assertDoesNotThrow(() -> model.ask("question", null, insight, parameters));
}
@Test
void thePlatformReadsADiscoverableEngineForAUserWithNoPermission() {
this.access.when(() -> SecurityEngineAccessUtils.isReadableThroughPlatform(this.user, ENGINE, false))
.thenReturn(true);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(engine::getEngineId);
assertDoesNotThrow(() -> engine.getCatalogSubType());
assertThrows(SemossPixelException.class, engine::getSmssProp);
assertThrows(SemossPixelException.class, () -> engine.insertData("insert into t values (1)"));
assertThrows(SemossPixelException.class, engine::delete);
}
@Test
void thePlatformReadsAProjectForAUserWhoCanOpenOneOfItsInsights() {
this.access.when(() -> SecurityEngineAccessUtils.isReadableThroughPlatform(this.user, PROJECT, true))
.thenReturn(true);
IProject project = StubEngines.project(PROJECT);
assertDoesNotThrow(project::getProjectId);
assertDoesNotThrow(project::getInsightDatabase);
assertThrows(SemossPixelException.class, project::writeNotebooks);
}
@Test
void aProjectIsCheckedAgainstTheUsersPermissionOnTheProject() {
IProject project = StubEngines.project(PROJECT);
SemossPixelException refusal = assertThrows(SemossPixelException.class, project::getProjectId);
assertEquals("Project " + PROJECT + " does not exist or user does not have READ_ONLY permission on it",
refusal.getMessage());
userHolds(PROJECT, true, AccessPermissionEnum.READ_ONLY);
assertDoesNotThrow(project::getProjectId);
}
@Test
void anEngineTheSecurityDatabaseDoesNotListGoesThrough() {
this.access.when(() -> SecurityEngineAccessUtils.isListed(ENGINE, false)).thenReturn(false);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(engine::delete);
}
@Test
void anEngineCallingAnEngineThatGaveItNothingIsRefused() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE, Map.of("getSchema", configured::getEngineName));
SemossPixelException refusal = assertThrows(SemossPixelException.class, engine::getSchema);
assertTrue(refusal.getMessage().contains("engine " + ENGINE), refusal.getMessage());
}
@Test
void anEngineCallsAnEngineThatGaveItAPermissionThatCoversTheMethod() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
engineHolds(OTHER_ENGINE, new EngineGrantee(ENGINE, "DATABASE"), AccessPermissionEnum.READ_ONLY);
AtomicReference<EngineGrantee> seenByTheConfiguredEngine = new AtomicReference<>();
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE, Map.of("getSchema", () -> {
seenByTheConfiguredEngine.set(EngineCallPermission.granteeMakingTheCall());
return null;
}));
IRDBMSEngine reads = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
configured.getSchema();
return null;
}));
IRDBMSEngine deletes = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
configured.delete();
return null;
}));
assertDoesNotThrow(reads::getSchema);
assertEquals(new EngineGrantee(ENGINE, "DATABASE"), seenByTheConfiguredEngine.get());
assertThrows(SemossPixelException.class, deletes::getSchema);
assertThrows(SemossPixelException.class, configured::getSchema);
assertNull(EngineCallPermission.granteeMakingTheCall());
}
@Test
void anEngineThatGaveAnotherEditorStillKeepsItsSettings() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
engineHolds(OTHER_ENGINE, new EngineGrantee(ENGINE, "DATABASE"), AccessPermissionEnum.EDIT);
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine readsOneSetting = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
configured.getCatalogSubType();
return null;
}));
IRDBMSEngine readsTheSettings = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
configured.getSmssProp();
return null;
}));
assertDoesNotThrow(readsOneSetting::getSchema);
assertThrows(SemossPixelException.class, readsTheSettings::getSchema);
}
@Test
void aPermissionGivenToAnAppDoesNotCoverAnEngineWithTheSameId() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
engineHolds(OTHER_ENGINE, EngineGrantee.ofApp(ENGINE), AccessPermissionEnum.EDIT);
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE, Map.of("getSchema", configured::getEngineName));
assertThrows(SemossPixelException.class, engine::getSchema);
}
@Test
void anEngineCallingAnEngineTheUserCanUseGoesThrough() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
userHolds(OTHER_ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE, Map.of("getSchema", configured::getEngineName));
assertEquals("Stub " + OTHER_ENGINE, engine.getSchema());
}
@Test
void anEngineCallingItselfGoesThrough() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine itself = StubEngines.database(ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
itself.delete();
return null;
}));
assertDoesNotThrow(engine::getSchema);
}
@Test
void anEngineReadsAPublicEngineWithNoPermission() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
this.access.when(() -> SecurityEngineAccessUtils.isPublic(OTHER_ENGINE)).thenReturn(true);
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine reads = StubEngines.database(ENGINE, Map.of("getSchema", configured::getEngineName));
IRDBMSEngine deletes = StubEngines.database(ENGINE, Map.of("getSchema", () -> {
configured.delete();
return null;
}));
assertEquals("Stub " + OTHER_ENGINE, reads.getSchema());
assertThrows(SemossPixelException.class, deletes::getSchema);
}
@Test
void anEngineUsesAVenvEngineWithNoPermission() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IVenvEngine venv = StubEngines.venv(OTHER_ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE, Map.of("getSchema", venv::pathToExecutable));
assertDoesNotThrow(engine::getSchema);
assertThrows(SemossPixelException.class, venv::pathToExecutable);
}
@Test
void anEngineOpeningCallsAnotherEngineAsItself() {
IRDBMSEngine configured = StubEngines.database(OTHER_ENGINE);
try (MockedStatic<Utility> utility = Mockito.mockStatic(Utility.class)) {
utility.when(() -> Utility.getDatabase(eq(ENGINE))).thenAnswer(loading -> {
try (EngineCallPermission.Opening opening = EngineCallPermission.opening(ENGINE, "DATABASE")) {
SandboxProbe.heldObject = configured;
SandboxProbe.readHeldEngineSettingsAsAPlatformEngine();
return configured;
} finally {
SandboxProbe.heldObject = null;
}
});
assertThrows(SemossPixelException.class, () -> Utility.getDatabase(ENGINE));
engineHolds(OTHER_ENGINE, new EngineGrantee(ENGINE, "DATABASE"), AccessPermissionEnum.OWNER);
assertDoesNotThrow(() -> Utility.getDatabase(ENGINE));
}
}
@Test
void aReactorAUserRunsInsideAnEngineCallIsCheckedAsTheUser() {
userHolds(ENGINE, false, AccessPermissionEnum.READ_ONLY);
IRDBMSEngine other = StubEngines.database(OTHER_ENGINE);
IRDBMSEngine engine = StubEngines.database(ENGINE,
Map.of("getSchema", () -> new ReadsAnEngine(other).execute().getValue()));
assertThrows(SemossPixelException.class, engine::getSchema);
}
@Test
void thePlatformLoadingAnEngineGoesThrough() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
try (MockedStatic<Utility> utility = Mockito.mockStatic(Utility.class)) {
utility.when(() -> Utility.getDatabase(eq(ENGINE))).thenAnswer(loading -> {
engine.getSmssProp();
return engine;
});
assertDoesNotThrow(() -> Utility.getDatabase(ENGINE));
}
assertThrows(SemossPixelException.class, engine::getSmssProp);
}
@Test
void aProxyHandedOutForThePlatformsOwnUseServesAnyUser() {
IModelEngine platformModel = StubEngines.platformModel(ENGINE);
IModelEngine model = StubEngines.model(ENGINE);
assertDoesNotThrow(platformModel::getModelType);
assertThrows(SemossPixelException.class, model::getModelType);
}
@Test
void methodsOfObjectAreNotChecked() {
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(engine::toString);
assertDoesNotThrow(engine::hashCode);
assertDoesNotThrow(() -> engine.equals(engine));
}
@Test
void aJvmWithoutTheSecurityDatabaseChecksNothing() {
this.access.when(SecurityEngineAccessUtils::isSecurityDatabaseLoaded).thenReturn(false);
IRDBMSEngine engine = StubEngines.database(ENGINE);
assertDoesNotThrow(engine::delete);
this.access.verify(() -> SecurityEngineAccessUtils.getUserPermission(any(), anyString(), anyBoolean()),
Mockito.never());
}
private void userHolds(String id, boolean project, AccessPermissionEnum level) {
this.access.when(() -> SecurityEngineAccessUtils.getUserPermission(this.user, id, project)).thenReturn(level);
}
private void engineHolds(String engineId, EngineGrantee grantee, AccessPermissionEnum level) {
this.access.when(() -> SecurityEngineAccessUtils.givesAGranteePermission(engineId)).thenReturn(true);
this.access.when(() -> SecurityEngineAccessUtils.getGranteePermission(grantee, engineId)).thenReturn(level);
}
private static boolean allowed(Executable call) {
try {
call.execute();
return true;
} catch (SemossPixelException refused) {
return false;
} catch (Throwable other) {
throw new AssertionError(other);
}
}
/**
* A reactor of the platform that reads one engine, as a tool a user runs does.
*/
private static final class ReadsAnEngine extends AbstractReactor {
private final IRDBMSEngine engine;
private ReadsAnEngine(IRDBMSEngine engine) {
this.engine = engine;
}
@Override
public NounMetadata execute() {
return new NounMetadata(this.engine.getEngineName(), PixelDataType.CONST_STRING);
}
}
}