UNPKG

@umbraco/playwright-testhelpers

Version:

Test helpers for making playwright tests for Umbraco solutions

472 lines 19.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserGroupApiHelper = void 0; const json_models_builders_1 = require("@umbraco/json-models-builders"); const ConstantHelper_1 = require("./ConstantHelper"); class UserGroupApiHelper { api; constructor(api) { this.api = api; } async ensureNameNotExists(name) { const json = await this.getAll(); for (const sb of json.items) { if (sb.name === name) { if (sb.id !== null) { return await this.api.delete(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + sb.id); } } } return null; } async doesExist(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + id); return response.status() === 200; } async create(userGroupData) { const response = await this.api.post(this.api.baseUrl + '/umbraco/management/api/v1/user-group', userGroupData); // Returns the id of the userGroup return response.headers().location.split("/").pop(); } async getByName(name) { const json = await this.getAll(); for (const sb of json.items) { if (sb.name === name) { if (sb.id !== null) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + sb.id); return await response.json(); } } } return null; } async get(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + id); const json = await response.json(); if (json !== null) { return json; } return null; } async getAll() { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/user-group?skip=0&take=10000'); const json = await response.json(); if (json !== null) { return json; } return null; } async update(id, userGroup) { const response = await this.api.put(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + id, userGroup); return response.text(); } async doesNameExist(name) { const json = await this.getAll(); for (const sb of json.items) { if (sb.name === name) { return true; } } return false; } async doesUserGroupContainLanguage(userGroupName, languageName) { const userGroup = await this.getByName(userGroupName); return userGroup.languages.includes(languageName); } async doesUserGroupContainAccessToAllLanguages(userGroupName) { const userGroup = await this.getByName(userGroupName); return userGroup.hasAccessToAllLanguages; } async doesUserGroupContainDocumentRootAccess(userGroupName) { const userGroup = await this.getByName(userGroupName); return userGroup.documentRootAccess; } async doesUserGroupContainMediaRootAccess(userGroupName) { const userGroup = await this.getByName(userGroupName); return userGroup.mediaRootAccess; } async delete(id) { return await this.api.delete(this.api.baseUrl + '/umbraco/management/api/v1/user-group/' + id); } async createEmptyUserGroup(name) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .build(); return await this.create(userGroup); } async createSimpleUserGroupWithContentSection(name) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .addFallbackPermission() .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createSimpleUserGroupWithMediaSection(name) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Media') .addFallbackPermission() .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithDocumentAccess(name) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .withDocumentRootAccess(true) .build(); return await this.create(userGroup); } async createUserGroupWithDocumentStartNode(name, startNodeId) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(false) .withDocumentStartNodeId(startNodeId) .addFallbackPermission() .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithMediaStartNode(name, startNodeId) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Media') .withMediaRootAccess(false) .withMediaStartNodeId(startNodeId) .addFallbackPermission() .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithLanguage(name, languageName) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addLanguage(languageName) .build(); return await this.create(userGroup); } async createUserGroupWithLanguageAndContentSection(name, languageName) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .addLanguage(languageName) .withDocumentRootAccess(true) .addFallbackPermission() .withBrowseNodePermission(true) .withUpdatePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithMemberSection(name) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Members') .build(); return await this.create(userGroup); } async createUserGroupWithPermissionsForSpecificDocumentWithBrowseNode(name, documentId) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addPermission() .withDocumentId(documentId) .addVerbs() .withBrowseNodePermission(true) .done() .done() .build(); return await this.create(userGroup); } async createUserGroupWithBrowseNodePermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withBrowseNodePermission(enabled) .done() .build(); return await this.create(userGroup); } async createUserGroupWithCreateDocumentBlueprintPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withCreateDocumentBlueprintPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithDeleteDocumentPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withDeletePermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithCreateDocumentPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withCreatePermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithNotificationsPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withNotificationsPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithPublishPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withPublishPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithSetPermissionsPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withSetPermissionsPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithUnpublishPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withUnpublishPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithUpdatePermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withUpdatePermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithDuplicatePermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withDuplicatePermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithMoveToPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withMoveToPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithSortChildrenPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withSortChildrenPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithCultureAndHostnamesPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withCultureAndHostnamesPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithPublicAccessPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .addSection('Umb.Section.Members') .withDocumentRootAccess(true) .addFallbackPermission() .withPublicAccessPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithRollbackPermission(name, enabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withRollbackPermission(enabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async createUserGroupWithDeletePermissionAndCreatePermission(name, deleteEnabled = true, createEnabled = true) { await this.ensureNameNotExists(name); const userGroup = new json_models_builders_1.UserGroupBuilder() .withName(name) .addSection('Umb.Section.Content') .withDocumentRootAccess(true) .addFallbackPermission() .withDeletePermission(deleteEnabled) .withCreatePermission(createEnabled) .withBrowseNodePermission(true) .done() .build(); return await this.create(userGroup); } async doesUserGroupContainContentStartNodeId(userGroupName, documentStartNodeId) { const userGroup = await this.getByName(userGroupName); if (userGroup.documentStartNode === null) { return false; } return userGroup.documentStartNode.id.includes(documentStartNodeId); } async doesUserGroupContainMediaStartNodeId(userGroupName, mediaStartNodeId) { const userGroup = await this.getByName(userGroupName); if (userGroup.mediaStartNode === null) { return false; } return userGroup.mediaStartNode.id.includes(mediaStartNodeId); } async doesUserGroupContainGranularPermissionsForDocument(userGroupName, documentId, granularPermissions) { const userGroup = await this.getByName(userGroupName); for (const permission of userGroup.permissions) { if (permission.document.id === documentId) { for (const verb of permission.verbs) { if (!granularPermissions.includes(verb)) { return false; } } return true; } } return false; } async doesUserGroupHaveFallbackPermissions(userGroupName, permissions) { const userGroup = await this.getByName(userGroupName); const fallbackPermissions = userGroup.fallbackPermissions; if (permissions.length !== fallbackPermissions.length) { return false; } return permissions.every(item => fallbackPermissions.includes(item)); } async convertApiPermissionsToUiPermissions(apiPermissions) { return apiPermissions.map(permission => { for (const key in ConstantHelper_1.ConstantHelper.userGroupPermissionsSettings) { if (ConstantHelper_1.ConstantHelper.userGroupPermissionsSettings[key][2].toLowerCase() === permission.toLowerCase()) { return ConstantHelper_1.ConstantHelper.userGroupPermissionsSettings[key][0]; } } return null; }); } async convertApiSectionsToUiSections(apiSections) { return apiSections.map(permission => { for (const key in ConstantHelper_1.ConstantHelper.userGroupSectionsSettings) { if (ConstantHelper_1.ConstantHelper.userGroupSectionsSettings[key][1].toLowerCase() === permission.toLowerCase()) { return ConstantHelper_1.ConstantHelper.userGroupSectionsSettings[key][0]; } } return null; }); } async doesUserGroupHaveSections(userGroupName, sections) { const userGroup = await this.getByName(userGroupName); const sectionsData = userGroup.sections; if (sectionsData.length !== sections.length) { return false; } return sections.every(item => sectionsData.includes(item)); } } exports.UserGroupApiHelper = UserGroupApiHelper; //# sourceMappingURL=UserGroupApiHelper.js.map