tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
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
package com.cloudroam.service.impl;
 
import io.github.talelin.core.util.EncryptUtil;
import com.cloudroam.common.constant.IdentityConstant;
import com.cloudroam.model.UserIdentityDO;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
 
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
 
@SpringBootTest
@Transactional
@Rollback
@Slf4j
@ActiveProfiles("test")
public class UserIdentityServiceImplTest {
 
    @Autowired
    private UserIdentityServiceImpl userIdentityService;
 
    public UserIdentityDO setUp1() {
        UserIdentityDO userIdentity = new UserIdentityDO();
        userIdentity.setUserId(1);
        userIdentity.setIdentityType(IdentityConstant.USERNAME_PASSWORD_IDENTITY);
        userIdentity.setIdentifier("pedro");
        userIdentity.setCredential(EncryptUtil.encrypt("123456"));
        return userIdentity;
    }
 
    public UserIdentityDO setUp2(Integer userId, String identityType, String identifier, String credential) {
        UserIdentityDO userIdentity = new UserIdentityDO();
        userIdentity.setUserId(userId);
        userIdentity.setIdentityType(identityType);
        userIdentity.setIdentifier(identifier);
        userIdentity.setCredential(EncryptUtil.encrypt(credential));
        return userIdentity;
    }
 
    @Test
    public void createIdentity() {
        UserIdentityDO userIdentity = userIdentityService.createIdentity(
                1,
                IdentityConstant.USERNAME_PASSWORD_IDENTITY,
                "pedro",
                EncryptUtil.encrypt("123456")
        );
        assertNotNull(userIdentity.getId());
        assertTrue(EncryptUtil.verify(userIdentity.getCredential(), "123456"));
    }
 
    @Test
    public void createIdentity1() {
        UserIdentityDO userIdentity = setUp1();
        userIdentityService.createIdentity(userIdentity);
        assertNotNull(userIdentity.getId());
        assertTrue(EncryptUtil.verify(userIdentity.getCredential(), "123456"));
    }
 
    @Test
    public void createUsernamePasswordIdentity() {
        UserIdentityDO userIdentity = userIdentityService.createUsernamePasswordIdentity(
                1,
                "pedro",
                "123456");
        assertNotNull(userIdentity.getId());
        assertTrue(EncryptUtil.verify(userIdentity.getCredential(), "123456"));
    }
 
//    @Test
    public void verifyUsernamePassword() {
        UserIdentityDO userIdentity = setUp1();
        userIdentityService.createIdentity(userIdentity);
 
        boolean valid = userIdentityService.verifyUsernamePassword(userIdentity.getUserId(), "pedro", "123456");
        assertTrue(valid);
    }
 
//    @Test
    public void changePassword() {
        UserIdentityDO userIdentity = setUp1();
        userIdentityService.createIdentity(userIdentity);
 
        boolean b = userIdentityService.changePassword(userIdentity.getUserId(), "147258");
        assertTrue(b);
 
        boolean valid = userIdentityService.verifyUsernamePassword(userIdentity.getUserId(), "pedro", "147258");
        assertTrue(valid);
    }
 
//    @Test
    public void changeUsername() {
        UserIdentityDO userIdentity = setUp1();
        userIdentityService.createIdentity(userIdentity);
 
        boolean b = userIdentityService.changeUsername(userIdentity.getUserId(), "pedro1");
        assertTrue(b);
 
        boolean valid = userIdentityService.verifyUsernamePassword(userIdentity.getUserId(), "pedro1", "123456");
        assertTrue(valid);
    }
}