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

View File

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

View File

@ -31,13 +31,17 @@
<form [formGroup]="trendzSettingsForm" (ngSubmit)="save()"> <form [formGroup]="trendzSettingsForm" (ngSubmit)="save()">
<fieldset [disabled]="isLoading$ | async"> <fieldset [disabled]="isLoading$ | async">
<section class="tb-trendz-section flex flex-col gt-sm:flex-row"> <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-form-field class="tb-trendz-url mat-block flex-1" subscriptSizing="dynamic">
<mat-label translate>admin.trendz-url</mat-label> <mat-label translate>admin.trendz-url</mat-label>
<input matInput formControlName="trendzUrl"> <input matInput formControlName="trendzUrl">
</mat-form-field> </mat-form-field>
<mat-checkbox class="flex flex-1" formControlName="isTrendzEnabled"> <mat-form-field class="tb-trendz-api-key mat-block flex-1" subscriptSizing="dynamic">
{{ 'admin.trendz-enable' | translate }} <mat-label translate>admin.trendz-api-key</mat-label>
</mat-checkbox> <input matInput formControlName="apiKey">
</mat-form-field>
</section> </section>
<div class="flex w-full flex-row flex-wrap items-center justify-end"> <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"> <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; 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 { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TrendzSettingsService } from '@core/http/trendz-settings.service'; import { TrendzSettingsService } from '@core/http/trendz-settings.service';
import { TrendzSettings } from '@shared/models/trendz-settings.models'; import { TrendzSettings } from '@shared/models/trendz-settings.models';
import { isDefinedAndNotNull } from '@core/utils';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Component({ @Component({
@ -40,8 +39,9 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
ngOnInit() { ngOnInit() {
this.trendzSettingsForm = this.fb.group({ this.trendzSettingsForm = this.fb.group({
isTrendzEnabled: [false],
trendzUrl: [null, [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]], trendzUrl: [null, [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]],
isTrendzEnabled: [false] apiKey: [null]
}); });
this.trendzSettingsService.getTrendzSettings().subscribe((trendzSettings) => { this.trendzSettingsService.getTrendzSettings().subscribe((trendzSettings) => {
@ -67,8 +67,9 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
setTrendzSettings(trendzSettings: TrendzSettings) { setTrendzSettings(trendzSettings: TrendzSettings) {
this.trendzSettingsForm.reset({ this.trendzSettingsForm.reset({
isTrendzEnabled: trendzSettings?.enabled ?? false,
trendzUrl: trendzSettings?.baseUrl, trendzUrl: trendzSettings?.baseUrl,
isTrendzEnabled: trendzSettings?.enabled ?? false apiKey: trendzSettings?.apiKey
}); });
this.toggleUrlRequired(this.trendzSettingsForm.get('isTrendzEnabled').value); this.toggleUrlRequired(this.trendzSettingsForm.get('isTrendzEnabled').value);
@ -79,16 +80,19 @@ export class TrendzSettingsComponent extends PageComponent implements OnInit, Ha
} }
save(): void { save(): void {
const trendzUrl = this.trendzSettingsForm.get('trendzUrl').value;
const isTrendzEnabled = this.trendzSettingsForm.get('isTrendzEnabled').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 = { const trendzSettings: TrendzSettings = {
enabled: isTrendzEnabled,
baseUrl: trendzUrl, baseUrl: trendzUrl,
enabled: isTrendzEnabled apiKey: apiKey
}; };
this.trendzSettingsService.saveTrendzSettings(trendzSettings).subscribe(() => { this.trendzSettingsService.saveTrendzSettings(trendzSettings)
this.setTrendzSettings(trendzSettings); .subscribe(() => {
}) this.setTrendzSettings(trendzSettings);
})
} }
} }

View File

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

View File

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