|
14 | 14 | // You should have received a copy of the GNU General Public License |
15 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 |
|
17 | | - |
18 | 17 | package de.doubleslash.keeptime.REST_API_Test; |
19 | 18 |
|
20 | | -//import de.doubleslash.keeptime.model.Authorities; |
21 | | -//import de.doubleslash.keeptime.model.User; |
22 | | -//import de.doubleslash.keeptime.model.repos.AuthoritiesRepository; |
23 | | -//import de.doubleslash.keeptime.model.repos.UserRepository; |
24 | | -import org.junit.jupiter.api.Test; |
25 | | -import org.springframework.beans.factory.annotation.Autowired; |
26 | | -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
27 | 22 |
|
28 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
29 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.util.Properties; |
| 25 | + |
| 26 | +import de.doubleslash.keeptime.view.SettingsController; |
| 27 | +import org.junit.jupiter.api.Test; |
30 | 28 |
|
31 | | -@DataJpaTest |
32 | 29 | public class ApiTest { |
33 | 30 |
|
34 | | -// @Autowired |
35 | | -// private UserRepository userRepository; |
36 | | -// |
37 | | -// @Autowired |
38 | | -// private AuthoritiesRepository authoritiesRepository; |
| 31 | + @Test |
| 32 | + public void testSaveUserAndAuthorities() { |
| 33 | + String username = "user"; |
| 34 | + String password = "1234"; |
| 35 | + SettingsController settingsController; |
39 | 36 |
|
40 | | -// @Test |
41 | | -// public void testSaveUserAndAuthorities() { |
42 | | -// String username = "user"; |
43 | | -// String password = "1234"; |
44 | | -// |
45 | | -// User user = new User(); |
46 | | -// Authorities authorities = new Authorities(); |
47 | | -// user.setUserName(username); |
48 | | -// user.setPassword("{noop}" + password); |
49 | | -// user.setEnabled(true); |
50 | | -// authorities.setUserName(username); |
51 | | -// authorities.setAuthority("ROLE_USER"); |
52 | | -// |
53 | | -// userRepository.save(user); |
54 | | -// authoritiesRepository.save(authorities); |
55 | | -// |
56 | | -// User savedUser = userRepository.findByUserName(username); |
57 | | -// assertNotNull(savedUser); |
58 | | -// assertEquals(username, savedUser.getUserName()); |
59 | | -// |
60 | | -// Authorities savedAuthorities = authoritiesRepository.findByUserName(username); |
61 | | -// assertNotNull(savedAuthorities); |
62 | | -// assertEquals("ROLE_USER", savedAuthorities.getAuthority()); |
63 | | -// } |
| 37 | + |
| 38 | + Properties properties = new Properties(); |
| 39 | + properties.put("spring.security.user.name", "${BASIC_AUTH_USER:" + username+ "}"); |
| 40 | + properties.setProperty("spring.security.user.password", "${BASIC_AUTH_PASSWORD:" + password + "}"); |
| 41 | + |
| 42 | + assertEquals(properties.getProperty("spring.security.user.password"),"${BASIC_AUTH_PASSWORD:1234}"); |
| 43 | + assertEquals(properties.getProperty("spring.security.user.name"),"${BASIC_AUTH_USER:user}"); |
| 44 | + } |
64 | 45 | } |
65 | 46 |
|
| 47 | +//@Test public void testIsPropertyPresent() throws Exception { |
| 48 | +// // Mock InputStream und Properties |
| 49 | +// InputStream mockInputStream = mock(InputStream.class); |
| 50 | +// Properties mockProperties = mock(Properties.class); |
| 51 | +// when(mockProperties.containsKey("api")).thenReturn(true); |
| 52 | +// when(mockProperties.containsKey("non_existing_property")).thenReturn(false); |
| 53 | +// |
| 54 | +// // Mock SettingsController und setzen Sie den InputStream |
| 55 | +// SettingsController settingsController = new SettingsController(); |
| 56 | +// SettingsController spySettingsController = org.mockito.Mockito.spy(settingsController); |
| 57 | +// org.mockito.Mockito.doReturn(mockInputStream).when(spySettingsController).getClass().getClassLoader().getResourceAsStream("application.properties"); |
| 58 | +// org.mockito.Mockito.doReturn(mockProperties).when(spySettingsController).loadProperties(mockInputStream); |
| 59 | +// |
| 60 | +// // Testen, ob die Eigenschaft vorhanden ist |
| 61 | +// assertTrue(spySettingsController.isPropertyPresent("api")); |
| 62 | +// assertFalse(spySettingsController.isPropertyPresent("non_existing_property")); |
| 63 | +//} |
| 64 | +//} |
66 | 65 |
|
0 commit comments