Skip to content

Commit

Permalink
feat: support xspace fields [Dante-708] (contentful#1482)
Browse files Browse the repository at this point in the history
* feat: adding new xspace fields definitions

* test: ceate and publish with xspace fields

* fix: code review

* fix: code review minor fix
  • Loading branch information
Cyberxon authored Sep 19, 2022
1 parent fb019fa commit f54681e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/entities/content-type-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ interface Item {
validations?: ContentTypeFieldValidation[]
}

interface ContentTypeAllowedResources {
type: string
source: string
contentTypes: string[]
}

export interface ContentFields<T = KeyValueMap> extends Item {
id: string
name: string
Expand All @@ -51,4 +57,5 @@ export interface ContentFields<T = KeyValueMap> extends Item {
items?: Item
apiName?: string
defaultValue?: T
allowedResources?: ContentTypeAllowedResources[]
}
2 changes: 2 additions & 0 deletions lib/entities/field-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type FieldType =
| { type: 'Location' }
| { type: 'Link'; linkType: 'Asset' }
| { type: 'Link'; linkType: 'Entry' }
| { type: 'ResourceLink'; linkType: 'Contentful:Entry' }
| { type: 'Array'; items: { type: 'Symbol' } }
| { type: 'Array'; items: { type: 'Link'; linkType: 'Entry' } }
| { type: 'Array'; items: { type: 'Resourcelink'; linkType: 'Contentful:Entry' } }
| { type: 'Array'; items: { type: 'Link'; linkType: 'Asset' } }
20 changes: 20 additions & 0 deletions test/integration/content-type-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,33 @@ describe('ContentType Api', async function () {
name: 'field2delete',
type: 'Text',
},
{
id: 'multiRefXSpace',
name: 'multiRefXSpace',
type: 'Array',
items: {
type: 'ResourceLink',
validations: [],
},
allowedResources: [
{
type: 'Contentful:Entry',
source: 'crn:contentful:::content:spaces/bkg4k0bz2fhq',
contentTypes: ['textBlock'],
},
],
},
]
return publishedContentType
.update() // update with fields
.then((updatedContentType) => {
expect(updatedContentType.isUpdated(), 'contentType is updated').to.be.ok
expect(updatedContentType.fields[0].id).equals('field', 'field id')
expect(updatedContentType.fields[1].id).equals('field2delete', 'field2delete id')
expect(updatedContentType.fields[2].id).equals(
'multiRefXSpace',
'multiRefXSpace id'
)
return updatedContentType
.omitAndDeleteField('field2delete') // omit and delete field
.then((deletedFieldContentType) => {
Expand Down
137 changes: 136 additions & 1 deletion test/integration/entry-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('Entry Api', () => {
})
})

describe('write', function () {
describe('write', () => {
let space
let environment
let contentType
Expand Down Expand Up @@ -419,4 +419,139 @@ describe('Entry Api', () => {
expect(response.items[0].sys.publishedAt).to.not.be.undefined
})
})

/**
* This test was created to make sure the SDK supports
* creating/updating/publishing/upublishing/deleting of
* entries with cross space links
*
* This test is slightly fragile as it can break if the
* entry '4zimQzVMxDsSX607PCfo2u' from the space '6mqcevu5a50r' is deleted
*/
describe('write with xspace references', () => {
let xSpaceEnabledSpace
let xSpaceEnabledEnvironment
let xSpaceDisabledSpace
let xSpaceDisabledEnvironment
let xSpaceDisabledContentType
let xSpaceEnabledContentType
let xSpaceDisabledEntryId

let contentTypeData = {
name: 'testCTXSpace',
fields: [
{
id: 'title',
name: 'Title',
type: 'Text',
},
{
id: 'multiRefXSpace',
name: 'multiRefXSpace',
type: 'Array',
items: {
type: 'ResourceLink',
validations: [],
},
allowedResources: [
{
type: 'Contentful:Entry',
source: 'crn:contentful:::content:spaces/6mqcevu5a50r',
contentTypes: ['testCtxSpace'],
},
],
},
],
}

let entryData = {
fields: {
title: { 'en-US': 'this is the title' },
multiRefXSpace: {
'en-US': [
{
sys: {
type: 'ResourceLink',
linkType: 'Contentful:Entry',
urn: 'crn:contentful:::content:spaces/6mqcevu5a50r/entries/4zimQzVMxDsSX607PCfo2u',
},
},
],
},
},
}

before(async () => {
// The default space has xspace enabled in it through the feature flags
xSpaceEnabledSpace = await getDefaultSpace()
xSpaceEnabledEnvironment = await xSpaceEnabledSpace.getEnvironment('master')
xSpaceEnabledContentType = await xSpaceEnabledEnvironment.createContentTypeWithId(
generateRandomId('test-content-type'),
contentTypeData
)
await xSpaceEnabledContentType.publish()

// Creating a new space that doesn't have the xspace feature enabled
xSpaceDisabledSpace = await createTestSpace(initClient(), 'XSpaceDisabledEntry')
xSpaceDisabledEnvironment = await createTestEnvironment(
xSpaceDisabledSpace,
'Testing Environment'
)
xSpaceDisabledContentType = await xSpaceDisabledEnvironment.createContentTypeWithId(
generateRandomId('test-content-type'),
contentTypeData
)
await xSpaceDisabledContentType.publish()
})

after(async () => {
// Cleaning up the xSpaceDisabled Space
await xSpaceDisabledEnvironment.deleteEntry(xSpaceDisabledEntryId)
await xSpaceDisabledContentType
.unpublish()
.then((unpublishedContentType) => unpublishedContentType.delete())

await xSpaceDisabledSpace.delete()

// Cleaning up the the default Space (xSpace feature enabled)
await xSpaceEnabledContentType
.unpublish()
.then((unpublishedContentType) => unpublishedContentType.delete())
})

test('Blocks publishing an entry with xspace references if the feature is disabled for the space', async () => {
return xSpaceDisabledEnvironment
.createEntry(xSpaceDisabledContentType.sys.id, entryData)
.then((entry) => {
xSpaceDisabledEntryId = entry.sys.id
expect(entry.isDraft(), 'entry is in draft').ok
expect(entry.fields.title['en-US']).equals('this is the title', 'original title')
return entry.publish().catch((accessDeniedError) => {
let errorMessage = JSON.parse(accessDeniedError.message)
expect(accessDeniedError.name).equals('AccessDenied', 'Access Denied Error')
expect(errorMessage.status).equals(403, '403 forbidden status')
expect(errorMessage.details.reasons).equals(
'Cross space links feature is not enabled for this space',
'reason explained'
)
})
})
})

test('can create, publish, unpublish and delete an entry with xspace references if the feature is enabled for the space', async () => {
return xSpaceEnabledEnvironment
.createEntry(xSpaceEnabledContentType.sys.id, entryData)
.then((entry) => {
expect(entry.isDraft(), 'entry is in draft').ok
expect(entry.fields.title['en-US']).equals('this is the title', 'original title')
return entry.publish().then((publishedEntry) => {
expect(publishedEntry.isPublished(), 'entry is published').ok
return publishedEntry.unpublish().then((unpublishedEntry) => {
expect(unpublishedEntry.isDraft(), 'entry is back in draft').ok
return publishedEntry.delete()
})
})
})
})
})
})

0 comments on commit f54681e

Please sign in to comment.