Search in sources :

Example 11 with WxAuthInfo

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

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

the class WxLoginWithTokenService method loginWithToken.

public User loginWithToken(String code, WxAuthInfo wxAuthInfo, User loginUser) {
    WxOpenId openid = wxLoginService.wxLogin(code, wxAuthInfo);
    User user = userExistenceService.ensureUser(openid, loginUser);
    userTokenService.generateLoginToken(user, wxAuthInfo.getSessionKey());
    return user;
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId)

Aggregations

WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)10 Test (org.junit.jupiter.api.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (com.stardata.starshop2.authcontext.domain.user.User)7 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)7 Rollback (org.springframework.test.annotation.Rollback)5 SessionUser (com.stardata.starshop2.sharedcontext.pl.SessionUser)4 LongIdentity (com.stardata.starshop2.sharedcontext.domain.LongIdentity)3 MobileNumber (com.stardata.starshop2.sharedcontext.domain.MobileNumber)3 Transactional (jakarta.transaction.Transactional)3 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)2 ApplicationValidationException (com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 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