ciyidogan commited on
Commit
9ea1f78
·
verified ·
1 Parent(s): 5162e80

Update flare-ui/src/app/components/test/test.component.ts

Browse files
flare-ui/src/app/components/test/test.component.ts CHANGED
@@ -54,10 +54,11 @@ interface TestCase {
54
  templateUrl: './test.component.html',
55
  styleUrls: ['./test.component.scss']
56
  })
57
- export class TestComponent implements OnInit {
58
  private apiService = inject(ApiService);
59
  private authService = inject(AuthService);
60
  private http = inject(HttpClient);
 
61
 
62
  running = false;
63
  currentTest: string = '';
@@ -124,6 +125,11 @@ export class TestComponent implements OnInit {
124
  this.updateAllSelected();
125
  }
126
 
 
 
 
 
 
127
  updateAllSelected() {
128
  this.allSelected = this.categories.length > 0 && this.categories.every(c => c.selected);
129
  }
@@ -133,42 +139,53 @@ export class TestComponent implements OnInit {
133
  }
134
 
135
  // Helper method to ensure authentication
136
- private async ensureAuth(): Promise<boolean> {
137
- try {
138
- // Check if we already have a valid token
139
- const token = this.authService.getToken();
140
- if (token) {
141
- // Try to make a simple authenticated request to verify token is still valid
142
- try {
143
- await this.apiService.getEnvironment().toPromise();
144
- return true;
145
- } catch (error: any) {
146
- if (error.status === 401) {
147
- // Token expired, need to re-login
148
- this.authService.logout();
149
- } else {
150
- // Other error, assume auth is ok
151
- return true;
152
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  }
 
 
154
  }
155
-
156
- // Login with test credentials
157
- const response = await this.http.post('/api/login', {
158
- username: 'admin',
159
- password: 'admin'
160
- }).toPromise() as any;
161
-
162
- if (response?.token) {
163
- this.authService.setToken(response.token);
164
- this.authService.setUsername(response.username);
165
- return true;
166
- }
167
- return false;
168
- } catch {
169
- return false;
170
- }
171
  }
 
172
  initializeTests() {
173
  // Authentication Tests
174
  this.addTest('auth', 'Login with valid credentials', async () => {
@@ -616,41 +633,64 @@ export class TestComponent implements OnInit {
616
  }
617
 
618
  async runTests() {
619
- if (this.running) return;
620
 
621
  this.running = true;
622
  this.testResults = [];
623
  this.currentTest = '';
624
-
625
- const testsToRun = this.selectedTests;
626
-
627
- for (const test of testsToRun) {
628
- this.currentTest = test.name;
629
-
630
- try {
631
- const result = await test.testFn();
632
- this.testResults.push(result);
633
- } catch (error: any) {
634
  this.testResults.push({
635
- name: test.name,
636
  status: 'FAIL',
637
- error: error.message || 'Unexpected error'
 
638
  });
 
 
639
  }
640
 
641
- // Small delay between tests
642
- await new Promise(resolve => setTimeout(resolve, 100));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643
  }
644
-
645
- this.running = false;
646
- this.currentTest = '';
647
  }
648
 
649
  stopTests() {
650
  this.running = false;
651
  this.currentTest = '';
652
  }
653
-
654
  getTestResult(testName: string): TestResult | undefined {
655
  return this.testResults.find(r => r.name === testName);
656
  }
 
54
  templateUrl: './test.component.html',
55
  styleUrls: ['./test.component.scss']
56
  })
57
+ export class TestComponent implements OnInit, OnDestroy {
58
  private apiService = inject(ApiService);
59
  private authService = inject(AuthService);
60
  private http = inject(HttpClient);
61
+ private destroyed$ = new Subject<void>();
62
 
63
  running = false;
64
  currentTest: string = '';
 
125
  this.updateAllSelected();
126
  }
127
 
128
+ ngOnDestroy() {
129
+ this.destroyed$.next();
130
+ this.destroyed$.complete();
131
+ }
132
+
133
  updateAllSelected() {
134
  this.allSelected = this.categories.length > 0 && this.categories.every(c => c.selected);
135
  }
 
139
  }
140
 
141
  // Helper method to ensure authentication
142
+ private ensureAuth(): Promise<boolean> {
143
+ return new Promise((resolve) => {
144
+ try {
145
+ // Check if we already have a valid token
146
+ const token = this.authService.getToken();
147
+ if (token) {
148
+ // Try to make a simple authenticated request to verify token is still valid
149
+ this.apiService.getEnvironment()
150
+ .pipe(takeUntil(this.destroyed$))
151
+ .subscribe({
152
+ next: () => resolve(true),
153
+ error: (error: any) => {
154
+ if (error.status === 401) {
155
+ // Token expired, need to re-login
156
+ this.authService.logout();
157
+ resolve(false);
158
+ } else {
159
+ // Other error, assume auth is ok
160
+ resolve(true);
161
+ }
162
+ }
163
+ });
164
+ } else {
165
+ // Login with test credentials
166
+ this.http.post('/api/login', {
167
+ username: 'admin',
168
+ password: 'admin'
169
+ }).pipe(takeUntil(this.destroyed$))
170
+ .subscribe({
171
+ next: (response: any) => {
172
+ if (response?.token) {
173
+ this.authService.setToken(response.token);
174
+ this.authService.setUsername(response.username);
175
+ resolve(true);
176
+ } else {
177
+ resolve(false);
178
+ }
179
+ },
180
+ error: () => resolve(false)
181
+ });
182
  }
183
+ } catch {
184
+ resolve(false);
185
  }
186
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  }
188
+
189
  initializeTests() {
190
  // Authentication Tests
191
  this.addTest('auth', 'Login with valid credentials', async () => {
 
633
  }
634
 
635
  async runTests() {
636
+ if (this.running || this.selectedTests.length === 0) return;
637
 
638
  this.running = true;
639
  this.testResults = [];
640
  this.currentTest = '';
641
+
642
+ try {
643
+ // Ensure we're authenticated before running tests
644
+ const authOk = await this.ensureAuth();
645
+ if (!authOk) {
 
 
 
 
 
646
  this.testResults.push({
647
+ name: 'Authentication',
648
  status: 'FAIL',
649
+ error: 'Failed to authenticate for tests',
650
+ duration_ms: 0
651
  });
652
+ this.running = false;
653
+ return;
654
  }
655
 
656
+ // Run selected tests
657
+ for (const test of this.selectedTests) {
658
+ if (!this.running) break; // Allow cancellation
659
+
660
+ this.currentTest = test.name;
661
+
662
+ try {
663
+ const result = await test.testFn();
664
+ this.testResults.push(result);
665
+ } catch (error: any) {
666
+ // Catch any uncaught errors from test
667
+ this.testResults.push({
668
+ name: test.name,
669
+ status: 'FAIL',
670
+ error: error.message || 'Test threw an exception',
671
+ duration_ms: 0
672
+ });
673
+ }
674
+ }
675
+ } catch (error: any) {
676
+ console.error('Test runner error:', error);
677
+ this.testResults.push({
678
+ name: 'Test Runner',
679
+ status: 'FAIL',
680
+ error: 'Test runner encountered an error',
681
+ duration_ms: 0
682
+ });
683
+ } finally {
684
+ this.running = false;
685
+ this.currentTest = '';
686
  }
 
 
 
687
  }
688
 
689
  stopTests() {
690
  this.running = false;
691
  this.currentTest = '';
692
  }
693
+
694
  getTestResult(testName: string): TestResult | undefined {
695
  return this.testResults.find(r => r.name === testName);
696
  }