Skip to content

Commit

Permalink
Fix mock SDS in test environment (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccar authored Sep 24, 2024
1 parent c74b8a9 commit 5e9e4e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ To update the design system version, you need to update the version within the C
| OIDC_TOKEN_LEEWAY_IN_SECONDS | The leeway to use when validating OIDC tokens | 300 |
| SDS_OAUTH2_CLIENT_ID | The OAuth2 Client ID used when setting up IAP on the SDS | |
| CIR_OAUTH2_CLIENT_ID | The OAuth2 Client ID used when setting up IAP on the CIR | |
| SDS_ENABLED_IN_ENV | Signifies if the SDS service is enabled in the environment | true |
7 changes: 7 additions & 0 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type page struct {
CirSchemas []surveys.CIMetadata
AccountServiceURL string
AccountServiceLogOutURL string
SdsEnabled string
}

func getStatusPage(w http.ResponseWriter, r *http.Request) {
Expand All @@ -77,6 +78,7 @@ func getLaunchHandler(w http.ResponseWriter, r *http.Request) {
CirSchemas: surveys.GetAvailableSchemasFromCIR(),
AccountServiceURL: getAccountServiceURL(r),
AccountServiceLogOutURL: getAccountServiceURL(r),
SdsEnabled: settings.Get("SDS_ENABLED_IN_ENV"),
}
serveTemplate("launch.html", p, w, r)
}
Expand Down Expand Up @@ -116,6 +118,11 @@ func getSurveyDataHandler(w http.ResponseWriter, r *http.Request) {
func getSupplementaryDataHandler(w http.ResponseWriter, r *http.Request) {
surveyId := r.URL.Query().Get("survey_id")
periodId := r.URL.Query().Get("period_id")
sdsEnabled := settings.Get("SDS_ENABLED_IN_ENV")

if sdsEnabled != "true" {
return
}

datasets, err := surveys.GetSupplementaryDataSets(surveyId, periodId)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
setSetting("OIDC_TOKEN_BACKEND", "local")
setSetting("SDS_OAUTH2_CLIENT_ID", "")
setSetting("CIR_OAUTH2_CLIENT_ID", "")
setSetting("SDS_ENABLED_IN_ENV", "true")
}

// Get returns the value for the specified named setting
Expand Down
18 changes: 10 additions & 8 deletions static/javascript/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ function setLaunchType(launchType) {
const schemaName = document.querySelector("#schema_name");
const remoteSchemaUrl = document.querySelector("#remote-schema-url");
const cirSchemas = document.querySelector("#cir-schemas");
console.log(schemaName);
const remoteSchemaSurveyType = document.querySelector(
"#remote-schema-survey-type",
);

if (["cir", "remote", "url"].includes(launchType)) {
if (schemaName.selectedIndex) {
Expand Down Expand Up @@ -380,9 +382,9 @@ function getInputField(
return `<input ${readOnly} id="${fieldName}" name="${fieldName}" type="${type}" ${value} class="ons-input ons-input--text ons-input--w-20" onchange="${onChangeCallback}">`;
}

async function loadSDSDatasetMetadata(survey_id, period_id) {
if (survey_id && period_id) {
const sds_dataset_metadata_url = `/supplementary-data?survey_id=${survey_id}&period_id=${period_id}`;
async function loadSDSDatasetMetadata(surveyId, periodId, sdsEnabled) {
if (surveyId && periodId && sdsEnabled) {
const sds_dataset_metadata_url = `/supplementary-data?survey_id=${surveyId}&period_id=${periodId}`;
return await getDataAsync(sds_dataset_metadata_url);
}
return null;
Expand Down Expand Up @@ -410,11 +412,11 @@ function showCIRMetadata(cirInstrumentId, cirSchema) {
setTabIndex("cir_metadata_detail", 0);
}

function updateSDSDropdown() {
function updateSDSDropdown(sdsEnabled) {
const surveyId = schemaSurveyId;
const periodId = document.getElementById("period_id")?.value;
const sdsDatasetIdElement = document.querySelector("#sds_dataset_id");
loadSDSDatasetMetadata(surveyId, periodId)
loadSDSDatasetMetadata(surveyId, periodId, sdsEnabled)
.then((sds_metadata_response) => {
if (sds_metadata_response?.length) {
document.querySelector("#supplementary_data").innerHTML = "";
Expand Down Expand Up @@ -497,7 +499,7 @@ function loadSchemaMetadata(schemaName, schemaUrl, cirInstrumentId) {
? schema_response.survey_id
: defaultValue,
false,
"updateSDSDropdown()",
"updateSDSDropdown(sdsEnabled)",
);
} else if (fieldName === "sds_dataset_id") {
return `<select id="${fieldName}" name="${fieldName}" class="ons-input ons-input--select ons-input--w-20" onchange="loadSupplementaryDataInfo()"></select>`;
Expand All @@ -507,7 +509,7 @@ function loadSchemaMetadata(schemaName, schemaUrl, cirInstrumentId) {
})()}</div>`;
})
.join("");
updateSDSDropdown();
updateSDSDropdown(sdsEnabled);
} else {
document.querySelector("#survey_metadata").innerHTML =
"No metadata required for this survey";
Expand Down
1 change: 1 addition & 0 deletions templates/launch.html
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ <h2 class="ons-details__title">Runner Metadata</h2>
</div>
</form>
<script src="https://cdn.ons.gov.uk/sdc/design-system/70.0.7/scripts/main.js"></script>
<script>const sdsEnabled = "{{ .SdsEnabled }}" === "true"</script>
<script src="../static/javascript/launch.js"></script>
</body>
{{ end }}

0 comments on commit 5e9e4e9

Please sign in to comment.