Search in sources :

Example 1 with JSApiChangeNumberPermission

use of com.icodici.universa.contract.jsapi.permissions.JSApiChangeNumberPermission in project universa by UniversaBlockchain.

the class ScriptEngineTest method testChangeNumberPermission.

@Test
public void testChangeNumberPermission() throws Exception {
    KeyAddress k0 = TestKeys.publicKey(0).getShortAddress();
    String p0 = TestKeys.publicKey(0).packToBase64String();
    String p1 = TestKeys.publicKey(1).packToBase64String();
    Contract contract = new Contract(TestKeys.privateKey(0));
    String js = "";
    js += "print('testChangeNumberPermission');";
    js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '" + k0.toString() + "');";
    js += "var changeNumberPermission = jsApi.getPermissionBuilder().createChangeNumberPermission(simpleRole, " + "{field_name: 'testval', min_value: 44, max_value: 55, min_step: 1, max_step: 2}" + ");";
    js += "jsApi.getCurrentContract().addPermission(changeNumberPermission);";
    js += "var isPermitted0 = jsApi.getCurrentContract().isPermitted('decrement_permission', jsApi.base64toPublicKey('" + p0 + "'));";
    js += "var isPermitted1 = jsApi.getCurrentContract().isPermitted('decrement_permission', jsApi.base64toPublicKey('" + p1 + "'));";
    js += "print('isPermitted0: ' + isPermitted0);";
    js += "print('isPermitted1: ' + isPermitted1);";
    js += "result = [changeNumberPermission, isPermitted0, isPermitted1];";
    contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
    contract.seal();
    ScriptObjectMirror res = (ScriptObjectMirror) contract.execJS(js.getBytes());
    ChangeNumberPermission changeNumberPermission = (ChangeNumberPermission) ((JSApiChangeNumberPermission) res.get("0")).extractPermission(new JSApiAccessor());
    ChangeNumberPermission sample = new ChangeNumberPermission(new SimpleRole("test"), Binder.of("field_name", "testval", "min_value", 44, "max_value", 55, "min_step", 1, "max_step", 2));
    Field field = ChangeNumberPermission.class.getDeclaredField("fieldName");
    field.setAccessible(true);
    assertEquals(field.get(sample), field.get(changeNumberPermission));
    field = ChangeNumberPermission.class.getDeclaredField("minValue");
    field.setAccessible(true);
    assertEquals(field.get(sample), field.get(changeNumberPermission));
    field = ChangeNumberPermission.class.getDeclaredField("maxValue");
    field.setAccessible(true);
    assertEquals(field.get(sample), field.get(changeNumberPermission));
    field = ChangeNumberPermission.class.getDeclaredField("minStep");
    field.setAccessible(true);
    assertEquals(field.get(sample), field.get(changeNumberPermission));
    field = ChangeNumberPermission.class.getDeclaredField("maxStep");
    field.setAccessible(true);
    assertEquals(field.get(sample), field.get(changeNumberPermission));
    assertTrue((boolean) res.get("1"));
    assertFalse((boolean) res.get("2"));
}
Also used : JSApiChangeNumberPermission(com.icodici.universa.contract.jsapi.permissions.JSApiChangeNumberPermission) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Field(java.lang.reflect.Field) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) KeyAddress(com.icodici.crypto.KeyAddress) Contract(com.icodici.universa.contract.Contract) NSmartContract(com.icodici.universa.contract.services.NSmartContract) SlotContract(com.icodici.universa.contract.services.SlotContract) Test(org.junit.Test)

Aggregations

KeyAddress (com.icodici.crypto.KeyAddress)1 Contract (com.icodici.universa.contract.Contract)1 JSApiChangeNumberPermission (com.icodici.universa.contract.jsapi.permissions.JSApiChangeNumberPermission)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1 NSmartContract (com.icodici.universa.contract.services.NSmartContract)1 SlotContract (com.icodici.universa.contract.services.SlotContract)1 Field (java.lang.reflect.Field)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 Test (org.junit.Test)1