Search in sources :

Example 41 with AuthManager

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

the class AuthTest method testGetAccess.

@Test
public void testGetAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    Id id1 = authManager.createAccess(makeAccess(group, target1, HugePermission.READ));
    Id id2 = authManager.createAccess(makeAccess(group, target2, HugePermission.READ));
    HugeAccess access1 = authManager.getAccess(id1);
    Assert.assertEquals(target1, access1.target());
    HugeAccess access2 = authManager.getAccess(id2);
    Assert.assertEquals(target2, access2.target());
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getAccess(IdGenerator.of("fake"));
    });
    Assert.assertThrows(NotFoundException.class, () -> {
        authManager.getAccess(null);
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id user = authManager.createUser(makeUser("tom", "pass1"));
        Id belong = authManager.createBelong(makeBelong(user, group));
        authManager.getAccess(belong);
    });
}
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) Test(org.junit.Test)

Example 42 with AuthManager

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

the class AuthTest method testCreateUser.

@Test
public void testCreateUser() {
    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.assertEquals(user.create(), user.update());
    Assert.assertNull(user.phone());
    Assert.assertNull(user.email());
    Assert.assertNull(user.avatar());
    Map<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("user_name", "tom", "user_password", "pass1", "user_creator", "admin"));
    expected.putAll(ImmutableMap.of("user_create", user.create(), "user_update", user.update(), "id", user.id()));
    Assert.assertEquals(expected, user.asMap());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        authManager.createUser(makeUser("tom", "pass1"));
    }, e -> {
        Assert.assertContains("Can't save user", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 43 with AuthManager

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

the class AuthTest method testListUsers.

@Test
public void testListUsers() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createUser(makeUser("tom", "pass1"));
    Id id2 = authManager.createUser(makeUser("james", "pass2"));
    List<HugeUser> users = authManager.listUsers(ImmutableList.of(id1, id2));
    Assert.assertEquals(2, users.size());
    Assert.assertEquals("tom", users.get(0).name());
    Assert.assertEquals("james", users.get(1).name());
    users = authManager.listUsers(ImmutableList.of(id1, id2, id2));
    Assert.assertEquals(3, users.size());
    Assert.assertEquals("tom", users.get(0).name());
    Assert.assertEquals("james", users.get(1).name());
    Assert.assertEquals("james", users.get(2).name());
    users = authManager.listUsers(ImmutableList.of(id1, id2, id1));
    Assert.assertEquals(3, users.size());
    Assert.assertEquals("tom", users.get(0).name());
    Assert.assertEquals("james", users.get(1).name());
    Assert.assertEquals("tom", users.get(2).name());
    users = authManager.listUsers(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
    Assert.assertEquals(2, users.size());
}
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 44 with AuthManager

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

the class AuthTest method testListAllTargets.

@Test
public void testListAllTargets() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    authManager.createTarget(makeTarget("target1", "url1"));
    authManager.createTarget(makeTarget("target2", "url1"));
    List<HugeTarget> targets = authManager.listAllTargets(-1);
    Assert.assertEquals(2, targets.size());
    Assert.assertEquals(ImmutableSet.of("target1", "target2"), ImmutableSet.of(targets.get(0).name(), targets.get(1).name()));
    Assert.assertEquals(0, authManager.listAllTargets(0).size());
    Assert.assertEquals(1, authManager.listAllTargets(1).size());
    Assert.assertEquals(2, authManager.listAllTargets(2).size());
    Assert.assertEquals(2, authManager.listAllTargets(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Test(org.junit.Test)

Example 45 with AuthManager

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

the class AuthTest method testUpdateGroup.

@Test
public void testUpdateGroup() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    HugeGroup group = makeGroup("group1");
    group.description("description1");
    Id id = authManager.createGroup(group);
    group = authManager.getGroup(id);
    Assert.assertEquals("group1", group.name());
    Assert.assertEquals("description1", group.description());
    Assert.assertEquals(group.create(), group.update());
    Date oldUpdateTime = group.update();
    Thread.sleep(1L);
    group.description("description2");
    authManager.updateGroup(group);
    HugeGroup group2 = authManager.getGroup(id);
    Assert.assertEquals("group1", group2.name());
    Assert.assertEquals("description2", group2.description());
    Assert.assertEquals(oldUpdateTime, group2.create());
    Assert.assertNotEquals(oldUpdateTime, group2.update());
}
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) Date(java.util.Date) 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