Search in sources :

Example 16 with HugeUser

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

the class AuthTest method testListAllUsers.

@Test
public void testListAllUsers() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    authManager.createUser(makeUser("tom", "pass1"));
    authManager.createUser(makeUser("james", "pass2"));
    List<HugeUser> users = authManager.listAllUsers(-1);
    Assert.assertEquals(2, users.size());
    Assert.assertEquals(ImmutableSet.of("tom", "james"), ImmutableSet.of(users.get(0).name(), users.get(1).name()));
    Assert.assertEquals(0, authManager.listAllUsers(0).size());
    Assert.assertEquals(1, authManager.listAllUsers(1).size());
    Assert.assertEquals(2, authManager.listAllUsers(2).size());
    Assert.assertEquals(2, authManager.listAllUsers(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 17 with HugeUser

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

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

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

the class RolePermissionTest method testHugeResourceFilterUser.

@Test
public void testHugeResourceFilterUser() {
    HugeResource all = HugeResource.ALL;
    // user
    ResourceObject<?> r3 = ResourceObject.of("g1", ResourceType.USER_GROUP, NameObject.ANY);
    Assert.assertFalse(all.filter(r3));
    HugeResource user = new HugeResource(ResourceType.USER_GROUP, HugeResource.ANY, null);
    Assert.assertTrue(user.filter(r3));
    ResourceObject<?> r4 = ResourceObject.of("g1", new HugeUser("fake"));
    Assert.assertTrue(user.filter(r4));
    HugeResource user2 = new HugeResource(ResourceType.USER_GROUP, "bj-.*", null);
    Assert.assertTrue(user2.filter(r3));
    Assert.assertFalse(user2.filter(r4));
    HugeResource user3 = new HugeResource(ResourceType.USER_GROUP, "fa.*", null);
    Assert.assertTrue(user3.filter(r3));
    Assert.assertTrue(user3.filter(r4));
    ResourceObject<?> r5 = ResourceObject.of("g1", new HugeTarget("g", ""));
    Assert.assertFalse(user.filter(r5));
    HugeResource root = new HugeResource(ResourceType.ROOT, HugeResource.ANY, null);
    Assert.assertTrue(root.filter(r3));
    Assert.assertTrue(root.filter(r4));
    Assert.assertTrue(root.filter(r5));
}
Also used : HugeResource(com.baidu.hugegraph.auth.HugeResource) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Aggregations

HugeUser (com.baidu.hugegraph.auth.HugeUser)19 HugeGraph (com.baidu.hugegraph.HugeGraph)13 AuthManager (com.baidu.hugegraph.auth.AuthManager)12 Test (org.junit.Test)12 Id (com.baidu.hugegraph.backend.id.Id)9 Timed (com.codahale.metrics.annotation.Timed)5 Produces (jakarta.ws.rs.Produces)5 GET (jakarta.ws.rs.GET)3 Path (jakarta.ws.rs.Path)3 HugeTarget (com.baidu.hugegraph.auth.HugeTarget)2 UserWithRole (com.baidu.hugegraph.auth.UserWithRole)2 Consumes (jakarta.ws.rs.Consumes)2 HashMap (java.util.HashMap)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 HugeGroup (com.baidu.hugegraph.auth.HugeGroup)1 HugeProject (com.baidu.hugegraph.auth.HugeProject)1 HugeResource (com.baidu.hugegraph.auth.HugeResource)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1