-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98cf424
commit b7e4f6f
Showing
3 changed files
with
178 additions
and
40 deletions.
There are no files selected for viewing
83 changes: 58 additions & 25 deletions
83
marketplace-ui/src/app/modules/product/product-detail/product-detail.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,80 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ProductDetailService } from './product-detail.service'; | ||
import { DisplayValue } from '../../../shared/models/display-value.model'; | ||
import { HttpClient, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; | ||
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; | ||
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; | ||
import { environment } from '../../../../environments/environment'; | ||
import { ProductSecurityInfo } from '../../../shared/models/product-security-info-model'; | ||
import { SecurityMonitorService } from '../../security-monitor/security-monitor.service'; | ||
import { SecurityMonitorComponent } from '../../security-monitor/security-monitor.component'; | ||
|
||
describe('ProductDetailService', () => { | ||
let service: ProductDetailService; | ||
describe('SecurityMonitorService', () => { | ||
let service: SecurityMonitorService; | ||
let httpMock: HttpTestingController; | ||
let httpClient: jasmine.SpyObj<HttpClient>; | ||
|
||
const mockApiUrl = environment.apiUrl + '/api/security-monitor'; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ProductDetailService, | ||
imports: [SecurityMonitorComponent], | ||
providers: [ | ||
SecurityMonitorService, | ||
provideHttpClient(withInterceptorsFromDi()), | ||
provideHttpClientTesting(), | ||
{ provide: HttpClient, useValue: httpClient } | ||
] | ||
], | ||
}); | ||
service = TestBed.inject(ProductDetailService); | ||
|
||
service = TestBed.inject(SecurityMonitorService); | ||
httpMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
afterEach(() => { | ||
httpMock.verify(); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should have a default productId signal', () => { | ||
expect(service.productId()).toBe(''); | ||
}); | ||
it('should call API with token and return security details', () => { | ||
const mockToken = 'valid-token'; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "valid-token" is used as
authorization header Error loading related location Loading |
||
const mockResponse: ProductSecurityInfo[] = [ | ||
{ | ||
repoName: 'repo1', | ||
visibility: 'public', | ||
archived: false, | ||
dependabot: { status: 'ENABLED', alerts: {} }, | ||
codeScanning: { status: 'ENABLED', alerts: {} }, | ||
secretScanning: { status: 'ENABLED', numberOfAlerts: 0 }, | ||
branchProtectionEnabled: true, | ||
lastCommitSHA: '12345', | ||
lastCommitDate: new Date(), | ||
}, | ||
]; | ||
|
||
it('should update productId signal', () => { | ||
const newProductId = '12345'; | ||
service.productId.set(newProductId); | ||
expect(service.productId()).toBe(newProductId); | ||
}); | ||
service.getSecurityDetails(mockToken).subscribe((data) => { | ||
expect(data).toEqual(mockResponse); | ||
}); | ||
|
||
const req = httpMock.expectOne(mockApiUrl); | ||
expect(req.request.method).toBe('GET'); | ||
expect(req.request.headers.get('Authorization')).toBe(`Bearer ${mockToken}`); | ||
|
||
it('should have a default productNames signal', () => { | ||
expect(service.productNames()).toEqual({} as DisplayValue); | ||
req.flush(mockResponse); | ||
}); | ||
|
||
it('should update productNames signal', () => { | ||
const newProductNames: DisplayValue = { en: 'en', de: 'de' }; | ||
service.productNames.set(newProductNames); | ||
expect(service.productNames()).toEqual(newProductNames); | ||
it('should handle error response gracefully', () => { | ||
const mockToken = 'invalid-token'; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "invalid-token" is used as
authorization header Error loading related location Loading |
||
|
||
service.getSecurityDetails(mockToken).subscribe({ | ||
next: () => fail('Expected an error, but received data.'), | ||
error: (error) => { | ||
expect(error.status).toBe(401); | ||
}, | ||
}); | ||
|
||
const req = httpMock.expectOne(mockApiUrl); | ||
expect(req.request.method).toBe('GET'); | ||
expect(req.request.headers.get('Authorization')).toBe(`Bearer ${mockToken}`); | ||
|
||
req.flush({ message: 'Unauthorized' }, { status: 401, statusText: 'Unauthorized' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
marketplace-ui/src/app/modules/security-monitor/security-monitor.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; | ||
import { SecurityMonitorService } from './security-monitor.service'; | ||
import { environment } from '../../../environments/environment'; | ||
import { ProductSecurityInfo } from '../../shared/models/product-security-info-model'; | ||
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; | ||
|
||
describe('SecurityMonitorService', () => { | ||
let service: SecurityMonitorService; | ||
let httpMock: HttpTestingController; | ||
|
||
const mockApiUrl = environment.apiUrl + '/api/security-monitor'; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
SecurityMonitorService, | ||
provideHttpClient(withInterceptorsFromDi()), | ||
provideHttpClientTesting() | ||
] | ||
}); | ||
service = TestBed.inject(SecurityMonitorService); | ||
httpMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
afterEach(() => { | ||
httpMock.verify(); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should call API with token and return security details', () => { | ||
const mockToken = 'valid-token'; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "valid-token" is used as
authorization header Error loading related location Loading |
||
const mockResponse: ProductSecurityInfo[] = [ | ||
{ | ||
repoName: 'repo1', | ||
visibility: 'public', | ||
archived: false, | ||
dependabot: { status: 'ENABLED', alerts: {} }, | ||
codeScanning: { status: 'ENABLED', alerts: {} }, | ||
secretScanning: { status: 'ENABLED', numberOfAlerts: 0 }, | ||
branchProtectionEnabled: true, | ||
lastCommitSHA: '12345', | ||
lastCommitDate: new Date(), | ||
}, | ||
]; | ||
|
||
service.getSecurityDetails(mockToken).subscribe((data) => { | ||
expect(data).toEqual(mockResponse); | ||
}); | ||
|
||
const req = httpMock.expectOne(mockApiUrl); | ||
expect(req.request.method).toBe('GET'); | ||
expect(req.request.headers.get('Authorization')).toBe(`Bearer ${mockToken}`); | ||
|
||
req.flush(mockResponse); | ||
}); | ||
|
||
it('should handle error response gracefully', () => { | ||
const mockToken = 'invalid-token'; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "invalid-token" is used as
authorization header Error loading related location Loading |
||
|
||
service.getSecurityDetails(mockToken).subscribe({ | ||
next: () => fail('Expected an error, but received data.'), | ||
error: (error) => { | ||
expect(error.status).toBe(401); | ||
}, | ||
}); | ||
|
||
const req = httpMock.expectOne(mockApiUrl); | ||
expect(req.request.method).toBe('GET'); | ||
expect(req.request.headers.get('Authorization')).toBe(`Bearer ${mockToken}`); | ||
|
||
req.flush({ message: 'Unauthorized' }, { status: 401, statusText: 'Unauthorized' }); | ||
}); | ||
}); |