#1712 - Removed old private api call for wokflows users and groups (#1799)

* #1712 - changed workflow api call from private to enterprise

* #1712 - removed Testbed added injector
This commit is contained in:
Vito
2017-04-05 05:25:49 -07:00
committed by Mario Romano
parent b3e2c28582
commit 25a430c933
2 changed files with 532 additions and 524 deletions

View File

@@ -15,25 +15,48 @@
* limitations under the License.
*/
import { TestBed } from '@angular/core/testing';
import { ReflectiveInjector } from '@angular/core';
import {
AlfrescoAuthenticationService,
AlfrescoSettingsService,
AlfrescoApiService,
StorageService,
LogService
} from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
import { CoreModule, AlfrescoApiService, LogService, LogServiceMock } from 'ng2-alfresco-core';
import { FormService } from './form.service';
import { Response, ResponseOptions } from '@angular/http';
import { EcmModelService } from './ecm-model.service';
import { FormDefinitionModel } from '../models/form-definition.model';
import { Response, ResponseOptions } from '@angular/http';
declare let jasmine: any;
describe('FormService', () => {
let fakeGroupResponse = {
'size': 2,
'total': 2,
'start': 0,
'data': [{
'id': 2004,
'name': 'PEOPLE_GROUP',
'externalId': null,
'status': 'active',
'groups': null
}, { 'id': 2005, 'name': 'PEOPLE_GROUP_2', 'externalId': null, 'status': 'active', 'groups': null }]
};
let responseBody: any;
let service: FormService;
let apiService: AlfrescoApiService;
let logService: LogService;
let bpmCli: any;
let fakePeopleResponse = {
'size': 3,
'total': 3,
'start': 0,
'data': [{ 'id': 2002, 'firstName': 'Peo', 'lastName': 'Ple', 'email': 'people' }, {
'id': 2003,
'firstName': 'Peo02',
'lastName': 'Ple02',
'email': 'people02'
}, { 'id': 2004, 'firstName': 'Peo03', 'lastName': 'Ple03', 'email': 'people03' }]
};
function createFakeBlob() {
function createFakeBlob() {
let data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
let bytes = new Uint8Array(data.length / 2);
@@ -41,24 +64,29 @@ describe('FormService', () => {
for (let i = 0; i < data.length; i += 2) {
bytes[i / 2] = parseInt(data.substring(i, i + 2), /* base = */ 16);
}
return new Blob([bytes], {type: 'image/png'});
}
return new Blob([bytes], { type: 'image/png' });
}
describe('Form service', () => {
let service, injector, apiService, logService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
CoreModule.forRoot()
],
providers: [
injector = ReflectiveInjector.resolveAndCreate([
AlfrescoSettingsService,
AlfrescoApiService,
AlfrescoAuthenticationService,
EcmModelService,
StorageService,
FormService,
{ provide: LogService, useClass: LogServiceMock }
]
LogService
]);
});
service = TestBed.get(FormService);
apiService = TestBed.get(AlfrescoApiService);
bpmCli = apiService.getInstance().bpmAuth;
logService = TestBed.get(LogService);
beforeEach(() => {
service = injector.get(FormService);
apiService = injector.get(AlfrescoApiService);
logService = injector.get(LogService);
});
beforeEach(() => {
@@ -69,14 +97,51 @@ describe('FormService', () => {
jasmine.Ajax.uninstall();
});
it('should fetch and parse process definitions', (done) => {
responseBody = {
describe('Content tests', () => {
let responseBody = {
data: [
{id: '1'},
{id: '2'}
{ id: '1' },
{ id: '2' }
]
};
let values = {
field1: 'one',
field2: 'two'
};
let simpleResponseBody = { id: 1, modelType: 'test' };
let fileContentPdfResponseBody = {
id: 999,
name: 'fake-name.pdf',
created: '2017-01-23T12:12:53.219+0000',
createdBy: { id: 2, firstName: 'fake-admin', lastName: 'fake-last', 'email': 'fake-admin' },
relatedContent: false,
contentAvailable: true,
link: false,
mimeType: 'application/pdf',
simpleType: 'pdf',
previewStatus: 'created',
thumbnailStatus: 'created'
};
let fileContentJpgResponseBody = {
id: 888,
name: 'fake-name.jpg',
created: '2017-01-23T12:12:53.219+0000',
createdBy: { id: 2, firstName: 'fake-admin', lastName: 'fake-last', 'email': 'fake-admin' },
relatedContent: false,
contentAvailable: true,
link: false,
mimeType: 'image/jpeg',
simpleType: 'image',
previewStatus: 'unsupported',
thumbnailStatus: 'unsupported'
};
it('should fetch and parse process definitions', (done) => {
service.getProcessDefinitions().subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/process-definitions')).toBeTruthy();
expect(result).toEqual(JSON.parse(jasmine.Ajax.requests.mostRecent().response).data);
@@ -91,13 +156,6 @@ describe('FormService', () => {
});
it('should fetch and parse tasks', (done) => {
responseBody = {
data: [
{id: '1'},
{id: '2'}
]
};
service.getTasks().subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/tasks/query')).toBeTruthy();
expect(result).toEqual(JSON.parse(jasmine.Ajax.requests.mostRecent().response).data);
@@ -112,29 +170,20 @@ describe('FormService', () => {
});
it('should fetch and parse the task by id', (done) => {
responseBody = {
id: '1'
};
service.getTask('1').subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/tasks/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
expect(result.id).toEqual('1');
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify({ id: '1' })
});
});
it('should save task form', (done) => {
let values = {
field1: 'one',
field2: 'two'
};
service.saveTaskForm('1', values).subscribe(() => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1/save-form')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field1).toEqual(values.field1);
@@ -150,11 +199,6 @@ describe('FormService', () => {
});
it('should complete task form', (done) => {
let values = {
field1: 'one',
field2: 'two'
};
service.completeTaskForm('1', values).subscribe(() => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field1).toEqual(values.field1);
@@ -170,11 +214,6 @@ describe('FormService', () => {
});
it('should complete task form with a specific outcome', (done) => {
let values = {
field1: 'one',
field2: 'two'
};
service.completeTaskForm('1', values, 'custom').subscribe(() => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field2).toEqual(values.field2);
@@ -191,43 +230,39 @@ describe('FormService', () => {
});
it('should get task form by id', (done) => {
responseBody = {id: 1};
service.getTaskForm('1').subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
expect(result.id).toEqual(1);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify({ id: 1 })
});
});
it('should get form definition by id', (done) => {
responseBody = {id: 1};
service.getFormDefinitionById('1').subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/form-models/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
expect(result.id).toEqual(1);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify({ id: 1 })
});
});
it('should get form definition id by name', (done) => {
const formName = 'form1';
const formId = 1;
responseBody = {
let response = {
data: [
{id: formId}
{ id: formId }
]
};
@@ -240,7 +275,7 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(response)
});
});
@@ -261,16 +296,16 @@ describe('FormService', () => {
});
it('should not get form id from response', () => {
let response = new Response(new ResponseOptions({body: null}));
let response = new Response(new ResponseOptions({ body: null }));
expect(service.getFormId(response)).toBeNull();
response = new Response(new ResponseOptions({body: {}}));
response = new Response(new ResponseOptions({ body: {} }));
expect(service.getFormId(response)).toBeNull();
response = new Response(new ResponseOptions({body: {data: null}}));
response = new Response(new ResponseOptions({ body: { data: null } }));
expect(service.getFormId(response)).toBeNull();
response = new Response(new ResponseOptions({body: {data: []}}));
response = new Response(new ResponseOptions({ body: { data: [] } }));
expect(service.getFormId(response)).toBeNull();
expect(service.getFormId(null)).toBeNull();
@@ -279,62 +314,64 @@ describe('FormService', () => {
it('should fallback to empty json array', () => {
expect(service.toJsonArray(null)).toEqual([]);
let response = new Response(new ResponseOptions({body: {}}));
let response = new Response(new ResponseOptions({ body: {} }));
expect(service.toJsonArray(response)).toEqual([]);
response = new Response(new ResponseOptions({body: {data: null}}));
response = new Response(new ResponseOptions({ body: { data: null } }));
expect(service.toJsonArray(response)).toEqual([]);
});
it('should handle error with generic message', () => {
spyOn(logService, 'error').and.stub();
service.handleError(null);
expect(logService.error).toHaveBeenCalledWith(FormService.UNKNOWN_ERROR_MESSAGE);
service.handleError(null).subscribe(() => {
}, (error) => {
expect(error).toBe(FormService.UNKNOWN_ERROR_MESSAGE);
});
});
it('should handle error with error message', () => {
spyOn(logService, 'error').and.stub();
const message = '<error>';
service.handleError({message: message});
expect(logService.error).toHaveBeenCalledWith(message);
service.handleError({ message: message }).subscribe(() => {
}, (error) => {
expect(error).toBe(message);
});
});
it('should handle error with detailed message', () => {
spyOn(logService, 'error').and.stub();
service.handleError({
status: '400',
statusText: 'Bad request'
}).subscribe(
() => {
},
(error) => {
expect(error).toBe('400 - Bad request');
});
expect(logService.error).toHaveBeenCalledWith('400 - Bad request');
});
it('should handle error with generic message', () => {
spyOn(logService, 'error').and.stub();
service.handleError({});
expect(logService.error).toHaveBeenCalledWith(FormService.GENERIC_ERROR_MESSAGE);
service.handleError({}).subscribe(() => {
}, (error) => {
expect(error).toBe(FormService.GENERIC_ERROR_MESSAGE);
});
});
it('should get all the forms with modelType=2', (done) => {
responseBody = {};
service.getForms().subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('models?modelType=2')).toBeTruthy();
expect(result).toEqual(responseBody);
expect(result).toEqual({});
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify({})
});
});
it('should search for Form with modelType=2', (done) => {
responseBody = {data: [{id: 1, name: 'findme'}, {id: 2, name: 'testform'}]};
let response = { data: [{ id: 1, name: 'findme' }, { id: 2, name: 'testform' }] };
service.searchFrom('findme').subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('models?modelType=2')).toBeTruthy();
@@ -346,13 +383,11 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(response)
});
});
it('should create a Form with modelType=2', (done) => {
responseBody = {id: 1, modelType: 'test'};
service.createForm('testName').subscribe(result => {
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/models')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).modelType).toEqual(2);
@@ -363,16 +398,14 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(simpleResponseBody)
});
});
it('should add form fields to a form', (done) => {
responseBody = {id: 1, modelType: 'test'};
let formId = '100';
let name = 'testName';
let data = [{name: 'name'}, {name: 'email'}];
let data = [{ name: 'name' }, { name: 'email' }];
let formDefinitionModel = new FormDefinitionModel(formId, name, 'testUserName', '2016-09-05T14:41:19.049Z', data);
service.addFieldsToAForm(formId, formDefinitionModel).subscribe(result => {
@@ -384,25 +417,12 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(simpleResponseBody)
});
});
it('should return the unsupported content when the file is an image', (done) => {
let contentId: number = 999;
responseBody = {
id: contentId,
name: 'fake-name.jpg',
created: '2017-01-23T12:12:53.219+0000',
createdBy: {id: 2, firstName: 'fake-admin', lastName: 'fake-last', 'email': 'fake-admin'},
relatedContent: false,
contentAvailable: true,
link: false,
mimeType: 'image/jpeg',
simpleType: 'image',
previewStatus: 'unsupported',
thumbnailStatus: 'unsupported'
};
let contentId: number = 888;
service.getFileContent(contentId).subscribe(result => {
expect(result.id).toEqual(contentId);
@@ -415,25 +435,12 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(fileContentJpgResponseBody)
});
});
it('should return the supported content when the file is a pdf', (done) => {
let contentId: number = 888;
responseBody = {
id: contentId,
name: 'fake-name.pdf',
created: '2017-01-23T12:12:53.219+0000',
createdBy: {id: 2, firstName: 'fake-admin', lastName: 'fake-last', 'email': 'fake-admin'},
relatedContent: false,
contentAvailable: true,
link: false,
mimeType: 'application/pdf',
simpleType: 'pdf',
previewStatus: 'created',
thumbnailStatus: 'created'
};
let contentId: number = 999;
service.getFileContent(contentId).subscribe(result => {
expect(result.id).toEqual(contentId);
@@ -446,22 +453,20 @@ describe('FormService', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
responseText: JSON.stringify(fileContentPdfResponseBody)
});
});
it('should return the raw content URL', () => {
let contentId: number = 999;
let contentRawUrl = service.getFileRawContentUrl(contentId);
expect(contentRawUrl).toEqual(`${bpmCli.basePath}/api/enterprise/content/${contentId}/raw`);
let contentUrl = service.getFileRawContentUrl(contentId);
expect(contentUrl).toContain(`/api/enterprise/content/${contentId}/raw`);
});
it('should return a Blob as thumbnail', (done) => {
let contentId: number = 999;
let blob = createFakeBlob();
spyOn(service, 'getContentThumbnailUrl').and.returnValue(Observable.of(blob));
service.getContentThumbnailUrl(contentId).subscribe(result => {
expect(result).toEqual(jasmine.any(Blob));
expect(result.size).toEqual(48);
@@ -470,12 +475,46 @@ describe('FormService', () => {
});
});
it('should return list of people', (done) => {
let fakeFilter: string = 'whatever';
service.getWorkflowUsers(fakeFilter).subscribe(result => {
expect(result).toBeDefined();
expect(result.length).toBe(3);
expect(result[0].id).toBe(2002);
expect(result[0].firstName).toBe('Peo');
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(fakePeopleResponse)
});
});
it('should return list of groups', (done) => {
let fakeFilter: string = 'whatever';
service.getWorkflowGroups(fakeFilter).subscribe(result => {
expect(result).toBeDefined();
expect(result.length).toBe(2);
expect(result[0].id).toBe(2004);
expect(result[0].name).toBe('PEOPLE_GROUP');
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(fakeGroupResponse)
});
});
it('should create a Form form a Node', (done) => {
let nameForm = 'testNode';
responseBody = {id: 1, modelType: 'test'};
let formId = 100;
stubCreateForm();
@@ -496,7 +535,7 @@ describe('FormService', () => {
status: 200,
statusText: 'HTTP/1.1 200 OK',
contentType: 'text/xml;charset=UTF-8',
responseText: {id: formId, name: 'test', lastUpdatedByFullName: 'uset', lastUpdated: '12-12-2016'}
responseText: { id: formId, name: 'test', lastUpdatedByFullName: 'uset', lastUpdated: '12-12-2016' }
});
}
@@ -513,9 +552,9 @@ describe('FormService', () => {
entry: {
prefixedName: nameForm,
title: nameForm,
properties: [{name: 'name'}, {name: 'email'}]
properties: [{ name: 'name' }, { name: 'email' }]
}
}, {entry: {prefixedName: 'notme', title: 'notme'}}]
}, { entry: { prefixedName: 'notme', title: 'notme' } }]
}
}
});
@@ -528,8 +567,10 @@ describe('FormService', () => {
status: 200,
statusText: 'HTTP/1.1 200 OK',
contentType: 'text/xml;charset=UTF-8',
responseText: {id: formId, name: 'test', lastUpdatedByFullName: 'user', lastUpdated: '12-12-2016'}
responseText: { id: formId, name: 'test', lastUpdatedByFullName: 'user', lastUpdated: '12-12-2016' }
});
}
});
});
});

View File

@@ -158,7 +158,7 @@ export class FormService {
* @returns {Observable<any>}
*/
saveTaskForm(taskId: string, formValues: FormValues): Observable<any> {
let body = JSON.stringify({values: formValues});
let body = JSON.stringify({ values: formValues });
return Observable.fromPromise(this.apiService.getInstance().activiti.taskApi.saveTaskForm(taskId, body))
.catch(err => this.handleError(err));
@@ -172,7 +172,7 @@ export class FormService {
* @returns {Observable<any>}
*/
completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable<any> {
let data: any = {values: formValues};
let data: any = { values: formValues };
if (outcome) {
data.outcome = outcome;
}
@@ -294,67 +294,34 @@ export class FormService {
return Observable.fromPromise(alfrescoApi.activiti.taskApi.getRestFieldValuesColumn(taskId, field, column));
}
// TODO: uses private webApp api
getWorkflowGroups(filter: string, groupId?: string): Observable<GroupModel[]> {
return Observable.create(observer => {
let xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let json = JSON.parse(xhr.response);
let data: GroupModel[] = (json.data || []).map(item => <GroupModel> item);
observer.next(data);
observer.complete();
} else {
this.logService.error(xhr.response);
Observable.throw(new Error(xhr.response));
}
}
};
let host = this.apiService.getInstance().config.hostBpm;
let url = `${host}/activiti-app/app/rest/workflow-groups?filter=${filter}`;
if (groupId) {
url += `&groupId=${groupId}`;
}
xhr.open('GET', url, true);
xhr.setRequestHeader('Authorization', this.apiService.getInstance().getTicketBpm());
xhr.send();
});
}
getWorkflowUsers(filter: string, groupId?: string): Observable<GroupUserModel[]> {
return Observable.create(observer => {
let xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let json = JSON.parse(xhr.response);
let data: GroupUserModel[] = (json.data || []).map(item => <GroupUserModel> item);
observer.next(data);
observer.complete();
} else {
this.logService.error(xhr.response);
Observable.throw(new Error(xhr.response));
}
}
};
let host = this.apiService.getInstance().config.hostBpm;
let url = `${host}/activiti-app/app/rest/workflow-users?filter=${filter}`;
let option: any = { filter: filter };
if (groupId) {
url += `&groupId=${groupId}`;
option.groupId = groupId;
}
xhr.open('GET', url, true);
xhr.setRequestHeader('Authorization', this.apiService.getInstance().getTicketBpm());
xhr.send();
});
return Observable.fromPromise(this.getWorkflowUserApi(option))
.map((response: any) => <GroupUserModel[]> response.data || [])
.catch(err => this.handleError(err));
}
private getWorkflowUserApi(options: any) {
let alfrescoApi = this.apiService.getInstance();
return alfrescoApi.activiti.usersWorkflowApi.getUsers(options);
}
getWorkflowGroups(filter: string, groupId?: string): Observable<GroupModel[]> {
let option: any = { filter: filter };
if (groupId) {
option.groupId = groupId;
}
return Observable.fromPromise(this.getWorkflowGroupsApi(option))
.map((response: any) => <GroupModel[]> response.data || [])
.catch(err => this.handleError(err));
}
private getWorkflowGroupsApi(options: any) {
let alfrescoApi = this.apiService.getInstance();
return alfrescoApi.activiti.groupsApi.getGroups(options);
}
getFormId(res: any): string {