Search in sources :

Example 21 with User

use of com.stardata.starshop2.authcontext.domain.user.User in project starshop by beautautumn.

the class StarshopAuthLoginTests method after_set_openid_should_get_openid_correctly.

// 1.2. 设置新用户openid;(相关任务,"用户"聚合行为)
@Test
void after_set_openid_should_get_openid_correctly() {
    // given: 准备好openid,并创建user对象
    WxOpenId openId = WxOpenId.of("o9Nvx4gUq9dfO1KQy7LL-gXS_EkI");
    User user = User.of("testUser", 1);
    // when: 为user对象赋值openId
    user.setOpenid(openId);
    // then: 新用户的OpenId等于原先的OpenId
    assertEquals(user.getOpenid(), openId);
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with User

use of com.stardata.starshop2.authcontext.domain.user.User in project starshop by beautautumn.

the class StarshopAuthLoginTests method should_update_token_to_different_if_refresh_twice_for_given_user.

// 2.2. 创建用户登录令牌;(相关任务,用户聚合行为,用户原来有令牌,更新令牌)
@Test
void should_update_token_to_different_if_refresh_twice_for_given_user() {
    // given: 一个新用户
    User newUser = User.of("testUser", 1);
    String sessionKey = "testSessionKey";
    // when: 调用User.refreshLoginToken创建两次令牌
    UserToken tokenOld = newUser.refreshLoginToken(sessionKey);
    UserToken tokenNew = newUser.refreshLoginToken(sessionKey);
    // then: 两次用户令牌不一样
    assertNotNull(tokenOld);
    assertNotNull(tokenNew);
    assertNotNull(newUser.currentToken());
    assertNotEquals(tokenOld.getToken(), tokenNew.getToken());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) UserToken(com.stardata.starshop2.authcontext.domain.user.UserToken) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with User

use of com.stardata.starshop2.authcontext.domain.user.User in project starshop by beautautumn.

the class StarshopAuthLoginTests method should_load_exists_user_correctly_by_openid.

// 1.3. 根据openid查找用户记录;(相关任务,资源库端口,访问数据库)
@Test
@Transactional
@Rollback(true)
void should_load_exists_user_correctly_by_openid() {
    // given: 创建新用户,并设置其openid后保存
    WxOpenId openId = WxOpenId.of("testOpenIdX");
    User user = User.of("testUserX", 1);
    user.setOpenid(openId);
    userRepository.add(user);
    // when: 向资源库加入该用户,并重新从资源库根据openid重建用户对象
    User loadedUser = userRepository.findByOpenId(openId);
    // then: 重建的用户对象存在且OpenId等于给定的openid
    assertNotNull(loadedUser);
    assertEquals(openId, loadedUser.getOpenid());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with User

use of com.stardata.starshop2.authcontext.domain.user.User in project starshop by beautautumn.

the class WxLoginWithTokenService method loginWithToken.

public User loginWithToken(String code, WxAuthInfo wxAuthInfo, User loginUser) {
    WxOpenId openid = wxLoginService.wxLogin(code, wxAuthInfo);
    User user = userExistenceService.ensureUser(openid, loginUser);
    userTokenService.generateLoginToken(user, wxAuthInfo.getSessionKey());
    return user;
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId)

Example 25 with User

use of com.stardata.starshop2.authcontext.domain.user.User in project starshop by beautautumn.

the class LoginLogService method recordLogin.

public LoginLog recordLogin(User user, String loginIp) {
    LoginLog log = LoginLog.createFor(user, loginIp);
    logRepository.add(log);
    return log;
}
Also used : LoginLog(com.stardata.starshop2.authcontext.domain.loginlog.LoginLog)

Aggregations

User (com.stardata.starshop2.authcontext.domain.user.User)25 Test (org.junit.jupiter.api.Test)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)21 Rollback (org.springframework.test.annotation.Rollback)17 Transactional (org.springframework.transaction.annotation.Transactional)13 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)10 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)8 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)6 LocalDateTime (java.time.LocalDateTime)6 UserResponse (com.stardata.starshop2.authcontext.pl.UserResponse)5 WxLoginRequest (com.stardata.starshop2.authcontext.pl.WxLoginRequest)5 SessionUser (com.stardata.starshop2.sharedcontext.pl.SessionUser)5 MobileNumber (com.stardata.starshop2.sharedcontext.domain.MobileNumber)4 Transactional (jakarta.transaction.Transactional)4 LongIdentity (com.stardata.starshop2.sharedcontext.domain.LongIdentity)3 ApplicationValidationException (com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException)3 LoginLog (com.stardata.starshop2.authcontext.domain.loginlog.LoginLog)2 MobileNumberResponse (com.stardata.starshop2.authcontext.pl.MobileNumberResponse)1 WxEncryptedUserInfo (com.stardata.starshop2.authcontext.pl.WxEncryptedUserInfo)1