liujie
2026-09-17 825a575deb96cce822468bb9d9ac3e4eea0f91d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.tourguide.modules.user.service;
 
import com.tourguide.modules.operation.mapper.OperationCityMapper;
import com.tourguide.modules.operation.mapper.OperationScenicSpotMapper;
import com.tourguide.modules.user.dto.GuideUserDetailUpdateRequest;
import com.tourguide.modules.user.dto.GuideUserDetailResponse;
import com.tourguide.modules.user.entity.GuideApplication;
import com.tourguide.modules.user.entity.GuideUser;
import com.tourguide.modules.user.mapper.GuideAutoDisableConfigMapper;
import com.tourguide.modules.user.mapper.GuideApplicationMapper;
import com.tourguide.modules.user.mapper.GuideScenicConfigMapper;
import com.tourguide.modules.user.mapper.GuideTeamMapper;
import com.tourguide.modules.user.mapper.GuideUserMapper;
import com.tourguide.common.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.security.crypto.password.PasswordEncoder;
 
import java.time.LocalDate;
import java.time.Period;
import java.util.Collections;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;
 
class GuideUserServiceProfileTest {
    private GuideUserMapper userMapper;
    private GuideApplicationMapper applicationMapper;
    private GuideScenicConfigMapper scenicConfigMapper;
    private GuideUserService service;
 
    @BeforeEach
    void setUp() {
        userMapper = mock(GuideUserMapper.class);
        applicationMapper = mock(GuideApplicationMapper.class);
        scenicConfigMapper = mock(GuideScenicConfigMapper.class);
        when(scenicConfigMapper.selectList(any())).thenReturn(Collections.emptyList());
        when(userMapper.selectOne(any())).thenReturn(null);
        PasswordEncoder passwordEncoder = mock(PasswordEncoder.class);
        when(passwordEncoder.encode(any())).thenReturn("encoded-password");
        service = new GuideUserService(
                userMapper,
                mock(GuideTeamMapper.class),
                applicationMapper,
                scenicConfigMapper,
                mock(GuideAutoDisableConfigMapper.class),
                mock(OperationScenicSpotMapper.class),
                mock(OperationCityMapper.class),
                passwordEncoder);
    }
 
    @Test
    void shouldDeriveBirthDateAndWorkYearsForPendingGuide() {
        GuideApplication application = new GuideApplication();
        application.setMobile("13800138000");
        application.setRealName("导游");
        application.setGender(1);
        application.setIdCardNo("11010519491231002X");
        application.setWorkYearRange("3-5年");
 
        service.createPendingFromApplication(application);
 
        ArgumentCaptor<GuideUser> captor = ArgumentCaptor.forClass(GuideUser.class);
        verify(userMapper).insert(captor.capture());
        GuideUser guide = captor.getValue();
        assertThat(guide.getBirthDate()).isEqualTo(LocalDate.of(1949, 12, 31));
        assertThat(guide.getWorkYears()).isEqualTo(3);
    }
 
    @Test
    void shouldCopyRegistrationProfileWhenApprovingPendingGuide() {
        GuideUser pendingGuide = new GuideUser();
        pendingGuide.setId(12L);
        pendingGuide.setStatus(GuideUserService.STATUS_PENDING);
        pendingGuide.setMobile("13800138000");
        when(userMapper.selectOne(any())).thenReturn(pendingGuide);
 
        GuideApplication application = new GuideApplication();
        application.setMobile("13800138000");
        application.setIdCardNo("11010519491231002X");
        application.setWorkYearRange("5年以上");
 
        service.createFromApprovedApplication(application);
 
        ArgumentCaptor<GuideUser> captor = ArgumentCaptor.forClass(GuideUser.class);
        verify(userMapper).updateById(captor.capture());
        assertThat(captor.getValue().getBirthDate()).isEqualTo(LocalDate.of(1949, 12, 31));
        assertThat(captor.getValue().getWorkYears()).isEqualTo(5);
    }
 
    @Test
    void shouldCreateNewGuideForCancelledRegistration() {
        GuideUser cancelledGuide = new GuideUser();
        cancelledGuide.setId(12L);
        cancelledGuide.setStatus(GuideUserService.STATUS_CANCELLED);
        cancelledGuide.setMobile("13800138000");
        cancelledGuide.setUsername("13800138000");
        cancelledGuide.setCertificateNo("CERT-12");
        cancelledGuide.setIdCardNo("11010519491231002X");
        cancelledGuide.setAutoDisableWhitelisted(1);
        cancelledGuide.setAutoDisabled(1);
        when(userMapper.selectOne(any())).thenReturn(cancelledGuide);
 
        GuideApplication application = new GuideApplication();
        application.setMobile("13800138000");
        application.setRealName("瀵兼父");
        application.setIdCardNo("11010519491231002X");
 
        service.createPendingFromApplication(application);
 
        verify(userMapper).anonymizeCancelledGuide(eq(12L), org.mockito.ArgumentMatchers.argThat(
                username -> username.startsWith("deleted_") && username.length() == 32));
        verify(userMapper).clearCancelledIdentityByIdCard("11010519491231002X");
 
        ArgumentCaptor<GuideUser> insertCaptor = ArgumentCaptor.forClass(GuideUser.class);
        verify(userMapper).insert(insertCaptor.capture());
        GuideUser newGuide = insertCaptor.getValue();
        assertThat(newGuide.getStatus()).isEqualTo(GuideUserService.STATUS_PENDING);
        assertThat(newGuide.getUsername()).isEqualTo("13800138000");
        assertThat(newGuide.getMobile()).isEqualTo("13800138000");
        assertThat(newGuide.getAutoDisableWhitelisted()).isNull();
        assertThat(newGuide.getAutoDisabled()).isNull();
    }
 
    @Test
    void shouldReleaseIdentityFromStaleCancelledGuideBeforeNewRegistration() {
        GuideApplication application = new GuideApplication();
        application.setMobile("13900139000");
        application.setRealName("新导游");
        application.setIdCardNo("11010519491231002X");
        when(userMapper.selectOne(any())).thenReturn(null);
 
        service.createPendingFromApplication(application);
 
        verify(userMapper).clearCancelledIdentityByIdCard("11010519491231002X");
        verify(userMapper).insert(any(GuideUser.class));
    }
 
    @Test
    void shouldReleaseIdentityFromStalePendingGuideBeforeNewRegistration() {
        GuideApplication application = new GuideApplication();
        application.setMobile("13900139000");
        application.setRealName("新导游");
        application.setIdCardNo("11010519491231002X");
        when(userMapper.selectOne(any())).thenReturn(null);
 
        service.createPendingFromApplication(application);
 
        verify(userMapper).clearCancelledIdentityByIdCard("11010519491231002X");
        verify(userMapper).clearPendingIdentityByIdCard("11010519491231002X");
        verify(userMapper).insert(any(GuideUser.class));
    }
 
    @Test
    void shouldReleasePendingIdentityWhenRejectingApplication() {
        GuideUser pendingGuide = new GuideUser();
        pendingGuide.setId(12L);
        pendingGuide.setStatus(GuideUserService.STATUS_PENDING);
        pendingGuide.setMobile("13800138000");
        when(userMapper.selectOne(any())).thenReturn(pendingGuide);
 
        service.releaseIdentityForRejectedApplication("13800138000");
 
        verify(userMapper).releasePendingIdentity(12L);
    }
 
    @Test
    void shouldTreatOneYearOrLessAsZeroWorkYears() {
        GuideApplication application = new GuideApplication();
        application.setMobile("13800138000");
        application.setIdCardNo("11010519491231002X");
        application.setWorkYearRange("1年以内");
 
        service.createPendingFromApplication(application);
 
        ArgumentCaptor<GuideUser> captor = ArgumentCaptor.forClass(GuideUser.class);
        verify(userMapper).insert(captor.capture());
        assertThat(captor.getValue().getWorkYears()).isZero();
    }
 
    @Test
    void shouldUseRegistrationProfileAsDetailFallbackForExistingGuide() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setMobile("13800138000");
        guide.setIdCardNo("11010519491231002X");
        when(userMapper.selectById(12L)).thenReturn(guide);
 
        GuideApplication application = new GuideApplication();
        application.setIdCardNo(guide.getIdCardNo());
        application.setAgeRange("26-35岁");
        application.setWorkYearRange("1-3年");
        when(applicationMapper.selectOne(any())).thenReturn(application);
 
        GuideUserDetailResponse response = service.detail(12L);
 
        assertThat(response.getBirthDate()).isEqualTo(LocalDate.of(1949, 12, 31));
        assertThat(response.getAge()).isEqualTo(Period.between(LocalDate.of(1949, 12, 31), LocalDate.now()).getYears());
        assertThat(response.getWorkYears()).isEqualTo(1);
        assertThat(response.getAgeRange()).isEqualTo("26-35岁");
        assertThat(response.getWorkYearRange()).isEqualTo("1-3年");
    }
 
    @Test
    void adminPhoneChangeShouldUpdatePhoneLoginIdentity() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setStatus(GuideUserService.STATUS_ENABLED);
        guide.setUsername("13800138000");
        guide.setMobile("13800138000");
        when(userMapper.selectForUpdate(12L)).thenReturn(guide);
        when(userMapper.selectById(12L)).thenReturn(guide);
 
        GuideUserDetailUpdateRequest request = new GuideUserDetailUpdateRequest();
        request.setRealName("导游");
        request.setMobile("13900139000");
 
        service.updateDetail(12L, request);
 
        assertThat(guide.getMobile()).isEqualTo("13900139000");
        assertThat(guide.getUsername()).isEqualTo("13900139000");
        verify(userMapper).updateById(guide);
    }
 
    @Test
    void shouldRequireWhitelistBeforeEnablingAutoDisabledGuide() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setStatus(GuideUserService.STATUS_DISABLED);
        guide.setAutoDisabled(1);
        guide.setAutoDisableWhitelisted(0);
        when(userMapper.selectForUpdate(12L)).thenReturn(guide);
 
        assertThatThrownBy(() -> service.updateStatus(12L, GuideUserService.STATUS_ENABLED))
                .isInstanceOf(BusinessException.class)
                .hasMessage("该导游由自动禁用规则停用,请先在自动禁用设置中添加白名单后再启用");
        verify(userMapper, never()).updateById(any());
    }
 
    @Test
    void shouldEnableAutoDisabledGuideAfterWhitelistIsAdded() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setStatus(GuideUserService.STATUS_DISABLED);
        guide.setAutoDisabled(1);
        guide.setAutoDisableWhitelisted(1);
        when(userMapper.selectForUpdate(12L)).thenReturn(guide);
 
        service.updateStatus(12L, GuideUserService.STATUS_ENABLED);
 
        assertThat(guide.getStatus()).isEqualTo(GuideUserService.STATUS_ENABLED);
        assertThat(guide.getAutoDisabled()).isZero();
        verify(userMapper).updateById(guide);
    }
 
    @Test
    void shouldAutomaticallyEnableGuideWhenAddedToWhitelist() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setStatus(GuideUserService.STATUS_DISABLED);
        guide.setAutoDisabled(1);
        guide.setAutoDisableWhitelisted(0);
        when(userMapper.selectById(12L)).thenReturn(guide);
 
        service.updateWhitelist(12L, true);
 
        assertThat(guide.getAutoDisableWhitelisted()).isEqualTo(1);
        assertThat(guide.getStatus()).isEqualTo(GuideUserService.STATUS_ENABLED);
        assertThat(guide.getAutoDisabled()).isZero();
        verify(userMapper).updateById(guide);
    }
 
    @Test
    void shouldKeepManuallyDisabledGuideDisabledWhenAddedToWhitelist() {
        GuideUser guide = new GuideUser();
        guide.setId(12L);
        guide.setStatus(GuideUserService.STATUS_DISABLED);
        guide.setAutoDisabled(0);
        guide.setAutoDisableWhitelisted(0);
        when(userMapper.selectById(12L)).thenReturn(guide);
 
        service.updateWhitelist(12L, true);
 
        assertThat(guide.getAutoDisableWhitelisted()).isEqualTo(1);
        assertThat(guide.getStatus()).isEqualTo(GuideUserService.STATUS_DISABLED);
        assertThat(guide.getAutoDisabled()).isZero();
        verify(userMapper).updateById(guide);
    }
}