Search in sources :

Example 1 with User

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

the class StarshopAuthDecryptingTests method should_get_mobile_phone_correctly_given_exists_userid_and_correct_encrypted_data_iv_by_domain_service.

// 1.1. 正常的已有用户、加密数据encryptedData、iv,成功解密手机号,并更新用户对象手机号
@Test
@Transactional
@Rollback(true)
void should_get_mobile_phone_correctly_given_exists_userid_and_correct_encrypted_data_iv_by_domain_service() {
    // given: 正常的已有用户、加密数据encryptedData、iv
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = user.getId();
    String encryptedData = "NkSHcnyy8jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    // when: 解密手机号
    MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
    userRepository.update(user);
    entityManager.flush();
    User loadedUser = userRepository.instanceOf(userId);
    // then: 判定解密手机号是否正确
    assertNotNull(mobileNumber);
    assertEquals(mobileNumber.value(), "18652012976");
    assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 2 with User

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

the class StarshopAuthDecryptingTests method should_throw_exception_given_not_exists_user_and_correct_encrypted_data_iv_by_domain_service.

// 1.2. 系统中不存在该用户,解密报业务异常 ApplicationValidationException
@Test
@Transactional
@Rollback(true)
void should_throw_exception_given_not_exists_user_and_correct_encrypted_data_iv_by_domain_service() {
    // given: 正确的加密数据encryptedData、iv,不存在的userId
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = LongIdentity.snowflakeId();
    String encryptedData = "NkSHcnyy8jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    try {
        // when: 解密手机号
        MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
        // 下面这些不应该被执行到
        userRepository.update(user);
        entityManager.flush();
        User loadedUser = userRepository.instanceOf(userId);
        // then: 判定解密手机号是否正确
        assertNotNull(mobileNumber);
        assertEquals(mobileNumber.value(), "18652012976");
        assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
    } catch (ApplicationValidationException e) {
        assertNotNull(e);
        assertEquals(e.getErrCode(), ApplicationValidationException.INVALID_REQUEST_ENTITY);
    }
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) ApplicationValidationException(com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 3 with User

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

the class StarshopAuthDecryptingTests method should_throw_exception_given_exists_user_and_incorrect_encrypted_data_iv_by_domain_service.

// 1.3. 加密数据encryptedData、iv内容不合格,解密报业务异常 ApplicationValidationException
@Test
@Transactional
@Rollback(true)
void should_throw_exception_given_exists_user_and_incorrect_encrypted_data_iv_by_domain_service() {
    // given: 正常的已有用户,不正确的加密数据encryptedData、iv
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = user.getId();
    String encryptedData = "NkSHcnyy88jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    try {
        // when: 解密手机号
        MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
        // 下面这些不应该被执行到
        userRepository.update(user);
        entityManager.flush();
        User loadedUser = userRepository.instanceOf(userId);
        // then: 判定解密手机号是否正确
        assertNotNull(mobileNumber);
        assertEquals(mobileNumber.value(), "18652012976");
        assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
    } catch (ApplicationValidationException e) {
        assertNotNull(e);
        assertEquals(e.getErrCode(), ApplicationValidationException.INVALID_REQUEST_DATA);
    }
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) ApplicationValidationException(com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 4 with User

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

the class StarshopAuthLoginTests method should_update_wx_user_info_correctly_for_exists_user_by_openid.

// 1.4. 确保前端微信用户信息对应的记录存在;(对应openid的用户存在,根据微信前端用户信息更新用户);
@Test
@Transactional
@Rollback(true)
void should_update_wx_user_info_correctly_for_exists_user_by_openid() {
    // given: 已有微信用户信息、openid、并根据该openid在系统中插入用户记录
    WxOpenId openId = WxOpenId.of("testOpenId");
    User existsUser = User.of("testUserX", 1).avatarUrl("testUrlX").country("testCountryX").province("testProvinceX").city("testCityX").language("testLanguageX");
    existsUser.setOpenid(openId);
    userRepository.add(existsUser);
    User frontUser = User.of("testUserY", 2).avatarUrl("testUrlY").country("testCountryY").province("testProvinceY").city("testCityY").language("testLanguageY");
    frontUser.setOpenid(openId);
    // when: 调用领域服务 UserExistenceService.ensureUser 更新用户信息,然后从db重建用户对象
    User user = userExistenceService.ensureUser(openId, frontUser);
    User loadedUser = userRepository.instanceOf(user.getId());
    // then: 返回用户对象的所有前端小程序可传入属性字段,与给定的用户信息完全相同
    assertNotNull(loadedUser);
    assertTrue(isSameMiniAppUserInfo(loadedUser, frontUser));
}
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 5 with User

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

the class StarshopAuthLoginTests method should_update_user_token_to_different_given_token_updated.

// 2.4. 生成用户登录令牌;(组合任务,领域服务,老用户更新令牌后保存,再重新加载后令牌正确)
@Test
@Transactional
@Rollback(true)
void should_update_user_token_to_different_given_token_updated() {
    // given: 创建一个用户及其登录令牌保存到db
    String sessionKey = "testSessionKey";
    User user = User.of("testUser", 1);
    user.refreshLoginToken(sessionKey);
    userRepository.add(user);
    entityManager.flush();
    String oldToken = user.currentToken().getToken();
    // when: 调用User.refreshLoginToken更新令牌,并持久化用户对象后再重建
    user.refreshLoginToken(sessionKey);
    userRepository.update(user);
    entityManager.flush();
    User loadedUser = userRepository.instanceOf(user.getId());
    // then: 用户令牌被保存,且内容正确
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.currentToken());
    assertNotEquals(loadedUser.currentToken().getToken(), oldToken);
    logger.info("oldToken: " + oldToken);
    logger.info("newToken: " + loadedUser.currentToken().getToken());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) 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)

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