Search in sources :

Example 6 with HugeBelong

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);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 7 with HugeBelong

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);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 8 with HugeBelong

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());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 9 with HugeBelong

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);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 10 with HugeBelong

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());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) Date(java.util.Date) Test(org.junit.Test)

Aggregations

HugeBelong (com.baidu.hugegraph.auth.HugeBelong)11 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Id (com.baidu.hugegraph.backend.id.Id)7 AuthManager (com.baidu.hugegraph.auth.AuthManager)6 Test (org.junit.Test)6 Timed (com.codahale.metrics.annotation.Timed)4 Produces (jakarta.ws.rs.Produces)4 Consumes (jakarta.ws.rs.Consumes)2 GET (jakarta.ws.rs.GET)2 Path (jakarta.ws.rs.Path)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1