Skip to content

Commit

Permalink
✨ feat(demo): restrict some demo features
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Apr 4, 2024
1 parent 87b4781 commit 8d7d49f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ AUTH_TOKEN_KEY=TEST123*!@#testtesttesttesttesttesttesttesttesttesttesttesttestte
NEXT_PUBLIC_BASE_URL=http://api.test.com

DEBUG_PRINT_LIMIT=1000

NEXT_PUBLIC_SHOW_UNFINISHED_FEATURES=true
21 changes: 21 additions & 0 deletions .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,98 @@
- name: "breaking-change"
color: ee0701
description: "A change that changes the API or breaks backward compatibility for users."

- name: "bugfix"
color: ee0701
description: "Inconsistencies or issues which will cause a problem for users or implementors."

- name: "documentation"
color: 0052cc
description: "Solely about the documentation of the project."

- name: "enhancement"
color: 1d76db
description: "Enhancement of the code, not introducing new features."

- name: "refactor"
color: 1d76db
description: "Updating the code with simpler, easier to understand or more efficient syntax or methods, but not introducing new features."

- name: "performance"
color: 1d76db
description: "Improving performance of the project, not introducing new features."

- name: "new-feature"
color: 0e8a16
description: "New features or options."

- name: "maintenance"
color: 2af79e
description: "Generic maintenance tasks."

- name: "ci"
color: 1d76db
description: "Work that improves the continuous integration."

- name: "dependencies"
color: 1d76db
description: "Change in project dependencies."

- name: "in-progress"
color: fbca04
description: "Issue is currently being worked on by a developer."

- name: "stale"
color: fef2c0
description: "No activity for quite some time."

- name: "no-stale"
color: fef2c0
description: "This is exempt from the stale bot."

- name: "security"
color: ee0701
description: "Addressing a vulnerability or security risk in this project."

- name: "incomplete"
color: fef2c0
description: "Missing information."

- name: "invalid"
color: fef2c0
description: "This is off-topic, spam, or otherwise doesn't apply to this project."

- name: "beginner-friendly"
color: 0e8a16
description: "Good first issue for people wanting to contribute to this project."

- name: "help-wanted"
color: 0e8a16
description: "We need some extra helping hands or expertise in order to resolve this!"

- name: "priority-critical"
color: ee0701
description: "Must be addressed as soon as possible."

- name: "priority-high"
color: b60205
description: "After critical issues are fixed, these should be dealt with before any further issues."

- name: "priority-medium"
color: 0e8a16
description: "This issue may be useful, and needs some attention."

- name: "priority-low"
color: e4ea8a
description: "Nice addition, maybe... someday..."

- name: "major"
color: b60205
description: "This PR causes a major bump in the version number."

- name: "minor"
color: 0e8a16
description: "This PR causes a minor bump in the version number."


# green chore #0e8a16
98 changes: 74 additions & 24 deletions src/__tests__/api/config/[key]/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ describe("/api/config/[key]/index", () => {
});

describe("App config key validation", () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
});

afterEach(() => {
process.env = OLD_ENV;
});

it("should return error for invalid config key", async () => {
const { req, res } = createAuthenticatedMocks({
method: "GET",
Expand All @@ -131,14 +142,14 @@ describe("/api/config/[key]/index", () => {

expect(res._getStatusCode()).toBe(400);
expect(res._getJSONData()).toMatchInlineSnapshot(`
{
"message": "Configuration key 'some-invalid-key' doesn't exist",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
{
"message": "Configuration key 'some-invalid-key' doesn't exist",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
});

it("should return error for no config key", async () => {
Expand All @@ -151,14 +162,14 @@ describe("/api/config/[key]/index", () => {

expect(res._getStatusCode()).toBe(400);
expect(res._getJSONData()).toMatchInlineSnapshot(`
{
"message": "Configuration key 'undefined' doesn't exist",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
{
"message": "Configuration key 'undefined' doesn't exist",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
});

it("should return error for entity config keys when the entity is not passed", async () => {
Expand All @@ -173,14 +184,53 @@ describe("/api/config/[key]/index", () => {

expect(res._getStatusCode()).toBe(400);
expect(res._getJSONData()).toMatchInlineSnapshot(`
{
"message": "Configuration of key 'table_views' requires entity",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
{
"message": "Configuration of key 'table_views' requires entity",
"method": "GET",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);
});

it("should throw error when updating a restricted config on demo", async () => {
process.env.NEXT_PUBLIC_IS_DEMO = "true";

const putReq = createAuthenticatedMocks({
method: "PUT",
query: {
key: "disabled_entities",
},
body: {
data: ["updated-1", "updated-3"],
},
});

await handler(putReq.req, putReq.res);

expect(putReq.res._getStatusCode()).toBe(400);
expect(putReq.res._getJSONData()).toMatchInlineSnapshot(`
{
"message": "This action is not available on the demo site",
"method": "PUT",
"name": "BadRequestError",
"path": "",
"statusCode": 400,
}
`);

const getReq = createAuthenticatedMocks({
method: "GET",
query: {
key: "disabled_entities",
},
});

await handler(getReq.req, getReq.res);

expect(getReq.res._getStatusCode()).toBe(200);
expect(getReq.res._getJSONData()).toEqual(["updated-1", "updated-2"]);
});
});
});
9 changes: 9 additions & 0 deletions src/backend/configuration/configuration.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { progammingError } from "backend/lib/errors";
import { AppConfigurationValueType } from "shared/configurations/constants";
import { notAllowedOnDemoValidation } from "backend/lib/request/validations/implementations/not-allowed-on-demo";
import {
createConfigDomainPersistenceService,
AbstractConfigDataPersistenceService,
Expand Down Expand Up @@ -42,6 +43,14 @@ export class ConfigurationApiService {
): Promise<void> {
this.checkConfigKeyEntityRequirement(key, entity);

const disabledConfigKeysOnDemo: AppConfigurationKeys[] = [
"disabled_entities",
];

if (disabledConfigKeysOnDemo.includes(key)) {
notAllowedOnDemoValidation();
}

return await this._appConfigPersistenceService.upsertItem(
this._appConfigPersistenceService.mergeKeyWithSecondaryKey(key, entity),
value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { BadRequestError } from "backend/lib/errors";
import { ValidationImplType } from "./types";

export const notAllowedOnDemoValidationImpl: ValidationImplType<
void
> = async () => {
export const notAllowedOnDemoValidation = () => {
if (process.env.NEXT_PUBLIC_IS_DEMO) {
throw new BadRequestError("This action is not available on the demo site");
}
};

export const notAllowedOnDemoValidationImpl: ValidationImplType<
void
> = async () => {
notAllowedOnDemoValidation();
};
6 changes: 6 additions & 0 deletions src/frontend/views/settings/System/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export function SystemSettings() {
{
validationType: "required",
},
{
validationType: "min",
constraint: {
value: 1,
},
},
],
},
}}
Expand Down

0 comments on commit 8d7d49f

Please sign in to comment.