Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: materialized view privileges #2209

Merged
merged 10 commits into from Nov 12, 2021
27 changes: 23 additions & 4 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Expand Up @@ -512,16 +512,28 @@ public static void createUnloggedTable(Connection con, String table, String colu
*/
public static void createView(Connection con, String viewName, String query)
throws SQLException {
Statement st = con.createStatement();
try {
try ( Statement st = con.createStatement() ) {
// Drop the view
dropView(con, viewName);

String sql = "CREATE VIEW " + viewName + " AS " + query;

st.executeUpdate(sql);
} finally {
closeQuietly(st);
}
}

/*
* Helper - creates a materialized view
*/
public static void createMaterializedView(Connection con, String matViewName, String query)
throws SQLException {
try ( Statement st = con.createStatement() ) {
// Drop the view
dropMaterializedView(con, matViewName);

String sql = "CREATE MATERIALIZED VIEW " + matViewName + " AS " + query;

st.executeUpdate(sql);
}
}

Expand Down Expand Up @@ -628,6 +640,13 @@ public static void dropView(Connection con, String view) throws SQLException {
dropObject(con, "VIEW", view);
}

/*
* Helper - drops a materialized view
*/
public static void dropMaterializedView(Connection con, String matView) throws SQLException {
dropObject(con, "MATERIALIZED VIEW", matView);
}

/*
* Helper - drops a type
*/
Expand Down
Expand Up @@ -82,6 +82,7 @@ public void setUp() throws Exception {
TestUtil.createTable(con, "arraytable", "a numeric(5,2)[], b varchar(100)[]");
TestUtil.createTable(con, "intarraytable", "a int4[], b int4[][]");
TestUtil.createView(con, "viewtest", "SELECT id, quest FROM metadatatest");
TestUtil.createMaterializedView(con, "matviewtest", "SELECT id, quest FROM metadatatest");
TestUtil.dropType(con, "custom");
TestUtil.dropType(con, "_custom");
TestUtil.createCompositeType(con, "custom", "i int", false);
Expand Down Expand Up @@ -127,6 +128,7 @@ public void tearDown() throws Exception {
stmt.execute("DROP FUNCTION f4(int)");

TestUtil.dropView(con, "viewtest");
TestUtil.dropMaterializedView(con, "matviewtest");
TestUtil.dropTable(con, "metadatatest");
TestUtil.dropTable(con, "sercoltest");
TestUtil.dropSequence(con, "sercoltest_b_seq");
Expand Down Expand Up @@ -612,11 +614,16 @@ public void testColumnPrivileges() throws SQLException {
rs.close();
}

@Test
public void testTablePrivileges() throws SQLException {
/*
* Helper function - this logic is used several times to test relation privileges
*/
public void relationPrivilegesHelper(String relationName) throws SQLException {
davecramer marked this conversation as resolved.
Show resolved Hide resolved
// Query PG catalog for privileges
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getTablePrivileges(null, null, "metadatatest");
ResultSet rs = dbmd.getTablePrivileges(null, null, relationName);

// Parse result to check if table/view owner has select privileges
boolean foundSelect = false;
while (rs.next()) {
if (rs.getString("GRANTEE").equals(TestUtil.getUser())
Expand All @@ -625,9 +632,26 @@ public void testTablePrivileges() throws SQLException {
}
}
rs.close();
// Test that the table owner has select priv
assertTrue("Couldn't find SELECT priv on table metadatatest for " + TestUtil.getUser(),
foundSelect);

// Check test condition
assertTrue("Couldn't find SELECT priv on relation "
+ relationName + " for " + TestUtil.getUser(),
foundSelect);
}

@Test
public void testTablePrivileges() throws SQLException {
relationPrivilegesHelper("metadatatest");
}

@Test
public void testViewPrivileges() throws SQLException {
relationPrivilegesHelper("viewtest");
}

@Test
public void testMaterializedViewPrivileges() throws SQLException {
relationPrivilegesHelper("matviewtest");
}

@Test
Expand All @@ -640,24 +664,6 @@ public void testNoTablePrivileges() throws SQLException {
assertTrue(!rs.next());
}

@Test
public void testViewPrivileges() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getTablePrivileges(null, null, "viewtest");
boolean foundSelect = false;
while (rs.next()) {
if (rs.getString("GRANTEE").equals(TestUtil.getUser())
&& rs.getString("PRIVILEGE").equals("SELECT")) {
foundSelect = true;
}
}
rs.close();
// Test that the view owner has select priv
assertTrue("Couldn't find SELECT priv on table metadatatest for " + TestUtil.getUser(),
foundSelect);
}

@Test
public void testPrimaryKeys() throws SQLException {
// At the moment just test that no exceptions are thrown KJ
Expand Down