add api key

This commit is contained in:
Anatolii Davydko 2025-05-22 12:49:11 +01:00
parent f6511f2a44
commit 6b651e1992
7 changed files with 46 additions and 15 deletions

View File

@ -27,6 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class TrendzControllerTest extends AbstractControllerTest {
private final String trendzUrl = "https://some.domain.com:18888/also_necessary_prefix";
private final String apiKey = "$2a$10$iDjfqYmnrw9gkdw4XhgzFOU.R/pVz3OKgXOdpbR2LuXaKatGcGLiG";
@Before
public void setUp() throws Exception {
@ -35,6 +36,7 @@ public class TrendzControllerTest extends AbstractControllerTest {
TrendzSettings trendzSettings = new TrendzSettings();
trendzSettings.setEnabled(true);
trendzSettings.setBaseUrl(trendzUrl);
trendzSettings.setApiKey(apiKey);
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk());
}
@ -48,9 +50,12 @@ public class TrendzControllerTest extends AbstractControllerTest {
assertThat(trendzSettings).isNotNull();
assertThat(trendzSettings.isEnabled()).isTrue();
assertThat(trendzSettings.getBaseUrl()).isEqualTo(trendzUrl);
trendzSettings.setApiKey(apiKey);
String updatedUrl = "https://some.domain.com:18888/tenant_trendz";
String updatedApiKey = "$2a$10$aRR0bHa8rtzP5jRcE72vp.hRFsGQz4MGIs62oogLbfOCFK3.RIESG";
trendzSettings.setBaseUrl(updatedUrl);
trendzSettings.setApiKey(updatedApiKey);
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk());
@ -65,6 +70,7 @@ public class TrendzControllerTest extends AbstractControllerTest {
TrendzSettings newTrendzSettings = new TrendzSettings();
newTrendzSettings.setEnabled(true);
newTrendzSettings.setBaseUrl("https://some.domain.com:18888/customer_trendz");
newTrendzSettings.setApiKey("some_api_key");
doPost("/api/trendz/settings", newTrendzSettings).andExpect(status().isForbidden());
@ -72,6 +78,6 @@ public class TrendzControllerTest extends AbstractControllerTest {
assertThat(fetchedTrendzSettings).isNotNull();
assertThat(fetchedTrendzSettings.isEnabled()).isTrue();
assertThat(fetchedTrendzSettings.getBaseUrl()).isEqualTo(trendzUrl);
assertThat(fetchedTrendzSettings.getApiKey()).isEqualTo(apiKey);
}
}

View File

@ -15,12 +15,16 @@
*/
package org.thingsboard.server.common.data.trendz;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TrendzSettings {
private boolean enabled;
private String baseUrl;
private String apiKey;
}

View File

@ -31,13 +31,17 @@
<form [formGroup]="trendzSettingsForm" (ngSubmit)="save()">
<fieldset [disabled]="isLoading$ | async">
<section class="tb-trendz-section flex flex-col gt-sm:flex-row">
<mat-checkbox class="flex flex-1" formControlName="isTrendzEnabled">
{{ 'admin.trendz-enable' | translate }}
</mat-checkbox>
<mat-form-field class="tb-trendz-url mat-block flex-1" subscriptSizing="dynamic">
<mat-label translate>admin.trendz-url</mat-label>
<input matInput formControlName="trendzUrl">
</mat-form-field>
<mat-checkbox class="flex flex-1" formControlName="isTrendzEnabled">
{{ 'admin.trendz-enable' | translate }}
</mat-checkbox>
<mat-form-field class="tb-trendz-api-key mat-block flex-1" subscriptSizing="dynamic">
<mat-label translate>admin.trendz-api-key</mat-label>
<input matInput formControlName="apiKey">
</mat-form-field>
</section>
<div class="flex w-full flex-row flex-wrap items-center justify-end">
<button mat-button mat-raised-button color="primary" [disabled]="(isLoading$ | async) || trendzSettingsForm.invalid || !trendzSettingsForm.dirty" type="submit">

View File

@ -33,4 +33,14 @@
padding-bottom: 12px;
}
}
.tb-trendz-api-key {
@media #{$mat-gt-sm} {
padding-right: 12px;
}
@media #{$mat-lt-md} {
padding-bottom: 12px;
}
}
}

View File

@ -20,7 +20,6 @@ import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TrendzSettingsService } from '@core/http/trendz-settings.service';
import { TrendzSettings } from '@shared/models/trendz-settings.models';
import { isDefinedAndNotNull } from '@core/utils';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Component({
@ -40,8 +39,9 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
ngOnInit() {
this.trendzSettingsForm = this.fb.group({
isTrendzEnabled: [false],
trendzUrl: [null, [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]],
isTrendzEnabled: [false]
apiKey: [null]
});
this.trendzSettingsService.getTrendzSettings().subscribe((trendzSettings) => {
@ -67,8 +67,9 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
setTrendzSettings(trendzSettings: TrendzSettings) {
this.trendzSettingsForm.reset({
isTrendzEnabled: trendzSettings?.enabled ?? false,
trendzUrl: trendzSettings?.baseUrl,
isTrendzEnabled: trendzSettings?.enabled ?? false
apiKey: trendzSettings?.apiKey
});
this.toggleUrlRequired(this.trendzSettingsForm.get('isTrendzEnabled').value);
@ -79,16 +80,19 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
}
save(): void {
const trendzUrl = this.trendzSettingsForm.get('trendzUrl').value;
const isTrendzEnabled = this.trendzSettingsForm.get('isTrendzEnabled').value;
const trendzUrl = this.trendzSettingsForm.get('trendzUrl').value;
const apiKey = this.trendzSettingsForm.get('apiKey').value;
const trendzSettings: TrendzSettings = {
enabled: isTrendzEnabled,
baseUrl: trendzUrl,
enabled: isTrendzEnabled
apiKey: apiKey
};
this.trendzSettingsService.saveTrendzSettings(trendzSettings).subscribe(() => {
this.setTrendzSettings(trendzSettings);
})
this.trendzSettingsService.saveTrendzSettings(trendzSettings)
.subscribe(() => {
this.setTrendzSettings(trendzSettings);
})
}
}

View File

@ -15,11 +15,13 @@
///
export interface TrendzSettings {
enabled: boolean,
baseUrl: string,
enabled: boolean
apiKey: string
}
export const initialTrendzSettings: TrendzSettings = {
enabled: false,
baseUrl: null,
enabled: false
apiKey: null
}

View File

@ -549,6 +549,7 @@
"trendz": "Trendz",
"trendz-settings": "Trendz settings",
"trendz-url": "Trendz URL",
"trendz-api-key": "Trendz API key",
"trendz-enable": "Enable Trendz"
},
"alarm": {