use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testListTargets.
@Test
public void testListTargets() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id id1 = authManager.createTarget(makeTarget("target1", "url1"));
Id id2 = authManager.createTarget(makeTarget("target2", "url2"));
List<HugeTarget> targets = authManager.listTargets(ImmutableList.of(id1, id2));
Assert.assertEquals(2, targets.size());
Assert.assertEquals("target1", targets.get(0).name());
Assert.assertEquals("target2", targets.get(1).name());
targets = authManager.listTargets(ImmutableList.of(id1, id2, id2));
Assert.assertEquals(3, targets.size());
Assert.assertEquals("target1", targets.get(0).name());
Assert.assertEquals("target2", targets.get(1).name());
Assert.assertEquals("target2", targets.get(2).name());
targets = authManager.listTargets(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
Assert.assertEquals(2, targets.size());
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testCreateAccess.
@Test
public void testCreateAccess() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id group1 = authManager.createGroup(makeGroup("group1"));
Id group2 = authManager.createGroup(makeGroup("group2"));
Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
Id id1 = authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
Id id2 = authManager.createAccess(makeAccess(group1, target1, HugePermission.WRITE));
Id id3 = authManager.createAccess(makeAccess(group1, target2, HugePermission.READ));
Id id4 = authManager.createAccess(makeAccess(group2, target2, HugePermission.READ));
HugeAccess access = authManager.getAccess(id1);
Assert.assertEquals(group1, access.source());
Assert.assertEquals(target1, access.target());
Assert.assertEquals(HugePermission.READ, access.permission());
Assert.assertEquals(access.create(), access.update());
Map<String, Object> expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.READ, "access_creator", "admin"));
expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
Assert.assertEquals(expected, access.asMap());
access = authManager.getAccess(id2);
Assert.assertEquals(group1, access.source());
Assert.assertEquals(target1, access.target());
Assert.assertEquals(HugePermission.WRITE, access.permission());
Assert.assertEquals(access.create(), access.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
Assert.assertEquals(expected, access.asMap());
access = authManager.getAccess(id3);
Assert.assertEquals(group1, access.source());
Assert.assertEquals(target2, access.target());
Assert.assertEquals(HugePermission.READ, access.permission());
Assert.assertEquals(access.create(), access.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
Assert.assertEquals(expected, access.asMap());
access = authManager.getAccess(id4);
Assert.assertEquals(group2, access.source());
Assert.assertEquals(target2, access.target());
Assert.assertEquals(HugePermission.READ, access.permission());
Assert.assertEquals(access.create(), access.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
Assert.assertEquals(expected, access.asMap());
List<HugeAccess> accesses = authManager.listAccessByGroup(group1, -1);
Assert.assertEquals(3, accesses.size());
accesses = authManager.listAccessByGroup(group2, -1);
Assert.assertEquals(1, accesses.size());
accesses = authManager.listAccessByTarget(target1, -1);
Assert.assertEquals(2, accesses.size());
accesses = authManager.listAccessByTarget(target2, -1);
Assert.assertEquals(2, accesses.size());
// Create access with description
access = makeAccess(group2, target2, HugePermission.WRITE);
access.description("something3");
Id id5 = authManager.createAccess(access);
access = authManager.getAccess(id5);
Assert.assertEquals(group2, access.source());
Assert.assertEquals(target2, access.target());
Assert.assertEquals(HugePermission.WRITE, access.permission());
Assert.assertEquals("something3", access.description());
Assert.assertEquals(access.create(), access.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
expected.putAll(ImmutableMap.of("access_description", "something3", "access_create", access.create(), "access_update", access.update()));
Assert.assertEquals(expected, access.asMap());
Assert.assertThrows(IllegalArgumentException.class, () -> {
authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
}, e -> {
Assert.assertContains("Can't save access", e.getMessage());
Assert.assertContains("that already exists", e.getMessage());
});
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testCreateTarget.
@Test
public void testCreateTarget() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
HugeTarget target = makeTarget("graph1", "127.0.0.1:8080");
target.creator("admin");
Id id = authManager.createTarget(target);
target = authManager.getTarget(id);
Assert.assertEquals("graph1", target.name());
Assert.assertEquals("127.0.0.1:8080", target.url());
Assert.assertEquals(target.create(), target.update());
HashMap<String, Object> expected = new HashMap<>();
expected.putAll(ImmutableMap.of("target_name", "graph1", "target_graph", "graph1", "target_url", "127.0.0.1:8080", "target_creator", "admin"));
expected.putAll(ImmutableMap.of("target_create", target.create(), "target_update", target.update(), "id", target.id()));
Assert.assertEquals(expected, target.asMap());
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testUpdateTarget.
@Test
public void testUpdateTarget() throws InterruptedException {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
HugeTarget target = makeTarget("target1", "url1");
Id id = authManager.createTarget(target);
target = authManager.getTarget(id);
Assert.assertEquals("target1", target.name());
Assert.assertEquals("url1", target.url());
Assert.assertEquals(target.create(), target.update());
Date oldUpdateTime = target.update();
Thread.sleep(1L);
target.url("url2");
authManager.updateTarget(target);
HugeTarget target2 = authManager.getTarget(id);
Assert.assertEquals("target1", target2.name());
Assert.assertEquals("url2", target2.url());
Assert.assertEquals(oldUpdateTime, target2.create());
Assert.assertNotEquals(oldUpdateTime, target2.update());
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testCreateTargetWithRess.
@Test
public void testCreateTargetWithRess() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
String ress = "[{\"type\": \"VERTEX\", \"label\": \"person\", " + "\"properties\":{\"city\": \"Beijing\"}}, " + "{\"type\": \"EDGE\", \"label\": \"transfer\"}]";
HugeTarget target = makeTarget("graph1", "127.0.0.1:8080");
target.resources(ress);
Id id = authManager.createTarget(target);
target = authManager.getTarget(id);
Assert.assertEquals("graph1", target.name());
Assert.assertEquals("127.0.0.1:8080", target.url());
Assert.assertEquals(target.create(), target.update());
String expect = "[{\"type\":\"VERTEX\",\"label\":\"person\"," + "\"properties\":{\"city\":\"Beijing\"}}," + "{\"type\":\"EDGE\",\"label\":\"transfer\"," + "\"properties\":null}]";
Assert.assertEquals(expect, JsonUtil.toJson(target.asMap().get("target_resources")));
}
Aggregations