-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break out okta endpoint from health check (#7891)
* add in separate okta component * add in okta messages * add in tests * add in okta message * undo hardcode for test branch * fix frontend test * fix frontend test * make status check reflect api shape for okta * make status check reflect api shape for okta * make tests more readable and handle NPE in health check * make smoke test script include a check for okta * woops * weave in config values into endpoint Co-authored-by: Merethe Hansen <[email protected]> * add in old api exception * status check reflect subapi endpoint (sorry merethe) * dedupe application.yaml --------- Co-authored-by: Merethe Hansen <[email protected]>
- Loading branch information
Showing
12 changed files
with
266 additions
and
47 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
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/gov/cdc/usds/simplereport/api/heathcheck/OktaHealthIndicator.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,38 @@ | ||
package gov.cdc.usds.simplereport.api.heathcheck; | ||
|
||
import com.okta.sdk.resource.client.ApiException; | ||
import gov.cdc.usds.simplereport.idp.repository.OktaRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class OktaHealthIndicator implements HealthIndicator { | ||
private final OktaRepository _oktaRepo; | ||
public static final String ACTIVE_LITERAL = "ACTIVE"; | ||
|
||
@Override | ||
public Health health() { | ||
Health.Builder oktaDegradedWarning = Health.status("OKTA_DEGRADED"); | ||
try { | ||
String oktaStatus = _oktaRepo.getApplicationStatusForHealthCheck(); | ||
if (!ACTIVE_LITERAL.equals(oktaStatus)) { | ||
log.info("Okta status didn't return ACTIVE, instead returned " + oktaStatus); | ||
return oktaDegradedWarning.build(); | ||
} | ||
} catch (NullPointerException e) { | ||
log.info("Call to Okta repository status returned null"); | ||
return oktaDegradedWarning.build(); | ||
} catch (ApiException e) { | ||
// Okta API call errored | ||
log.info("Okta status call raised an exception: " + e.getMessage()); | ||
return oktaDegradedWarning.build(); | ||
} | ||
|
||
return Health.up().build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/gov/cdc/usds/simplereport/idp/repository/DemoOktaRepository.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
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
53 changes: 53 additions & 0 deletions
53
backend/src/test/java/gov/cdc/usds/simplereport/api/healthcheck/OktaHealthIndicatorTest.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,53 @@ | ||
package gov.cdc.usds.simplereport.api.healthcheck; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.okta.sdk.resource.client.ApiException; | ||
import gov.cdc.usds.simplereport.api.heathcheck.OktaHealthIndicator; | ||
import gov.cdc.usds.simplereport.db.repository.BaseRepositoryTest; | ||
import gov.cdc.usds.simplereport.idp.repository.OktaRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
|
||
@RequiredArgsConstructor | ||
@EnableConfigurationProperties | ||
class OktaHealthIndicatorTest extends BaseRepositoryTest { | ||
|
||
@SpyBean private OktaRepository mockOktaRepo; | ||
|
||
@Autowired private OktaHealthIndicator indicator; | ||
|
||
@Test | ||
void health_SucceedsWhenOktaRepoReturnsActive() { | ||
when(mockOktaRepo.getApplicationStatusForHealthCheck()).thenReturn("ACTIVE"); | ||
assertThat(indicator.health()).isEqualTo(Health.up().build()); | ||
} | ||
|
||
@Test | ||
void health_FailsWhenOktaApiThrowsErrors() { | ||
when(mockOktaRepo.getApplicationStatusForHealthCheck()) | ||
.thenThrow(new ApiException("some api error")); | ||
Health.Builder oktaDegradedWarning = Health.status("OKTA_DEGRADED"); | ||
assertThat(indicator.health()).isEqualTo(oktaDegradedWarning.build()); | ||
} | ||
|
||
@Test | ||
void health_FailsWhenOktaApiThrowsNPE() { | ||
when(mockOktaRepo.getApplicationStatusForHealthCheck()).thenThrow(new NullPointerException()); | ||
Health.Builder oktaDegradedWarning = Health.status("OKTA_DEGRADED"); | ||
assertThat(indicator.health()).isEqualTo(oktaDegradedWarning.build()); | ||
} | ||
|
||
@Test | ||
void health_failsWhenOktaRepoDoesntReturnActive() { | ||
when(mockOktaRepo.getApplicationStatusForHealthCheck()).thenReturn("INACTIVE"); | ||
Health.Builder oktaDegradedWarning = Health.status("OKTA_DEGRADED"); | ||
|
||
assertThat(indicator.health()).isEqualTo(oktaDegradedWarning.build()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import { FetchMock } from "jest-fetch-mock"; | ||
|
||
import DeploySmokeTest from "./DeploySmokeTest"; | ||
import DeploySmokeTest, { | ||
APP_STATUS_FAILURE, | ||
APP_STATUS_LOADING, | ||
APP_STATUS_SUCCESS, | ||
OKTA_STATUS_LOADING, | ||
OKTA_STATUS_SUCCESS, | ||
} from "./DeploySmokeTest"; | ||
import { generateBackendApiHealthResponse } from "./deploySmokeTestTestConstants"; | ||
|
||
describe("DeploySmokeTest", () => { | ||
beforeEach(() => { | ||
(fetch as FetchMock).resetMocks(); | ||
}); | ||
|
||
it("renders success when returned from the API endpoint", async () => { | ||
it("renders success when returned from the backend API smoke test endpoint", async () => { | ||
(fetch as FetchMock).mockResponseOnce(JSON.stringify({ status: "UP" })); | ||
(fetch as FetchMock).mockResponseOnce(JSON.stringify({ status: "DOWN" })); | ||
|
||
render(<DeploySmokeTest />); | ||
await waitFor(() => | ||
expect(screen.queryByText("Status loading...")).not.toBeInTheDocument() | ||
expect(screen.queryByText(APP_STATUS_LOADING)).not.toBeInTheDocument() | ||
); | ||
expect(screen.getByText("Status returned success :)")); | ||
expect(screen.getByText(APP_STATUS_SUCCESS)); | ||
}); | ||
|
||
it("renders failure when returned from the API endpoint", async () => { | ||
(fetch as FetchMock).mockResponseOnce(JSON.stringify({ status: "DOWN" })); | ||
it("renders failure when returned from the backend API smoke test endpoint", async () => { | ||
(fetch as FetchMock).mockResponse(JSON.stringify({ status: "DOWN" })); | ||
|
||
render(<DeploySmokeTest />); | ||
await waitFor(() => | ||
expect(screen.queryByText(APP_STATUS_LOADING)).not.toBeInTheDocument() | ||
); | ||
expect(screen.getByText(APP_STATUS_FAILURE)); | ||
}); | ||
|
||
it("renders Okta success when returned from the backend API health endpoint", async () => { | ||
(fetch as FetchMock).mockResponse( | ||
JSON.stringify(generateBackendApiHealthResponse()) | ||
); | ||
|
||
render(<DeploySmokeTest />); | ||
await waitFor(() => | ||
expect(screen.queryByText("Status loading...")).not.toBeInTheDocument() | ||
expect(screen.queryByText(OKTA_STATUS_LOADING)).not.toBeInTheDocument() | ||
); | ||
expect(screen.getByText("Status returned failure :(")); | ||
expect(screen.getByText(OKTA_STATUS_SUCCESS)); | ||
}); | ||
}); |
Oops, something went wrong.