-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/api-headless-cms/__tests__/mocks/preventIdsWithWrongFormat.js
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,104 @@ | ||
import { locales } from "./I18NLocales"; | ||
|
||
export default { | ||
modelWithFieldIdSetToId: ({ contentModelGroupId }) => ({ | ||
data: { | ||
name: "Book", | ||
group: contentModelGroupId, | ||
fields: [ | ||
{ | ||
_id: "vqk-UApa0-1", | ||
fieldId: "id", | ||
type: "text", | ||
label: { | ||
values: [ | ||
{ | ||
locale: locales.en.id, | ||
value: "Title" | ||
}, | ||
{ | ||
locale: locales.de.id, | ||
value: "Titel" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}), | ||
modelWithFieldIdSetToIdIncludingWhiteSpace: ({ contentModelGroupId }) => ({ | ||
data: { | ||
name: "Book", | ||
group: contentModelGroupId, | ||
fields: [ | ||
{ | ||
_id: "vqk-UApa0-1", | ||
fieldId: " iD ", | ||
type: "text", | ||
label: { | ||
values: [ | ||
{ | ||
locale: locales.en.id, | ||
value: "Title" | ||
}, | ||
{ | ||
locale: locales.de.id, | ||
value: "Titel" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}), | ||
modelWithFieldIdContainingWhiteSpace: ({ contentModelGroupId }) => ({ | ||
data: { | ||
name: "Book", | ||
group: contentModelGroupId, | ||
fields: [ | ||
{ | ||
_id: "vqk-UApa0-1", | ||
fieldId: " title ", | ||
type: "text", | ||
label: { | ||
values: [ | ||
{ | ||
locale: locales.en.id, | ||
value: "Title" | ||
}, | ||
{ | ||
locale: locales.de.id, | ||
value: "Titel" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}), | ||
modelWithValidFieldIds: ({ contentModelGroupId }) => ({ | ||
data: { | ||
name: "Book", | ||
group: contentModelGroupId, | ||
fields: [ | ||
{ | ||
_id: "vqk-UApa0-1", | ||
fieldId: "title", | ||
type: "text", | ||
label: { | ||
values: [ | ||
{ | ||
locale: locales.en.id, | ||
value: "Title" | ||
}, | ||
{ | ||
locale: locales.de.id, | ||
value: "Titel" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
}; |
98 changes: 98 additions & 0 deletions
98
packages/api-headless-cms/__tests__/preventIdsWithWrongFormat.test.js
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,98 @@ | ||
import useContentHandler from "./utils/useContentHandler"; | ||
import mocks from "./mocks/preventIdsWithWrongFormat"; | ||
import { createContentModelGroup, createEnvironment } from "@webiny/api-headless-cms/testing"; | ||
|
||
describe("Fields ID Test", () => { | ||
const { environment, database } = useContentHandler(); | ||
|
||
const initial = {}; | ||
|
||
beforeAll(async () => { | ||
// Let's create a basic environment and a content model group. | ||
initial.environment = await createEnvironment({ database }); | ||
initial.contentModelGroup = await createContentModelGroup({ database }); | ||
}); | ||
|
||
it(`should not allow fields with fieldId set to "id"`, async () => { | ||
const { createContentModel } = environment(initial.environment.id); | ||
|
||
let error = null; | ||
try { | ||
await createContentModel( | ||
mocks.modelWithFieldIdSetToId({ contentModelGroupId: initial.contentModelGroup.id }) | ||
); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error.data).toEqual({ | ||
invalidFields: { | ||
"fields.fieldId": 'Provided ID id is not valid - "id" is an auto-generated field.' | ||
} | ||
}); | ||
|
||
error = null; | ||
try { | ||
await createContentModel( | ||
mocks.modelWithFieldIdSetToIdIncludingWhiteSpace({ | ||
contentModelGroupId: initial.contentModelGroup.id | ||
}) | ||
); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error.data).toEqual({ | ||
invalidFields: { | ||
"fields.fieldId": 'Provided ID iD is not valid - "id" is an auto-generated field.' | ||
} | ||
}); | ||
|
||
error = null; | ||
try { | ||
await createContentModel( | ||
mocks.modelWithValidFieldIds({ contentModelGroupId: initial.contentModelGroup.id }) | ||
); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBe(null); | ||
|
||
error = null; | ||
}); | ||
|
||
it(`should trim the "fieldId" before saving to DB`, async () => { | ||
const { createContentModel } = environment(initial.environment.id); | ||
|
||
let error = null; | ||
|
||
let contentModel; | ||
try { | ||
contentModel = await createContentModel( | ||
mocks.modelWithFieldIdContainingWhiteSpace({ | ||
contentModelGroupId: initial.contentModelGroup.id | ||
}) | ||
); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBe(null); | ||
|
||
expect(contentModel.fields[0].fieldId).toEqual("title"); | ||
|
||
error = null; | ||
try { | ||
await createContentModel( | ||
mocks.modelWithValidFieldIds({ contentModelGroupId: initial.contentModelGroup.id }) | ||
); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error).toBe(null); | ||
|
||
error = null; | ||
}); | ||
}); |