-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from pluginpal/feature/unit-tests
Feature/unit tests
- Loading branch information
Showing
15 changed files
with
826 additions
and
73 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
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
175 changes: 175 additions & 0 deletions
175
packages/core/server/admin-api/__tests__/query-layer-decorator.test.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,175 @@ | ||
import request from 'supertest'; | ||
// @ts-ignore | ||
// eslint-disable-next-line import/no-relative-packages | ||
import { setupStrapi, stopStrapi } from '../../../../../playground/tests/helpers'; | ||
|
||
beforeAll(async () => { | ||
await setupStrapi(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopStrapi(); | ||
}); | ||
|
||
describe('Query layer decorator', () => { | ||
it('Create - Should generate a new URL alias', async () => { | ||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some amazing new page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.url_path', '/page/some-amazing-new-page'); | ||
expect(page).toHaveProperty('url_alias.generated', true); | ||
expect(page).toHaveProperty('url_alias.contenttype', 'api::test.test'); | ||
}); | ||
|
||
it('Create - Should re-generate a pre-created URL alias if generated is set to true', async () => { | ||
const alias = await strapi.entityService.create("plugin::webtools.url-alias", { | ||
data: { | ||
url_path: '/generated-pre-created-path', | ||
generated: true, | ||
contenttype: 'api::test.test', | ||
}, | ||
}); | ||
|
||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Generated amazing new page', | ||
url_alias: alias.id, | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.id', alias.id); | ||
expect(page).toHaveProperty('url_alias.url_path', '/page/generated-amazing-new-page'); | ||
expect(page).toHaveProperty('url_alias.generated', true); | ||
expect(page).toHaveProperty('url_alias.contenttype', 'api::test.test'); | ||
}); | ||
|
||
it('Create - Should not re-generate a pre-created URL alias if generated is set to false', async () => { | ||
const alias = await strapi.entityService.create("plugin::webtools.url-alias", { | ||
data: { | ||
url_path: '/pre-created-path', | ||
generated: false, | ||
contenttype: 'api::test.test', | ||
}, | ||
}); | ||
|
||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some amazing new page', | ||
url_alias: alias.id, | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.id', alias.id); | ||
expect(page).toHaveProperty('url_alias.url_path', '/pre-created-path'); | ||
expect(page).toHaveProperty('url_alias.generated', false); | ||
}); | ||
|
||
it('Update - Should generate a new URL alias if none is present', async () => { | ||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some about to be updated new page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
const oldAliasId = page.url_alias.id; | ||
|
||
// Delete the created url alias to make sure none is present | ||
// at the time of running the .update() query. | ||
await strapi.entityService.delete("plugin::webtools.url-alias", oldAliasId); | ||
|
||
const updatedPage = await strapi.entityService.update("api::test.test", page.id, { | ||
data: { | ||
// @ts-ignore | ||
title: 'Some updated page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(updatedPage?.url_alias?.id).not.toBe(oldAliasId); | ||
expect(updatedPage).toHaveProperty('url_alias.url_path', '/page/some-updated-page'); | ||
expect(updatedPage).toHaveProperty('url_alias.generated', true); | ||
expect(updatedPage).toHaveProperty('url_alias.contenttype', 'api::test.test'); | ||
}); | ||
|
||
it('Update - Should re-generate an existing URL alias if generated is set to true', async () => { | ||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some about to be updated new page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.url_path', '/page/some-about-to-be-updated-new-page') | ||
expect(page).toHaveProperty('url_alias.generated', true); | ||
|
||
const updatedPage = await strapi.entityService.update("api::test.test", page.id, { | ||
data: { | ||
// @ts-ignore | ||
title: 'Some updated page with overwritten url alias', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(updatedPage).toHaveProperty('url_alias.id', page.url_alias.id); | ||
expect(updatedPage).toHaveProperty('url_alias.url_path', '/page/some-updated-page-with-overwritten-url-alias'); | ||
expect(updatedPage).toHaveProperty('url_alias.generated', true); | ||
}); | ||
|
||
it('Update - Should not re-generate an existing URL alias if generated is set to false', async () => { | ||
const alias = await strapi.entityService.create("plugin::webtools.url-alias", { | ||
data: { | ||
url_path: '/path-should-not-update', | ||
generated: false, | ||
contenttype: 'api::test.test', | ||
}, | ||
}); | ||
|
||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some about to be updated new page', | ||
url_alias: alias.id, | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.url_path', '/path-should-not-update') | ||
expect(page).toHaveProperty('url_alias.generated', false); | ||
|
||
const updatedPage = await strapi.entityService.update("api::test.test", page.id, { | ||
data: { | ||
// @ts-ignore | ||
title: 'Some updated page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(updatedPage).toHaveProperty('url_alias.id', page.url_alias.id); | ||
expect(updatedPage).toHaveProperty('url_alias.url_path', '/path-should-not-update'); | ||
expect(updatedPage).toHaveProperty('url_alias.generated', false); | ||
}); | ||
|
||
it('Delete - Should delete the corresponding URL alias as wel', async () => { | ||
const page = await strapi.entityService.create("api::test.test", { | ||
data: { | ||
title: 'Some about to be deleted new page', | ||
}, | ||
populate: ['url_alias'] | ||
}); | ||
|
||
expect(page).toHaveProperty('url_alias.url_path', '/page/some-about-to-be-deleted-new-page') | ||
expect(page).toHaveProperty('url_alias.generated', true); | ||
|
||
await strapi.entityService.delete("api::test.test", page.id); | ||
|
||
const alias = await strapi.entityService.findOne("plugin::webtools.url-alias", page.url_alias.id); | ||
|
||
expect(alias).toBe(null); | ||
}); | ||
}); |
115 changes: 115 additions & 0 deletions
115
packages/core/server/content-api/__tests__/core.test.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,115 @@ | ||
import request from 'supertest'; | ||
// @ts-ignore | ||
// eslint-disable-next-line import/no-relative-packages | ||
import { setupStrapi, stopStrapi } from '../../../../../playground/tests/helpers'; | ||
|
||
beforeAll(async () => { | ||
await setupStrapi(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopStrapi(); | ||
}); | ||
|
||
describe('Core controller - Router', () => { | ||
it('Should return a 200 if a page was found', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page).toHaveProperty('data.attributes.title', 'Published test page'); | ||
expect(page).toHaveProperty('data.attributes.contentType', 'api::test.test'); | ||
}); | ||
|
||
it('Should return a 404 if no page was found', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/does-not-exist') | ||
.expect(404) | ||
.then((data) => data.body); | ||
|
||
expect(page).toHaveProperty('data', null); | ||
expect(page).toHaveProperty('error.status', 404); | ||
}); | ||
|
||
it('Should return a 403 if the user has insufficient rights', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/private-category/published') | ||
.expect(403) | ||
.then((data) => data.body); | ||
|
||
expect(page).toHaveProperty('data', null); | ||
expect(page).toHaveProperty('error.status', 403); | ||
}); | ||
|
||
it('Should fetch a draft entries by default', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/unpublished-test-page') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page).toHaveProperty('data.attributes.title', 'Unpublished test page'); | ||
}); | ||
|
||
it('Should not fetch draft entries with publicationState set to live', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/unpublished-test-page&publicationState=live') | ||
.expect(404) | ||
.then((data) => data.body); | ||
}); | ||
|
||
it('Should allow query parameters for population', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/unpublished-test-page&publicationState=preview&populate=*') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page.data.attributes.category.data).not.toBe(null); | ||
}); | ||
|
||
it('Should sanitize populated relations without the "find" permission', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page&populate=*') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page).not.toHaveProperty('data.attributes.private-category'); | ||
}); | ||
|
||
it('Should not sanitize unpublished populated relations', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page&populate=*') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page.data.attributes.category.data).not.toBe(null); | ||
}); | ||
|
||
it('Should sanitize unpublished populated relations with publicationState set to live', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page&populate=*&publicationState=live') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page.data.attributes.category.data).toBe(null); | ||
}); | ||
|
||
it('Should allow query parameters for field selection', async () => { | ||
const page = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(page).toHaveProperty("data.attributes.createdAt"); | ||
|
||
const filteredPage = await request(strapi.server.httpServer) | ||
.get('/api/webtools/router?path=/page/published-test-page&fields[0]=title') | ||
.expect(200) | ||
.then((data) => data.body); | ||
|
||
expect(filteredPage).not.toHaveProperty("data.attributes.createdAt"); | ||
expect(page).toHaveProperty("data.attributes.title"); | ||
}); | ||
|
||
// it('Should allow query parameters for localization', async () => {}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
playground/src/api/category/content-types/category/schema.json
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,29 @@ | ||
{ | ||
"kind": "collectionType", | ||
"collectionName": "categories", | ||
"info": { | ||
"singularName": "category", | ||
"pluralName": "categories", | ||
"displayName": "Category", | ||
"description": "" | ||
}, | ||
"options": { | ||
"draftAndPublish": true | ||
}, | ||
"pluginOptions": { | ||
"webtools": { | ||
"enabled": true | ||
} | ||
}, | ||
"attributes": { | ||
"test": { | ||
"type": "relation", | ||
"relation": "oneToOne", | ||
"target": "api::test.test", | ||
"inversedBy": "category" | ||
}, | ||
"title": { | ||
"type": "string" | ||
} | ||
} | ||
} |
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,7 @@ | ||
/** | ||
* category controller | ||
*/ | ||
|
||
import { factories } from '@strapi/strapi' | ||
|
||
export default factories.createCoreController('api::category.category'); |
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,7 @@ | ||
/** | ||
* category router | ||
*/ | ||
|
||
import { factories } from '@strapi/strapi'; | ||
|
||
export default factories.createCoreRouter('api::category.category'); |
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,7 @@ | ||
/** | ||
* category service | ||
*/ | ||
|
||
import { factories } from '@strapi/strapi'; | ||
|
||
export default factories.createCoreService('api::category.category'); |
29 changes: 29 additions & 0 deletions
29
playground/src/api/private-category/content-types/private-category/schema.json
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,29 @@ | ||
{ | ||
"kind": "collectionType", | ||
"collectionName": "private_categories", | ||
"info": { | ||
"singularName": "private-category", | ||
"pluralName": "private-categories", | ||
"displayName": "Private category", | ||
"description": "" | ||
}, | ||
"options": { | ||
"draftAndPublish": true | ||
}, | ||
"pluginOptions": { | ||
"webtools": { | ||
"enabled": true | ||
} | ||
}, | ||
"attributes": { | ||
"title": { | ||
"type": "string" | ||
}, | ||
"test": { | ||
"type": "relation", | ||
"relation": "oneToOne", | ||
"target": "api::test.test", | ||
"mappedBy": "private_category" | ||
} | ||
} | ||
} |
Oops, something went wrong.