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);
});
}
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());
});
}
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());
}
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());
});
}
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);
});
}
Aggregations