Search in sources :

Example 6 with UserToken

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

the class StarshopAuthLoginTests method app_service_should_login_exception_given_valid_and_used_code.

// 6.4 正确的微信code,但已过期,要求返回业务失败
@Test
@Transactional
@Rollback(true)
void app_service_should_login_exception_given_valid_and_used_code() {
    // given: 准备好输入数据
    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";
    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);
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
    }
}
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 7 with UserToken

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

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

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

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

the class StarshopAuthLoginTests method should_update_token_to_different_if_refresh_twice_for_given_user.

// 2.2. 创建用户登录令牌;(相关任务,用户聚合行为,用户原来有令牌,更新令牌)
@Test
void should_update_token_to_different_if_refresh_twice_for_given_user() {
    // given: 一个新用户
    User newUser = User.of("testUser", 1);
    String sessionKey = "testSessionKey";
    // when: 调用User.refreshLoginToken创建两次令牌
    UserToken tokenOld = newUser.refreshLoginToken(sessionKey);
    UserToken tokenNew = newUser.refreshLoginToken(sessionKey);
    // then: 两次用户令牌不一样
    assertNotNull(tokenOld);
    assertNotNull(tokenNew);
    assertNotNull(newUser.currentToken());
    assertNotEquals(tokenOld.getToken(), tokenNew.getToken());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) UserToken(com.stardata.starshop2.authcontext.domain.user.UserToken) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.stardata.starshop2.authcontext.domain.user.User)10 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)10 Test (org.junit.jupiter.api.Test)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 Rollback (org.springframework.test.annotation.Rollback)8 Transactional (org.springframework.transaction.annotation.Transactional)8 UserResponse (com.stardata.starshop2.authcontext.pl.UserResponse)5 WxLoginRequest (com.stardata.starshop2.authcontext.pl.WxLoginRequest)5 LocalDateTime (java.time.LocalDateTime)5 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)2 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)2