“mitao”
2026-04-27 69c2c400e6e3393424d704742cd9de0e6da07e2f
tcm-training-admin-backend/src/test/java/com/tcm/training/admin/app/auth/service/AppAuthServiceTest.java
@@ -5,6 +5,7 @@
import com.tcm.training.admin.app.auth.dto.AppPasswordLoginRequest;
import com.tcm.training.admin.app.auth.dto.AppPasswordResetRequest;
import com.tcm.training.admin.app.auth.dto.AppPasswordResetVerifyRequest;
import com.tcm.training.admin.app.auth.dto.AppAppleLoginRequest;
import com.tcm.training.admin.app.auth.dto.AppSmsCodeSendRequest;
import com.tcm.training.admin.app.auth.dto.AppWechatLoginRequest;
import com.tcm.training.admin.app.auth.vo.AppLoginResponse;
@@ -68,6 +69,9 @@
    private AppWechatAuthClient appWechatAuthClient;
    @Mock
    private AppAppleAuthClient appAppleAuthClient;
    @Mock
    private StringRedisTemplate stringRedisTemplate;
    @Mock
@@ -89,6 +93,7 @@
                passwordEncoder,
                appAuthSessionService,
                appWechatAuthClient,
                appAppleAuthClient,
                stringRedisTemplate
        );
    }
@@ -295,6 +300,92 @@
    }
    /**
     * 苹果已绑定用户登录时应更新苹果绑定信息并返回统一登录响应。
     */
    @Test
    void appleLoginShouldReuseExistingUser() {
        AppAppleLoginRequest request = new AppAppleLoginRequest();
        request.setIdentityToken("identity-token");
        request.setUserIdentifier("apple-user-1");
        AppAppleUserProfile profile = new AppAppleUserProfile();
        profile.setSubject("apple-subject-1");
        profile.setUserIdentifier("apple-user-1");
        AppUser user = buildEnabledUser("13800138009", "ENCODED");
        user.setAppleIdentitySubject("apple-subject-1");
        user.setAppleUserIdentifier("apple-user-legacy");
        user.setNickname("已存在用户");
        SaTokenInfo tokenInfo = new SaTokenInfo();
        tokenInfo.setTokenValue("apple-token");
        tokenInfo.setTokenTimeout(7200L);
        when(appAppleAuthClient.resolveUserProfile(request)).thenReturn(profile);
        when(userMapper.selectOne(any())).thenReturn(user);
        when(appAuthSessionService.getTokenInfo()).thenReturn(tokenInfo);
        AppLoginResponse response = appAuthService.appleLogin(request);
        verify(userMapper).updateById(appUserCaptor.capture());
        verify(userMapper, never()).insert(any());
        assertEquals("apple-subject-1", appUserCaptor.getValue().getAppleIdentitySubject());
        assertEquals("apple-user-1", appUserCaptor.getValue().getAppleUserIdentifier());
        assertEquals("已存在用户", response.getUser().getNickname());
        assertEquals("apple-token", response.getToken());
    }
    /**
     * 苹果首次登录时应自动创建普通用户并等待后续绑定手机号。
     */
    @Test
    void appleLoginShouldCreateUserWhenSubjectNotExists() {
        AppAppleLoginRequest request = new AppAppleLoginRequest();
        request.setIdentityToken("identity-token");
        request.setUserIdentifier("apple-user-new");
        AppAppleUserProfile profile = new AppAppleUserProfile();
        profile.setSubject("apple-subject-new");
        profile.setUserIdentifier("apple-user-new");
        SaTokenInfo tokenInfo = new SaTokenInfo();
        tokenInfo.setTokenValue("apple-new-token");
        tokenInfo.setTokenTimeout(7200L);
        when(appAppleAuthClient.resolveUserProfile(request)).thenReturn(profile);
        when(userMapper.selectOne(any())).thenReturn(null);
        when(appAuthSessionService.getTokenInfo()).thenReturn(tokenInfo);
        AppLoginResponse response = appAuthService.appleLogin(request);
        verify(userMapper).insert(appUserCaptor.capture());
        verify(userMapper, never()).updateById(any());
        assertNull(appUserCaptor.getValue().getPhone());
        assertEquals("apple-subject-new", appUserCaptor.getValue().getAppleIdentitySubject());
        assertEquals("apple-user-new", appUserCaptor.getValue().getAppleUserIdentifier());
        assertEquals("苹果用户", appUserCaptor.getValue().getNickname());
        assertEquals(Integer.valueOf(0), appUserCaptor.getValue().getType());
        assertEquals("apple-new-token", response.getToken());
    }
    /**
     * 苹果授权无效时应直接拒绝登录。
     */
    @Test
    void appleLoginShouldFailWhenIdentityTokenInvalid() {
        AppAppleLoginRequest request = new AppAppleLoginRequest();
        request.setIdentityToken("bad-token");
        request.setUserIdentifier("apple-user-bad");
        when(appAppleAuthClient.resolveUserProfile(request)).thenThrow(new IllegalArgumentException("苹果登录授权失败"));
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
                () -> appAuthService.appleLogin(request));
        assertEquals("苹果登录授权失败", exception.getMessage());
    }
    /**
     * 统一构造启用中的 APP 用户测试桩。
     */
    private AppUser buildEnabledUser(String phone, String password) {