|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 The University of Manchester |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package uk.ac.manchester.spinnaker.alloc.admin; |
| 17 | + |
| 18 | +import static uk.ac.manchester.spinnaker.alloc.admin.AdminController.BASE_PATH; |
| 19 | +import static uk.ac.manchester.spinnaker.alloc.admin.AdminController.CREATE_USER_PATH; |
| 20 | +import static uk.ac.manchester.spinnaker.alloc.admin.AdminController.USERS_PATH; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; |
| 24 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 25 | + |
| 26 | +import java.net.URI; |
| 27 | +import java.util.Base64; |
| 28 | + |
| 29 | +import javax.ws.rs.core.MediaType; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 34 | +import org.springframework.boot.test.context.SpringBootTest; |
| 35 | +import org.springframework.test.context.ActiveProfiles; |
| 36 | +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; |
| 37 | +import org.springframework.test.web.servlet.MockMvc; |
| 38 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 39 | + |
| 40 | +import uk.ac.manchester.spinnaker.alloc.TestSupport; |
| 41 | +import uk.ac.manchester.spinnaker.alloc.model.UserRecord; |
| 42 | +import uk.ac.manchester.spinnaker.alloc.security.TrustLevel; |
| 43 | + |
| 44 | +@AutoConfigureMockMvc |
| 45 | +@SpringBootTest |
| 46 | +@SpringJUnitWebConfig(TestSupport.Config.class) |
| 47 | +@ActiveProfiles("unittest") |
| 48 | +public class AdminControllerTest extends TestSupport { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private MockMvc mvc; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private UserControl userControl; |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testCreateUser() throws Exception { |
| 58 | + doTransactionalTest(() -> { |
| 59 | + // Create an admin |
| 60 | + UserRecord admin = new UserRecord(); |
| 61 | + admin.setUserName("admin"); |
| 62 | + admin.setEnabled(true); |
| 63 | + admin.setHasPassword(true); |
| 64 | + admin.setTrustLevel(TrustLevel.ADMIN); |
| 65 | + admin.setPassword("admin"); |
| 66 | + |
| 67 | + var adminRecord = userControl.createUser(admin, |
| 68 | + m -> URI.create("")); |
| 69 | + assertTrue(adminRecord.isPresent()); |
| 70 | + |
| 71 | + String userPass = Base64.getEncoder().encodeToString( |
| 72 | + "admin:admin".getBytes()); |
| 73 | + try { |
| 74 | + var result = mvc.perform( |
| 75 | + MockMvcRequestBuilders |
| 76 | + .post(BASE_PATH + CREATE_USER_PATH) |
| 77 | + .with(csrf()) |
| 78 | + .header("Authorization", "Basic " + userPass) |
| 79 | + .param("internal", "true") |
| 80 | + .param("userName", "test") |
| 81 | + .param("trustLevel", "USER") |
| 82 | + .param("password", "test") |
| 83 | + .param("enabled", "true") |
| 84 | + .param("hasPassword", "true") |
| 85 | + .accept(MediaType.APPLICATION_JSON)); |
| 86 | + result.andExpect( |
| 87 | + redirectedUrlPattern(BASE_PATH + USERS_PATH + "/*")); |
| 88 | + } catch (Exception e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void updateUserPassword() throws Exception { |
| 96 | + doTransactionalTest(() -> { |
| 97 | + // Create an admin |
| 98 | + UserRecord admin = new UserRecord(); |
| 99 | + admin.setUserName("admin"); |
| 100 | + admin.setEnabled(true); |
| 101 | + admin.setHasPassword(true); |
| 102 | + admin.setTrustLevel(TrustLevel.ADMIN); |
| 103 | + admin.setPassword("admin"); |
| 104 | + |
| 105 | + var adminRecord = userControl.createUser(admin, |
| 106 | + m -> URI.create("")); |
| 107 | + assertTrue(adminRecord.isPresent()); |
| 108 | + log.info("Admin created: {}", adminRecord.get()); |
| 109 | + |
| 110 | + String userPass = Base64.getEncoder().encodeToString( |
| 111 | + "admin:admin".getBytes()); |
| 112 | + |
| 113 | + // Create a user |
| 114 | + UserRecord test = new UserRecord(); |
| 115 | + test.setUserName("test"); |
| 116 | + test.setEnabled(true); |
| 117 | + test.setHasPassword(true); |
| 118 | + test.setTrustLevel(TrustLevel.USER); |
| 119 | + test.setPassword("test"); |
| 120 | + |
| 121 | + var testRecord = userControl.createUser(test, |
| 122 | + m -> URI.create("")); |
| 123 | + assertTrue(testRecord.isPresent()); |
| 124 | + UserRecord testUser = testRecord.get(); |
| 125 | + |
| 126 | + try { |
| 127 | + var result = mvc.perform( |
| 128 | + MockMvcRequestBuilders |
| 129 | + .post(BASE_PATH + USERS_PATH + "/" + testUser.getUserId()) |
| 130 | + .with(csrf()) |
| 131 | + .header("Authorization", "Basic " + userPass) |
| 132 | + .param("internal", "true") |
| 133 | + .param("userName", "test") |
| 134 | + .param("trustLevel", "USER") |
| 135 | + .param("password", "testchange") |
| 136 | + .accept(MediaType.APPLICATION_JSON)); |
| 137 | + |
| 138 | + // Note: not sure how to get this dynamically! |
| 139 | + result.andExpect( |
| 140 | + forwardedUrl("/WEB-INF/views/admin/userdetails.jsp")); |
| 141 | + } catch (Exception e) { |
| 142 | + throw new RuntimeException(e); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | +} |
0 commit comments