Skip to content

Commit

Permalink
[NOID] Add QueryType validation in apoc.trigger.add
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Oct 18, 2022
1 parent 3700669 commit 6de102f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/main/java/apoc/trigger/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import java.util.Map;
import java.util.stream.Stream;

import static org.neo4j.graphdb.QueryExecutionType.QueryType.READ_ONLY;
import static org.neo4j.graphdb.QueryExecutionType.QueryType.READ_WRITE;
import static org.neo4j.graphdb.QueryExecutionType.QueryType.WRITE;

/**
* @author mh
* @since 20.09.16
Expand Down Expand Up @@ -52,7 +56,8 @@ public TriggerInfo( String name, String query, Map<String,Object> selector, Map<
@Procedure(mode = Mode.WRITE)
@Description("add a trigger kernelTransaction under a name, in the kernelTransaction you can use {createdNodes}, {deletedNodes} etc., the selector is {phase:'before/after/rollback/afterAsync'} returns previous and new trigger information. Takes in an optional configuration.")
public Stream<TriggerInfo> add(@Name("name") String name, @Name("kernelTransaction") String statement, @Name(value = "selector"/*, defaultValue = "{}"*/) Map<String,Object> selector, @Name(value = "config", defaultValue = "{}") Map<String,Object> config) {
Util.validateQuery(db, statement);
Util.validateQuery(db, statement,
READ_ONLY, WRITE, READ_WRITE);
Map<String,Object> params = (Map)config.getOrDefault("params", Collections.emptyMap());
Map<String, Object> removed = triggerHandler.add(name, statement, selector, params);
if (removed != null) {
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/apoc/trigger/TriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import static apoc.ApocConfig.APOC_TRIGGER_ENABLED;
import static apoc.ApocConfig.apocConfig;
import static apoc.util.MapUtil.map;
import static apoc.util.TestUtil.testCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.neo4j.configuration.GraphDatabaseSettings.procedure_unrestricted;

/**
Expand Down Expand Up @@ -371,5 +373,16 @@ public void testDeleteRelationships() throws Exception {
, (value) -> value, 30L, TimeUnit.SECONDS);
}

@Test
public void testTriggerWithSchemaOperation() {
try {
testCall(db, "CALL apoc.trigger.add('triggerSchema','CREATE INDEX periodicIdx FOR (n:Bar) ON (n.first_name, n.last_name)', {phase: 'before'})",
(row) -> fail("Should fail because of unsupported schema operation"));
} catch (RuntimeException e) {
final String expected = "Failed to invoke procedure `apoc.trigger.add`: " +
"Caused by: java.lang.RuntimeException: Supported query types for the operation are [READ_ONLY, WRITE, READ_WRITE]";
assertEquals(expected, e.getMessage());
}
}

}

0 comments on commit 6de102f

Please sign in to comment.