|
16 | 16 |
|
17 | 17 | package org.labkey.nab; |
18 | 18 |
|
| 19 | +import jakarta.servlet.http.HttpServletResponse; |
19 | 20 | import org.apache.logging.log4j.LogManager; |
20 | 21 | import org.apache.logging.log4j.Logger; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
21 | 24 | import org.labkey.api.assay.AssayService; |
22 | 25 | import org.labkey.api.data.ColumnInfo; |
23 | 26 | import org.labkey.api.data.Container; |
|
38 | 41 | import org.labkey.api.query.FieldKey; |
39 | 42 | import org.labkey.api.query.QueryService; |
40 | 43 | import org.labkey.api.security.User; |
| 44 | +import org.labkey.api.security.permissions.AbstractContainerScopingTest; |
| 45 | +import org.labkey.api.security.roles.EditorRole; |
| 46 | +import org.labkey.api.security.roles.ReaderRole; |
| 47 | +import org.labkey.api.view.ActionURL; |
41 | 48 | import org.labkey.api.study.Dataset; |
42 | 49 | import org.labkey.api.study.Study; |
43 | 50 | import org.labkey.api.study.StudyService; |
44 | 51 | import org.labkey.api.util.PageFlowUtil; |
45 | 52 | import org.labkey.api.util.Pair; |
46 | 53 | import org.labkey.nab.query.NabProtocolSchema; |
47 | 54 | import org.labkey.nab.query.NabRunDataTable; |
| 55 | +import org.springframework.mock.web.MockHttpServletResponse; |
48 | 56 |
|
49 | 57 | import java.sql.SQLException; |
50 | 58 | import java.util.Collection; |
@@ -206,4 +214,70 @@ public void getDataPropertiesFromNabRunData(NabRunDataTable nabRunDataTable, Str |
206 | 214 | throw new RuntimeSQLException(e); |
207 | 215 | } |
208 | 216 | } |
| 217 | + |
| 218 | + public static class ContainerScopingTestCase extends AbstractContainerScopingTest |
| 219 | + { |
| 220 | + private Container _folderA; |
| 221 | + private Container _folderB; |
| 222 | + private User _admin; |
| 223 | + |
| 224 | + @Before |
| 225 | + public void setup() |
| 226 | + { |
| 227 | + _admin = getAdmin(); |
| 228 | + _folderA = createContainer("A"); |
| 229 | + _folderB = createContainer("B"); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testDeleteRunContainerScoping() throws Exception |
| 234 | + { |
| 235 | + // Regression test for NAB-1. NabAssayController.DeleteRunAction resolves a |
| 236 | + // run via ExperimentService.getExpRun(rowId) from a global rowId and then deletes it. |
| 237 | + ExpProtocol protocol = ExperimentService.get().ensureSampleDerivationProtocol(_admin); |
| 238 | + ExpRun run = ExperimentService.get().createExperimentRun(_folderB, "scope-test-run"); |
| 239 | + run.setProtocol(protocol); |
| 240 | + run.save(_admin); |
| 241 | + int rowId = run.getRowId(); |
| 242 | + |
| 243 | + ActionURL foreignUrl = new ActionURL(NabAssayController.DeleteRunAction.class, _folderA) |
| 244 | + .addParameter("rowId", rowId); |
| 245 | + |
| 246 | + User deleterAonly = createUserInRole(_folderA, EditorRole.class); |
| 247 | + assertStatus(HttpServletResponse.SC_NOT_FOUND, post(foreignUrl, deleterAonly)); |
| 248 | + assertNotNull("Foreign-container run must not be deleted", ExperimentService.get().getExpRun(rowId)); |
| 249 | + |
| 250 | + ActionURL ownUrl = new ActionURL(NabAssayController.DeleteRunAction.class, _folderB) |
| 251 | + .addParameter("rowId", rowId); |
| 252 | + assertStatus(HttpServletResponse.SC_FOUND, post(ownUrl, _admin)); |
| 253 | + assertNull("Run should have been deleted from its own container", ExperimentService.get().getExpRun(rowId)); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void testDownloadDatafileContainerScoping() throws Exception |
| 258 | + { |
| 259 | + // Regression test for NAB-2. NabAssayController.DownloadDatafileAction resolves a run via |
| 260 | + // ExperimentService.getExpRun(rowId) from a global rowId |
| 261 | + ExpProtocol protocol = ExperimentService.get().ensureSampleDerivationProtocol(_admin); |
| 262 | + ExpRun run = ExperimentService.get().createExperimentRun(_folderB, "scope-test-download-run"); |
| 263 | + run.setProtocol(protocol); |
| 264 | + run.save(_admin); |
| 265 | + int rowId = run.getRowId(); |
| 266 | + |
| 267 | + User readerAonly = createUserInRole(_folderA, ReaderRole.class); |
| 268 | + ActionURL foreignUrl = new ActionURL(NabAssayController.DownloadDatafileAction.class, _folderA) |
| 269 | + .addParameter("rowId", rowId); |
| 270 | + MockHttpServletResponse foreignResponse = get(foreignUrl, readerAonly); |
| 271 | + assertStatus(HttpServletResponse.SC_NOT_FOUND, foreignResponse); |
| 272 | + assertTrue("Foreign-container download must be rejected by the container check", |
| 273 | + foreignResponse.getContentAsString().contains("does not exist")); |
| 274 | + |
| 275 | + ActionURL ownUrl = new ActionURL(NabAssayController.DownloadDatafileAction.class, _folderB) |
| 276 | + .addParameter("rowId", rowId); |
| 277 | + MockHttpServletResponse ownResponse = get(ownUrl, _admin); |
| 278 | + assertStatus(HttpServletResponse.SC_NOT_FOUND, ownResponse); |
| 279 | + assertTrue("Same-container request should advance past the container check to the data handler", |
| 280 | + ownResponse.getContentAsString().contains("is not a NAb run")); |
| 281 | + } |
| 282 | + } |
209 | 283 | } |
0 commit comments