-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
CustomViewInstallation
model (#422)
* 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
Showing
29 changed files
with
582 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
``` |
56 changes: 56 additions & 0 deletions
56
models/custom-view/src/custom-view-installation-permission/builder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_/), | ||
]), | ||
}) | ||
) | ||
); | ||
}); |
15 changes: 15 additions & 0 deletions
15
models/custom-view/src/custom-view-installation-permission/builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
19 changes: 19 additions & 0 deletions
19
models/custom-view/src/custom-view-installation-permission/generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
3 changes: 3 additions & 0 deletions
3
models/custom-view/src/custom-view-installation-permission/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
4 changes: 4 additions & 0 deletions
4
models/custom-view/src/custom-view-installation-permission/presets/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
26 changes: 26 additions & 0 deletions
26
...ls/custom-view/src/custom-view-installation-permission/presets/manage-only-permissions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
26 changes: 26 additions & 0 deletions
26
models/custom-view/src/custom-view-installation-permission/presets/presets.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]), | ||
}) | ||
); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
models/custom-view/src/custom-view-installation-permission/presets/view-only-permissions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
22 changes: 22 additions & 0 deletions
22
models/custom-view/src/custom-view-installation-permission/transformers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
15 changes: 15 additions & 0 deletions
15
models/custom-view/src/custom-view-installation-permission/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
57 changes: 57 additions & 0 deletions
57
models/custom-view/src/custom-view-installation/custom-view-installation/builder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
) | ||
); | ||
}); |
15 changes: 15 additions & 0 deletions
15
models/custom-view/src/custom-view-installation/custom-view-installation/builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
24 changes: 24 additions & 0 deletions
24
models/custom-view/src/custom-view-installation/custom-view-installation/generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
3 changes: 3 additions & 0 deletions
3
models/custom-view/src/custom-view-installation/custom-view-installation/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
3 changes: 3 additions & 0 deletions
3
models/custom-view/src/custom-view-installation/custom-view-installation/presets/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const presets = {}; | ||
|
||
export default presets; |
25 changes: 25 additions & 0 deletions
25
models/custom-view/src/custom-view-installation/custom-view-installation/transformers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
13 changes: 13 additions & 0 deletions
13
models/custom-view/src/custom-view-installation/custom-view-installation/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.