Search in sources :

Example 1 with AuthManager

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

the class AuthTest method testCreateGroup.

@Test
public void testCreateGroup() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    HugeGroup group = makeGroup("group1");
    Id id = authManager.createGroup(group);
    group = authManager.getGroup(id);
    Assert.assertEquals("group1", group.name());
    Assert.assertEquals(null, group.description());
    Assert.assertEquals(group.create(), group.update());
    Assert.assertEquals(ImmutableMap.of("group_name", "group1", "group_create", group.create(), "group_update", group.update(), "group_creator", "admin", "id", group.id()), group.asMap());
    group = makeGroup("group2");
    group.description("something");
    id = authManager.createGroup(group);
    group = authManager.getGroup(id);
    Assert.assertEquals("group2", group.name());
    Assert.assertEquals("something", group.description());
    Assert.assertEquals(group.create(), group.update());
    HashMap<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("group_name", "group2", "group_description", "something", "group_creator", "admin"));
    expected.putAll(ImmutableMap.of("group_create", group.create(), "group_update", group.update(), "id", group.id()));
    Assert.assertEquals(expected, group.asMap());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) HugeGroup(com.baidu.hugegraph.auth.HugeGroup) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 2 with AuthManager

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

the class AuthTest method testListGroups.

@Test
public void testListGroups() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createGroup(makeGroup("group1"));
    Id id2 = authManager.createGroup(makeGroup("group2"));
    List<HugeGroup> groups = authManager.listGroups(ImmutableList.of(id1, id2));
    Assert.assertEquals(2, groups.size());
    Assert.assertEquals("group1", groups.get(0).name());
    Assert.assertEquals("group2", groups.get(1).name());
    groups = authManager.listGroups(ImmutableList.of(id1, id2, id2));
    Assert.assertEquals(3, groups.size());
    Assert.assertEquals("group1", groups.get(0).name());
    Assert.assertEquals("group2", groups.get(1).name());
    Assert.assertEquals("group2", groups.get(2).name());
    groups = authManager.listGroups(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
    Assert.assertEquals(2, groups.size());
}
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)

Example 3 with AuthManager

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

the class AuthTest method testLogin.

@Test
public void testLogin() throws AuthenticationException {
    AuthManager authManager = graph().authManager();
    HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
    authManager.createUser(user);
    // Login
    authManager.loginUser("test", "pass");
    // Invalid username or password
    Assert.assertThrows(AuthenticationException.class, () -> {
        authManager.loginUser("huge", "graph");
    }, e -> {
        Assert.assertContains("Incorrect username or password", e.getMessage());
    });
}
Also used : AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 4 with AuthManager

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

the class AuthTest method testDeleteUser.

@Test
public void testDeleteUser() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createUser(makeUser("tom", "pass1"));
    Id id2 = authManager.createUser(makeUser("james", "pass2"));
    Assert.assertEquals(2, authManager.listAllUsers(-1).size());
    HugeUser user = authManager.deleteUser(id1);
    Assert.assertEquals("tom", user.name());
    Assert.assertEquals(1, authManager.listAllUsers(-1).size());
    user = authManager.deleteUser(id2);
    Assert.assertEquals("james", user.name());
    Assert.assertEquals(0, authManager.listAllUsers(-1).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 5 with AuthManager

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

the class AuthTest method testListAccess.

@Test
public void testListAccess() {
    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));
    List<HugeAccess> access = authManager.listAccess(ImmutableList.of(id1, id2));
    Assert.assertEquals(2, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(group, access.get(1).source());
    Assert.assertEquals(target1, access.get(0).target());
    Assert.assertEquals(target2, access.get(1).target());
    access = authManager.listAccess(ImmutableList.of(id1, id2, id2));
    Assert.assertEquals(3, access.size());
    access = authManager.listAccess(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
    Assert.assertEquals(2, access.size());
    access = authManager.listAccessByGroup(group, -1);
    Assert.assertEquals(2, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(group, access.get(1).source());
    access = authManager.listAccessByTarget(target1, -1);
    Assert.assertEquals(1, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(target1, access.get(0).target());
    access = authManager.listAccessByTarget(target2, -1);
    Assert.assertEquals(1, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(target2, access.get(0).target());
}
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)

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