Skip to content

Commit

Permalink
[ACS-5992] refactor api wrappers (#3425)
Browse files Browse the repository at this point in the history
* [ACS-5992] refactor api wrappers

* code review fixes

* review siteVisibility fix

* deleting configs from index

* updating breadcrumb preconditions
  • Loading branch information
azakrzewski-hy authored Sep 11, 2023
1 parent b086093 commit 8f0e4df
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 244 deletions.
13 changes: 5 additions & 8 deletions e2e/playwright/navigation/src/tests/breadcrumb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
*/

import { expect } from '@playwright/test';
import { ApiClientFactory, getUserState, SITE_VISIBILITY, test, Utils } from '@alfresco/playwright-shared';
import { getUserState, test, Utils } from '@alfresco/playwright-shared';
import { Site } from '@alfresco/js-api';

test.use({ storageState: getUserState('hruser') });
test.describe('viewer action file', () => {
const apiClientFactory = new ApiClientFactory();

const parent = `parent-${Utils.random()}`;
let parentId: string;
const subFolder1 = `subFolder1-${Utils.random()}`;
Expand All @@ -53,8 +52,6 @@ test.describe('viewer action file', () => {
const folder1Renamed = `renamed-${Utils.random()}`;

test.beforeAll(async ({ nodesApiAction, sitesApiAction }) => {
await apiClientFactory.setUpAcaBackend('hruser');

const parentNode = await nodesApiAction.createFolder(parent);
parentId = parentNode.entry.id;
subFolder1Id = (await nodesApiAction.createFolder(subFolder1, parentId)).entry.id;
Expand All @@ -64,16 +61,16 @@ test.describe('viewer action file', () => {
parent2Id = (await nodesApiAction.createFolder(parent2)).entry.id;
folder1Id = (await nodesApiAction.createFolder(folder1, parent2Id)).entry.id;

await sitesApiAction.createSite(siteName, SITE_VISIBILITY.PUBLIC);
await sitesApiAction.createSite(siteName, Site.VisibilityEnum.PUBLIC);
const docLibId = await sitesApiAction.getDocLibId(siteName);
parentFromSiteId = (await nodesApiAction.createFolder(parentFromSite, docLibId)).entry.id;
subFolder1FromSiteId = (await nodesApiAction.createFolder(subFolder1FromSite, parentFromSiteId)).entry.id;
subFolder2FromSiteId = (await nodesApiAction.createFolder(subFolder2FromSite, subFolder1FromSiteId)).entry.id;
await nodesApiAction.createFile(fileName1FromSite, subFolder2FromSiteId);
});

test.afterAll(async () => {
await apiClientFactory.nodes.deleteNode(parentId, { permanent: true });
test.afterAll(async ({ nodesApiAction }) => {
await nodesApiAction.deleteNodes([parentId], true);
});

test('[C260964] Personal Files breadcrumb main node', async ({ personalFiles }) => {
Expand Down
30 changes: 15 additions & 15 deletions e2e/playwright/viewer/src/tests/viewer-useraction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import { expect } from '@playwright/test';
import { ApiClientFactory, LoginPage, test, TEST_FILES, Utils } from '@alfresco/playwright-shared';
import { ApiClientFactory, FileActionsApi, LoginPage, NodesApi, SitesApi, test, TEST_FILES, Utils } from '@alfresco/playwright-shared';
import { SiteBodyCreate } from '@alfresco/js-api';
import { Logger } from '@alfresco/adf-testing';

Expand All @@ -34,21 +34,22 @@ test.describe('from File Libraries', () => {
const destination = `destFL-${Utils.random()}`;
let destinationId: string;
const xlsxLibraries = `xlsxFL-${Utils.random()}`;
let nodesApi: NodesApi;
let sitesApi: SitesApi;
let fileApi: FileActionsApi;

test.beforeAll(async ({ userActions }) => {
test.beforeAll(async () => {
await apiClientFactory.setUpAcaBackend('admin');
await apiClientFactory.createUser({ username });
await userActions.setUpUserAcaBackend(username, username);
nodesApi = await NodesApi.initialize(username, username);
sitesApi = await SitesApi.initialize(username, username);
fileApi = await FileActionsApi.initialize(username, username);
try {
await userActions.sitesApi.createSite({
id: siteName,
title: siteName,
visibility: SiteBodyCreate.VisibilityEnum.PUBLIC
});
const docLibId = (await userActions.sitesApi.listSiteContainers(siteName)).list.entries[0].entry.id;
const node = await userActions.nodesApi.createNode('-my-', { name: destination, nodeType: 'cm:folder', relativePath: '/' });
await sitesApi.createSite(siteName, SiteBodyCreate.VisibilityEnum.PUBLIC);
const docLibId = await sitesApi.getDocLibId(siteName);
const node = await nodesApi.createFolder(destination);
destinationId = node.entry.id;
await userActions.uploadFile(TEST_FILES.XLSX.path, xlsxLibraries, docLibId);
await fileApi.uploadFile(TEST_FILES.XLSX.path, xlsxLibraries, docLibId);
} catch (error) {
Logger.error(`beforeAll failed : ${error}`);
}
Expand All @@ -65,11 +66,10 @@ test.describe('from File Libraries', () => {
);
});

test.afterAll(async ({ userActions }) => {
test.afterAll(async () => {
try {
await userActions.setUpUserAcaBackend(username, username);
await userActions.deleteSites([siteName]);
await userActions.deleteNodes([destinationId]);
await sitesApi.deleteSites([siteName]);
await nodesApi.deleteNodes([destinationId]);
} catch (error) {
Logger.error(`afterAll failed : ${error}`);
}
Expand Down
20 changes: 9 additions & 11 deletions projects/aca-playwright-shared/src/api/api-client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class ApiClientFactory {
this.alfrescoApi = new AlfrescoApi(config);
}

public async setUpAcaBackend(userProfile: keyof typeof users): Promise<AcaBackend> {
await this.login(userProfile);
public async setUpAcaBackend(userName: string, password?: string): Promise<AcaBackend> {
await this.login(userName, password);

this.sites = new SitesApi(this.alfrescoApi);
this.upload = new UploadApi(this.alfrescoApi);
Expand Down Expand Up @@ -135,19 +135,17 @@ export class ApiClientFactory {
return response;
}

async login(userProfile: keyof typeof users) {
const userToLog =
users[
Object.keys(users)
.filter((user) => user.match(new RegExp(`^${userProfile.toString()}$`)))
.toString()
] || userProfile;
async login(userName: string, password?: string) {
const predefinedUserKey = Object.keys(users).find((user) => user === userName || users[user].username === userName);
const userToLog = predefinedUserKey ? users[predefinedUserKey] : undefined;
let e: any;

const user = userToLog?.username ?? userName;
const userPassword = userToLog?.password ?? password;
try {
e = await this.alfrescoApi.login(userToLog.username, userToLog.password);
e = await this.alfrescoApi.login(user, userPassword);
} catch (error) {
logger.error(`[API Client Factory] Log in user ${userToLog.username} failed ${e}`);
logger.error(`[API Client Factory] Log in user ${user} failed ${e}`);
throw error;
}
}
Expand Down
10 changes: 4 additions & 6 deletions projects/aca-playwright-shared/src/api/favorites-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@

import { ApiClientFactory } from './api-client-factory';
import { FavoriteEntry } from '@alfresco/js-api';
import { users } from '../base-config/global-variables';

export class FavoritesPageApi extends ApiClientFactory {
export class FavoritesPageApi {
private apiService: ApiClientFactory;

constructor() {
super();
this.apiService = new ApiClientFactory();
}
static async initialize(userProfile: keyof typeof users): Promise<FavoritesPageApi> {
static async initialize(userName: string, password?: string): Promise<FavoritesPageApi> {
const classObj = new FavoritesPageApi();
await classObj.apiService.setUpAcaBackend(userProfile);
await classObj.apiService.setUpAcaBackend(userName, password);
return classObj;
}
async addFavoriteById(nodeType: 'file' | 'folder' | 'site', id: string): Promise<FavoriteEntry | null> {
let guid = nodeType === 'site' ? (await this.sites.getSite(id)).entry.guid : id;
let guid = nodeType === 'site' ? (await this.apiService.sites.getSite(id)).entry.guid : id;
const data = {
target: {
[nodeType]: {
Expand Down
10 changes: 3 additions & 7 deletions projects/aca-playwright-shared/src/api/file-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@

import * as fs from 'fs';
import { ApiClientFactory } from './api-client-factory';
import { users } from '../base-config/global-variables';

export class FileActionsApi extends ApiClientFactory {
export class FileActionsApi {
private apiService: ApiClientFactory;

constructor() {
super();
this.apiService = new ApiClientFactory();
}

static async initialize(
userProfile: keyof typeof users
): Promise<FileActionsApi> {
static async initialize(userName: string, password?: string): Promise<FileActionsApi> {
const classObj = new FileActionsApi();
await classObj.apiService.setUpAcaBackend(userProfile);
await classObj.apiService.setUpAcaBackend(userName, password);
return classObj;
}

Expand Down
2 changes: 1 addition & 1 deletion projects/aca-playwright-shared/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export * from './api-client-factory';
export * from './file-actions';
export * from './shared-links-api';
export * from './favorites-api';
export * from './user-actions';
export * from './people-api-models';
export * from './nodes-api';
export * from './sites-api';
export * from './node-content-tree';
90 changes: 90 additions & 0 deletions projects/aca-playwright-shared/src/api/node-content-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*!
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/

import { NodeBodyCreate } from '@alfresco/js-api';

const NODE_TYPE_FILE = 'cm:content';
const NODE_TYPE_FOLDER = 'cm:folder';
const NODE_TITLE = 'cm:title';
const NODE_DESCRIPTION = 'cm:description';

export interface NodeContentTree {
name?: string;
files?: string[];
folders?: (string | NodeContentTree)[];
title?: string;
description?: string;
}

export function flattenNodeContentTree(content: NodeContentTree, relativePath: string = '/'): NodeBodyCreate[] {
const { name, files, folders, title, description } = content;
const aspectNames: string[] = ['cm:versionable'];
let data: NodeBodyCreate[] = [];
let properties: any;

properties = {
[NODE_TITLE]: title,
[NODE_DESCRIPTION]: description
};

if (name) {
data = data.concat([
{
nodeType: NODE_TYPE_FOLDER,
name,
relativePath,
properties
}
]);

relativePath = relativePath === '/' ? `/${name}` : `${relativePath}/${name}`;
}

if (folders) {
const foldersData: NodeBodyCreate[] = folders
.map((folder: string | NodeContentTree): NodeBodyCreate[] => {
const folderData: NodeContentTree = typeof folder === 'string' ? { name: folder } : folder;

return flattenNodeContentTree(folderData, relativePath);
})
.reduce((nodesData: NodeBodyCreate[], folderData: NodeBodyCreate[]) => nodesData.concat(folderData), []);

data = data.concat(foldersData);
}

if (files) {
const filesData: NodeBodyCreate[] = files.map(
(filename: string): NodeBodyCreate => ({
nodeType: NODE_TYPE_FILE,
name: filename,
relativePath,
aspectNames
})
);

data = data.concat(filesData);
}

return data;
}
Loading

0 comments on commit 8f0e4df

Please sign in to comment.