Skip to content

Commit

Permalink
fix(api-headless-cms): dates and identity in entry input (#3718)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored and Pavel910 committed Nov 23, 2023
1 parent d6385e3 commit b63c1f8
Show file tree
Hide file tree
Showing 42 changed files with 890 additions and 115 deletions.
7 changes: 7 additions & 0 deletions packages/api-aco/__tests__/snapshots/customAppsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ export const createCustomAppsSchemaSnapshot = () => {
savedOn_lte: DateTime
savedOn_between: [DateTime!]
savedOn_not_between: [DateTime!]
publishedOn: DateTime
publishedOn_gt: DateTime
publishedOn_gte: DateTime
publishedOn_lt: DateTime
publishedOn_lte: DateTime
publishedOn_between: [DateTime!]
publishedOn_not_between: [DateTime!]
createdBy: String
createdBy_not: String
createdBy_in: [String!]
Expand Down
7 changes: 7 additions & 0 deletions packages/api-aco/__tests__/snapshots/defaultAppsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export const createDefaultAppsSchemaSnapshot = () => {
savedOn_lte: DateTime
savedOn_between: [DateTime!]
savedOn_not_between: [DateTime!]
publishedOn: DateTime
publishedOn_gt: DateTime
publishedOn_gte: DateTime
publishedOn_lt: DateTime
publishedOn_lte: DateTime
publishedOn_between: [DateTime!]
publishedOn_not_between: [DateTime!]
createdBy: String
createdBy_not: String
createdBy_in: [String!]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export const createChangeRequestStorageOperations = (
const entry = await security.withoutAuthorization(async () => {
return cms.updateEntry(model, params.id, {
...existingEntry,
...params.data
...params.data,
savedOn: new Date()
});
});
return getFieldValues({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export const createCommentStorageOperations = ({
const entry = await security.withoutAuthorization(async () => {
return cms.updateEntry(model, params.id, {
...existingEntry,
...params.data
...params.data,
savedOn: new Date()
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export const createContentReviewStorageOperations = ({
const entry = await security.withoutAuthorization(async () => {
return cms.updateEntry(model, params.id, {
...existingEntry,
...params.data
...params.data,
savedOn: new Date()
});
});
return getFieldValues(entry, baseFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export const createReviewerStorageOperations = ({
const entry = await security.withoutAuthorization(async () => {
return cms.updateEntry(model, params.id, {
...existingEntry,
...params.data
...params.data,
savedOn: new Date()
});
});
return getFieldValues(entry, baseFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export const createWorkflowStorageOperations = (
const existingEntry = await getWorkflow({ id: params.id });
const input = {
...existingEntry,
...params.data
...params.data,
savedOn: new Date()
};
const data = formatReviewersForRefInput(
input as CreateApwWorkflowParams,
Expand Down
58 changes: 58 additions & 0 deletions packages/api-file-manager/__tests__/file.customDates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import useGqlHandler from "./utils/useGqlHandler";
import { fileAData, ids } from "./mocks/files";

describe("file custom dates", () => {
const { createFile, updateFile } = useGqlHandler();

it("should create and update file with custom dates", async () => {
const [createResponse] = await createFile(
{
data: {
...fileAData,
createdOn: "1995-01-01T00:00:00.000Z",
savedOn: "1995-01-01T00:00:00.000Z"
}
},
["createdOn", "savedOn"]
);
expect(createResponse).toEqual({
data: {
fileManager: {
createFile: {
data: {
...fileAData,
createdOn: "1995-01-01T00:00:00.000Z",
savedOn: "1995-01-01T00:00:00.000Z"
},
error: null
}
}
}
});

const [updateResponse] = await updateFile(
{
id: ids.A,
data: {
createdOn: "2005-01-01T00:00:00.000Z",
savedOn: "2005-01-01T00:00:00.000Z"
}
},
["createdOn", "savedOn"]
);
expect(updateResponse).toEqual({
data: {
fileManager: {
updateFile: {
data: {
...fileAData,
createdOn: "2005-01-01T00:00:00.000Z",
savedOn: "2005-01-01T00:00:00.000Z"
},
error: null
}
}
}
});
});
});
107 changes: 107 additions & 0 deletions packages/api-file-manager/__tests__/file.customIdentities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import useGqlHandler from "./utils/useGqlHandler";
import { fileAData } from "./mocks/files";
import { SecurityIdentity } from "@webiny/api-security/types";

const extraFields = ["createdBy {id displayName type}", "modifiedBy {id displayName type}"];
describe("file custom identities", () => {
const { createFile, updateFile, identity: defaultIdentity } = useGqlHandler();

const mockIdentityOne: SecurityIdentity = {
id: "mock-identity-one",
displayName: "Mock Identity One",
type: "mockOne"
};
const mockIdentityTwo: SecurityIdentity = {
id: "mock-identity-two",
displayName: "Mock Identity Two",
type: "mockTwo"
};

it("should create a file with custom identity", async () => {
const [createRegularResponse] = await createFile(
{
data: {
...fileAData
}
},
extraFields
);
expect(createRegularResponse).toEqual({
data: {
fileManager: {
createFile: {
data: {
...fileAData,
createdBy: defaultIdentity,
modifiedBy: null
},
error: null
}
}
}
});

const [createCustomIdentityResponse] = await createFile(
{
data: {
...fileAData,
createdBy: mockIdentityOne,
modifiedBy: mockIdentityTwo
}
},
extraFields
);
expect(createCustomIdentityResponse).toEqual({
data: {
fileManager: {
createFile: {
data: {
...fileAData,
createdBy: mockIdentityOne,
modifiedBy: mockIdentityTwo
},
error: null
}
}
}
});
});

it("should update a file with custom identity", async () => {
const [createResponse] = await createFile(
{
data: {
...fileAData
}
},
extraFields
);

const id = createResponse.data.fileManager.createFile.data.id;

const [updateResponse] = await updateFile(
{
id,
data: {
createdBy: mockIdentityOne,
modifiedBy: mockIdentityTwo
}
},
extraFields
);
expect(updateResponse).toEqual({
data: {
fileManager: {
updateFile: {
data: {
...fileAData,
createdBy: mockIdentityOne,
modifiedBy: mockIdentityTwo
},
error: null
}
}
}
});
});
});
10 changes: 6 additions & 4 deletions packages/api-file-manager/__tests__/file.lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("File lifecycle events", () => {
* Parameters that were received in the lifecycle hooks must be valid as well.
*/
const beforeCreate = tracker.getLast("file:beforeCreate");
expect(beforeCreate && beforeCreate.params[0]).toEqual({
expect(beforeCreate && beforeCreate.params[0]).toMatchObject({
file: {
...fileData,
...hookParamsExpected,
Expand All @@ -88,7 +88,7 @@ describe("File lifecycle events", () => {
}
});
const afterCreate = tracker.getLast("file:beforeCreate");
expect(afterCreate && afterCreate.params[0]).toEqual({
expect(afterCreate && afterCreate.params[0]).toMatchObject({
file: {
...fileData,
...hookParamsExpected,
Expand Down Expand Up @@ -138,7 +138,7 @@ describe("File lifecycle events", () => {
* Parameters that were received in the lifecycle hooks must be valid as well.
*/
const beforeUpdate = tracker.getLast("file:beforeUpdate");
expect(beforeUpdate && beforeUpdate.params[0]).toEqual({
expect(beforeUpdate && beforeUpdate.params[0]).toMatchObject({
input: { tags: [...fileData.tags, TAG] },
original: {
...fileData,
Expand All @@ -160,7 +160,7 @@ describe("File lifecycle events", () => {
}
});
const afterUpdate = tracker.getLast("file:afterUpdate");
expect(afterUpdate && afterUpdate.params[0]).toEqual({
expect(afterUpdate && afterUpdate.params[0]).toMatchObject({
input: { tags: [...fileData.tags, TAG] },
original: {
...fileData,
Expand Down Expand Up @@ -219,6 +219,7 @@ describe("File lifecycle events", () => {
file: {
...fileData,
...hookParamsExpected,
modifiedBy: null,
location: {
folderId: ROOT_FOLDER
},
Expand All @@ -230,6 +231,7 @@ describe("File lifecycle events", () => {
file: {
...fileData,
...hookParamsExpected,
modifiedBy: null,
location: {
folderId: ROOT_FOLDER
},
Expand Down
22 changes: 22 additions & 0 deletions packages/api-file-manager/__tests__/mocks/file.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default /* GraphQL */ `
savedOn: DateTime!
createdOn: DateTime!
createdBy: FmCreatedBy!
modifiedBy: FmCreatedBy
src: String
location: FmFile_Location
name: String
Expand Down Expand Up @@ -127,8 +128,18 @@ export default /* GraphQL */ `
article: RefFieldInput
}
input FmCreatedByInput {
id: ID!
displayName: String!
type: String!
}
input FmFileCreateInput {
id: ID!
createdOn: DateTime
savedOn: DateTime
createdBy: FmCreatedByInput
modifiedBy: FmCreatedByInput
location: FmFile_LocationInput
name: String
key: String
Expand All @@ -141,6 +152,10 @@ export default /* GraphQL */ `
}
input FmFileUpdateInput {
createdOn: DateTime
savedOn: DateTime
createdBy: FmCreatedByInput
modifiedBy: FmCreatedByInput
location: FmFile_LocationInput
name: String
key: String
Expand Down Expand Up @@ -176,6 +191,13 @@ export default /* GraphQL */ `
savedOn_lte: DateTime
savedOn_between: [DateTime!]
savedOn_not_between: [DateTime!]
publishedOn: DateTime
publishedOn_gt: DateTime
publishedOn_gte: DateTime
publishedOn_lt: DateTime
publishedOn_lte: DateTime
publishedOn_between: [DateTime!]
publishedOn_not_between: [DateTime!]
createdBy: String
createdBy_not: String
createdBy_in: [String!]
Expand Down
14 changes: 7 additions & 7 deletions packages/api-file-manager/__tests__/utils/tenancySecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ interface Config {
identity?: SecurityIdentity | null;
}

export const defaultIdentity: SecurityIdentity = {
id: "12345678",
type: "admin",
displayName: "John Doe"
};

export const createTenancyAndSecurity = ({ permissions, identity }: Config) => {
const securityStorage = getStorageOps<SecurityStorageOperations>("security");
const tenancyStorage = getStorageOps<TenancyStorageOperations>("tenancy");
Expand All @@ -41,13 +47,7 @@ export const createTenancyAndSecurity = ({ permissions, identity }: Config) => {
});

context.security.addAuthenticator(async () => {
return (
identity || {
id: "12345678",
type: "admin",
displayName: "John Doe"
}
);
return identity || defaultIdentity;
});

context.security.addAuthorizer(async () => {
Expand Down
Loading

0 comments on commit b63c1f8

Please sign in to comment.