liujie
2026-07-30 dc7835c8a9258cbcbe0dbecd4e1c24039ee09e58
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
package com.tourguide.modules.user.service;
 
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.user.dto.GuidePhoneChangeTokenResponse;
import com.tourguide.modules.user.dto.GuideUserResponse;
import com.tourguide.modules.user.entity.GuideUser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
 
import java.time.Duration;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class GuidePhoneChangeServiceTest {
    private StringRedisTemplate redisTemplate;
    private ValueOperations<String, String> valueOperations;
    private GuideVerificationCodeService verificationCodeService;
    private GuideUserService userService;
    private GuidePhoneChangeService service;
    private GuideUser guide;
 
    @SuppressWarnings("unchecked")
    @BeforeEach
    void setUp() {
        redisTemplate = mock(StringRedisTemplate.class);
        valueOperations = mock(ValueOperations.class);
        verificationCodeService = mock(GuideVerificationCodeService.class);
        userService = mock(GuideUserService.class);
        when(redisTemplate.opsForValue()).thenReturn(valueOperations);
        guide = new GuideUser();
        guide.setId(8L);
        guide.setMobile("13800138000");
        when(userService.findEnabledById(8L)).thenReturn(guide);
        service = new GuidePhoneChangeService(redisTemplate, verificationCodeService, userService);
    }
 
    @Test
    void shouldRejectMismatchedOldPhone() {
        assertThatThrownBy(() -> service.sendOldPhoneCode(8L, "13900139000"))
                .isInstanceOf(BusinessException.class)
                .hasMessage("旧手机号与当前账号不一致");
    }
 
    @Test
    void shouldIssueTokenAfterOldPhoneVerification() {
        GuidePhoneChangeTokenResponse response = service.verifyOldPhone(8L, "13800138000", "123456");
 
        assertThat(response.getChangeToken()).isNotBlank();
        assertThat(response.getExpiresIn()).isEqualTo(Duration.ofMinutes(10).getSeconds());
        verify(verificationCodeService).verifyAndConsume("13800138000", "123456", "change-old");
        verify(valueOperations).set(anyString(), eq("8"), eq(Duration.ofMinutes(10)));
    }
 
    @Test
    void shouldChangePhoneWithValidToken() {
        when(valueOperations.get("tourguide:guide-phone-change:token:token123")).thenReturn("8");
        GuideUserResponse response = mock(GuideUserResponse.class);
        when(userService.changeMobile(8L, "13900139000")).thenReturn(response);
 
        assertThat(service.changePhone(8L, "token123", "13900139000", "654321")).isSameAs(response);
        verify(userService).ensurePhoneAvailable(8L, "13900139000");
        verify(verificationCodeService).verifyAndConsume("13900139000", "654321", "change-new");
        verify(redisTemplate).delete("tourguide:guide-phone-change:token:token123");
    }
 
    @Test
    void shouldRejectCurrentPhoneAsNewPhone() {
        when(valueOperations.get("tourguide:guide-phone-change:token:token123")).thenReturn("8");
 
        assertThatThrownBy(() -> service.sendNewPhoneCode(8L, "token123", "13800138000"))
                .isInstanceOf(BusinessException.class)
                .hasMessage("新手机号不能与当前手机号相同");
    }
}