Search in sources :

Example 1 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class TargetApiTest method testUpdate.

@Test
public void testUpdate() {
    Target target1 = createTarget("test1", HugeResourceType.VERTEX);
    Target target2 = createTarget("test2", HugeResourceType.EDGE);
    Assert.assertEquals(HugeResourceType.VERTEX, target1.resource().resourceType());
    Assert.assertEquals(HugeResourceType.EDGE, target2.resource().resourceType());
    target1.resources(new HugeResource(HugeResourceType.ALL));
    Target updated = api.update(target1);
    Assert.assertEquals(HugeResourceType.ALL, updated.resource().resourceType());
    Assert.assertNotEquals(target1.updateTime(), updated.updateTime());
    Assert.assertThrows(ServerException.class, () -> {
        target2.name("test2-updated");
        api.update(target2);
    }, e -> {
        Assert.assertContains("The name of target can't be updated", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Whitebox.setInternalState(target2, "id", "fake-id");
        api.update(target2);
    }, e -> {
        Assert.assertContains("Invalid target id: fake-id", e.getMessage());
    });
}
Also used : Target(com.baidu.hugegraph.structure.auth.Target) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource) Test(org.junit.Test)

Example 2 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class TargetApiTest method testDelete.

@Test
public void testDelete() {
    Target target1 = createTarget("test1", HugeResourceType.VERTEX);
    Target target2 = createTarget("test2", HugeResourceType.EDGE);
    Assert.assertEquals(2, api.list(-1).size());
    api.delete(target1.id());
    Assert.assertEquals(1, api.list(-1).size());
    Assert.assertEquals(target2, api.list(-1).get(0));
    api.delete(target2.id());
    Assert.assertEquals(0, api.list(-1).size());
    Assert.assertThrows(ServerException.class, () -> {
        api.delete(target2.id());
    }, e -> {
        Assert.assertContains("Invalid target id:", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        api.delete("fake-id");
    }, e -> {
        Assert.assertContains("Invalid target id: fake-id", e.getMessage());
    });
}
Also used : Target(com.baidu.hugegraph.structure.auth.Target) Test(org.junit.Test)

Example 3 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class TargetApiTest method testGet.

@Test
public void testGet() {
    Target target1 = createTarget("test1", HugeResourceType.VERTEX);
    Target target2 = createTarget("test2", HugeResourceType.EDGE);
    Assert.assertEquals(HugeResourceType.VERTEX, target1.resource().resourceType());
    Assert.assertEquals(HugeResourceType.EDGE, target2.resource().resourceType());
    target1 = api.get(target1.id());
    target2 = api.get(target2.id());
    Assert.assertEquals("test1", target1.name());
    Assert.assertEquals(HugeResourceType.VERTEX, target1.resource().resourceType());
    Assert.assertEquals("test2", target2.name());
    Assert.assertEquals(HugeResourceType.EDGE, target2.resource().resourceType());
}
Also used : Target(com.baidu.hugegraph.structure.auth.Target) Test(org.junit.Test)

Example 4 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class AccessApiTest method testUpdate.

@Test
public void testUpdate() {
    Access access1 = createAccess(HugePermission.WRITE, "description 1");
    Access access2 = createAccess(HugePermission.READ, "description 2");
    Assert.assertEquals("description 1", access1.description());
    Assert.assertEquals("description 2", access2.description());
    access1.description("description updated");
    Access updated = api.update(access1);
    Assert.assertEquals("description updated", updated.description());
    Assert.assertNotEquals(access1.updateTime(), updated.updateTime());
    Assert.assertThrows(ServerException.class, () -> {
        Group hk = GroupApiTest.createGroup("group-hk", "");
        access2.group(hk);
        api.update(access2);
    }, e -> {
        Assert.assertContains("The group of access can't be updated", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Target task = TargetApiTest.createTarget("task", HugeResourceType.TASK);
        access2.group(group);
        access2.target(task);
        api.update(access2);
    }, e -> {
        Assert.assertContains("The target of access can't be updated", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Whitebox.setInternalState(access2, "id", "fake-id");
        api.update(access2);
    }, e -> {
        Assert.assertContains("Invalid access id: fake-id", e.getMessage());
    });
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) Target(com.baidu.hugegraph.structure.auth.Target) Access(com.baidu.hugegraph.structure.auth.Access) Test(org.junit.Test)

Example 5 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class AuthRestoreTest method testAuthRestoreForAllType.

@Test
public void testAuthRestoreForAllType() {
    this.loadData(HugeType.USER, "auth_users.txt");
    this.loadData(HugeType.TARGET, "auth_targets.txt");
    this.loadData(HugeType.GROUP, "auth_groups.txt");
    this.loadData(HugeType.BELONG, "auth_belongs.txt");
    this.loadData(HugeType.ACCESS, "auth_accesses.txt");
    String[] args = new String[] { "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", "--directory", DEFAULT_URL, "--init-password", "123456", "--strategy", "ignore" };
    HugeGraphCommand.main(args);
    List<String> idList = Lists.newArrayList();
    List<User> userList = this.client.auth().listUsers();
    Map<String, User> userMap = Maps.newHashMap();
    for (User user1 : userList) {
        userMap.put(user1.name(), user1);
    }
    Assert.assertTrue(userMap.containsKey("test_user1"));
    idList.add(userMap.get("test_user1").id().toString());
    List<Group> groups = this.client.auth().listGroups();
    Map<String, Group> groupMap = Maps.newHashMap();
    for (Group group : groups) {
        groupMap.put(group.name(), group);
    }
    Assert.assertTrue(groupMap.containsKey("test_group6"));
    idList.add(groupMap.get("test_group6").id().toString());
    List<Target> targets = this.client.auth().listTargets();
    Map<String, Target> targetMap = Maps.newHashMap();
    for (Target target : targets) {
        targetMap.put(target.name(), target);
    }
    Assert.assertTrue(targetMap.containsKey("test_target1"));
    idList.add(targetMap.get("test_target1").id().toString());
    List<Belong> belongs = this.client.auth().listBelongs();
    Assert.assertTrue(CollectionUtils.isNotEmpty(belongs));
    boolean checkUserAndGroup = false;
    for (Belong belong : belongs) {
        if (idList.contains(belong.user().toString()) && idList.contains(belong.group().toString())) {
            checkUserAndGroup = true;
            break;
        }
    }
    Assert.assertTrue(checkUserAndGroup);
    List<Access> accesses = this.client.auth().listAccesses();
    Assert.assertTrue(CollectionUtils.isNotEmpty(accesses));
    boolean checkGroupAndTarget = false;
    for (Access access : accesses) {
        if (idList.contains(access.group().toString()) && idList.contains(access.target().toString())) {
            checkGroupAndTarget = true;
            break;
        }
    }
    Assert.assertTrue(checkGroupAndTarget);
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) User(com.baidu.hugegraph.structure.auth.User) Access(com.baidu.hugegraph.structure.auth.Access) Target(com.baidu.hugegraph.structure.auth.Target) Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Aggregations

Target (com.baidu.hugegraph.structure.auth.Target)10 Test (org.junit.Test)8 Access (com.baidu.hugegraph.structure.auth.Access)5 Group (com.baidu.hugegraph.structure.auth.Group)5 HugeResource (com.baidu.hugegraph.structure.auth.HugeResource)4 Belong (com.baidu.hugegraph.structure.auth.Belong)3 User (com.baidu.hugegraph.structure.auth.User)3 Project (com.baidu.hugegraph.structure.auth.Project)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 Login (com.baidu.hugegraph.structure.auth.Login)1 LoginResult (com.baidu.hugegraph.structure.auth.LoginResult)1 TokenPayload (com.baidu.hugegraph.structure.auth.TokenPayload)1 UserRole (com.baidu.hugegraph.structure.auth.User.UserRole)1