Search in sources :

Example 16 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testGetUser.

@Test
public void testGetUser() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id = authManager.createUser(makeUser("tom", "pass1"));
    HugeUser user = authManager.getUser(id);
    Assert.assertEquals("tom", user.name());
    Assert.assertEquals("pass1", user.password());
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getUser(IdGenerator.of("fake"));
    });
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getUser(null);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 17 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager 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 18 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testListAllGroups.

@Test
public void testListAllGroups() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    authManager.createGroup(makeGroup("group1"));
    authManager.createGroup(makeGroup("group2"));
    List<HugeGroup> groups = authManager.listAllGroups(-1);
    Assert.assertEquals(2, groups.size());
    Assert.assertEquals(ImmutableSet.of("group1", "group2"), ImmutableSet.of(groups.get(0).name(), groups.get(1).name()));
    Assert.assertEquals(0, authManager.listAllGroups(0).size());
    Assert.assertEquals(1, authManager.listAllGroups(1).size());
    Assert.assertEquals(2, authManager.listAllGroups(2).size());
    Assert.assertEquals(2, authManager.listAllGroups(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeGroup(com.baidu.hugegraph.auth.HugeGroup) Test(org.junit.Test)

Example 19 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testUpdateAccess.

@Test
public void testUpdateAccess() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target = authManager.createTarget(makeTarget("graph1", "url1"));
    Id id = authManager.createAccess(makeAccess(group, target, HugePermission.READ));
    HugeAccess access = authManager.getAccess(id);
    Assert.assertEquals(group, access.source());
    Assert.assertEquals(target, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    Date oldUpdateTime = access.update();
    Thread.sleep(1L);
    access.permission(HugePermission.READ);
    authManager.updateAccess(access);
    HugeAccess access2 = authManager.getAccess(id);
    Assert.assertEquals(group, access.source());
    Assert.assertEquals(target, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(oldUpdateTime, access2.create());
    Assert.assertNotEquals(oldUpdateTime, access2.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        access.permission(HugePermission.WRITE);
        authManager.updateAccess(access);
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
    access.permission(HugePermission.READ);
    access.description("description updated");
    id = authManager.updateAccess(access);
    HugeAccess access3 = authManager.getAccess(id);
    Assert.assertEquals(group, access3.source());
    Assert.assertEquals(target, access3.target());
    Assert.assertEquals("description updated", access3.description());
    Assert.assertEquals(HugePermission.READ, access3.permission());
    Assert.assertEquals(oldUpdateTime, access3.create());
    Assert.assertNotEquals(access3.create(), access3.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        HugeAccess access4 = makeAccess(group, target, HugePermission.DELETE);
        authManager.updateAccess(access4);
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Date(java.util.Date) Test(org.junit.Test)

Example 20 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testGetGroup.

@Test
public void testGetGroup() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id = authManager.createGroup(makeGroup("group-test"));
    HugeGroup group = authManager.getGroup(id);
    Assert.assertEquals("group-test", group.name());
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getGroup(IdGenerator.of("fake"));
    });
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getGroup(null);
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id user = authManager.createUser(makeUser("tom", "pass1"));
        authManager.getGroup(user);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeGroup(com.baidu.hugegraph.auth.HugeGroup) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Aggregations

AuthManager (com.baidu.hugegraph.auth.AuthManager)46 Test (org.junit.Test)43 Id (com.baidu.hugegraph.backend.id.Id)39 HugeGraph (com.baidu.hugegraph.HugeGraph)36 HugeUser (com.baidu.hugegraph.auth.HugeUser)12 HugeProject (com.baidu.hugegraph.auth.HugeProject)8 HugeTarget (com.baidu.hugegraph.auth.HugeTarget)8 HugeGroup (com.baidu.hugegraph.auth.HugeGroup)7 HugeAccess (com.baidu.hugegraph.auth.HugeAccess)6 HugeBelong (com.baidu.hugegraph.auth.HugeBelong)6 HashMap (java.util.HashMap)6 Date (java.util.Date)5 UserWithRole (com.baidu.hugegraph.auth.UserWithRole)2 HugeResource (com.baidu.hugegraph.auth.HugeResource)1 RolePermission (com.baidu.hugegraph.auth.RolePermission)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 Timed (com.codahale.metrics.annotation.Timed)1 Consumes (jakarta.ws.rs.Consumes)1 PUT (jakarta.ws.rs.PUT)1 Path (jakarta.ws.rs.Path)1