Search in sources :

Example 6 with WxLoginErrorException

use of com.stardata.starshop2.authcontext.domain.WxLoginErrorException 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)

Example 7 with WxLoginErrorException

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

the class StarshopAuthLoginTests method should_wx_login_exception_given_valid_and_unused_code_but_signature_error.

// 3.2. 微信后台登录并校验;(组合任务,领域服务,微信code有效且未被使用过,但signature 不正确)
@Test
void should_wx_login_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);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
    } catch (WxLoginErrorException e) {
        // then: 抛出"Checking userinfo integrity failed"异常
        assertNotNull(e);
        assertEquals(e.getErrCode(), -99);
    }
}
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 8 with WxLoginErrorException

use of com.stardata.starshop2.authcontext.domain.WxLoginErrorException 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)

Aggregations

Test (org.junit.jupiter.api.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 User (com.stardata.starshop2.authcontext.domain.user.User)4 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)4 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)4 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)4 Rollback (org.springframework.test.annotation.Rollback)4 Transactional (org.springframework.transaction.annotation.Transactional)4 UserResponse (com.stardata.starshop2.authcontext.pl.UserResponse)3 WxLoginRequest (com.stardata.starshop2.authcontext.pl.WxLoginRequest)3 LocalDateTime (java.time.LocalDateTime)3 WxMaService (cn.binarywang.wx.miniapp.api.WxMaService)1 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)1 WxLoginErrorException (com.stardata.starshop2.authcontext.domain.WxLoginErrorException)1 WxError (me.chanjar.weixin.common.error.WxError)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1