package org.elasticsearch.rest.action.readonlyrest.acl.test;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugin.readonlyrest.acl.ACL;
import org.elasticsearch.plugin.readonlyrest.acl.RequestContext;
import org.elasticsearch.plugin.readonlyrest.acl.blocks.Block;
import org.elasticsearch.plugin.readonlyrest.acl.blocks.BlockExitResult;
import org.elasticsearch.rest.RestRequest;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ScriptACLTest {
  private static Settings s = null;
  private static RequestContext rc = null;

  @BeforeClass
  public static void setUpBeforeClass() throws Throwable {
    s = ACLTest.getSettings("/src/test/script_test_rules.yml");
    rc = ACLTest.mockReq("/path", "1.1.1.1", "", "", 0, RestRequest.Method.PUT, null, new String[]{"index1"}, "action");

  }

  private static ACL setScript(String script) {
    s = Settings.builder().put(s).put("readonlyrest.access_control_rules.0.script", script).build();
    return new ACL(s);
  }

  @Test
  public final void testActionIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "  rc.getAction() == 'action'" +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }

  @Test
  public final void testOAIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "    rc.getRemoteAddress() == '1.1.1.1'" +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }

  @Test
  public final void testIndicesIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "    rc.getIndices().length == 1 && rc.getIndices()[0] == 'index1' " +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }

  @Test
  public final void testMethodIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "    rc.getRequest().method().toString() == 'PUT'" +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }

  @Test
  public final void testPathIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "    rc.getRequest().path() == '/path'" +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }

  @Test
  public final void testContentIsRead() throws Throwable {
    BlockExitResult res = setScript("function onRequest(rc){\n" +
        "  print('hello: ' + rc.toString());\n" +
        "  if(" +
        "    rc.getContent() == 'test'" +
        "  ){ return true;} return false;}").check(rc);
    assertTrue(res.isMatch());
    assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
    assertEquals("1", res.getBlock().getName());
  }



}
@@ -1216,34 +1216,32 @@
 n onRequest(rc)%7B
-%5Cn
 %22 +%0A        %22  p
@@ -1243,75 +1243,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22 
+return
  rc.
@@ -1272,16 +1272,17 @@
 'action'
+;
 %22 +%0A    
@@ -1292,38 +1292,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).
@@ -1568,34 +1568,32 @@
 n onRequest(rc)%7B
-%5Cn
 %22 +%0A        %22  p
@@ -1595,77 +1595,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22   
+return
  rc.
@@ -1651,38 +1651,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).
@@ -1932,34 +1932,32 @@
 n onRequest(rc)%7B
-%5Cn
 %22 +%0A        %22  p
@@ -1959,77 +1959,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22   
+return
  rc.
@@ -2043,38 +2043,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).
@@ -2323,34 +2323,32 @@
 n onRequest(rc)%7B
-%5Cn
 %22 +%0A        %22  p
@@ -2350,76 +2350,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22  
+return
   rc
@@ -2417,38 +2417,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).
@@ -2695,34 +2695,32 @@
 n onRequest(rc)%7B
-%5Cn
 %22 +%0A        %22  p
@@ -2722,77 +2722,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22   
+return
  rc.
@@ -2777,38 +2777,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).
@@ -2780,32 +2780,33 @@
  %7D%22).check(rc);%0A
+%0A
     assertTrue(r
@@ -3067,18 +3067,16 @@
 est(rc)%7B
-%5Cn
 %22 +%0A    
@@ -3086,77 +3086,14 @@
  %22  
-print('hello: ' + rc.toString());%5Cn%22 +%0A        %22  if(%22 +%0A        %22   
+return
  rc.
@@ -3133,38 +3133,8 @@
  %22  
-)%7B return true;%7D return false;
 %7D%22).

make tests smaller