use of com.baidu.hugegraph.auth.HugeUser in project incubator-hugegraph by apache.
the class AuthTest method testUpdateUser.
@Test
public void testUpdateUser() throws InterruptedException {
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());
Date oldUpdateTime = user.update();
Thread.sleep(1L);
user.password("pass2");
authManager.updateUser(user);
HugeUser user2 = authManager.getUser(id);
Assert.assertEquals("tom", user2.name());
Assert.assertEquals("pass2", user2.password());
Assert.assertEquals(oldUpdateTime, user2.create());
Assert.assertNotEquals(oldUpdateTime, user2.update());
Assert.assertThrows(IllegalArgumentException.class, () -> {
authManager.updateUser(makeUser("tom2", "pass1"));
}, e -> {
Assert.assertContains("Can't save user", e.getMessage());
Assert.assertContains("that not exists", e.getMessage());
});
}
use of com.baidu.hugegraph.auth.HugeUser in project incubator-hugegraph by apache.
the class AuthTest method testLogout.
@Test
public void testLogout() throws AuthenticationException {
AuthManager authManager = graph().authManager();
HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
@SuppressWarnings("unused") Id userId = authManager.createUser(user);
// Login
String token = authManager.loginUser("test", "pass");
// Logout
Cache<Id, String> tokenCache = Whitebox.getInternalState(authManager, "tokenCache");
Assert.assertTrue(tokenCache.containsKey(IdGenerator.of(token)));
authManager.logoutUser(token);
Assert.assertFalse(tokenCache.containsKey(IdGenerator.of(token)));
}
use of com.baidu.hugegraph.auth.HugeUser in project incubator-hugegraph by apache.
the class AuthTest method testValidateUserByNameAndPassword.
@Test
public void testValidateUserByNameAndPassword() {
AuthManager authManager = graph().authManager();
HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
Id userId = authManager.createUser(user);
UserWithRole userWithRole;
userWithRole = authManager.validateUser("test", "pass");
Assert.assertEquals(userId, userWithRole.userId());
Assert.assertEquals("test", userWithRole.username());
Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
// Error case
userWithRole = authManager.validateUser("huge", "graph");
Assert.assertNull(userWithRole.userId());
Assert.assertEquals("huge", userWithRole.username());
Assert.assertNull(userWithRole.role());
}
use of com.baidu.hugegraph.auth.HugeUser 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.HugeUser 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());
}
Aggregations