Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add restrictions when creating, editing mock server [INS-4667] [INS-4645] #8168

Conversation

yaoweiprc
Copy link
Contributor

@yaoweiprc yaoweiprc commented Nov 12, 2024

There are two types of mock server. One is cloud mock server which is hosted on Insomnia backend. The other is self-hosted mock server which is hosted by enterprise users themselves.
Cloud mock server were allowed to be created in cloud projects by any logined users.
Self-hosted mock server were only allowed to be created by enterprise users.

But there were on situation that we did not take into account, which is that one organization may have its storage rule which can restrict the type of projects in it, including the type of mock servers in it.

So I add this restriction when user create a mock server (in packages/insomnia/src/ui/components/modals/mock-server-settings-modal.tsx) or editing an existing mock server (in packages/insomnia/src/ui/components/modals/workspace-settings-modal.tsx).

Also I defined a enum ORG_STORAGE_RULE in packages/insomnia/src/ui/routes/organization.tsx to reuse it anywhere we need to refer to organization storage rule.

Add some tips and limits for these situations when creating mock server route from response in local project:

  1. In a local project, users are not allowed to create a cloud mock server, only enterprise users can create a self-hosted mock server.
  2. In a local project, users without enterprise plan can't create cloud mock server route from a request response.
  3. In a local project, users who are with a enterprise plan but does not have an existing self-hosted mock server need to create a self-hosted mock server manually before they create a self-hosted mock server route from a request response.
  4. In a local project, users who are with a enterprise plan and have an existing self-hosted mock server as well can create a mock server route from a request response.

@yaoweiprc yaoweiprc changed the title Mock server type should be restricted when created from response Mock server type should be restricted when created from response [INS-4645] Nov 12, 2024
@yaoweiprc yaoweiprc changed the title Mock server type should be restricted when created from response [INS-4645] Add restrictions and tips when creating mock server route from response in local project [INS-4645] Nov 12, 2024
@yaoweiprc yaoweiprc force-pushed the fix/mock-server-type-should-be-restricted-when-created-from-response branch from cb0ce1c to 2131b57 Compare November 13, 2024 07:46
@yaoweiprc yaoweiprc requested a review from a team November 13, 2024 07:49
@@ -1146,6 +1146,9 @@ async function renderApp() {

// Store the last location in local storage
router.subscribe(({ location, navigation }) => {
if (location.pathname && navigation.location?.pathname) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this log

const { currentPlan } = useRouteLoaderData('/organization') as OrganizationLoaderData;
const [orgStorageRule, setOrgStorageRule] = useState<OrgStorageRuleType>(ORG_STORAGE_RULE.CLOUD_PLUS_LOCAL);
useEffect(() => {
Copy link
Member

@CurryYangxx CurryYangxx Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to fetch storageRule outside this hook. Whatever we use loader&action or react context, we should make it consistent.


// https://stackoverflow.com/a/59496175/5714454
export type OrgStorageRuleType = `${ORG_STORAGE_RULE}`;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type seems unnecessary.

Comment on lines +33 to +59
const isLocalProject = !activeProject?.remoteId;
const { currentPlan } = useRouteLoaderData('/organization') as OrganizationLoaderData;
const isEnterprise = currentPlan?.type.includes('enterprise');

// In a local project, users are not allowed to create a cloud mock server, only enterprise users can create a self-hosted mock server.
// In a local project, users without enterprise plan can't create cloud mock server route from a request response
// In a local project, users who are with an enterprise plan but does not have an existing self-hosted mock server need to create a self-hosted mock server manually before they create a self-hosted mock server route from a request response
// In a local project, users who are with an enterprise plan and have existing self-hosted mock servers as well can create a mock server route from a request response in existing self-hosted mock servers
let tipPreventingUserFromCreatingMockRoute = '';
let canOnlyChooseExistingMockServer = false;
if (isLocalProject) {
if (!isEnterprise) {
tipPreventingUserFromCreatingMockRoute = `You can't create a cloud mock server route in a local project.
Please alter your project to a cloud project to create a cloud mock server route.
If you want to create a self-hosted mock server route, you need to upgrade to an enterprise plan.`;
} else {
mockServerAndRoutes = mockServerAndRoutes.filter(({ useInsomniaCloud }) => !useInsomniaCloud);
if (mockServerAndRoutes.length === 0) {
// does not have existing self-hosted mock server
tipPreventingUserFromCreatingMockRoute = `You can't create a cloud mock server route in a local project.
If you want to create a self-hosted mock server route from a request response in a local project, please create a self-hosted mock server in project panel manually first.`;
} else {
// has existing self-hosted mock server
canOnlyChooseExistingMockServer = true;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be better if we extract this logic into a hook or a function? What do you think about?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I have the sample logic to check enterprise plan and has implemented a hook in PR: https://github.com/Kong/insomnia/pull/8171/files#diff-07610c2b0b750115e5b2957ed391d2d8578d1dfdc87903d7f0d3e0093eb248b0

@CurryYangxx CurryYangxx force-pushed the fix/mock-server-type-should-be-restricted-when-created-from-response branch from 2131b57 to c5c0e7a Compare November 13, 2024 08:43
@yaoweiprc yaoweiprc changed the title Add restrictions and tips when creating mock server route from response in local project [INS-4645] Add restrictions when creating, editing mock server Nov 13, 2024
@yaoweiprc yaoweiprc force-pushed the fix/mock-server-type-should-be-restricted-when-created-from-response branch from c5c0e7a to cf2dec5 Compare November 13, 2024 08:52
@yaoweiprc yaoweiprc merged commit 74b9ffb into develop Nov 13, 2024
8 checks passed
@yaoweiprc yaoweiprc deleted the fix/mock-server-type-should-be-restricted-when-created-from-response branch November 13, 2024 09:06
@yaoweiprc yaoweiprc changed the title Add restrictions when creating, editing mock server Add restrictions when creating, editing mock server [INS-4667] [INS-4645] Nov 13, 2024
Copy link
Contributor

@jackkav jackkav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the file links near actions

Comment on lines +339 to +343
export enum ORG_STORAGE_RULE {
CLOUD_PLUS_LOCAL = 'cloud_plus_local',
CLOUD_ONLY = 'cloud_only',
LOCAL_ONLY = 'local_only',
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to use string unions over enums. For simplicity and to avoid side effects. The dx is identical

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants