use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method testDeleteBelong.
@Test
public void testDeleteBelong() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id user = authManager.createUser(makeUser("tom", "pass1"));
Id group1 = authManager.createGroup(makeGroup("group1"));
Id group2 = authManager.createGroup(makeGroup("group2"));
Id id1 = authManager.createBelong(makeBelong(user, group1));
Id id2 = authManager.createBelong(makeBelong(user, group2));
Assert.assertEquals(2, authManager.listAllBelong(-1).size());
HugeBelong belong = authManager.deleteBelong(id1);
Assert.assertEquals(group1, belong.target());
Assert.assertEquals(1, authManager.listAllBelong(-1).size());
Assert.assertEquals(1, authManager.listAllBelong(2).size());
belong = authManager.deleteBelong(id2);
Assert.assertEquals(group2, belong.target());
Assert.assertEquals(0, authManager.listAllBelong(-1).size());
Assert.assertThrows(IllegalArgumentException.class, () -> {
Id target = authManager.createTarget(makeTarget("graph1", ""));
Id access = authManager.createAccess(makeAccess(group1, target, HugePermission.READ));
authManager.deleteBelong(access);
});
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method testGetBelong.
@Test
public void testGetBelong() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id user = authManager.createUser(makeUser("tom", "pass1"));
Id group1 = authManager.createGroup(makeGroup("group1"));
Id group2 = authManager.createGroup(makeGroup("group2"));
Id id1 = authManager.createBelong(makeBelong(user, group1));
Id id2 = authManager.createBelong(makeBelong(user, group2));
HugeBelong belong1 = authManager.getBelong(id1);
Assert.assertEquals(group1, belong1.target());
HugeBelong belong2 = authManager.getBelong(id2);
Assert.assertEquals(group2, belong2.target());
Assert.assertThrows(NotFoundException.class, () -> {
authManager.getBelong(IdGenerator.of("fake"));
});
Assert.assertThrows(NotFoundException.class, () -> {
authManager.getBelong(null);
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
Id target = authManager.createTarget(makeTarget("graph1", ""));
Id access = authManager.createAccess(makeAccess(group1, target, HugePermission.READ));
authManager.getBelong(access);
});
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method testCreateBelong.
@Test
public void testCreateBelong() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id user = authManager.createUser(makeUser("tom", "pass1"));
Id group1 = authManager.createGroup(makeGroup("group1"));
Id group2 = authManager.createGroup(makeGroup("group2"));
Id id1 = authManager.createBelong(makeBelong(user, group1));
Id id2 = authManager.createBelong(makeBelong(user, group2));
HugeBelong belong = authManager.getBelong(id1);
Assert.assertEquals(user, belong.source());
Assert.assertEquals(group1, belong.target());
Assert.assertEquals(null, belong.description());
Assert.assertEquals(belong.create(), belong.update());
Map<String, Object> expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", belong.id(), "user", user, "group", group1));
expected.putAll(ImmutableMap.of("belong_creator", "admin", "belong_create", belong.create(), "belong_update", belong.update()));
Assert.assertEquals(expected, belong.asMap());
belong = authManager.getBelong(id2);
Assert.assertEquals(user, belong.source());
Assert.assertEquals(group2, belong.target());
Assert.assertEquals(null, belong.description());
Assert.assertEquals(belong.create(), belong.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", belong.id(), "user", user, "group", group2));
expected.putAll(ImmutableMap.of("belong_creator", "admin", "belong_create", belong.create(), "belong_update", belong.update()));
Assert.assertEquals(expected, belong.asMap());
List<HugeBelong> belongs = authManager.listBelongByUser(user, -1);
Assert.assertEquals(2, belongs.size());
belongs = authManager.listBelongByGroup(group1, -1);
Assert.assertEquals(1, belongs.size());
belongs = authManager.listBelongByGroup(group2, -1);
Assert.assertEquals(1, belongs.size());
// Create belong with description
Id user1 = authManager.createUser(makeUser("user1", "pass1"));
belong = makeBelong(user1, group1);
belong.description("something2");
Id id3 = authManager.createBelong(belong);
belong = authManager.getBelong(id3);
Assert.assertEquals(user1, belong.source());
Assert.assertEquals(group1, belong.target());
Assert.assertEquals("something2", belong.description());
Assert.assertEquals(belong.create(), belong.update());
expected = new HashMap<>();
expected.putAll(ImmutableMap.of("id", belong.id(), "user", user1, "group", group1));
expected.putAll(ImmutableMap.of("belong_description", "something2", "belong_creator", "admin", "belong_create", belong.create(), "belong_update", belong.update()));
Assert.assertEquals(expected, belong.asMap());
Assert.assertThrows(IllegalArgumentException.class, () -> {
authManager.createBelong(makeBelong(user, group1));
}, e -> {
Assert.assertContains("Can't save belong", e.getMessage());
Assert.assertContains("that already exists", e.getMessage());
});
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class BelongAPI method get.
@GET
@Timed
@Path("{id}")
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id) {
LOG.debug("Graph [{}] get belong: {}", graph, id);
HugeGraph g = graph(manager, graph);
HugeBelong belong = manager.authManager().getBelong(UserAPI.parseId(id));
return manager.serializer(g).writeAuthElement(belong);
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method testUpdateBelong.
@Test
public void testUpdateBelong() throws InterruptedException {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id user = authManager.createUser(makeUser("tom", "pass1"));
Id group = authManager.createGroup(makeGroup("group1"));
HugeBelong belong = makeBelong(user, group);
belong.description("description1");
Id id = authManager.createBelong(belong);
belong = authManager.getBelong(id);
Assert.assertEquals(user, belong.source());
Assert.assertEquals(group, belong.target());
Assert.assertEquals("description1", belong.description());
Assert.assertEquals(belong.create(), belong.update());
Date oldUpdateTime = belong.update();
Thread.sleep(1L);
belong.description("description2");
authManager.updateBelong(belong);
HugeBelong belong2 = authManager.getBelong(id);
Assert.assertEquals(user, belong.source());
Assert.assertEquals(group, belong.target());
Assert.assertEquals("description2", belong.description());
Assert.assertEquals(oldUpdateTime, belong2.create());
Assert.assertNotEquals(oldUpdateTime, belong2.update());
Assert.assertThrows(IllegalArgumentException.class, () -> {
Id group2 = authManager.createGroup(makeGroup("group2"));
HugeBelong belong3 = makeBelong(user, group2);
authManager.updateBelong(belong3);
}, e -> {
Assert.assertContains("Can't save belong", e.getMessage());
Assert.assertContains("that not exists", e.getMessage());
});
}
Aggregations