Search in sources :

Example 11 with User

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

the class StarshopAuthLoginTests method should_create_new_token_correctly_for_given_user.

/**
 * 任务级测试:微信用户登录——2.生成用户登录令牌;(组合任务,领域服务)
 * 按照先聚合再端口、先原子再组合、从内向外的原则。
 * 设计相关任务级测试案例包括:
 * 2.1. 创建用户登录令牌;(相关任务,用户聚合行为,用户原来无令牌,创建新令牌)
 * 2.2. 创建用户登录令牌;(相关任务,用户聚合行为,用户原来有令牌,更新令牌)
 * 2.3. 生成用户登录令牌;(组合任务,领域服务,保存令牌后重建用户对象,令牌正确)
 * 2.4. 生成用户登录令牌;(组合任务,领域服务,老用户更新令牌后保存,再重新加载后令牌已更新)
 */
// 2.1. 创建用户登录令牌;(相关任务,用户聚合行为,用户原来无令牌,创建新令牌)
@Test
void should_create_new_token_correctly_for_given_user() {
    // given: 一个新用户
    User newUser = User.of("testUser", 1);
    String sessionKey = "testSessionKey";
    // when: 调用User.refreshLoginToken创建令牌
    UserToken token = newUser.refreshLoginToken(sessionKey);
    // then: 有效的用户令牌
    assertNotNull(token);
    assertNotNull(newUser.currentToken());
    assertFalse(StringUtils.isBlank(token.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 12 with User

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

the class MobileNumberDecryptingService method decryptWxMobileNumber.

public MobileNumber decryptWxMobileNumber(LongIdentity userId, String encryptedData, String iv) {
    User user = repository.instanceOf(userId);
    if (user == null) {
        throw new ApplicationValidationException(ApplicationValidationException.INVALID_REQUEST_ENTITY, "The user is not exists.");
    }
    MobileNumber mobileNumber = decryptingClient.decryptMobileNumber(user.currentToken(), encryptedData, iv);
    user.updateMobileNumber(mobileNumber);
    repository.update(user);
    return mobileNumber;
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) User(com.stardata.starshop2.authcontext.domain.user.User) ApplicationValidationException(com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException)

Example 13 with User

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

the class AuthAppService method loginByWx.

public UserResponse loginByWx(@NotNull WxLoginRequest request) {
    String code = request.getCode();
    WxAuthInfo wxAuthInfo = request.getWxAuthInfo();
    User loginUser = request.getRequestUser();
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, loginUser);
    logService.recordLogin(user, request.getRequestIp());
    return UserResponse.from(user);
}
Also used : SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)

Example 14 with User

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

the class StarshopAuthLoginTests method app_service_should_login_exception_given_valid_and_used_code.

// 6.4 正确的微信code,但已过期,要求返回业务失败
@Test
@Transactional
@Rollback(true)
void app_service_should_login_exception_given_valid_and_used_code() {
    // given: 准备好输入数据
    String code = "091FHp0w3KT3yY2VjV1w3aMiIB3FHp0B";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "af36606701965ce329c2613b91f801d2c07b332d";
    WxLoginRequest request = new WxLoginRequest();
    request.setCode(code);
    request.setRequestIp("testLoginIp");
    request.setRawData(rawData);
    request.setSignature(signature);
    request.setNickName("testUser1");
    request.setGender(1);
    request.setAvatarUrl("https://www.somehost.com/someAvatar.png");
    request.setCountry("中国");
    request.setProvince("江苏");
    request.setCity("南京");
    request.setLanguage("zh_CN");
    try {
        // when: 执行authAppService.loginByWx方法调用
        UserResponse response = authAppService.loginByWx(request);
        // 如下这些应该不被执行到
        assertNotNull(response);
        assertNotNull(response.getId());
        assertNotNull(response.getToken());
        User loadedUser = userRepository.instanceOf(LongIdentity.from(response.getId()));
        User frontUser = User.of("testUser1", 1).avatarUrl("https://www.somehost.com/someAvatar.png").country("中国").province("江苏").city("南京").language("zh_CN");
        assertTrue(isSameMiniAppUserInfo(loadedUser, frontUser));
        UserToken userToken = loadedUser.currentToken();
        LocalDateTime now = LocalDateTime.now();
        userToken.getExpireTime().isAfter(now.minusMinutes(1).plusHours(72));
    } catch (WxLoginErrorException e) {
        // then: 抛出"Checking userinfo integrity failed"异常
        assertNotNull(e);
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
    }
}
Also used : UserResponse(com.stardata.starshop2.authcontext.pl.UserResponse) LocalDateTime(java.time.LocalDateTime) User(com.stardata.starshop2.authcontext.domain.user.User) WxLoginRequest(com.stardata.starshop2.authcontext.pl.WxLoginRequest) UserToken(com.stardata.starshop2.authcontext.domain.user.UserToken) 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 15 with User

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

the class StarshopAuthLoginTests method after_copy_user1_wxinfo_to_user2_should_equal_values_except_id.

/**
 * 任务级测试:微信用户登录——1. 确保用户记录存在;(组合任务,领域服务)
 * 按照先聚合再端口、先原子再组合、从内向外的原则。
 * 设计相关任务级测试案例包括:
 * 1.1. 针对微信前端小程序获得用户信息字段,更新已有用户信息(昵称、头像等);(相关任务,"用户"聚合行为)
 * 1.2. 设置新用户openid;(相关任务,"用户"聚合行为)
 * 1.3. 根据openid查找用户记录;(相关任务,资源库端口,访问数据库)
 * 1.4. 确保前端微信用户信息对应的记录存在;(对应openid的用户存在,根据微信前端用户信息更新用户);
 * 1.5. 确保前端微信用户信息对应的记录存在;(对应openid的用户不存在,根据微信前端用户信息新建用户);
 */
// 1.1. 针对微信前端小程序获得用户信息字段,更新已有用户信息(昵称、头像等);(相关任务,"用户"聚合行为)
@Test
void after_copy_user1_wxinfo_to_user2_should_equal_values_except_id() {
    // given: 准备好user1对象并给除ID之外所有属性赋值、以及空的user2对象
    User user1 = User.of("testUser1", 1).avatarUrl("https://www.somehost.com/someAvatar.png").country("中国").province("江苏").city("南京").language("zh_CN");
    User user2 = User.of("testUser2", 2);
    // when: 调用user2的copyInfoFrom方法
    user2.copyMiniAppInfoFrom(user1);
    // then: 检查user1和user2的相应属性是否相等
    assertEquals(user1.getNickName(), user2.getNickName());
    assertEquals(user1.getGender(), user2.getGender());
    assertEquals(user1.getAvatarUrl(), user2.getAvatarUrl());
    assertEquals(user1.getCountry(), user2.getCountry());
    assertEquals(user1.getProvince(), user2.getProvince());
    assertEquals(user1.getCity(), user2.getCity());
    assertEquals(user1.getLanguage(), user2.getLanguage());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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