Search in sources :

Example 6 with User

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

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

the class StarshopAuthLoginTests method app_service_should_login_exception_given_invalid_and_used_code.

// 6.5 错误的微信code,要求返回无效请求告警
@Test
@Transactional
@Rollback(true)
void app_service_should_login_exception_given_invalid_and_used_code() {
    // given: 准备好输入数据
    String code = "testCode";
    String rawData = "testRawData";
    String signature = "testSignature";
    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 8 with User

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

the class StarshopAuthLoginTests method should_save_token_correctly_for_given_user.

// 2.3. 生成用户登录令牌;(组合任务,领域服务,新用户创建令牌后保存,再重新加载后令牌正确)
@Test
@Transactional
@Rollback(true)
void should_save_token_correctly_for_given_user() {
    // given: 一个新用户
    User newUser = User.of("testUser", 1);
    String sessionKey = "testSessionKey";
    // when: 调用User.refreshLoginToken创建令牌,并持久化用户对象后再重建
    UserToken token = newUser.refreshLoginToken(sessionKey);
    userRepository.add(newUser);
    User loadedUser = userRepository.instanceOf(newUser.getId());
    // then: 用户令牌被保存,且内容正确
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.currentToken());
    assertEquals(loadedUser.currentToken(), token);
}
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) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with User

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

the class StarshopAuthLoginTests method app_service_should_login_success_given_new_user_valid_code_and_encrypt_info.

// 6.1 正确的微信code,且未过期,该用户为新用户,要求返回72小时内有效的新token
@Test
@Transactional
@Rollback(true)
void app_service_should_login_success_given_new_user_valid_code_and_encrypt_info() {
    // 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");
    // when: 执行authAppService.loginByWx方法调用
    UserResponse response = authAppService.loginByWx(request);
    // then: 检查调用结果是否正确
    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));
}
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 10 with User

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

the class StarshopAuthLoginTests method app_service_should_login_success_given_old_user_valid_code_and_encrypt_info.

// 6.2 正确的微信code,且未过期,该用户为老用户,要求返回72小时内有效的新token
@Test
@Transactional
@Rollback(true)
void app_service_should_login_success_given_old_user_valid_code_and_encrypt_info() {
    // given: 准备好输入数据
    User user = User.of("testUser", 1).avatarUrl("testUrl").country("testCountry").province("testProvince").city("testCity").language("testLanguage");
    user.setOpenid(WxOpenId.of("oVsAw5cdcnIxaae-x98ShoH93Hu0"));
    userRepository.add(user);
    entityManager.flush();
    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");
    // when: 执行authAppService.loginByWx方法调用
    UserResponse response = authAppService.loginByWx(request);
    // then: 检查调用结果是否正确
    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));
}
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)

Aggregations

User (com.stardata.starshop2.authcontext.domain.user.User)25 Test (org.junit.jupiter.api.Test)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)21 Rollback (org.springframework.test.annotation.Rollback)17 Transactional (org.springframework.transaction.annotation.Transactional)13 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)10 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)8 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)6 LocalDateTime (java.time.LocalDateTime)6 UserResponse (com.stardata.starshop2.authcontext.pl.UserResponse)5 WxLoginRequest (com.stardata.starshop2.authcontext.pl.WxLoginRequest)5 SessionUser (com.stardata.starshop2.sharedcontext.pl.SessionUser)5 MobileNumber (com.stardata.starshop2.sharedcontext.domain.MobileNumber)4 Transactional (jakarta.transaction.Transactional)4 LongIdentity (com.stardata.starshop2.sharedcontext.domain.LongIdentity)3 ApplicationValidationException (com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException)3 LoginLog (com.stardata.starshop2.authcontext.domain.loginlog.LoginLog)2 MobileNumberResponse (com.stardata.starshop2.authcontext.pl.MobileNumberResponse)1 WxEncryptedUserInfo (com.stardata.starshop2.authcontext.pl.WxEncryptedUserInfo)1