Search in sources :

Example 1 with WxOpenId

use of com.stardata.starshop2.authcontext.domain.user.WxOpenId 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 2 with WxOpenId

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

the class StarshopAuthLoginTests method should_wx_login_success_given_by_valid_and_unused_code.

// 3.1. 微信后台登录并校验;(组合任务,领域服务,微信code有效且未被使用过)
@Test
void should_wx_login_success_given_by_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);
    // when: 调用微信登录服务 WxLoginService.wxLogin
    WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
    // then: 返回有效的openid
    assertNotNull(wxOpenId);
    assertFalse(StringUtils.isBlank(wxOpenId.toString()));
}
Also used : WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with WxOpenId

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

the class StarshopAuthLoginTests method should_create_and_save_login_log_correctly_given_login_log.

// 5.1. 记录用户登录日志;(组合任务,领域服务)
@Test
@Transactional
@Rollback(true)
void should_create_and_save_login_log_correctly_given_login_log() {
    // given:
    WxOpenId openId = WxOpenId.of("testOpenId");
    User user = User.of("testUser", 2).avatarUrl("testUrl").country("testCountry").province("testProvince").city("testCity").language("testLanguage");
    user.setOpenid(openId);
    userRepository.add(user);
    // when: 调用 loginLogService.recordLogin
    LoginLog loginLog = loginLogService.recordLogin(user, "testIp");
    entityManager.flush();
    LoginLog loadedLog = loginLogRepository.instanceOf(loginLog.getId());
    // then: 创建了有效的登录日志
    assertNotNull(loadedLog);
    assertNotNull(loadedLog.getLastLoginTime());
    LocalDateTime now = LocalDateTime.now();
    assertTrue(loadedLog.getLastLoginTime().isBefore(now.plusSeconds(1)));
}
Also used : LocalDateTime(java.time.LocalDateTime) LoginLog(com.stardata.starshop2.authcontext.domain.loginlog.LoginLog) 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 4 with WxOpenId

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

the class StarshopAuthLoginTests method should_wx_login_login_biz_exception_given_valid_and_used_code.

// 3.3. 微信后台登录并校验;(组合任务,领域服务,微信code有效但已被使用过)
@Test
void should_wx_login_login_biz_exception_given_valid_and_used_code() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature
    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";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
        // 正常情况下不应该执行到这儿
        assertEquals(0, 1);
    } catch (WxLoginErrorException e) {
        // then: 抛出"微信登录错误"异常
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
        assertNotNull(e);
    }
}
Also used : WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with WxOpenId

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

the class StarshopAuthLoginTests method should_wx_login_by_invalid_code_given_login_biz_exception.

// 3.4. 微信后台登录并校验;(组合任务,领域服务,微信code无效)
@Test
void should_wx_login_by_invalid_code_given_login_biz_exception() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature
    String code = "testCode";
    String rawData = "testRawData";
    String signature = "testSignature";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
        // 正常情况下不应该执行到这儿
        assertEquals(0, 1);
    } catch (WxLoginErrorException e) {
        // then: 抛出"微信登录错误"异常
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
        assertNotNull(e);
    }
}
Also used : WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)12 Test (org.junit.jupiter.api.Test)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 User (com.stardata.starshop2.authcontext.domain.user.User)9 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)6 Rollback (org.springframework.test.annotation.Rollback)6 Transactional (org.springframework.transaction.annotation.Transactional)6 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)2 LoginLog (com.stardata.starshop2.authcontext.domain.loginlog.LoginLog)1 LocalDateTime (java.time.LocalDateTime)1