Search in sources :

Example 16 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_new_user_valid_code_but_signature_error.

// 6.3 正确的微信code,且未过期,但用户信息完整性被破坏,要求返回业务失败
@Test
@Transactional
@Rollback(true)
void app_service_should_login_exception_given_new_user_valid_code_but_signature_error() {
    // given: 准备好输入数据
    String code = "081sloll28T8d94nl8nl2hxKhh3slolv";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "06846a4ba8b003af5d98fadfaf376a652e5d75d7";
    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);
        assertEquals(e.getErrCode(), -99);
    }
}
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 17 with User

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

the class StarshopAuthLoginTests method should_wx_login_with_token_exception_given_valid_and_unused_code_but_signature_error.

// 4.2. 微信登录异常,未生成微信登录令牌;(组合任务,领域服务)
@Test
@Transactional
@Rollback(true)
void should_wx_login_with_token_exception_given_valid_and_unused_code_but_signature_error() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature、有效的用户信息
    String code = "081sloll28T8d94nl8nl2hxKhh3slolv";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "06846a4ba8b003af5d98fadfaf376a652e5d75d8";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User frontUser = User.of("testUser", 2).avatarUrl("testUrl").country("testCountry").province("testProvince").city("testCity").language("testLanguage");
    try {
        // when: 调用wxLoginWithTokenService.loginWithToken()
        User loginUser = wxLoginWithTokenService.loginWithToken(code, wxAuthInfo, frontUser);
        // 如下代码应该不被执行
        UserToken token = loginUser.currentToken();
        WxOpenId openid = loginUser.getOpenid();
        assertNotNull(token);
        assertFalse(StringUtils.isBlank(token.getToken()));
        assertNotNull(openid);
        assertFalse(StringUtils.isBlank(openid.toString()));
    } catch (WxLoginErrorException e) {
        // then: 有效的用户令牌
        // then: 抛出"Checking userinfo integrity failed"异常
        assertNotNull(e);
        assertEquals(e.getErrCode(), -99);
    }
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) 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 18 with User

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

the class StarshopAuthLoginTests method should_wx_login_with_token_success_given_login_valid_and_unused_code_.

/**
 * 任务级测试:微信用户登录——4.生成微信登录令牌;(组合任务,领域服务)
 * 按照先聚合再端口、先原子再组合、从内向外的原则。
 * 设计相关任务级测试案例包括:
 * 4.1. 微信登录成功,生成登录令牌;(组合任务,领域服务)
 * 4.2. 微信登录异常,未生成微信登录令牌;(组合任务,领域服务)
 */
// 4.1. 微信登录成功,生成登录令牌;(组合任务,领域服务)
@Test
@Transactional
@Rollback(true)
void should_wx_login_with_token_success_given_login_valid_and_unused_code_() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature、有效的用户信息
    String code = "081sloll28T8d94nl8nl2hxKhh3slolv";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "06846a4ba8b003af5d98fadfaf376a652e5d75d7";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User frontUser = User.of("testUser", 2).avatarUrl("testUrl").country("testCountry").province("testProvince").city("testCity").language("testLanguage");
    // when: 调用wxLoginWithTokenService.loginWithToken()
    User loginUser = wxLoginWithTokenService.loginWithToken(code, wxAuthInfo, frontUser);
    // then: 有效的用户令牌
    UserToken token = loginUser.currentToken();
    WxOpenId openid = loginUser.getOpenid();
    assertNotNull(token);
    assertFalse(StringUtils.isBlank(token.getToken()));
    assertNotNull(openid);
    assertFalse(StringUtils.isBlank(openid.toString()));
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) 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 19 with User

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

the class StarshopAuthLoginTests method should_create_new_user_for_not_exists_user_by_openid.

// 1.5. 确保前端微信用户信息对应的记录存在;(对应openid的用户不存在,根据微信前端用户信息新建用户);
@Test
@Transactional
@Rollback(true)
void should_create_new_user_for_not_exists_user_by_openid() {
    // given: 已有openid,但db中没有对应的User对象
    WxOpenId openId = WxOpenId.of("testOpenId");
    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 20 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.

// 2.1. 正常的已有用户、加密数据encryptedData、iv,成功解密手机号,并更新用户对象手机号
@Test
@Transactional
@Rollback(true)
void should_get_mobile_phone_correctly_given_exists_userid_and_correct_encrypted_data_iv() {
    // given: 正常的已有用户、加密数据encryptedData、iv
    User existsUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    existsUser.refreshLoginToken("EfUbtTDsTz/S3lSvdkx2jg==");
    userRepository.add(existsUser);
    Long userId = existsUser.getId().value();
    SessionUser sessionUser = new SessionUser();
    sessionUser.setId(userId);
    String encryptedData = "NkSHcnyy8jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    WxEncryptedUserInfo wxEncryptedUserInfo = new WxEncryptedUserInfo();
    wxEncryptedUserInfo.setEncryptedData(encryptedData);
    wxEncryptedUserInfo.setIv(iv);
    // when: 解密手机号
    MobileNumberResponse response = authAppService.decryptWxMobileNumber(sessionUser, wxEncryptedUserInfo);
    User loadedUser = userRepository.instanceOf(LongIdentity.from(userId));
    // then: 判定解密手机号是否正确
    assertNotNull(response.getMobileNumber());
    assertEquals(response.getMobileNumber(), "18652012976");
    assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
}
Also used : WxEncryptedUserInfo(com.stardata.starshop2.authcontext.pl.WxEncryptedUserInfo) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) MobileNumberResponse(com.stardata.starshop2.authcontext.pl.MobileNumberResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.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