Search in sources :

Example 6 with WxOpenId

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

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

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

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

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

the class StarshopAuthLoginTests method after_set_openid_should_get_openid_correctly.

// 1.2. 设置新用户openid;(相关任务,"用户"聚合行为)
@Test
void after_set_openid_should_get_openid_correctly() {
    // given: 准备好openid,并创建user对象
    WxOpenId openId = WxOpenId.of("o9Nvx4gUq9dfO1KQy7LL-gXS_EkI");
    User user = User.of("testUser", 1);
    // when: 为user对象赋值openId
    user.setOpenid(openId);
    // then: 新用户的OpenId等于原先的OpenId
    assertEquals(user.getOpenid(), openId);
}
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)

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