-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
/api/v1/event/{token}
not checking workflow states (#915)
- Loading branch information
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/org/dependencytrack/resources/v1/EventResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.resources.v1; | ||
|
||
import alpine.server.filters.ApiFilter; | ||
import alpine.server.filters.AuthenticationFilter; | ||
import jakarta.ws.rs.core.Response; | ||
import org.apache.http.HttpStatus; | ||
import org.dependencytrack.JerseyTestRule; | ||
import org.dependencytrack.ResourceTest; | ||
import org.dependencytrack.model.WorkflowState; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.dependencytrack.model.WorkflowStatus.COMPLETED; | ||
import static org.dependencytrack.model.WorkflowStatus.FAILED; | ||
import static org.dependencytrack.model.WorkflowStatus.PENDING; | ||
import static org.dependencytrack.model.WorkflowStep.BOM_CONSUMPTION; | ||
import static org.dependencytrack.model.WorkflowStep.BOM_PROCESSING; | ||
|
||
public class EventResourceTest extends ResourceTest { | ||
|
||
@ClassRule | ||
public static JerseyTestRule jersey = new JerseyTestRule( | ||
new ResourceConfig(EventResource.class) | ||
.register(ApiFilter.class) | ||
.register(AuthenticationFilter.class)); | ||
|
||
@Test | ||
public void isTokenBeingProcessedTrueTest() { | ||
final UUID uuid = UUID.randomUUID(); | ||
final WorkflowState workflowState1 = new WorkflowState(); | ||
workflowState1.setParent(null); | ||
workflowState1.setStep(BOM_CONSUMPTION); | ||
workflowState1.setStatus(COMPLETED); | ||
workflowState1.setToken(uuid); | ||
workflowState1.setUpdatedAt(new Date()); | ||
var workflowState1Persisted = qm.persist(workflowState1); | ||
final WorkflowState workflowState2 = new WorkflowState(); | ||
workflowState2.setParent(workflowState1Persisted); | ||
workflowState2.setStep(BOM_PROCESSING); | ||
workflowState2.setStatus(PENDING); | ||
workflowState2.setToken(uuid); | ||
workflowState2.setUpdatedAt(new Date()); | ||
qm.persist(workflowState2); | ||
|
||
final Response response = jersey.target(V1_EVENT + "/token/" + uuid).request() | ||
.header(X_API_KEY, apiKey) | ||
.get(Response.class); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); | ||
final String jsonResponse = getPlainTextBody(response); | ||
assertThatJson(jsonResponse).isEqualTo(""" | ||
{ | ||
"processing": true | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
public void isTokenBeingProcessedFalseTest() { | ||
final UUID uuid = UUID.randomUUID(); | ||
final WorkflowState workflowState1 = new WorkflowState(); | ||
workflowState1.setParent(null); | ||
workflowState1.setStep(BOM_CONSUMPTION); | ||
workflowState1.setStatus(COMPLETED); | ||
workflowState1.setToken(uuid); | ||
workflowState1.setUpdatedAt(new Date()); | ||
var workflowState1Persisted = qm.persist(workflowState1); | ||
final WorkflowState workflowState2 = new WorkflowState(); | ||
workflowState2.setParent(workflowState1Persisted); | ||
workflowState2.setStep(BOM_PROCESSING); | ||
workflowState2.setStatus(FAILED); | ||
workflowState2.setToken(uuid); | ||
workflowState2.setUpdatedAt(new Date()); | ||
qm.persist(workflowState2); | ||
|
||
final Response response = jersey.target(V1_EVENT + "/token/" + uuid).request() | ||
.header(X_API_KEY, apiKey) | ||
.get(Response.class); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); | ||
final String jsonResponse = getPlainTextBody(response); | ||
assertThatJson(jsonResponse).isEqualTo(""" | ||
{ | ||
"processing": false | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
public void isTokenBeingProcessedNotExistsTest() { | ||
final Response response = jersey.target(V1_EVENT + "/token/" + UUID.randomUUID()).request() | ||
.header(X_API_KEY, apiKey) | ||
.get(Response.class); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); | ||
final String jsonResponse = getPlainTextBody(response); | ||
assertThatJson(jsonResponse).isEqualTo(""" | ||
{ | ||
"processing": false | ||
} | ||
"""); | ||
} | ||
|
||
} |