Skip to content

Commit a507ccb

Browse files
committed
Added unit tests + Keycloak Auth URL is now configurable
1 parent 3e94ba7 commit a507ccb

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

__snapshots__/compas-settings.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
value="http://localhost:9091/compas-cim-mapping"
2020
>
2121
</mwc-textfield>
22+
<mwc-textfield
23+
dialoginitialfocus=""
24+
id="keycloakAuthUrl"
25+
label="[compas.settings.keycloakAuthUrl]"
26+
required=""
27+
value="http://localhost:8089/auth/"
28+
>
29+
</mwc-textfield>
2230
<mwc-button style="--mdc-theme-primary: var(--mdc-theme-error)">
2331
[reset]
2432
</mwc-button>

src/compas/CompasSettingsElement.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {TextFieldBase} from "@material/mwc-textfield/mwc-textfield-base";
88
export type CompasSettingsRecord = {
99
sclDataServiceUrl: string;
1010
cimMappingServiceUrl: string;
11+
keycloakAuthUrl: string;
1112
};
1213
export const defaults: CompasSettingsRecord = {
1314
sclDataServiceUrl: 'http://localhost:9090/compas-scl-data-service',
14-
cimMappingServiceUrl: 'http://localhost:9091/compas-cim-mapping'
15+
cimMappingServiceUrl: 'http://localhost:9091/compas-cim-mapping',
16+
keycloakAuthUrl: 'http://localhost:8089/auth/'
1517
};
1618

1719
export function CompasSettings() {
@@ -21,6 +23,7 @@ export function CompasSettings() {
2123
return {
2224
sclDataServiceUrl: this.getCompasSetting('sclDataServiceUrl'),
2325
cimMappingServiceUrl: this.getCompasSetting('cimMappingServiceUrl'),
26+
keycloakAuthUrl: this.getCompasSetting('keycloakAuthUrl')
2427
};
2528
},
2629

@@ -52,9 +55,14 @@ export class CompasSettingsElement extends LitElement {
5255
return <TextFieldBase>this.shadowRoot!.querySelector('mwc-textfield[id="cimMappingServiceUrl"]');
5356
}
5457

58+
getKeycloakAuthUrlField(): TextFieldBase {
59+
return <TextFieldBase>this.shadowRoot!.querySelector('mwc-textfield[id="keycloakAuthUrl"]');
60+
}
61+
5562
valid(): boolean {
5663
return this.getSclDataServiceUrlField().checkValidity()
57-
&& this.getCimMappingServiceUrlField().checkValidity();
64+
&& this.getCimMappingServiceUrlField().checkValidity()
65+
&& this.getKeycloakAuthUrlField().checkValidity();
5866
}
5967

6068
save(): boolean {
@@ -65,6 +73,7 @@ export class CompasSettingsElement extends LitElement {
6573
// Update settings from TextField.
6674
CompasSettings().setCompasSetting('sclDataServiceUrl', this.getSclDataServiceUrlField().value);
6775
CompasSettings().setCompasSetting('cimMappingServiceUrl', this.getCimMappingServiceUrlField().value);
76+
CompasSettings().setCompasSetting('keycloakAuthUrl', this.getKeycloakAuthUrlField().value);
6877
return true;
6978
}
7079

@@ -91,6 +100,10 @@ export class CompasSettingsElement extends LitElement {
91100
label="${translate('compas.settings.cimMappingServiceUrl')}"
92101
value="${this.compasSettings.cimMappingServiceUrl}" required>
93102
</mwc-textfield>
103+
<mwc-textfield dialogInitialFocus id="keycloakAuthUrl"
104+
label="${translate('compas.settings.keycloakAuthUrl')}"
105+
value="${this.compasSettings.keycloakAuthUrl}" required>
106+
</mwc-textfield>
94107
95108
<mwc-button style="--mdc-theme-primary: var(--mdc-theme-error)"
96109
@click=${() => {

src/state/auth/KeycloakManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Keycloak from 'keycloak-js';
2+
import { CompasSettings } from '../../compas/CompasSettingsElement';
23

34
const keycloakInstance = Keycloak({
4-
url: 'http://localhost:8089/auth/',
5+
url: CompasSettings().compasSettings.keycloakAuthUrl,
56
realm: 'compas',
67
clientId: 'openscd'
78
});

src/translations/de.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ export const de: Translations = {
370370
name: "CoMPAS Einstellungen",
371371
sclDataServiceUrl: "CoMPAS SCL Data Service URL",
372372
cimMappingServiceUrl: "CoMPAS CIM Mapping Service URL",
373+
keycloakAuthUrl: "Keycloak Auth URL",
373374
}
374375
}
375376
};

src/translations/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ export const en = {
367367
name: "CoMPAS Settings",
368368
sclDataServiceUrl: "CoMPAS SCL Data Service URL",
369369
cimMappingServiceUrl: "CoMPAS CIM Mapping Service URL",
370+
keycloakAuthUrl: "Keycloak Auth URL",
370371
}
371372
}
372373
};

test/unit/compas/CompasSettingsElement.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,35 @@ describe('compas-settings', () => {
2323
it('stores settings to localStorage', () => {
2424
CompasSettings().setCompasSetting('sclDataServiceUrl', 'http://localhost:9090/compas-scl-data-service');
2525
CompasSettings().setCompasSetting('cimMappingServiceUrl', 'http://localhost:9091/compas-cim-mapping');
26+
CompasSettings().setCompasSetting('keycloakAuthUrl', 'http://localhost:8089/auth/');
2627
expect(localStorage.getItem('sclDataServiceUrl')).to.equal('http://localhost:9090/compas-scl-data-service');
2728
expect(localStorage.getItem('cimMappingServiceUrl')).to.equal('http://localhost:9091/compas-cim-mapping');
29+
expect(localStorage.getItem('keycloakAuthUrl')).to.equal('http://localhost:8089/auth/');
2830
});
2931

3032
it('retrieves settings from localStorage', () => {
3133
localStorage.setItem('sclDataServiceUrl', 'http://localhost:9090/compas-scl-data-service');
3234
localStorage.setItem('cimMappingServiceUrl', 'http://localhost:9091/compas-cim-mapping');
35+
localStorage.setItem('keycloakAuthUrl', 'http://localhost:8089/auth/');
3336
expect(CompasSettings().compasSettings).to.have.property('sclDataServiceUrl', 'http://localhost:9090/compas-scl-data-service');
3437
expect(CompasSettings().compasSettings).to.have.property('cimMappingServiceUrl', 'http://localhost:9091/compas-cim-mapping');
38+
expect(CompasSettings().compasSettings).to.have.property('keycloakAuthUrl', 'http://localhost:8089/auth/');
3539
});
3640

3741
it('saves chosen settings on save button click', async () => {
3842
await element.updateComplete;
3943

4044
element.getSclDataServiceUrlField().value = 'http://localhost:9091/compas-scl-data-service';
4145
element.getCimMappingServiceUrlField().value = 'http://localhost:9092/compas-cim-mapping';
46+
element.getKeycloakAuthUrlField().value = 'http://localhost:9089/auth/';
4247
await element.getSclDataServiceUrlField().updateComplete;
4348
await element.getCimMappingServiceUrlField().updateComplete;
49+
await element.getKeycloakAuthUrlField().updateComplete;
4450

4551
expect(element.save()).to.be.true;
4652
expect(element.compasSettings).to.have.property('sclDataServiceUrl', 'http://localhost:9091/compas-scl-data-service');
4753
expect(element.compasSettings).to.have.property('cimMappingServiceUrl', 'http://localhost:9092/compas-cim-mapping');
54+
expect(element.compasSettings).to.have.property('keycloakAuthUrl', 'http://localhost:9089/auth/');
4855
});
4956

5057
it('save will not be done when invalid value (Scl Data Service)', async () => {
@@ -65,10 +72,20 @@ describe('compas-settings', () => {
6572
expect(element).to.have.deep.property('compasSettings', defaults);
6673
});
6774

75+
it('save will not be done when invalid value (Keycloak Auth URL)', async () => {
76+
await element.updateComplete;
77+
element.getKeycloakAuthUrlField().value = '';
78+
await element.getKeycloakAuthUrlField().updateComplete;
79+
80+
expect(element.save()).to.be.false;
81+
expect(element).to.have.deep.property('compasSettings', defaults);
82+
});
83+
6884
it('resets settings to default on reset button click', async () => {
6985
await element.updateComplete;
7086
CompasSettings().setCompasSetting('sclDataServiceUrl', 'http://localhost:9091/compas-scl-data-service');
7187
CompasSettings().setCompasSetting('cimMappingServiceUrl', 'http://localhost:9092/compas-cim-mapping');
88+
CompasSettings().setCompasSetting('keycloakAuthUrl', 'http://localhost:9089/auth/');
7289

7390
expect(element).to.not.have.deep.property('compasSettings', defaults);
7491
expect(element.reset()).to.be.true;

0 commit comments

Comments
 (0)