Przeglądaj źródła

Fixed the HelpDeskService so it returns the data from the server properly when calling getRecentVerifications().

jalbr74 7 lat temu
rodzic
commit
60de08ef61

+ 37 - 0
client/src/services/helpdesk.service.test-data.ts

@@ -0,0 +1,37 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2018 The PWM Project
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+/* tslint:disable */
+
+export const getRecentVerifications_response = {
+    "error": false,
+    "errorCode": 0,
+    "data": {
+        "records": [
+            {
+                "timestamp": "2018-02-22T15:14:39Z",
+                "profile": "default",
+                "username": "bjenner",
+                "method": "Personal Data"
+            }
+        ]
+    }
+};

+ 31 - 9
client/src/services/helpdesk.service.test.ts

@@ -20,11 +20,12 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-import HelpDeskService from './helpdesk.service';
-import {IHttpService, ILogService, IQService, IWindowService, module} from 'angular';
+import HelpDeskService, {IRecentVerifications} from './helpdesk.service';
+import {IHttpBackendService, IHttpService, ILogService, IQService, IWindowService, module} from 'angular';
 import ObjectService from './object.service';
 import {default as PwmService, IPwmService} from './pwm.service';
 import LocalStorageService from './local-storage.service';
+import {getRecentVerifications_response} from './helpdesk.service.test-data';
 
 describe('In helpdesk.service.test.ts', () => {
     beforeEach(() => {
@@ -35,26 +36,47 @@ describe('In helpdesk.service.test.ts', () => {
     let objectService: ObjectService;
     let pwmService: IPwmService;
     let helpDeskService: HelpDeskService;
+    let $httpBackend: IHttpBackendService;
+    let $window: IWindowService;
+
+    beforeEach(inject((
+        $http: IHttpService,
+        $log: ILogService,
+        $q: IQService,
+        _$window_: IWindowService,
+        _$httpBackend_: IHttpBackendService
+    ) => {
+        $httpBackend = _$httpBackend_;
+        $window = _$window_;
 
-    beforeEach(inject(($http: IHttpService, $log: ILogService, $q: IQService, $window: IWindowService) => {
         localStorageService = new LocalStorageService($log, $window);
         objectService = new ObjectService();
         pwmService = new PwmService($http, $log, $q, $window);
         helpDeskService = new HelpDeskService($log, $q, localStorageService, objectService, pwmService, $window);
     }));
 
-    it('a person can be obtained from the HelpDeskService', (done: DoneFn) => {
-        expect(helpDeskService).toBeDefined();
+    it('getRecentVerifications returns the right record data', (done: DoneFn) => {
+        (pwmService as PwmService).PWM_GLOBAL = { pwmFormID: 'fake-pwm-form-id' };
+
+        $httpBackend.whenPOST( '/context.html?processAction=showVerifications&pwmFormID=fake-pwm-form-id')
+            .respond(getRecentVerifications_response);
 
-        // TODO: need to come back and build up some tests...
-        helpDeskService.getPerson('123')
-            .then((result: any) => {
-                expect(result).toBeDefined();
+        helpDeskService.getRecentVerifications()
+            .then((recentVerifications: IRecentVerifications) => {
+                expect(recentVerifications.length).toBe(1);
+
+                expect(recentVerifications[0].username).toBe('bjenner');
+                expect(recentVerifications[0].profile).toBe('default');
+                expect(recentVerifications[0].timestamp).toBe('2018-02-22T15:14:39Z');
+                expect(recentVerifications[0].method).toBe('Personal Data');
 
                 done();
             })
             .catch((error: Error) => {
                 done.fail(error);
             });
+
+        // This causes the $http service to finally resolve the response:
+        $httpBackend.flush();
     });
 });

+ 2 - 2
client/src/services/helpdesk.service.ts

@@ -210,8 +210,8 @@ export default class HelpDeskService implements IHelpDeskService {
 
         return this.pwmService
             .httpRequest(url, { data: data })
-            .then((result: {records: IRecentVerifications}) => {
-                return this.$q.resolve(result.records);
+            .then((result: any) => {
+                return this.$q.resolve(result.data.records);
             });
     }
 

+ 6 - 2
client/test/karma-test-suite.ts

@@ -27,6 +27,10 @@ import 'angular-mocks';
 
 // This creates a single bundle with all test cases (*.test.ts), which improves performance
 // (i.e. we don't create a webpack bundle for each test):
-// var appContext = (require as any).context('../src', true, /\.test\.ts/);
-var appContext = (require as any).context('../src', true, /helpdesk-config\.service\.test\.ts/);
+var appContext = (require as any).context('../src', true, /\.test\.ts/);
+
+// If you want to run a specific test, comment out the general line above, and uncomment the specific one below:
+// var appContext = (require as any).context('../src', true, /helpdesk-config\.service\.test\.ts/);
+// var appContext = (require as any).context('../src', true, /helpdesk\.service\.test\.ts/);
+
 appContext.keys().forEach(appContext);