Skip to content

Commit

Permalink
feat: add CustomViewInstallation model (#422)
Browse files Browse the repository at this point in the history
* feat: add custom view installation model

* refactor: code review improvements

* chore: changeset

* refactor: cleanup

* refactor: use generated types and add CustomViewInstallation model

* chore: update changeset

* chore: add proper imports
  • Loading branch information
kark authored Dec 5, 2023
1 parent 2285e9a commit 555c8da
Show file tree
Hide file tree
Showing 29 changed files with 582 additions and 19 deletions.
20 changes: 20 additions & 0 deletions .changeset/blue-mangos-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@commercetools-test-data/custom-view': minor
---

Allow to generate test data for Custom Views Installations.

```ts
import {
CustomViewInstallation,
RestrictedCustomViewInstallationForOrganization,
type TCustomViewInstallationGraphql,
type TRestrictedCustomViewInstallationForOrganizationGraphql,
} from '@commercetools-test-data/custom-view';

const customViewInstallation =
CustomViewInstallation.random().buildGraphql<TCustomViewInstallationGraphql>();

const restrictedCustomViewInstallationForOrganization =
RestrictedCustomViewInstallationForOrganization.random().buildGraphql<TRestrictedCustomViewInstallationForOrganizationGraphql>();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type {
TCustomViewInstallationPermission,
TCustomViewInstallationPermissionGraphql,
} from './types';
import * as CustomViewInstallationPermissionModel from './index';

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

it(
...createBuilderSpec<
TCustomViewInstallationPermission,
TCustomViewInstallationPermissionGraphql
>(
'graphql',
CustomViewInstallationPermissionModel.presets.ViewOnlyPermissions(),
expect.objectContaining({
__typename: 'CustomViewInstallationPermission',
name: 'view',
oAuthScopes: expect.arrayContaining([expect.stringMatching(/^view_/)]),
})
)
);

it(
...createBuilderSpec<
TCustomViewInstallationPermission,
TCustomViewInstallationPermissionGraphql
>(
'graphql',
CustomViewInstallationPermissionModel.presets.ManageOnlyPermissions(),
expect.objectContaining({
__typename: 'CustomViewInstallationPermission',
name: 'manage',
oAuthScopes: expect.arrayContaining([
expect.stringMatching(/^manage_/),
]),
})
)
);
});
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 {
TCustomViewInstallationPermission,
TCreateCustomViewInstallationPermissionBuilder,
} from './types';

const Model: TCreateCustomViewInstallationPermissionBuilder = () =>
Builder<TCustomViewInstallationPermission>({
generator,
transformers,
});

export default Model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Generator, fake } from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import sampleSize from 'lodash/sampleSize';
import { supportedViewOAuthScopes } from '../custom-view-permission/constants';
import type { TCustomViewInstallationPermission } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

const generator = Generator<TCustomViewInstallationPermission>({
fields: {
id: fake((f) => f.string.uuid()),
createdAt: fake(getOlderDate),
updatedAt: fake(getNewerDate),
name: 'view',
oAuthScopes: sampleSize(supportedViewOAuthScopes, 2),
},
});

export default generator;
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 { supportedManageOAuthScopes } from '../../custom-view-permission/constants';
import CustomViewInstallationPermission from '../builder';

const preset = (additionalPermission = '') => {
const resourceAccesses = entryPointUriPathToResourceAccesses(
CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH,
[additionalPermission]
);
return CustomViewInstallationPermission()
.name(
!additionalPermission
? resourceAccesses.manage
: // @ts-ignore
resourceAccesses[
`manage${upperFirst(camelCase(additionalPermission))}`
]
)
.oAuthScopes(sampleSize(supportedManageOAuthScopes, 1));
};

export default preset;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ViewOnlyPermissions, ManageOnlyPermissions } from '.';

describe('view only permissions', () => {
it('should build important properties', () => {
const built = ViewOnlyPermissions().buildGraphql();

expect(built).toEqual(
expect.objectContaining({
name: 'view',
oAuthScopes: expect.arrayContaining([expect.any(String)]),
})
);
});
});
describe('with manage only permissions', () => {
it('should build important properties', () => {
const built = ManageOnlyPermissions().buildGraphql();

expect(built).toEqual(
expect.objectContaining({
name: 'manage',
oAuthScopes: expect.arrayContaining([expect.any(String)]),
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 { supportedViewOAuthScopes } from '../../custom-view-permission/constants';
import CustomViewInstallationPermission from '../builder';

const preset = (additionalPermission = '') => {
const resourceAccesses = entryPointUriPathToResourceAccesses(
CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH,
[additionalPermission]
);
return CustomViewInstallationPermission()
.name(
!additionalPermission
? resourceAccesses.view
: // @ts-ignore
resourceAccesses[`view${upperFirst(camelCase(additionalPermission))}`]
)
.oAuthScopes(sampleSize(supportedViewOAuthScopes, 1));
};

export default preset;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Transformer } from '@commercetools-test-data/core';
import type {
TCustomViewInstallationPermission,
TCustomViewInstallationPermissionGraphql,
} from './types';

const transformers = {
default: Transformer<
TCustomViewInstallationPermission,
TCustomViewInstallationPermission
>('default', {}),
graphql: Transformer<
TCustomViewInstallationPermission,
TCustomViewInstallationPermissionGraphql
>('graphql', {
addFields: () => ({
__typename: 'CustomViewInstallationPermission',
}),
}),
};

export default transformers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { TBuilder } from '@commercetools-test-data/core';
import type { TMcSettingsCustomViewInstallationPermission } from '@commercetools-test-data/graphql-types';

export type TCustomViewInstallationPermissionGraphql =
TMcSettingsCustomViewInstallationPermission;

export type TCustomViewInstallationPermission = Omit<
TCustomViewInstallationPermissionGraphql,
'__typename'
>;

export type TCustomViewInstallationPermissionBuilder =
TBuilder<TCustomViewInstallationPermission>;
export type TCreateCustomViewInstallationPermissionBuilder =
() => TCustomViewInstallationPermissionBuilder;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type {
TCustomViewInstallation,
TCustomViewInstallationGraphql,
} from './types';
import * as CustomViewInstallationModel from './index';

describe('builder', () => {
it(
...createBuilderSpec<TCustomViewInstallation, TCustomViewInstallation>(
'default',
CustomViewInstallationModel.random(),
expect.objectContaining({
id: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
acceptedPermissions: expect.arrayContaining([
expect.objectContaining({
name: expect.stringMatching(/^(view|manage)$/),
oAuthScopes: expect.arrayContaining([expect.any(String)]),
}),
]),
installInAllProjects: expect.any(Boolean),
projects: expect.any(Array),
ownerId: expect.any(String),
owner: expect.any(Object),
})
)
);
it(
...createBuilderSpec<
TCustomViewInstallation,
TCustomViewInstallationGraphql
>(
'graphql',
CustomViewInstallationModel.random(),
expect.objectContaining({
__typename: 'CustomViewInstallation',
id: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
acceptedPermissions: expect.arrayContaining([
expect.objectContaining({
name: expect.stringMatching(/^(view|manage)$/),
oAuthScopes: expect.arrayContaining([expect.any(String)]),
}),
]),
installInAllProjects: expect.any(Boolean),
projects: expect.any(Array),
ownerId: expect.any(String),
owner: expect.any(Object),
})
)
);
});
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 {
TCustomViewInstallation,
TCreateCustomViewInstallationBuilder,
} from './types';

const Model: TCreateCustomViewInstallationBuilder = () =>
Builder<TCustomViewInstallation>({
generator,
transformers,
});

export default Model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fake, Generator } from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import * as CustomViewInstallationPermission from '../../custom-view-installation-permission';
import type { TCustomViewInstallation } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

const generator = Generator<TCustomViewInstallation>({
fields: {
id: fake((f) => f.string.uuid()),
createdAt: fake(getOlderDate),
updatedAt: fake(getNewerDate),
acceptedPermissions: fake(() => [
CustomViewInstallationPermission.presets.ViewOnlyPermissions(),
CustomViewInstallationPermission.presets.ManageOnlyPermissions(),
]),
projects: fake(() => []),
installInAllProjects: fake((f) => f.datatype.boolean()),
owner: fake(() => ({})),
ownerId: fake((f) => f.string.uuid()),
},
});

export default generator;
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,3 @@
const presets = {};

export default presets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Transformer } from '@commercetools-test-data/core';
import type {
TCustomViewInstallation,
TCustomViewInstallationGraphql,
} from './types';

const transformers = {
default: Transformer<TCustomViewInstallation, TCustomViewInstallation>(
'default',
{
buildFields: ['acceptedPermissions', 'projects'],
}
),
graphql: Transformer<TCustomViewInstallation, TCustomViewInstallationGraphql>(
'graphql',
{
buildFields: ['acceptedPermissions', 'projects'],
addFields: () => ({
__typename: 'CustomViewInstallation',
}),
}
),
};

export default transformers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { TBuilder } from '@commercetools-test-data/core';
import type { TMcSettingsCustomViewInstallation } from '@commercetools-test-data/graphql-types';

export type TCustomViewInstallationGraphql = TMcSettingsCustomViewInstallation;

export type TCustomViewInstallation = Omit<
TCustomViewInstallationGraphql,
'__typename'
>;

export type TCustomViewInstallationBuilder = TBuilder<TCustomViewInstallation>;
export type TCreateCustomViewInstallationBuilder =
() => TCustomViewInstallationBuilder;
3 changes: 3 additions & 0 deletions models/custom-view/src/custom-view-installation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Export models
export * as CustomViewInstallation from './custom-view-installation';
export * as RestrictedCustomViewInstallationForOrganization from './restricted-custom-view-installation-for-organization';
Loading

0 comments on commit 555c8da

Please sign in to comment.