Skip to content

Commit

Permalink
NAS-134329 / 25.10 / Show core.download job errors (#11662)
Browse files Browse the repository at this point in the history
  • Loading branch information
undsoft authored Mar 5, 2025
1 parent efb6ef3 commit ce23e14
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatButtonHarness } from '@angular/material/button/testing';
import { createComponentFactory, mockProvider, Spectator } from '@ngneat/spectator/jest';
import { provideMockStore } from '@ngrx/store/testing';
import { mockCall, mockJob, mockApi } from 'app/core/testing/utils/mock-api.utils';
import { of } from 'rxjs';
import { mockJob, mockApi } from 'app/core/testing/utils/mock-api.utils';
import { ControllerType } from 'app/enums/controller-type.enum';
import { JobState } from 'app/enums/job-state.enum';
import { ApiJobMethod } from 'app/interfaces/api/api-job-directory.interface';
Expand All @@ -27,10 +28,9 @@ describe('ExportButtonComponent', () => {
providers: [
mockApi([
mockJob(jobMethod, { result: '/path/data.csv', state: JobState.Success } as Job<string>),
mockCall('core.download', [33456, '/_download/33456?auth_token=1234567890']),
]),
mockProvider(DownloadService, {
downloadUrl: jest.fn(),
coreDownload: jest.fn(() => of(undefined)),
}),
provideMockStore({
selectors: [
Expand Down Expand Up @@ -62,12 +62,12 @@ describe('ExportButtonComponent', () => {
'query-filters': [],
'query-options': {},
}]);
expect(spectator.inject(ApiService).call).toHaveBeenCalledWith('core.download', [jobMethod, [{}], '/path/data.csv']);
expect(spectator.inject(DownloadService).downloadUrl).toHaveBeenLastCalledWith(
'/_download/33456?auth_token=1234567890',
'data.csv',
'text/csv',
);
expect(spectator.inject(DownloadService).coreDownload).toHaveBeenLastCalledWith({
arguments: [{}],
fileName: 'data.csv',
method: 'audit.export',
mimeType: 'text/csv',
});
});

it('downloads a file when Export As CSV button is pressed with options', async () => {
Expand All @@ -92,11 +92,11 @@ describe('ExportButtonComponent', () => {
'query-options': { order_by: ['-service'] },
remote_controller: true,
}]);
expect(spectator.inject(ApiService).call).toHaveBeenCalledWith('core.download', [jobMethod, [{}], '/path/data.csv']);
expect(spectator.inject(DownloadService).downloadUrl).toHaveBeenLastCalledWith(
'/_download/33456?auth_token=1234567890',
'data.csv',
'text/csv',
);
expect(spectator.inject(DownloadService).coreDownload).toHaveBeenLastCalledWith({
arguments: [{}],
fileName: 'data.csv',
method: 'audit.export',
mimeType: 'text/csv',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ export class ExportButtonComponent<T, M extends ApiJobMethod> {
customArguments.report_name = url;
}

return this.api.call('core.download', [downloadMethod, [customArguments], url]);
return this.download.coreDownload({
method: downloadMethod,
arguments: [customArguments],
fileName: `${this.filename()}.${this.fileType()}`,
mimeType: this.fileMimeType(),
});
}),
switchMap(([, url]) => this.download.downloadUrl(url, `${this.filename()}.${this.fileType()}`, this.fileMimeType())),
catchError((error: unknown) => {
this.isLoading = false;
this.cdr.markForCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,47 +233,30 @@ export class CertificateAuthorityListComponent implements OnInit {
const isCsr = certificate.cert_type_CSR;
const path = isCsr ? certificate.csr_path : certificate.certificate_path;
const fileName = `${certificate.name}.${isCsr ? 'csr' : 'crt'}`;
this.api
.call('core.download', ['filesystem.get', [path], fileName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'application/x-x509-user-cert';
this.download
.streamDownloadFile(url, fileName, mimetype)
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe((file) => {
this.download.downloadBlob(file, fileName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});
const keyName = `${certificate.name}.key`;
this.api
.call('core.download', ['filesystem.get', [certificate.privatekey_path], keyName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'text/plain';
this.download
.streamDownloadFile(url, keyName, mimetype)
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe((file) => {
this.download.downloadBlob(file, keyName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});

this.download.coreDownload({
fileName,
method: 'filesystem.get',
arguments: [path],
mimeType: 'application/x-x509-user-cert',
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();

this.download.coreDownload({
fileName: `${certificate.name}.key`,
method: 'filesystem.get',
arguments: [certificate.privatekey_path],
mimeType: 'text/plain',
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();
}

doRevoke(certificate: CertificateAuthority): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,44 +227,30 @@ export class CertificateListComponent implements OnInit {
const isCsr = certificate.cert_type_CSR;
const path = isCsr ? certificate.csr_path : certificate.certificate_path;
const fileName = `${certificate.name}.${isCsr ? 'csr' : 'crt'}`;
this.api
.call('core.download', ['filesystem.get', [path], fileName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'application/x-x509-user-cert';
this.download
.streamDownloadFile(url, fileName, mimetype)
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe((file) => {
this.download.downloadBlob(file, fileName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});
const keyName = `${certificate.name}.key`;
this.api
.call('core.download', ['filesystem.get', [certificate.privatekey_path], keyName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'text/plain';
this.download
.streamDownloadFile(url, keyName, mimetype)
.pipe(untilDestroyed(this))
.subscribe((file) => {
this.download.downloadBlob(file, keyName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});

this.download.coreDownload({
fileName,
method: 'filesystem.get',
arguments: [path],
mimeType: 'application/x-x509-user-cert',
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();

this.download.coreDownload({
method: 'filesystem.get',
fileName: `${certificate.name}.key`,
arguments: [certificate.privatekey_path],
mimeType: 'text/plain',
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();
}

doRevoke(certificate: Certificate): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,47 +209,30 @@ export class CertificateSigningRequestsListComponent implements OnInit {
const isCsr = certificate.cert_type_CSR;
const path = isCsr ? certificate.csr_path : certificate.certificate_path;
const fileName = `${certificate.name}.${isCsr ? 'csr' : 'crt'}`;
this.api
.call('core.download', ['filesystem.get', [path], fileName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'application/x-x509-user-cert';
this.download
.streamDownloadFile(url, fileName, mimetype)
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe((file) => {
this.download.downloadBlob(file, fileName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});
const keyName = `${certificate.name}.key`;
this.api
.call('core.download', ['filesystem.get', [certificate.privatekey_path], keyName])
.pipe(untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'text/plain';
this.download
.streamDownloadFile(url, keyName, mimetype)
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe((file) => {
this.download.downloadBlob(file, keyName);
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});

this.download.coreDownload({
fileName,
method: 'filesystem.get',
mimeType: 'application/x-x509-user-cert',
arguments: [path],
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();

this.download.coreDownload({
method: 'filesystem.get',
fileName: `${certificate.name}.key`,
arguments: [certificate.privatekey_path],
mimeType: 'text/plain',
})
.pipe(
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();
}

doCreateAcmeCert(csr: Certificate): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe('ReplicationListComponent', () => {
mockCall('replication.update', { ...tasks[0], enabled: true }),
mockJob('replication.run', fakeSuccessfulJob()),
mockCall('replication.delete'),
mockCall('core.download', [undefined, 'http://someurl/file.json']),
]),
mockProvider(SlideIn, {
open: jest.fn(() => of()),
Expand All @@ -160,8 +159,7 @@ describe('ReplicationListComponent', () => {
})),
}),
mockProvider(DownloadService, {
streamDownloadFile: jest.fn(() => of()),
downloadBlob: jest.fn(),
coreDownload: jest.fn(() => of(undefined)),
}),
],
});
Expand Down Expand Up @@ -265,15 +263,11 @@ describe('ReplicationListComponent', () => {
const downloadKeysButtons = await loader.getHarness(MatButtonHarness.with({ text: 'Download Keys' }));
await downloadKeysButtons.click();

expect(spectator.inject(ApiService).call).toHaveBeenCalledWith('core.download', [
'pool.dataset.export_keys_for_replication',
[1],
'pewl - pewl_encryption_keys.json',
]);
expect(spectator.inject(DownloadService).streamDownloadFile).toHaveBeenCalledWith(
'http://someurl/file.json',
'pewl - pewl_encryption_keys.json',
'application/json',
);
expect(spectator.inject(DownloadService).coreDownload).toHaveBeenCalledWith({
arguments: [1],
fileName: 'pewl - pewl_encryption_keys.json',
method: 'pool.dataset.export_keys_for_replication',
mimeType: 'application/json',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -302,24 +302,18 @@ export class ReplicationListComponent implements OnInit {

downloadKeys(row: ReplicationTask): void {
const fileName = `${row.name}_encryption_keys.json`;
this.api.call('core.download', ['pool.dataset.export_keys_for_replication', [row.id], fileName])
.pipe(this.appLoader.withLoader(), untilDestroyed(this))
.subscribe({
next: ([, url]) => {
const mimetype = 'application/json';
this.download.streamDownloadFile(url, fileName, mimetype).pipe(untilDestroyed(this)).subscribe({
next: (file) => {
this.download.downloadBlob(file, fileName);
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});
},
error: (err: unknown) => {
this.dialogService.error(this.errorHandler.parseError(err));
},
});
this.download.coreDownload({
fileName,
method: 'pool.dataset.export_keys_for_replication',
arguments: [row.id],
mimeType: 'application/json',
})
.pipe(
this.appLoader.withLoader(),
this.errorHandler.catchError(),
untilDestroyed(this),
)
.subscribe();
}

private onChangeEnabledState(replicationTask: ReplicationTask): void {
Expand Down
Loading

0 comments on commit ce23e14

Please sign in to comment.