You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
7.4 KiB
174 lines
7.4 KiB
package de.superx.bianalysis; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
import org.junit.After; |
|
import org.junit.Before; |
|
import org.junit.Test; |
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
|
import de.superx.BaseDbTest; |
|
import de.superx.bianalysis.metadata.MetadataImporter; |
|
import de.superx.bianalysis.models.Right; |
|
import de.superx.bianalysis.models.RightParam; |
|
import de.superx.bianalysis.service.UserRestrictionService; |
|
|
|
public class UserRestrictionBuilderTest extends BaseDbTest { |
|
|
|
@Before |
|
public void beforeRun() { |
|
afterRun(); // cleanup |
|
// A |
|
// |-- B |
|
// | |-- C |
|
// | `-- D |
|
// |-- E |
|
// | `-- F |
|
// `-- G |
|
// `-- H |
|
String dummyData = """ |
|
INSERT INTO core.core_orgunit |
|
(id, lid, orgunit_name, uniquename, parent_id, parent_lid) |
|
VALUES |
|
(900001, 910001, 'A', 'dummy_a', NULL, NULL), |
|
(900002, 910002, 'B', 'dummy_b', 900001, 910001), |
|
(900003, 910003, 'C', 'dummy_c', 900002, 910002), |
|
(900004, 910004, 'D', 'dummy_d', 900002, 910002), |
|
(900005, 910005, 'E', 'dummy_e', 900001, 910001), |
|
(900006, 910006, 'F', 'dummy_f', 900005, 910005), |
|
(900007, 910007, 'G', 'dummy_g', 900001, 910001), |
|
(900008, 910008, 'H', 'dummy_h', 900007, 910007); |
|
"""; |
|
|
|
new JdbcTemplate(dataSource).execute(dummyData); |
|
|
|
String dummyFacttable = """ |
|
{ |
|
"namespace" : "dummmy_rights", |
|
"facts" : [ |
|
{ |
|
"id" : "dummmy_rights:2", |
|
"caption" : "Publikationsstern", |
|
"sachgebiettid" : 123, |
|
"facttable" : "fact_publication_dummy", |
|
"dimensions" : [ |
|
{ |
|
"id" : "dummmy_rights:3", |
|
"caption" : "Dozent", |
|
"dimension" : "dim_person_dummy", |
|
"fact_column" : "person_id", |
|
"attributes" : [ |
|
{ |
|
"id" : "dummmy_rights:4", |
|
"dim_column" : "year_of_birth" |
|
} |
|
] |
|
} |
|
], |
|
"measures" : [] |
|
}, |
|
{ |
|
"id" : "dummmy_rights:5", |
|
"caption" : "Bewerberstern", |
|
"facttable" : "fact_applicant_dummy", |
|
"sachgebiettid" : 321, |
|
"dimensions" : [ |
|
{ |
|
"id" : "dummmy_rights:6", |
|
"caption" : "Bewerber", |
|
"dimension" : "dim_person_dummy", |
|
"fact_column" : "person_id", |
|
"attributes" : [ |
|
{ |
|
"id" : "dummmy_rights:7", |
|
"dim_column" : "year_of_birth" |
|
} |
|
] |
|
} |
|
], |
|
"measures" : [] |
|
}, |
|
{ |
|
"id" : "dummmy_rights:8", |
|
"caption" : "Dummy Faktentabelle", |
|
"facttable" : "fact_applicant_more_dummy", |
|
"sachgebiettid" : 333, |
|
"dimensions" : [ |
|
{ |
|
"id" : "dummmy_rights:9", |
|
"caption" : "Dimension dummy", |
|
"dimension" : "dim_dummy", |
|
"fact_column" : "dummy_id", |
|
"attributes" : [ |
|
{ |
|
"id" : "dummmy_rights:10", |
|
"dim_column" : "year_of_birth" |
|
} |
|
] |
|
} |
|
], |
|
"measures" : [] |
|
} |
|
] |
|
} |
|
"""; |
|
|
|
JdbcTemplate jt = new JdbcTemplate(dataSource); |
|
MetadataImporter importer = new MetadataImporter(); |
|
importer.deserializeMetadataFromStrings(dummyFacttable); |
|
String upserts = String.join("\n", importer.getAllUpsertStrings(false)); |
|
jt.execute(upserts); |
|
} |
|
|
|
@After |
|
public void afterRun() { |
|
JdbcTemplate jt = new JdbcTemplate(dataSource); |
|
jt.execute("DELETE FROM core.core_orgunit WHERE uniquename LIKE 'dummy%';"); |
|
String sqlCleanup = """ |
|
DELETE FROM metadata.facttable WHERE namespace = 'dummmy_rights'; |
|
DELETE FROM metadata.dimension WHERE namespace = 'dummmy_rights' OR namespace = 'conf_dummy'; |
|
DELETE FROM metadata.dimension_attribute WHERE namespace = 'dummmy_rights' OR namespace = 'conf_dummy'; |
|
DELETE FROM metadata.dimension_attribute WHERE id like 'custom%'; |
|
DROP TABLE IF EXISTS presentation.dim_person_dummy; |
|
"""; |
|
jt.execute(sqlCleanup); |
|
} |
|
|
|
@Test |
|
public void testOrgunitRestriction() { |
|
user.setHisInOneOrgUnitLidOfRole(Integer.valueOf(910002)); |
|
|
|
//String expectedSql = "WHERE orgunit_lid IN (-1,910004,910003,910002)"; |
|
String expectedSql = "-1,910004,910003,910002"; |
|
|
|
UserRestrictionService builder = new UserRestrictionService(user, dataSource); |
|
//String sql = builder.buildOrgunitRestriction(Integer.valueOf(910002), "orgunit_lid"); |
|
String sql = builder.buildOrgunitGrantedIds(Integer.valueOf(910002)); |
|
assertEquals(expectedSql, sql); |
|
|
|
setRightParamValue(Right.CREATE_ANALYSIS, RightParam.ORGUNIT, "910005"); |
|
|
|
//expectedSql = "WHERE orgunit_lid IN (-1,910005,910004,910006,910003,910002)"; |
|
expectedSql = "-1,910005,910004,910006,910003,910002"; |
|
|
|
sql = builder.buildOrgunitGrantedIds(Integer.valueOf(910002)); |
|
assertEquals(expectedSql, sql); |
|
|
|
// TODO own test |
|
//builder.insertRightsViewRestriction(dataSource, "presentation", "dim_orgunit", null, sql, 910002); |
|
} |
|
|
|
@Test |
|
public void testMetadataRestrictionForTopicAndTopicArea() { |
|
user.setHisInOneOrgUnitLidOfRole(Integer.valueOf(2)); |
|
setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "123"); |
|
setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC, "dummmy_rights:8"); |
|
|
|
String expectedSql = "'dummmy_rights:4','dummmy_rights:10'"; |
|
UserRestrictionService builder = new UserRestrictionService(user, dataSource); |
|
String sql = builder.buildMetadataTableRestriction("dimension_attribute"); |
|
assertEquals(expectedSql, sql); |
|
} |
|
|
|
|
|
|
|
}
|
|
|