use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testDeleteTarget.
@Test
public void testDeleteTarget() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id id1 = authManager.createTarget(makeTarget("target1", "url1"));
Id id2 = authManager.createTarget(makeTarget("target2", "url2"));
Assert.assertEquals(2, authManager.listAllTargets(-1).size());
HugeTarget target = authManager.deleteTarget(id1);
Assert.assertEquals("target1", target.name());
Assert.assertEquals(1, authManager.listAllTargets(-1).size());
target = authManager.deleteTarget(id2);
Assert.assertEquals("target2", target.name());
Assert.assertEquals(0, authManager.listAllTargets(-1).size());
Assert.assertThrows(IllegalArgumentException.class, () -> {
Id user = authManager.createUser(makeUser("tom", "pass1"));
authManager.deleteTarget(user);
});
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testValidateUserByToken.
@Test
public void testValidateUserByToken() throws AuthenticationException {
AuthManager authManager = graph().authManager();
HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
Id userId = authManager.createUser(user);
String token = authManager.loginUser("test", "pass");
UserWithRole userWithRole;
userWithRole = authManager.validateUser(token);
Assert.assertEquals(userId, userWithRole.userId());
Assert.assertEquals("test", userWithRole.username());
Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
// Token cache missed
Cache<Id, String> tokenCache = Whitebox.getInternalState(authManager, "tokenCache");
tokenCache.invalidate(IdGenerator.of(token));
Assert.assertFalse(tokenCache.containsKey(IdGenerator.of(token)));
userWithRole = authManager.validateUser(token);
Assert.assertEquals(userId, userWithRole.userId());
Assert.assertEquals("test", userWithRole.username());
Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
Assert.assertTrue(tokenCache.containsKey(IdGenerator.of(token)));
// User deleted after login and token not expire
authManager.deleteUser(userId);
userWithRole = authManager.validateUser(token);
Assert.assertNull(null, userWithRole.userId());
Assert.assertEquals("test", userWithRole.username());
Assert.assertNull(userWithRole.role());
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method clearAll.
@After
public void clearAll() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
for (HugeUser user : authManager.listAllUsers(-1)) {
authManager.deleteUser(user.id());
}
for (HugeGroup group : authManager.listAllGroups(-1)) {
authManager.deleteGroup(group.id());
}
for (HugeTarget target : authManager.listAllTargets(-1)) {
authManager.deleteTarget(target.id());
}
for (HugeProject project : authManager.listAllProject(-1)) {
if (!CollectionUtils.isEmpty(project.graphs())) {
authManager.projectRemoveGraphs(project.id(), project.graphs());
}
authManager.deleteProject(project.id());
}
Assert.assertEquals(0, authManager.listAllAccess(-1).size());
Assert.assertEquals(0, authManager.listAllBelong(-1).size());
}
use of com.baidu.hugegraph.auth.AuthManager 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());
}
use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.
the class AuthTest method testListAllBelong.
@Test
public void testListAllBelong() {
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"));
authManager.createBelong(makeBelong(user, group1));
authManager.createBelong(makeBelong(user, group2));
List<HugeBelong> belongs = authManager.listAllBelong(-1);
Assert.assertEquals(2, belongs.size());
Assert.assertEquals(ImmutableSet.of(group1, group2), ImmutableSet.of(belongs.get(0).target(), belongs.get(1).target()));
Assert.assertEquals(0, authManager.listAllBelong(0).size());
Assert.assertEquals(1, authManager.listAllBelong(1).size());
Assert.assertEquals(2, authManager.listAllBelong(2).size());
Assert.assertEquals(2, authManager.listAllBelong(3).size());
}
Aggregations