2424import com .thoughtworks .go .server .service .RestfulService ;
2525import com .thoughtworks .go .server .web .ArtifactFolderViewFactory ;
2626import com .thoughtworks .go .server .web .ResponseCodeView ;
27+ import com .thoughtworks .go .util .SystemEnvironment ;
2728import org .junit .Before ;
2829import org .junit .Test ;
2930import org .springframework .mock .web .MockHttpServletRequest ;
3031import org .springframework .mock .web .MockMultipartFile ;
3132import org .springframework .mock .web .MockMultipartHttpServletRequest ;
33+ import org .springframework .web .multipart .MultipartHttpServletRequest ;
3234import org .springframework .web .servlet .ModelAndView ;
3335
3436import javax .servlet .http .HttpServletResponse ;
@@ -52,6 +54,7 @@ public class ArtifactsControllerTest {
5254 private RestfulService restfulService ;
5355 private ArtifactsService artifactService ;
5456 private ConsoleService consoleService ;
57+ private SystemEnvironment systemEnvironment ;
5558
5659 @ Before
5760 public void setUp () {
@@ -60,8 +63,8 @@ public void setUp() {
6063 restfulService = mock (RestfulService .class );
6164 artifactService = mock (ArtifactsService .class );
6265 consoleService = mock (ConsoleService .class );
63-
64- artifactsController = new ArtifactsController (artifactService , restfulService , mock (ZipArtifactCache .class ), consoleActivityMonitor , consoleService );
66+ systemEnvironment = mock ( SystemEnvironment . class );
67+ artifactsController = new ArtifactsController (artifactService , restfulService , mock (ZipArtifactCache .class ), consoleActivityMonitor , consoleService , systemEnvironment );
6568
6669 request = new MockHttpServletRequest ();
6770 }
@@ -102,7 +105,7 @@ public void shouldReturnHttpErrorCodeWhenChecksumFileSaveFails() throws Exceptio
102105 @ Test
103106 public void shouldFunnelAll_GET_calls () throws Exception {
104107 final ModelAndView returnVal = new ModelAndView ();
105- ArtifactsController controller = new ArtifactsController (artifactService , restfulService , mock (ZipArtifactCache .class ), consoleActivityMonitor , consoleService ) {
108+ ArtifactsController controller = new ArtifactsController (artifactService , restfulService , mock (ZipArtifactCache .class ), consoleActivityMonitor , consoleService , systemEnvironment ) {
106109 @ Override ModelAndView getArtifact (String filePath , ArtifactFolderViewFactory folderViewFactory , String pipelineName , String counterOrLabel , String stageName , String stageCounter ,
107110 String buildName , String sha , String serverAlias ) throws Exception {
108111 return returnVal ;
@@ -113,4 +116,17 @@ public void shouldFunnelAll_GET_calls() throws Exception {
113116 assertThat (controller .getArtifactAsZip ("pipeline" , "counter" , "stage" , "2" , "job" , "file_name" , "sha1" ), sameInstance (returnVal ));
114117 assertThat (controller .getArtifactAsJson ("pipeline" , "counter" , "stage" , "2" , "job" , "file_name" , "sha1" ), sameInstance (returnVal ));
115118 }
119+
120+ @ Test
121+ public void shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest () throws Exception {
122+ MultipartHttpServletRequest multipartHttpServletRequest = new MockMultipartHttpServletRequest ();
123+
124+ when (systemEnvironment .isApiSafeModeEnabled ()).thenReturn (true );
125+ ModelAndView modelAndView = artifactsController .postArtifact ("pipeline" , "invalid-label" , "stage" , "stage-counter" , "job-name" , 3L , "file-path" , 3 , multipartHttpServletRequest );
126+ ResponseCodeView codeView = (ResponseCodeView ) modelAndView .getView ();
127+
128+ assertThat (codeView .getStatusCode (), is (HttpServletResponse .SC_BAD_REQUEST ));
129+ assertThat (codeView .getContent (), is ("Missing required header 'Confirm'" ));
130+
131+ }
116132}
0 commit comments