Search in sources :

Example 1 with User

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

the class UserApiTest method createUser.

protected static User createUser(String name, String password) {
    User user = new User();
    user.name(name);
    user.password(password);
    user.email("test@hugegraph.com");
    user.phone("16812345678");
    user.avatar("image.jpg");
    return api.create(user);
}
Also used : User(com.baidu.hugegraph.structure.auth.User)

Example 2 with User

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

the class UserApiTest method testGetUserRole.

@Test
public void testGetUserRole() {
    User user1 = createUser("test1", "psw1");
    User user2 = createUser("test2", "psw2");
    Assert.assertEquals("test1", user1.name());
    Assert.assertEquals("test2", user2.name());
    UserRole role1 = api.getUserRole(user1.id());
    UserRole role2 = api.getUserRole(user2.id());
    Assert.assertEquals("{\"roles\":{}}", role1.toString());
    Assert.assertEquals("{\"roles\":{}}", role2.toString());
}
Also used : User(com.baidu.hugegraph.structure.auth.User) UserRole(com.baidu.hugegraph.structure.auth.User.UserRole) Test(org.junit.Test)

Example 3 with User

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

the class UserApiTest method testCreate.

@Test
public void testCreate() {
    User user1 = new User();
    user1.name("user1");
    user1.password("p1");
    user1.email("user1@hugegraph.com");
    user1.phone("123456789");
    user1.avatar("image1.jpg");
    User user2 = new User();
    user2.name("user2");
    user2.password("p2");
    user2.email("user2@hugegraph.com");
    user2.phone("1357924680");
    user2.avatar("image2.jpg");
    User result1 = api.create(user1);
    User result2 = api.create(user2);
    Assert.assertEquals("user1", result1.name());
    Assert.assertNotEquals("p1", result1.password());
    Assert.assertEquals("user1@hugegraph.com", result1.email());
    Assert.assertEquals("123456789", result1.phone());
    Assert.assertEquals("image1.jpg", result1.avatar());
    Assert.assertEquals("user2", result2.name());
    Assert.assertNotEquals("p2", result2.password());
    Assert.assertEquals("user2@hugegraph.com", result2.email());
    Assert.assertEquals("1357924680", result2.phone());
    Assert.assertEquals("image2.jpg", result2.avatar());
    Assert.assertThrows(ServerException.class, () -> {
        api.create(new User());
    }, e -> {
        Assert.assertContains("The name of user can't be null", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        User user3 = new User();
        user3.name("test");
        api.create(user3);
    }, e -> {
        Assert.assertContains("The password of user can't be null", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        api.create(user1);
    }, e -> {
        Assert.assertContains("Can't save user", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        user1.name("admin");
        api.create(user1);
    }, e -> {
        Assert.assertContains("Invalid user name 'admin'", e.getMessage());
    });
}
Also used : User(com.baidu.hugegraph.structure.auth.User) Test(org.junit.Test)

Example 4 with User

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

the class LogoutApiTest method testLogout.

@Test
public void testLogout() {
    User user1 = new User();
    user1.name("user1");
    user1.password("p1");
    userAPI.create(user1);
    Login login = new Login();
    login.name("user1");
    login.password("p1");
    LoginResult result = loginAPI.login(login);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.token());
    // Client will set Authentication Header use Basic
    Assert.assertThrows(ServerException.class, () -> {
        logoutAPI.logout();
    }, e -> {
        Assert.assertContains("Only HTTP Bearer authentication is supported", e.getMessage());
    });
    String token = result.token();
    RestClient client = Whitebox.getInternalState(logoutAPI, "client");
    client.setAuthContext("Bearer " + token);
    logoutAPI.logout();
}
Also used : User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) RestClient(com.baidu.hugegraph.client.RestClient) Login(com.baidu.hugegraph.structure.auth.Login) Test(org.junit.Test)

Example 5 with User

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

the class LoginApiTest method testLogin.

@Test
public void testLogin() {
    Login login = new Login();
    login.name("user1");
    login.password("p1");
    Assert.assertThrows(ServerException.class, () -> {
        loginAPI.login(login);
    }, e -> {
        Assert.assertContains("Incorrect username or password", e.getMessage());
    });
    User user1 = new User();
    user1.name("user1");
    user1.password("p1");
    userAPI.create(user1);
    LoginResult result = loginAPI.login(login);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.token());
}
Also used : User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) Login(com.baidu.hugegraph.structure.auth.Login) Test(org.junit.Test)

Aggregations

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