Skip to content

Commit

Permalink
Merge pull request #114 from pluginpal/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
boazpoolman authored Feb 4, 2024
2 parents f58bcae + a9805a4 commit 3c00789
Show file tree
Hide file tree
Showing 15 changed files with 826 additions and 73 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ jobs:
- name: Install playground dependencies
run: yarn playground:install
- name: Build the playground
run: NODE_ENV=ci yarn playground:build
run: yarn playground:build
- name: Run unit tests
run: yarn test:unit
- name: Run integration tests
run: NODE_ENV=ci yarn test:integration
run: yarn test:integration
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"playground:build": "cd playground && yarn build",
"playground:start": "cd playground && yarn start",
"playground:develop": "cd playground && yarn develop",
"test:unit": "ENV_PATH=./playground/.env jest --verbose --runInBand",
"test:integration": "ENV_PATH=./playground/.env jest --verbose --runInBand --forceExit --testMatch '**/healthcheck.test.js'"
},
"dependencies": {
Expand Down
175 changes: 175 additions & 0 deletions packages/core/server/admin-api/__tests__/query-layer-decorator.test.ts
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 packages/core/server/content-api/__tests__/core.test.ts
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 playground/src/api/category/content-types/category/schema.json
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"
}
}
}
7 changes: 7 additions & 0 deletions playground/src/api/category/controllers/category.ts
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');
7 changes: 7 additions & 0 deletions playground/src/api/category/routes/category.ts
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');
7 changes: 7 additions & 0 deletions playground/src/api/category/services/category.ts
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');
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"
}
}
}
Loading

0 comments on commit 3c00789

Please sign in to comment.