Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new model for Merchant Center CustomView entity #384

Merged
merged 11 commits into from
Oct 19, 2023
Merged
14 changes: 14 additions & 0 deletions .changeset/olive-jobs-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@commercetools-test-data/custom-view': minor
---

New package to generate test data for Custom Views.

```ts
import {
CustomView,
type TCustomViewGraphql,
} from '@commercetools-test-data/custom-view';

const customView = CustomView.random().buildGraphql<TCustomViewGraphql>();
```
5 changes: 5 additions & 0 deletions .changeset/warm-moons-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/commons': minor
---

Add new model `LocalizedField`
2 changes: 2 additions & 0 deletions models/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './address/types';
export * from './cent-precision-money/types';
export * from './client-logging/types';
export * from './key-reference/types';
export * from './localized-field/types';
export * from './localized-string/types';
export * from './money/types';
export * from './price/types';
Expand All @@ -15,6 +16,7 @@ export * as CentPrecisionMoney from './cent-precision-money';
export * as CentPrecisionMoneyDraft from './cent-precision-money/cent-precision-money-draft';
export * as ClientLogging from './client-logging';
export * as KeyReference from './key-reference';
export * as LocalizedField from './localized-field';
export * as LocalizedString from './localized-string';
export * as Money from './money';
export * as Price from './price';
Expand Down
30 changes: 30 additions & 0 deletions models/commons/src/localized-field/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type { TLocalizedField, TLocalizedFieldGraphql } from './types';
import * as LocalizedField from './index';

describe('builder', () => {
it(
...createBuilderSpec<TLocalizedField, TLocalizedField>(
'default',
LocalizedField.random(),
expect.objectContaining({
locale: expect.stringMatching(/^(de|en|es)$/),
value: expect.any(String),
})
)
);

it(
...createBuilderSpec<TLocalizedField, TLocalizedFieldGraphql>(
'graphql',
LocalizedField.random(),
expect.objectContaining({
__typename: 'LocalizedField',
locale: expect.stringMatching(/^(de|en|es)$/),
value: expect.any(String),
})
)
);
});
12 changes: 12 additions & 0 deletions models/commons/src/localized-field/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type { TCreateLocalizedFieldBuilder, TLocalizedField } from './types';

const Model: TCreateLocalizedFieldBuilder = () =>
Builder<TLocalizedField>({
generator,
transformers,
});

export default Model;
16 changes: 16 additions & 0 deletions models/commons/src/localized-field/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Generator, fake } from '@commercetools-test-data/core';
import type { TLocalizedField } from './types';

const generator = Generator<TLocalizedField>({
fields: {
locale: fake((f) => f.helpers.arrayElement(['en', 'de', 'es'])),
value: fake((f) =>
f.lorem.words({
min: 1,
max: 4,
})
),
},
});

export default generator;
2 changes: 2 additions & 0 deletions models/commons/src/localized-field/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as random } from './builder';
export { default as presets } from './presets';
3 changes: 3 additions & 0 deletions models/commons/src/localized-field/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
13 changes: 13 additions & 0 deletions models/commons/src/localized-field/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Transformer } from '@commercetools-test-data/core';
import type { TLocalizedField, TLocalizedFieldGraphql } from './types';

const transformers = {
default: Transformer<TLocalizedField, TLocalizedField>('default', {}),
graphql: Transformer<TLocalizedField, TLocalizedFieldGraphql>('graphql', {
addFields: () => ({
__typename: 'LocalizedField',
}),
}),
};

export default transformers;
12 changes: 12 additions & 0 deletions models/commons/src/localized-field/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { TBuilder } from '@commercetools-test-data/core';

export type TLocalizedField = {
locale: string;
value: string;
};
export type TLocalizedFieldGraphql = TLocalizedField & {
__typename: 'LocalizedField';
};

export type TLocalizedFieldBuilder = TBuilder<TLocalizedField>;
export type TCreateLocalizedFieldBuilder = () => TLocalizedFieldBuilder;
21 changes: 21 additions & 0 deletions models/custom-view/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) commercetools GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions models/custom-view/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @commercetools-test-data/custom-view

This package provides the data model for the commercetools internal API `CustomView` type

# Install

```bash
$ pnpm add -D @commercetools-test-data/custom-view
```

# Usage

```ts
import {
CustomView,
CustomViewDraft,
type TCustomViewGraphql,
type TCustomViewDraftGraphql,
} from '@commercetools-test-data/custom-view';

const customView = CustomView.random().buildGraphql<TCustomViewGraphql>();
const customViewDraft =
CustomViewDraft.random().buildGraphql<TCustomViewDraftGraphql>();
```
32 changes: 32 additions & 0 deletions models/custom-view/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@commercetools-test-data/custom-view",
"version": "6.0.0",
"description": "Data model for internal Merchan Center API Custom View",
"bugs": "https://github.com/commercetools/test-data/issues",
"repository": {
"type": "git",
"url": "https://github.com/commercetools/test-data.git",
"directory": "models/custom-view"
},
"keywords": ["javascript", "typescript", "test-data"],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "dist/commercetools-test-data-custom-view.cjs.js",
"module": "dist/commercetools-test-data-custom-view.esm.js",
"files": ["dist", "package.json", "LICENSE", "README.md"],
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime-corejs3": "^7.17.9",
"@commercetools-frontend/application-config": "^22.9.1",
"@commercetools-frontend/constants": "^22.9.1",
"@commercetools-test-data/commons": "6.3.8",
"@commercetools-test-data/core": "6.3.8",
"@commercetools-test-data/utils": "6.3.8",
"@commercetools/platform-sdk": "^6.0.0",
"@faker-js/faker": "^8.0.0",
"@types/lodash": "^4.14.182",
"lodash": "^4.17.21"
}
}
47 changes: 47 additions & 0 deletions models/custom-view/src/custom-view-permission/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type {
TCustomViewPermission,
TCustomViewPermissionGraphql,
} from './types';
import * as CustomViewPermissionModel from './index';

describe('CustomView model builder', () => {
it(
...createBuilderSpec<TCustomViewPermission, TCustomViewPermission>(
'default',
CustomViewPermissionModel.random(),
expect.objectContaining({
name: 'view',
oAuthScopes: expect.arrayContaining([expect.stringMatching(/^view_/)]),
})
)
);

it(
...createBuilderSpec<TCustomViewPermission, TCustomViewPermissionGraphql>(
'graphql',
CustomViewPermissionModel.presets.ViewOnlyPermissions(),
expect.objectContaining({
__typename: 'CustomViewPermission',
name: 'view',
oAuthScopes: expect.arrayContaining([expect.stringMatching(/^view_/)]),
})
)
);

it(
...createBuilderSpec<TCustomViewPermission, TCustomViewPermissionGraphql>(
'graphql',
CustomViewPermissionModel.presets.ManageOnlyPermissions(),
expect.objectContaining({
__typename: 'CustomViewPermission',
name: 'manage',
oAuthScopes: expect.arrayContaining([
expect.stringMatching(/^manage_/),
]),
})
)
);
});
15 changes: 15 additions & 0 deletions models/custom-view/src/custom-view-permission/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type {
TCustomViewPermission,
TCreateCustomViewPermissionBuilder,
} from './types';

const Model: TCreateCustomViewPermissionBuilder = () =>
Builder<TCustomViewPermission>({
generator,
transformers,
});

export default Model;
54 changes: 54 additions & 0 deletions models/custom-view/src/custom-view-permission/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export const supportedViewOAuthScopes = [
'view_api_clients',
'view_audit_log',
'view_cart_discounts',
'view_categories',
'view_customer_groups',
'view_customers',
'view_discount_codes',
'view_import_containers',
'view_key_value_documents',
'view_messages',
'view_order_edits',
'view_orders',
'view_payments',
'view_products',
'view_project_settings',
'view_published_products',
'view_shipping_methods',
'view_shopping_lists',
'view_states',
'view_stores',
'view_tax_categories',
'view_types',
];

export const supportedManageOAuthScopes = [
'manage_api_clients',
'manage_audit_log',
'manage_cart_discounts',
'manage_categories',
'manage_customer_groups',
'manage_customers',
'manage_discount_codes',
'manage_extensions',
'manage_import_containers',
'manage_key_value_documents',
'manage_order_edits',
'manage_orders',
'manage_payments',
'manage_products',
'manage_project_settings',
'manage_shipping_methods',
'manage_shopping_lists',
'manage_states',
'manage_stores',
'manage_subscriptions',
'manage_tax_categories',
'manage_types',
];

export const supportedOAuthScopes = [
...supportedViewOAuthScopes,
...supportedManageOAuthScopes,
];
13 changes: 13 additions & 0 deletions models/custom-view/src/custom-view-permission/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Generator } from '@commercetools-test-data/core';
import sampleSize from 'lodash/sampleSize';
import { supportedViewOAuthScopes } from './constants';
import type { TCustomViewPermission } from './types';

const generator = Generator<TCustomViewPermission>({
fields: {
name: 'view',
oAuthScopes: sampleSize(supportedViewOAuthScopes, 2),
},
});

export default generator;
3 changes: 3 additions & 0 deletions models/custom-view/src/custom-view-permission/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as random } from './builder';
export * as presets from './presets';
export * from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ManageOnlyPermissions from './manage-only-permissions';
import ViewOnlyPermissions from './view-only-permissions';

export { ViewOnlyPermissions, ManageOnlyPermissions };
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { entryPointUriPathToResourceAccesses } from '@commercetools-frontend/application-config/ssr';
import { CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH } from '@commercetools-frontend/constants';
import camelCase from 'lodash/camelCase';
import sampleSize from 'lodash/sampleSize';
import upperFirst from 'lodash/upperFirst';
import CustomApplicationPermission from '../builder';
import { supportedManageOAuthScopes } from '../constants';

const preset = (additionalPermission = '') => {
const resourceAccesses = entryPointUriPathToResourceAccesses(
CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH,
[additionalPermission]
);
Comment on lines +10 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ This requires the change in App Kit to be released: commercetools/merchant-center-application-kit#3272

return CustomApplicationPermission()
.name(
!additionalPermission
? resourceAccesses.manage
: // @ts-ignore
resourceAccesses[
`manage${upperFirst(camelCase(additionalPermission))}`
]
)
.oAuthScopes(sampleSize(supportedManageOAuthScopes, 1));
};

export default preset;
Loading
Loading