Compare commits

...

72 Commits

Author SHA1 Message Date
Dhruwang
5cbfc6956b fix 2025-05-06 14:00:20 +05:30
Dhruwang
62f19ba4d9 fix 2025-05-06 13:53:12 +05:30
Dhruwang
70aba27e82 add go in each stage 2025-05-06 13:50:06 +05:30
Dhruwang
e94cf10c36 removed gcc 2025-05-06 12:58:13 +05:30
Dhruwang
0f324c75ab removed superchronic 2025-05-06 12:26:20 +05:30
Dhruwang
4814f8821a fix 2025-05-06 12:21:01 +05:30
Dhruwang
b44df3b6e1 fix 2025-05-06 11:53:19 +05:30
Dhruwang
a626600786 fix 2025-05-06 11:34:16 +05:30
Dhruwang
6fc1f77845 commented add step 2025-05-06 11:19:50 +05:30
Dhruwang
defc5b29e1 added release version 2025-05-06 11:03:02 +05:30
Dhruwang
e6c741bd3b fix 2025-05-06 11:02:44 +05:30
Dhruwang
3207350bd5 fix 2025-05-05 18:17:29 +05:30
Dhruwang
bbe423319e fix 2025-05-05 16:58:37 +05:30
Dhruwang
40d8d86cd6 fix 2025-05-05 16:53:02 +05:30
Dhruwang
87934d9a68 fix 2025-05-05 16:48:54 +05:30
Dhruwang
0d19569936 fix 2025-05-05 16:44:32 +05:30
Dhruwang
d67dd965ab fix 2025-05-05 16:41:10 +05:30
Dhruwang
328e2db17f fixed zstd veraion 2025-05-05 16:36:24 +05:30
Dhruwang
46e5975653 fix build 2025-05-05 16:32:53 +05:30
Dhruwang
6145f11ddf fix build 2025-05-05 16:28:34 +05:30
Dhruwang
88cff4e52f adding missing package versions and removed edge repo 2025-05-05 16:25:50 +05:30
Dhruwang
801446bb86 Merge branch 'main' of https://github.com/formbricks/formbricks into docker-package-version-update 2025-05-05 16:14:26 +05:30
Harsh Bhat
a53c13d6ed docs: add enterprise features listed under a subpaage (#5594)
Co-authored-by: Johannes <johannes@formbricks.com>
2025-05-05 08:00:47 +00:00
dependabot[bot]
1a0c6e72b2 chore(deps): bump actions/dependency-review-action from 4.5.0 to 4.6.0 (#5270)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-05 10:08:58 +02:00
dependabot[bot]
ba7c8b79b1 chore(deps): bump actions/checkout from 2.7.0 to 4.2.2 (#5273)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2025-05-05 10:08:26 +02:00
dependabot[bot]
d7b504eed0 chore(deps): bump step-security/harden-runner from 2.11.0 to 2.12.0 (#5559)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-05 10:00:21 +02:00
dependabot[bot]
a1df10eb09 chore(deps): bump sigstore/cosign-installer from 3.5.0 to 3.8.2 (#5560)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-05 09:55:05 +02:00
victorvhs017
92be409d4f chore: add tests to api V1 - part 2 (#5605) 2025-05-05 05:55:18 +00:00
victorvhs017
665c7c6bf1 chore: add tests to api V1 (#5593)
Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
2025-05-05 05:38:16 +00:00
victorvhs017
6c2ff7ee08 chore: add tests to survey editor components - part 3 (#5587)
Co-authored-by: use-tusk[bot] <144006087+use-tusk[bot]@users.noreply.github.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
2025-05-05 04:43:37 +00:00
victorvhs017
295a1bf402 chore: add tests to survey editor components - part 2 (#5575)
Co-authored-by: use-tusk[bot] <144006087+use-tusk[bot]@users.noreply.github.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
2025-05-02 14:01:02 +00:00
Piyush Gupta
3e6f558b08 fix: recaptcha feature bugs (#5599) 2025-05-02 07:11:51 +00:00
Dhruwang Jariwala
aad5a59e82 fix: removed dynamic translation key (#5527)
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
2025-05-02 06:05:23 +00:00
victorvhs017
36d02480b2 chore: add tests to survey editor components (#5557)
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
2025-05-02 05:05:24 +00:00
Piyush Gupta
99454ac57b feat: add recaptcha v3 support to surveys (#5500)
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
2025-05-01 17:22:28 +00:00
Dhruwang Jariwala
e2915f878e chore: used ";" instead of "," for multi select response (#5596) 2025-05-01 06:23:01 +00:00
Dhruwang Jariwala
710a813e9b feat: added option 6 to rating (#5595) 2025-04-30 23:24:06 -07:00
Dhruwang
bc5d048c39 fix 2025-04-30 19:29:10 +05:30
Dhruwang
f236047438 fix 2025-04-30 19:25:34 +05:30
Dhruwang
beb7ed0f3f fix redirect 2025-04-30 19:20:57 +05:30
Dhruwang
184bcd12c9 fix test 2025-04-30 19:17:15 +05:30
Dhruwang
a21911b777 sonarqube fixes 2025-04-30 19:15:24 +05:30
Dhruwang
c1df575b83 removed unrelated changes 2025-04-30 18:22:35 +05:30
Dhruwang
c6dba4454f fix 2025-04-30 18:16:39 +05:30
Dhruwang
81c7b54eae restored changes 2025-04-30 16:28:20 +05:30
Dhruwang
f0c2d75a4b fix 2025-04-30 16:07:21 +05:30
Dhruwang
44feb59cfc fix build 2025-04-30 16:03:51 +05:30
Dhruwang
3a4885c459 fix build 2025-04-30 16:00:54 +05:30
Dhruwang
6076ddd8c8 fix build 2025-04-30 15:58:12 +05:30
Dhruwang
f96530fef5 fix 2025-04-30 15:54:23 +05:30
Dhruwang
3c22bd3ccb fix build 2025-04-30 15:51:19 +05:30
Dhruwang
d05f5b26f8 added verification step 2025-04-30 15:41:47 +05:30
Dhruwang
3765e0da54 fix build 2025-04-30 14:43:47 +05:30
Dhruwang
9eea429b44 fix copy syntax 2025-04-30 14:40:42 +05:30
Dhruwang
a05a391080 fix location 2025-04-30 14:37:55 +05:30
Dhruwang
d10da85ac0 fix 2025-04-30 14:34:15 +05:30
Dhruwang
19ea25d483 fix build 2025-04-30 14:31:07 +05:30
Dhruwang
60e26a9ada fix build 2025-04-30 14:24:02 +05:30
Dhruwang
579351cdcd custom versions 2025-04-30 14:19:28 +05:30
Dhruwang
2dbc9559d5 fix build 2025-04-30 10:56:39 +05:30
Dhruwang
fdd84f84a5 fix build 2025-04-30 10:41:23 +05:30
Dhruwang
6bfc54b43c fix 2025-04-30 10:27:43 +05:30
Dhruwang
d18003507e updated openssl 2025-04-30 10:04:58 +05:30
Dhruwang
777485e63d fix alpine version 2025-04-30 09:43:06 +05:30
Dhruwang
0471a0f0c3 Merge branch 'main' of https://github.com/formbricks/formbricks into docker-package-version-update 2025-04-30 09:35:48 +05:30
Dhruwang
6290c6020d manual setup for libxml2 2025-04-30 09:35:43 +05:30
Dhruwang
304db65c66 fix 2025-04-25 11:00:57 +05:30
Dhruwang
1f979c91d3 fix 2025-04-25 10:55:57 +05:30
Dhruwang
3f532b859c added libxml2 version 2025-04-25 10:39:41 +05:30
Dhruwang
05043b1762 custom package versions 2025-04-25 10:22:56 +05:30
Dhruwang
6c724a0b1b updated base image 2025-04-24 17:18:31 +05:30
Dhruwang
f185ff85c5 updated alpine version 2025-04-24 17:09:28 +05:30
194 changed files with 15311 additions and 6548 deletions

View File

@@ -120,6 +120,10 @@ IMPRINT_ADDRESS=
# TURNSTILE_SITE_KEY=
# TURNSTILE_SECRET_KEY=
# Google reCAPTCHA v3 keys
RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=
# Configure Github Login
GITHUB_ID=
GITHUB_SECRET=

View File

@@ -2,6 +2,7 @@
When generating test files inside the "/app/web" path, follow these rules:
- You are an experienced senior software engineer
- Use vitest
- Ensure 100% code coverage
- Add as few comments as possible
@@ -13,6 +14,7 @@ When generating test files inside the "/app/web" path, follow these rules:
- Add the original file path to the "test.coverage.include"array in the "apps/web/vite.config.mts" file. Do this only when the test file is created.
- Don't mock functions that are already mocked in the "apps/web/vitestSetup.ts" file
- When using "screen.getByText" check for the tolgee string if it is being used in the file.
- When mocking data check if the properties added are part of the type of the object being mocked. Don't add properties that are not part of the type.
If it's a test for a ".tsx" file, follow these extra instructions:
@@ -22,6 +24,7 @@ afterEach(() => {
cleanup();
});
- the "afterEach" function should only have "cleanup()" inside it and should be adde to the "vitest" imports
- The "afterEach" function should only have the "cleanup()" line inside it and should be adde to the "vitest" imports.
- For click events, import userEvent from "@testing-library/user-event"
- Mock other components that can make the text more complex and but at the same time mocking it wouldn't make the test flaky. It's ok to leave basic and simple components.
- You don't need to mock @tolgee/react

View File

@@ -19,7 +19,7 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -13,11 +13,11 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: ./.github/actions/dangerous-git-checkout
- name: Build & Cache Web Binaries

View File

@@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -17,11 +17,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- name: 'Checkout Repository'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: 'Dependency Review'
uses: actions/dependency-review-action@3b139cfc5fae8b618d3eae3675e383bb1769c019 # v4.5.0
uses: actions/dependency-review-action@ce3cf9537a52e8119d91fd484ab5b8a807627bf8 # v4.6.0

View File

@@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.2.2
- name: Tailscale
uses: tailscale/github-action@v3

View File

@@ -40,7 +40,7 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4.2.2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

View File

@@ -46,11 +46,11 @@ jobs:
--health-retries=5
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: ./.github/actions/dangerous-git-checkout
- name: Setup Node.js 20.x

View File

@@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -13,7 +13,7 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -51,7 +51,7 @@ jobs:
statuses: write
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0
with:
egress-policy: audit
- name: fail if conditional jobs failed

View File

@@ -31,12 +31,12 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- name: Checkout repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Depot CLI
uses: depot/setup-action@b0b1ea4f69e92ebf5dea3f8713a1b0c37b2126a5 # v1.6.0
@@ -45,7 +45,7 @@ jobs:
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 # v3.5.0
uses: sigstore/cosign-installer@3454372f43399081ed03b604cb2d021dabca52bb # v3.8.2
# Login against a Docker registry except on PR
# https://github.com/docker/login-action

View File

@@ -38,12 +38,12 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- name: Checkout repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Get Release Tag
id: extract_release_tag
@@ -65,7 +65,7 @@ jobs:
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 # v3.5.0
uses: sigstore/cosign-installer@3454372f43399081ed03b604cb2d021dabca52bb # v3.8.2
# Login against a Docker registry except on PR
# https://github.com/docker/login-action

View File

@@ -19,7 +19,7 @@ jobs:
contents: read
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -35,12 +35,12 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- name: "Checkout code"
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

View File

@@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -26,7 +26,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -14,11 +14,11 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: ./.github/actions/dangerous-git-checkout
- name: Setup Node.js 20.x

View File

@@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -16,7 +16,7 @@ jobs:
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -18,7 +18,7 @@ jobs:
if: github.event.action == 'opened'
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@4d991eb9b905ef189e4c376166672c3f2f230481 # v2.11.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

View File

@@ -84,6 +84,12 @@ RUN apk add --no-cache curl \
&& addgroup -S nextjs \
&& adduser -S -u 1001 -G nextjs nextjs
# In the runner stage
RUN apk update && \
apk upgrade && \
# This explicitly removes old package versions
rm -rf /var/cache/apk/*
WORKDIR /home/nextjs
# Ensure no write permissions are assigned to the copied resources

View File

@@ -9,6 +9,7 @@ import { checkAuthorizationUpdated } from "@/lib/utils/action-client-middleware"
import { getOrganizationIdFromSurveyId, getProjectIdFromSurveyId } from "@/lib/utils/helper";
import { checkMultiLanguagePermission } from "@/modules/ee/multi-language-surveys/lib/actions";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { checkSpamProtectionPermission } from "@/modules/survey/lib/permission";
import { z } from "zod";
import { ZId } from "@formbricks/types/common";
import { OperationNotAllowedError, ResourceNotFoundError } from "@formbricks/types/errors";
@@ -123,6 +124,10 @@ export const updateSurveyAction = authenticatedActionClient
const { followUps } = parsedInput;
if (parsedInput.recaptcha?.enabled) {
await checkSpamProtectionPermission(organizationId);
}
if (followUps?.length) {
await checkSurveyFollowUpsPermission(organizationId);
}

View File

@@ -156,6 +156,7 @@ export const mockSurvey: TSurvey = {
isVerifyEmailEnabled: false,
isSingleResponsePerEmailEnabled: false,
isBackButtonHidden: false,
recaptcha: null,
projectOverwrites: null,
styling: null,
surveyClosedMessage: null,

View File

@@ -0,0 +1,276 @@
import { convertResponseValue } from "@/lib/responses";
import { cleanup } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey, TSurveyQuestion } from "@formbricks/types/surveys/types";
import {
TWeeklyEmailResponseData,
TWeeklySummaryEnvironmentData,
TWeeklySummarySurveyData,
} from "@formbricks/types/weekly-summary";
import { getNotificationResponse } from "./notificationResponse";
vi.mock("@/lib/responses", () => ({
convertResponseValue: vi.fn(),
}));
vi.mock("@/lib/utils/recall", () => ({
replaceHeadlineRecall: vi.fn((survey) => survey),
}));
describe("getNotificationResponse", () => {
afterEach(() => {
cleanup();
});
test("should return a notification response with calculated insights and survey data when provided with an environment containing multiple surveys", () => {
const mockSurveys = [
{
id: "survey1",
name: "Survey 1",
status: "inProgress",
questions: [
{
id: "question1",
headline: { default: "Question 1" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display1" }],
responses: [
{ id: "response1", finished: true, data: { question1: "Answer 1" } },
{ id: "response2", finished: false, data: { question1: "Answer 2" } },
],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
{
id: "survey2",
name: "Survey 2",
status: "inProgress",
questions: [
{
id: "question2",
headline: { default: "Question 2" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display2" }],
responses: [
{ id: "response3", finished: true, data: { question2: "Answer 3" } },
{ id: "response4", finished: true, data: { question2: "Answer 4" } },
{ id: "response5", finished: false, data: { question2: "Answer 5" } },
],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
] as unknown as TWeeklySummarySurveyData[];
const mockEnvironment = {
id: "env1",
surveys: mockSurveys,
} as unknown as TWeeklySummaryEnvironmentData;
const projectName = "Project Name";
const notificationResponse = getNotificationResponse(mockEnvironment, projectName);
expect(notificationResponse).toBeDefined();
expect(notificationResponse.environmentId).toBe("env1");
expect(notificationResponse.projectName).toBe(projectName);
expect(notificationResponse.surveys).toHaveLength(2);
expect(notificationResponse.insights.totalCompletedResponses).toBe(3);
expect(notificationResponse.insights.totalDisplays).toBe(2);
expect(notificationResponse.insights.totalResponses).toBe(5);
expect(notificationResponse.insights.completionRate).toBe(60);
expect(notificationResponse.insights.numLiveSurvey).toBe(2);
expect(notificationResponse.surveys[0].id).toBe("survey1");
expect(notificationResponse.surveys[0].name).toBe("Survey 1");
expect(notificationResponse.surveys[0].status).toBe("inProgress");
expect(notificationResponse.surveys[0].responseCount).toBe(2);
expect(notificationResponse.surveys[1].id).toBe("survey2");
expect(notificationResponse.surveys[1].name).toBe("Survey 2");
expect(notificationResponse.surveys[1].status).toBe("inProgress");
expect(notificationResponse.surveys[1].responseCount).toBe(3);
});
test("should calculate the correct completion rate and other insights when surveys have responses with varying statuses", () => {
const mockSurveys = [
{
id: "survey1",
name: "Survey 1",
status: "inProgress",
questions: [
{
id: "question1",
headline: { default: "Question 1" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display1" }],
responses: [
{ id: "response1", finished: true, data: { question1: "Answer 1" } },
{ id: "response2", finished: false, data: { question1: "Answer 2" } },
],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
{
id: "survey2",
name: "Survey 2",
status: "inProgress",
questions: [
{
id: "question2",
headline: { default: "Question 2" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display2" }],
responses: [
{ id: "response3", finished: true, data: { question2: "Answer 3" } },
{ id: "response4", finished: true, data: { question2: "Answer 4" } },
{ id: "response5", finished: false, data: { question2: "Answer 5" } },
],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
{
id: "survey3",
name: "Survey 3",
status: "inProgress",
questions: [
{
id: "question3",
headline: { default: "Question 3" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display3" }],
responses: [{ id: "response6", finished: false, data: { question3: "Answer 6" } }],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
] as unknown as TWeeklySummarySurveyData[];
const mockEnvironment = {
id: "env1",
surveys: mockSurveys,
} as unknown as TWeeklySummaryEnvironmentData;
const projectName = "Project Name";
const notificationResponse = getNotificationResponse(mockEnvironment, projectName);
expect(notificationResponse).toBeDefined();
expect(notificationResponse.environmentId).toBe("env1");
expect(notificationResponse.projectName).toBe(projectName);
expect(notificationResponse.surveys).toHaveLength(3);
expect(notificationResponse.insights.totalCompletedResponses).toBe(3);
expect(notificationResponse.insights.totalDisplays).toBe(3);
expect(notificationResponse.insights.totalResponses).toBe(6);
expect(notificationResponse.insights.completionRate).toBe(50);
expect(notificationResponse.insights.numLiveSurvey).toBe(3);
expect(notificationResponse.surveys[0].id).toBe("survey1");
expect(notificationResponse.surveys[0].name).toBe("Survey 1");
expect(notificationResponse.surveys[0].status).toBe("inProgress");
expect(notificationResponse.surveys[0].responseCount).toBe(2);
expect(notificationResponse.surveys[1].id).toBe("survey2");
expect(notificationResponse.surveys[1].name).toBe("Survey 2");
expect(notificationResponse.surveys[1].status).toBe("inProgress");
expect(notificationResponse.surveys[1].responseCount).toBe(3);
expect(notificationResponse.surveys[2].id).toBe("survey3");
expect(notificationResponse.surveys[2].name).toBe("Survey 3");
expect(notificationResponse.surveys[2].status).toBe("inProgress");
expect(notificationResponse.surveys[2].responseCount).toBe(1);
});
test("should return default insights and an empty surveys array when the environment contains no surveys", () => {
const mockEnvironment = {
id: "env1",
surveys: [],
} as unknown as TWeeklySummaryEnvironmentData;
const projectName = "Project Name";
const notificationResponse = getNotificationResponse(mockEnvironment, projectName);
expect(notificationResponse).toBeDefined();
expect(notificationResponse.environmentId).toBe("env1");
expect(notificationResponse.projectName).toBe(projectName);
expect(notificationResponse.surveys).toHaveLength(0);
expect(notificationResponse.insights.totalCompletedResponses).toBe(0);
expect(notificationResponse.insights.totalDisplays).toBe(0);
expect(notificationResponse.insights.totalResponses).toBe(0);
expect(notificationResponse.insights.completionRate).toBe(0);
expect(notificationResponse.insights.numLiveSurvey).toBe(0);
});
test("should handle missing response data gracefully when a response doesn't contain data for a question ID", () => {
const mockSurveys = [
{
id: "survey1",
name: "Survey 1",
status: "inProgress",
questions: [
{
id: "question1",
headline: { default: "Question 1" },
type: "text",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display1" }],
responses: [
{ id: "response1", finished: true, data: {} }, // Response missing data for question1
],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
] as unknown as TWeeklySummarySurveyData[];
const mockEnvironment = {
id: "env1",
surveys: mockSurveys,
} as unknown as TWeeklySummaryEnvironmentData;
const projectName = "Project Name";
// Mock the convertResponseValue function to handle the missing data case
vi.mocked(convertResponseValue).mockReturnValue("");
const notificationResponse = getNotificationResponse(mockEnvironment, projectName);
expect(notificationResponse).toBeDefined();
expect(notificationResponse.surveys).toHaveLength(1);
expect(notificationResponse.surveys[0].responses).toHaveLength(1);
expect(notificationResponse.surveys[0].responses[0].responseValue).toBe("");
});
test("should handle unsupported question types gracefully", () => {
const mockSurveys = [
{
id: "survey1",
name: "Survey 1",
status: "inProgress",
questions: [
{
id: "question1",
headline: { default: "Question 1" },
type: "unsupported",
} as unknown as TSurveyQuestion,
],
displays: [{ id: "display1" }],
responses: [{ id: "response1", finished: true, data: { question1: "Answer 1" } }],
} as unknown as TSurvey & { responses: TWeeklyEmailResponseData[] },
] as unknown as TWeeklySummarySurveyData[];
const mockEnvironment = {
id: "env1",
surveys: mockSurveys,
} as unknown as TWeeklySummaryEnvironmentData;
const projectName = "Project Name";
vi.mocked(convertResponseValue).mockReturnValue("Unsupported Response");
const notificationResponse = getNotificationResponse(mockEnvironment, projectName);
expect(notificationResponse).toBeDefined();
expect(notificationResponse.surveys[0].responses[0].responseValue).toBe("Unsupported Response");
});
});

View File

@@ -0,0 +1,48 @@
import { cleanup } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getOrganizationIds } from "./organization";
vi.mock("@formbricks/database", () => ({
prisma: {
organization: {
findMany: vi.fn(),
},
},
}));
describe("Organization", () => {
afterEach(() => {
cleanup();
});
test("getOrganizationIds should return an array of organization IDs when the database contains multiple organizations", async () => {
const mockOrganizations = [{ id: "org1" }, { id: "org2" }, { id: "org3" }];
vi.mocked(prisma.organization.findMany).mockResolvedValue(mockOrganizations);
const organizationIds = await getOrganizationIds();
expect(organizationIds).toEqual(["org1", "org2", "org3"]);
expect(prisma.organization.findMany).toHaveBeenCalledTimes(1);
expect(prisma.organization.findMany).toHaveBeenCalledWith({
select: {
id: true,
},
});
});
test("getOrganizationIds should return an empty array when the database contains no organizations", async () => {
vi.mocked(prisma.organization.findMany).mockResolvedValue([]);
const organizationIds = await getOrganizationIds();
expect(organizationIds).toEqual([]);
expect(prisma.organization.findMany).toHaveBeenCalledTimes(1);
expect(prisma.organization.findMany).toHaveBeenCalledWith({
select: {
id: true,
},
});
});
});

View File

@@ -0,0 +1,570 @@
import { cleanup } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getProjectsByOrganizationId } from "./project";
const mockProjects = [
{
id: "project1",
name: "Project 1",
environments: [
{
id: "env1",
type: "production",
surveys: [],
attributeKeys: [],
},
],
organization: {
memberships: [
{
user: {
id: "user1",
email: "test@example.com",
notificationSettings: {
weeklySummary: {
project1: true,
},
},
locale: "en",
},
},
],
},
},
];
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 6); // Set to 6 days ago to be within the last 7 days
const mockProjectsWithNoEnvironments = [
{
id: "project3",
name: "Project 3",
environments: [],
organization: {
memberships: [
{
user: {
id: "user1",
email: "test@example.com",
notificationSettings: {
weeklySummary: {
project3: true,
},
},
locale: "en",
},
},
],
},
},
];
vi.mock("@formbricks/database", () => ({
prisma: {
project: {
findMany: vi.fn(),
},
},
}));
describe("Project Management", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
cleanup();
});
describe("getProjectsByOrganizationId", () => {
test("retrieves projects with environments, surveys, and organization memberships for a valid organization ID", async () => {
vi.mocked(prisma.project.findMany).mockResolvedValueOnce(mockProjects);
const organizationId = "testOrgId";
const projects = await getProjectsByOrganizationId(organizationId);
expect(projects).toEqual(mockProjects);
expect(prisma.project.findMany).toHaveBeenCalledWith({
where: {
organizationId: organizationId,
},
select: {
id: true,
name: true,
environments: {
where: {
type: "production",
},
select: {
id: true,
surveys: {
where: {
NOT: {
AND: [
{ status: "completed" },
{
responses: {
none: {
createdAt: {
gte: expect.any(Date),
},
},
},
},
],
},
status: {
not: "draft",
},
},
select: {
id: true,
name: true,
questions: true,
status: true,
responses: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
createdAt: true,
updatedAt: true,
finished: true,
data: true,
},
orderBy: {
createdAt: "desc",
},
},
displays: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
},
},
hiddenFields: true,
},
},
attributeKeys: {
select: {
id: true,
createdAt: true,
updatedAt: true,
name: true,
description: true,
type: true,
environmentId: true,
key: true,
isUnique: true,
},
},
},
},
organization: {
select: {
memberships: {
select: {
user: {
select: {
id: true,
email: true,
notificationSettings: true,
locale: true,
},
},
},
},
},
},
},
});
});
test("handles date calculations correctly across DST boundaries", async () => {
const mockDate = new Date(2024, 10, 3, 0, 0, 0); // November 3, 2024, 00:00:00 (example DST boundary)
const sevenDaysAgo = new Date(mockDate);
sevenDaysAgo.setDate(mockDate.getDate() - 7);
vi.useFakeTimers();
vi.setSystemTime(mockDate);
vi.mocked(prisma.project.findMany).mockResolvedValueOnce(mockProjects);
const organizationId = "testOrgId";
await getProjectsByOrganizationId(organizationId);
expect(prisma.project.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: {
organizationId: organizationId,
},
select: expect.objectContaining({
environments: expect.objectContaining({
select: expect.objectContaining({
surveys: expect.objectContaining({
where: expect.objectContaining({
NOT: expect.objectContaining({
AND: expect.arrayContaining([
expect.objectContaining({ status: "completed" }),
expect.objectContaining({
responses: expect.objectContaining({
none: expect.objectContaining({
createdAt: expect.objectContaining({
gte: sevenDaysAgo,
}),
}),
}),
}),
]),
}),
}),
}),
}),
}),
}),
})
);
vi.useRealTimers();
});
test("includes surveys with 'completed' status but responses within the last 7 days", async () => {
vi.mocked(prisma.project.findMany).mockResolvedValueOnce(mockProjects);
const organizationId = "testOrgId";
const projects = await getProjectsByOrganizationId(organizationId);
expect(projects).toEqual(mockProjects);
expect(prisma.project.findMany).toHaveBeenCalledWith({
where: {
organizationId: organizationId,
},
select: {
id: true,
name: true,
environments: {
where: {
type: "production",
},
select: {
id: true,
surveys: {
where: {
NOT: {
AND: [
{ status: "completed" },
{
responses: {
none: {
createdAt: {
gte: expect.any(Date),
},
},
},
},
],
},
status: {
not: "draft",
},
},
select: {
id: true,
name: true,
questions: true,
status: true,
responses: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
createdAt: true,
updatedAt: true,
finished: true,
data: true,
},
orderBy: {
createdAt: "desc",
},
},
displays: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
},
},
hiddenFields: true,
},
},
attributeKeys: {
select: {
id: true,
createdAt: true,
updatedAt: true,
name: true,
description: true,
type: true,
environmentId: true,
key: true,
isUnique: true,
},
},
},
},
organization: {
select: {
memberships: {
select: {
user: {
select: {
id: true,
email: true,
notificationSettings: true,
locale: true,
},
},
},
},
},
},
},
});
});
test("returns an empty array when an invalid organization ID is provided", async () => {
vi.mocked(prisma.project.findMany).mockResolvedValueOnce([]);
const invalidOrganizationId = "invalidOrgId";
const projects = await getProjectsByOrganizationId(invalidOrganizationId);
expect(projects).toEqual([]);
expect(prisma.project.findMany).toHaveBeenCalledWith({
where: {
organizationId: invalidOrganizationId,
},
select: {
id: true,
name: true,
environments: {
where: {
type: "production",
},
select: {
id: true,
surveys: {
where: {
NOT: {
AND: [
{ status: "completed" },
{
responses: {
none: {
createdAt: {
gte: expect.any(Date),
},
},
},
},
],
},
status: {
not: "draft",
},
},
select: {
id: true,
name: true,
questions: true,
status: true,
responses: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
createdAt: true,
updatedAt: true,
finished: true,
data: true,
},
orderBy: {
createdAt: "desc",
},
},
displays: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
},
},
hiddenFields: true,
},
},
attributeKeys: {
select: {
id: true,
createdAt: true,
updatedAt: true,
name: true,
description: true,
type: true,
environmentId: true,
key: true,
isUnique: true,
},
},
},
},
organization: {
select: {
memberships: {
select: {
user: {
select: {
id: true,
email: true,
notificationSettings: true,
locale: true,
},
},
},
},
},
},
},
});
});
test("handles projects with no environments", async () => {
vi.mocked(prisma.project.findMany).mockResolvedValueOnce(mockProjectsWithNoEnvironments);
const organizationId = "testOrgId";
const projects = await getProjectsByOrganizationId(organizationId);
expect(projects).toEqual(mockProjectsWithNoEnvironments);
expect(prisma.project.findMany).toHaveBeenCalledWith({
where: {
organizationId: organizationId,
},
select: {
id: true,
name: true,
environments: {
where: {
type: "production",
},
select: {
id: true,
surveys: {
where: {
NOT: {
AND: [
{ status: "completed" },
{
responses: {
none: {
createdAt: {
gte: expect.any(Date),
},
},
},
},
],
},
status: {
not: "draft",
},
},
select: {
id: true,
name: true,
questions: true,
status: true,
responses: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
createdAt: true,
updatedAt: true,
finished: true,
data: true,
},
orderBy: {
createdAt: "desc",
},
},
displays: {
where: {
createdAt: {
gte: expect.any(Date),
},
},
select: {
id: true,
},
},
hiddenFields: true,
},
},
attributeKeys: {
select: {
id: true,
createdAt: true,
updatedAt: true,
name: true,
description: true,
type: true,
environmentId: true,
key: true,
isUnique: true,
},
},
},
},
organization: {
select: {
memberships: {
select: {
user: {
select: {
id: true,
email: true,
notificationSettings: true,
locale: true,
},
},
},
},
},
},
},
});
});
});
});

View File

@@ -0,0 +1,99 @@
import { cache } from "@/lib/cache";
import { TContact } from "@/modules/ee/contacts/types/contact";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getContactByUserId } from "./contact";
// Mock prisma
vi.mock("@formbricks/database", () => ({
prisma: {
contact: {
findFirst: vi.fn(),
},
},
}));
// Mock cache\
vi.mock("@/lib/cache", async () => {
const actual = await vi.importActual("@/lib/cache");
return {
...(actual as any),
cache: vi.fn((fn) => fn()), // Mock cache function to just execute the passed function
};
});
const environmentId = "test-environment-id";
const userId = "test-user-id";
const contactId = "test-contact-id";
const contactMock: Partial<TContact> & {
attributes: { value: string; attributeKey: { key: string } }[];
} = {
id: contactId,
attributes: [
{ attributeKey: { key: "userId" }, value: userId },
{ attributeKey: { key: "email" }, value: "test@example.com" },
],
};
describe("getContactByUserId", () => {
beforeEach(() => {
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
});
afterEach(() => {
vi.resetAllMocks();
});
test("should return contact if found", async () => {
vi.mocked(prisma.contact.findFirst).mockResolvedValue(contactMock as any);
const contact = await getContactByUserId(environmentId, userId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId,
},
value: userId,
},
},
},
select: {
id: true,
attributes: { select: { attributeKey: { select: { key: true } }, value: true } },
},
});
expect(contact).toEqual(contactMock);
});
test("should return null if contact not found", async () => {
vi.mocked(prisma.contact.findFirst).mockResolvedValue(null);
const contact = await getContactByUserId(environmentId, userId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId,
},
value: userId,
},
},
},
select: {
id: true,
attributes: { select: { attributeKey: { select: { key: true } }, value: true } },
},
});
expect(contact).toBeNull();
});
});

View File

@@ -0,0 +1,309 @@
import { cache } from "@/lib/cache";
import { getProjectByEnvironmentId } from "@/lib/project/service";
import { getSurveys } from "@/lib/survey/service";
import { anySurveyHasFilters } from "@/lib/survey/utils";
import { diffInDays } from "@/lib/utils/datetime";
import { evaluateSegment } from "@/modules/ee/contacts/segments/lib/segments";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { TProject } from "@formbricks/types/project";
import { TSegment } from "@formbricks/types/segment";
import { TSurvey } from "@formbricks/types/surveys/types";
import { getSyncSurveys } from "./survey";
// Mock dependencies
vi.mock("@/lib/cache", async () => {
const actual = await vi.importActual("@/lib/cache");
return {
...(actual as any),
cache: vi.fn((fn) => fn()), // Mock cache function to just execute the passed function
};
});
vi.mock("@/lib/project/service", () => ({
getProjectByEnvironmentId: vi.fn(),
}));
vi.mock("@/lib/survey/service", () => ({
getSurveys: vi.fn(),
}));
vi.mock("@/lib/survey/utils", () => ({
anySurveyHasFilters: vi.fn(),
}));
vi.mock("@/lib/utils/datetime", () => ({
diffInDays: vi.fn(),
}));
vi.mock("@/lib/utils/validate", () => ({
validateInputs: vi.fn(),
}));
vi.mock("@/modules/ee/contacts/segments/lib/segments", () => ({
evaluateSegment: vi.fn(),
}));
vi.mock("@formbricks/database", () => ({
prisma: {
display: {
findMany: vi.fn(),
},
response: {
findMany: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
const environmentId = "test-env-id";
const contactId = "test-contact-id";
const contactAttributes = { userId: "user1", email: "test@example.com" };
const deviceType = "desktop";
const mockProject = {
id: "proj1",
name: "Test Project",
createdAt: new Date(),
updatedAt: new Date(),
organizationId: "org1",
environments: [],
recontactDays: 10,
inAppSurveyBranding: true,
linkSurveyBranding: true,
placement: "bottomRight",
clickOutsideClose: true,
darkOverlay: false,
languages: [],
} as unknown as TProject;
const baseSurvey: TSurvey = {
id: "survey1",
createdAt: new Date(),
updatedAt: new Date(),
name: "Test Survey 1",
environmentId: environmentId,
type: "app",
status: "inProgress",
questions: [],
displayOption: "displayOnce",
recontactDays: null,
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
segment: null,
surveyClosedMessage: null,
singleUse: null,
styling: null,
pin: null,
resultShareKey: null,
displayLimit: null,
welcomeCard: { enabled: false } as TSurvey["welcomeCard"],
endings: [],
triggers: [],
languages: [],
variables: [],
hiddenFields: { enabled: false },
createdBy: null,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
isBackButtonHidden: false,
followUps: [],
recaptcha: { enabled: false, threshold: 0.5 },
};
describe("getSyncSurveys", () => {
beforeEach(() => {
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
vi.mocked(getProjectByEnvironmentId).mockResolvedValue(mockProject);
vi.mocked(prisma.display.findMany).mockResolvedValue([]);
vi.mocked(prisma.response.findMany).mockResolvedValue([]);
vi.mocked(anySurveyHasFilters).mockReturnValue(false);
vi.mocked(evaluateSegment).mockResolvedValue(true);
vi.mocked(diffInDays).mockReturnValue(100); // Assume enough days passed
});
afterEach(() => {
vi.resetAllMocks();
});
test("should throw error if product not found", async () => {
vi.mocked(getProjectByEnvironmentId).mockResolvedValue(null);
await expect(getSyncSurveys(environmentId, contactId, contactAttributes, deviceType)).rejects.toThrow(
"Product not found"
);
});
test("should return empty array if no surveys found", async () => {
vi.mocked(getSurveys).mockResolvedValue([]);
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
});
test("should return empty array if no 'app' type surveys in progress", async () => {
const surveys: TSurvey[] = [
{ ...baseSurvey, id: "s1", type: "link", status: "inProgress" },
{ ...baseSurvey, id: "s2", type: "app", status: "paused" },
];
vi.mocked(getSurveys).mockResolvedValue(surveys);
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
});
test("should filter by displayOption 'displayOnce'", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", displayOption: "displayOnce" }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(prisma.display.findMany).mockResolvedValue([{ id: "d1", surveyId: "s1", contactId }]); // Already displayed
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
vi.mocked(prisma.display.findMany).mockResolvedValue([]); // Not displayed yet
const result2 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result2).toEqual(surveys);
});
test("should filter by displayOption 'displayMultiple'", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", displayOption: "displayMultiple" }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(prisma.response.findMany).mockResolvedValue([{ id: "r1", surveyId: "s1", contactId }]); // Already responded
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
vi.mocked(prisma.response.findMany).mockResolvedValue([]); // Not responded yet
const result2 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result2).toEqual(surveys);
});
test("should filter by displayOption 'displaySome'", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", displayOption: "displaySome", displayLimit: 2 }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(prisma.display.findMany).mockResolvedValue([
{ id: "d1", surveyId: "s1", contactId },
{ id: "d2", surveyId: "s1", contactId },
]); // Display limit reached
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
vi.mocked(prisma.display.findMany).mockResolvedValue([{ id: "d1", surveyId: "s1", contactId }]); // Within limit
const result2 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result2).toEqual(surveys);
// Test with response already submitted
vi.mocked(prisma.response.findMany).mockResolvedValue([{ id: "r1", surveyId: "s1", contactId }]);
const result3 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result3).toEqual([]);
});
test("should not filter by displayOption 'respondMultiple'", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", displayOption: "respondMultiple" }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(prisma.display.findMany).mockResolvedValue([{ id: "d1", surveyId: "s1", contactId }]);
vi.mocked(prisma.response.findMany).mockResolvedValue([{ id: "r1", surveyId: "s1", contactId }]);
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual(surveys);
});
test("should filter by product recontactDays if survey recontactDays is null", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", recontactDays: null }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
const displayDate = new Date();
vi.mocked(prisma.display.findMany).mockResolvedValue([
{ id: "d1", surveyId: "s2", contactId, createdAt: displayDate }, // Display for another survey
]);
vi.mocked(diffInDays).mockReturnValue(5); // Not enough days passed (product.recontactDays = 10)
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]);
expect(diffInDays).toHaveBeenCalledWith(expect.any(Date), displayDate);
vi.mocked(diffInDays).mockReturnValue(15); // Enough days passed
const result2 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result2).toEqual(surveys);
});
test("should return surveys if no segment filters exist", async () => {
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1" }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(anySurveyHasFilters).mockReturnValue(false);
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual(surveys);
expect(evaluateSegment).not.toHaveBeenCalled();
});
test("should evaluate segment filters if they exist", async () => {
const segment = { id: "seg1", filters: [{}] } as TSegment; // Mock filter structure
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", segment }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(anySurveyHasFilters).mockReturnValue(true);
// Case 1: Segment evaluation matches
vi.mocked(evaluateSegment).mockResolvedValue(true);
const result1 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result1).toEqual(surveys);
expect(evaluateSegment).toHaveBeenCalledWith(
{
attributes: contactAttributes,
deviceType,
environmentId,
contactId,
userId: contactAttributes.userId,
},
segment.filters
);
// Case 2: Segment evaluation does not match
vi.mocked(evaluateSegment).mockResolvedValue(false);
const result2 = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result2).toEqual([]);
});
test("should handle Prisma errors", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Test Prisma Error", {
code: "P2025",
clientVersion: "test",
});
vi.mocked(getSurveys).mockRejectedValue(prismaError);
await expect(getSyncSurveys(environmentId, contactId, contactAttributes, deviceType)).rejects.toThrow(
DatabaseError
);
expect(logger.error).toHaveBeenCalledWith(prismaError);
});
test("should handle general errors", async () => {
const generalError = new Error("Something went wrong");
vi.mocked(getSurveys).mockRejectedValue(generalError);
await expect(getSyncSurveys(environmentId, contactId, contactAttributes, deviceType)).rejects.toThrow(
generalError
);
});
test("should throw ResourceNotFoundError if resolved surveys are null after filtering", async () => {
const segment = { id: "seg1", filters: [{}] } as TSegment; // Mock filter structure
const surveys: TSurvey[] = [{ ...baseSurvey, id: "s1", segment }];
vi.mocked(getSurveys).mockResolvedValue(surveys);
vi.mocked(anySurveyHasFilters).mockReturnValue(true);
vi.mocked(evaluateSegment).mockResolvedValue(false); // Ensure all surveys are filtered out
// This scenario is tricky to force directly as the code checks `if (!surveys)` before returning.
// However, if `Promise.all` somehow resolved to null/undefined (highly unlikely), it should throw.
// We can simulate this by mocking `Promise.all` if needed, but the current code structure makes this hard to test.
// Let's assume the filter logic works correctly and test the intended path.
const result = await getSyncSurveys(environmentId, contactId, contactAttributes, deviceType);
expect(result).toEqual([]); // Expect empty array, not an error in this case.
});
});

View File

@@ -0,0 +1,247 @@
import { parseRecallInfo } from "@/lib/utils/recall";
import { describe, expect, test, vi } from "vitest";
import { TAttributes } from "@formbricks/types/attributes";
import { TLanguage } from "@formbricks/types/project";
import {
TSurvey,
TSurveyEnding,
TSurveyQuestion,
TSurveyQuestionTypeEnum,
} from "@formbricks/types/surveys/types";
import { replaceAttributeRecall } from "./utils";
vi.mock("@/lib/utils/recall", () => ({
parseRecallInfo: vi.fn((text, attributes) => {
const recallPattern = /recall:([a-zA-Z0-9_-]+)/;
const match = text.match(recallPattern);
if (match && match[1]) {
const recallKey = match[1];
const attributeValue = attributes[recallKey];
if (attributeValue !== undefined) {
return text.replace(recallPattern, `parsed-${attributeValue}`);
}
}
return text; // Return original text if no match or attribute not found
}),
}));
const baseSurvey: TSurvey = {
id: "survey1",
createdAt: new Date(),
updatedAt: new Date(),
name: "Test Survey",
environmentId: "env1",
type: "app",
status: "inProgress",
questions: [],
endings: [],
welcomeCard: { enabled: false } as TSurvey["welcomeCard"],
languages: [
{ language: { id: "lang1", code: "en" } as unknown as TLanguage, default: true, enabled: true },
],
triggers: [],
recontactDays: null,
displayLimit: null,
singleUse: null,
styling: null,
surveyClosedMessage: null,
hiddenFields: { enabled: false },
variables: [],
createdBy: null,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
isBackButtonHidden: false,
followUps: [],
recaptcha: { enabled: false, threshold: 0.5 },
displayOption: "displayOnce",
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
segment: null,
pin: null,
resultShareKey: null,
};
const attributes: TAttributes = {
name: "John Doe",
email: "john.doe@example.com",
plan: "premium",
};
describe("replaceAttributeRecall", () => {
test("should replace recall info in question headlines and subheaders", () => {
const surveyWithRecall: TSurvey = {
...baseSurvey,
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Hello recall:name!" },
subheader: { default: "Your email is recall:email" },
required: true,
buttonLabel: { default: "Next" },
placeholder: { default: "Type here..." },
longAnswer: false,
logic: [],
} as unknown as TSurveyQuestion,
],
};
const result = replaceAttributeRecall(surveyWithRecall, attributes);
expect(result.questions[0].headline.default).toBe("Hello parsed-John Doe!");
expect(result.questions[0].subheader?.default).toBe("Your email is parsed-john.doe@example.com");
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Hello recall:name!", attributes);
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Your email is recall:email", attributes);
});
test("should replace recall info in welcome card headline", () => {
const surveyWithRecall: TSurvey = {
...baseSurvey,
welcomeCard: {
enabled: true,
headline: { default: "Welcome, recall:name!" },
html: { default: "<p>Some content</p>" },
buttonLabel: { default: "Start" },
timeToFinish: false,
showResponseCount: false,
},
};
const result = replaceAttributeRecall(surveyWithRecall, attributes);
expect(result.welcomeCard.headline?.default).toBe("Welcome, parsed-John Doe!");
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Welcome, recall:name!", attributes);
});
test("should replace recall info in end screen headlines and subheaders", () => {
const surveyWithRecall: TSurvey = {
...baseSurvey,
endings: [
{
type: "endScreen",
headline: { default: "Thank you, recall:name!" },
subheader: { default: "Your plan: recall:plan" },
buttonLabel: { default: "Finish" },
buttonLink: "https://example.com",
} as unknown as TSurveyEnding,
],
};
const result = replaceAttributeRecall(surveyWithRecall, attributes);
expect(result.endings[0].type).toBe("endScreen");
if (result.endings[0].type === "endScreen") {
expect(result.endings[0].headline?.default).toBe("Thank you, parsed-John Doe!");
expect(result.endings[0].subheader?.default).toBe("Your plan: parsed-premium");
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Thank you, recall:name!", attributes);
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Your plan: recall:plan", attributes);
}
});
test("should handle multiple languages", () => {
const surveyMultiLang: TSurvey = {
...baseSurvey,
languages: [
{ language: { id: "lang1", code: "en" } as unknown as TLanguage, default: true, enabled: true },
{ language: { id: "lang2", code: "es" } as unknown as TLanguage, default: false, enabled: true },
],
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Hello recall:name!", es: "Hola recall:name!" },
required: true,
buttonLabel: { default: "Next", es: "Siguiente" },
placeholder: { default: "Type here...", es: "Escribe aquí..." },
longAnswer: false,
logic: [],
} as unknown as TSurveyQuestion,
],
};
const result = replaceAttributeRecall(surveyMultiLang, attributes);
expect(result.questions[0].headline.default).toBe("Hello parsed-John Doe!");
expect(result.questions[0].headline.es).toBe("Hola parsed-John Doe!");
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Hello recall:name!", attributes);
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Hola recall:name!", attributes);
});
test("should not replace if recall key is not in attributes", () => {
const surveyWithRecall: TSurvey = {
...baseSurvey,
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Your company: recall:company" },
required: true,
buttonLabel: { default: "Next" },
placeholder: { default: "Type here..." },
longAnswer: false,
logic: [],
} as unknown as TSurveyQuestion,
],
};
const result = replaceAttributeRecall(surveyWithRecall, attributes);
expect(result.questions[0].headline.default).toBe("Your company: recall:company");
expect(vi.mocked(parseRecallInfo)).toHaveBeenCalledWith("Your company: recall:company", attributes);
});
test("should handle surveys with no recall information", async () => {
const surveyNoRecall: TSurvey = {
...baseSurvey,
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Just a regular question" },
required: true,
buttonLabel: { default: "Next" },
placeholder: { default: "Type here..." },
longAnswer: false,
logic: [],
} as unknown as TSurveyQuestion,
],
welcomeCard: {
enabled: true,
headline: { default: "Welcome!" },
html: { default: "<p>Some content</p>" },
buttonLabel: { default: "Start" },
timeToFinish: false,
showResponseCount: false,
},
endings: [
{
type: "endScreen",
headline: { default: "Thank you!" },
buttonLabel: { default: "Finish" },
} as unknown as TSurveyEnding,
],
};
const parseRecallInfoSpy = vi.spyOn(await import("@/lib/utils/recall"), "parseRecallInfo");
const result = replaceAttributeRecall(surveyNoRecall, attributes);
expect(result).toEqual(surveyNoRecall); // Should be unchanged
expect(parseRecallInfoSpy).not.toHaveBeenCalled();
parseRecallInfoSpy.mockRestore();
});
test("should handle surveys with empty questions, endings, or disabled welcome card", async () => {
const surveyEmpty: TSurvey = {
...baseSurvey,
questions: [],
endings: [],
welcomeCard: { enabled: false } as TSurvey["welcomeCard"],
};
const parseRecallInfoSpy = vi.spyOn(await import("@/lib/utils/recall"), "parseRecallInfo");
const result = replaceAttributeRecall(surveyEmpty, attributes);
expect(result).toEqual(surveyEmpty);
expect(parseRecallInfoSpy).not.toHaveBeenCalled();
parseRecallInfoSpy.mockRestore();
});
});

View File

@@ -0,0 +1,86 @@
import { cache } from "@/lib/cache";
import { validateInputs } from "@/lib/utils/validate";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { TActionClassNoCodeConfig } from "@formbricks/types/action-classes";
import { DatabaseError } from "@formbricks/types/errors";
import { TJsEnvironmentStateActionClass } from "@formbricks/types/js";
import { getActionClassesForEnvironmentState } from "./actionClass";
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/utils/validate");
vi.mock("@formbricks/database", () => ({
prisma: {
actionClass: {
findMany: vi.fn(),
},
},
}));
const environmentId = "test-environment-id";
const mockActionClasses: TJsEnvironmentStateActionClass[] = [
{
id: "action1",
type: "code",
name: "Code Action",
key: "code-action",
noCodeConfig: null,
},
{
id: "action2",
type: "noCode",
name: "No Code Action",
key: null,
noCodeConfig: { type: "click" } as TActionClassNoCodeConfig,
},
];
describe("getActionClassesForEnvironmentState", () => {
test("should return action classes successfully", async () => {
vi.mocked(prisma.actionClass.findMany).mockResolvedValue(mockActionClasses);
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
const result = await getActionClassesForEnvironmentState(environmentId);
expect(result).toEqual(mockActionClasses);
expect(validateInputs).toHaveBeenCalledWith([environmentId, expect.any(Object)]); // ZId is an object
expect(prisma.actionClass.findMany).toHaveBeenCalledWith({
where: { environmentId },
select: {
id: true,
type: true,
name: true,
key: true,
noCodeConfig: true,
},
});
expect(cache).toHaveBeenCalledWith(
expect.any(Function),
[`getActionClassesForEnvironmentState-${environmentId}`],
{ tags: [`environments-${environmentId}-actionClasses`] }
);
});
test("should throw DatabaseError on prisma error", async () => {
const mockError = new Error("Prisma error");
vi.mocked(prisma.actionClass.findMany).mockRejectedValue(mockError);
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
await expect(getActionClassesForEnvironmentState(environmentId)).rejects.toThrow(DatabaseError);
await expect(getActionClassesForEnvironmentState(environmentId)).rejects.toThrow(
`Database error when fetching actions for environment ${environmentId}`
);
expect(validateInputs).toHaveBeenCalledWith([environmentId, expect.any(Object)]);
expect(prisma.actionClass.findMany).toHaveBeenCalled();
expect(cache).toHaveBeenCalledWith(
expect.any(Function),
[`getActionClassesForEnvironmentState-${environmentId}`],
{ tags: [`environments-${environmentId}-actionClasses`] }
);
});
});

View File

@@ -0,0 +1,372 @@
import { cache } from "@/lib/cache";
import { getEnvironment } from "@/lib/environment/service";
import {
getMonthlyOrganizationResponseCount,
getOrganizationByEnvironmentId,
} from "@/lib/organization/service";
import {
capturePosthogEnvironmentEvent,
sendPlanLimitsReachedEventToPosthogWeekly,
} from "@/lib/posthogServer";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { TActionClass } from "@formbricks/types/action-classes";
import { TEnvironment } from "@formbricks/types/environment";
import { ResourceNotFoundError } from "@formbricks/types/errors";
import { TJsEnvironmentState } from "@formbricks/types/js";
import { TOrganization } from "@formbricks/types/organizations";
import { TProject } from "@formbricks/types/project";
import { TSurvey } from "@formbricks/types/surveys/types";
import { getActionClassesForEnvironmentState } from "./actionClass";
import { getEnvironmentState } from "./environmentState";
import { getProjectForEnvironmentState } from "./project";
import { getSurveysForEnvironmentState } from "./survey";
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/environment/service");
vi.mock("@/lib/organization/service");
vi.mock("@/lib/posthogServer");
vi.mock("@/modules/ee/license-check/lib/utils");
vi.mock("@formbricks/database", () => ({
prisma: {
environment: {
update: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
vi.mock("./actionClass");
vi.mock("./project");
vi.mock("./survey");
vi.mock("@/lib/constants", () => ({
IS_FORMBRICKS_CLOUD: true, // Default to false, override in specific tests
RECAPTCHA_SITE_KEY: "mock_recaptcha_site_key",
RECAPTCHA_SECRET_KEY: "mock_recaptcha_secret_key",
IS_RECAPTCHA_CONFIGURED: true,
IS_PRODUCTION: true,
IS_POSTHOG_CONFIGURED: false,
ENTERPRISE_LICENSE_KEY: "mock_enterprise_license_key",
}));
const environmentId = "test-environment-id";
const mockEnvironment: TEnvironment = {
id: environmentId,
createdAt: new Date(),
updatedAt: new Date(),
projectId: "test-project-id",
type: "production",
appSetupCompleted: true, // Default to true
};
const mockOrganization: TOrganization = {
id: "test-org-id",
name: "Test Organization",
createdAt: new Date(),
updatedAt: new Date(),
billing: {
plan: "free",
stripeCustomerId: null,
period: "monthly",
limits: {
projects: 1,
monthly: {
responses: 100, // Default limit
miu: 1000,
},
},
periodStart: new Date(),
},
isAIEnabled: false,
};
const mockProject: TProject = {
id: "test-project-id",
createdAt: new Date(),
updatedAt: new Date(),
name: "Test Project",
config: {
channel: "link",
industry: "eCommerce",
},
organizationId: mockOrganization.id,
styling: {
allowStyleOverwrite: false,
},
recontactDays: 30,
inAppSurveyBranding: true,
linkSurveyBranding: true,
placement: "bottomRight",
clickOutsideClose: true,
darkOverlay: false,
environments: [],
languages: [],
};
const mockSurveys: TSurvey[] = [
{
id: "survey-app-inProgress",
createdAt: new Date(),
updatedAt: new Date(),
name: "App Survey In Progress",
environmentId: environmentId,
type: "app",
status: "inProgress",
displayLimit: null,
endings: [],
followUps: [],
isBackButtonHidden: false,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
questions: [],
displayOption: "displayOnce",
recontactDays: null,
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
singleUse: null,
triggers: [],
languages: [],
pin: null,
resultShareKey: null,
segment: null,
styling: null,
surveyClosedMessage: null,
hiddenFields: { enabled: false },
welcomeCard: { enabled: false, showResponseCount: false, timeToFinish: false },
variables: [],
createdBy: null,
recaptcha: { enabled: false, threshold: 0.5 },
},
{
id: "survey-app-paused",
createdAt: new Date(),
updatedAt: new Date(),
name: "App Survey Paused",
environmentId: environmentId,
displayLimit: null,
endings: [],
followUps: [],
isBackButtonHidden: false,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
type: "app",
status: "paused",
questions: [],
displayOption: "displayOnce",
recontactDays: null,
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
singleUse: null,
triggers: [],
languages: [],
pin: null,
resultShareKey: null,
segment: null,
styling: null,
surveyClosedMessage: null,
hiddenFields: { enabled: false },
welcomeCard: { enabled: false, showResponseCount: false, timeToFinish: false },
variables: [],
createdBy: null,
recaptcha: { enabled: false, threshold: 0.5 },
},
{
id: "survey-web-inProgress",
createdAt: new Date(),
updatedAt: new Date(),
name: "Web Survey In Progress",
environmentId: environmentId,
type: "link",
displayLimit: null,
endings: [],
followUps: [],
isBackButtonHidden: false,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
status: "inProgress",
questions: [],
displayOption: "displayOnce",
recontactDays: null,
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
singleUse: null,
triggers: [],
languages: [],
pin: null,
resultShareKey: null,
segment: null,
styling: null,
surveyClosedMessage: null,
hiddenFields: { enabled: false },
welcomeCard: { enabled: false, showResponseCount: false, timeToFinish: false },
variables: [],
createdBy: null,
recaptcha: { enabled: false, threshold: 0.5 },
},
];
const mockActionClasses: TActionClass[] = [
{
id: "action-1",
createdAt: new Date(),
updatedAt: new Date(),
name: "Action 1",
description: null,
type: "code",
noCodeConfig: null,
environmentId: environmentId,
key: "action1",
},
];
describe("getEnvironmentState", () => {
beforeEach(() => {
vi.resetAllMocks();
// Mock the cache implementation
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
// Default mocks for successful retrieval
vi.mocked(getEnvironment).mockResolvedValue(mockEnvironment);
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization);
vi.mocked(getProjectForEnvironmentState).mockResolvedValue(mockProject);
vi.mocked(getSurveysForEnvironmentState).mockResolvedValue(mockSurveys);
vi.mocked(getActionClassesForEnvironmentState).mockResolvedValue(mockActionClasses);
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(50); // Default below limit
});
afterEach(() => {
vi.resetAllMocks();
});
test("should return the correct environment state", async () => {
const result = await getEnvironmentState(environmentId);
const expectedData: TJsEnvironmentState["data"] = {
recaptchaSiteKey: "mock_recaptcha_site_key",
surveys: [mockSurveys[0]], // Only app, inProgress survey
actionClasses: mockActionClasses,
project: mockProject,
};
expect(result.data).toEqual(expectedData);
expect(result.revalidateEnvironment).toBe(false);
expect(getEnvironment).toHaveBeenCalledWith(environmentId);
expect(getOrganizationByEnvironmentId).toHaveBeenCalledWith(environmentId);
expect(getProjectForEnvironmentState).toHaveBeenCalledWith(environmentId);
expect(getSurveysForEnvironmentState).toHaveBeenCalledWith(environmentId);
expect(getActionClassesForEnvironmentState).toHaveBeenCalledWith(environmentId);
expect(prisma.environment.update).not.toHaveBeenCalled();
expect(capturePosthogEnvironmentEvent).not.toHaveBeenCalled();
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalled(); // Not cloud
expect(sendPlanLimitsReachedEventToPosthogWeekly).not.toHaveBeenCalled();
});
test("should throw ResourceNotFoundError if environment not found", async () => {
vi.mocked(getEnvironment).mockResolvedValue(null);
await expect(getEnvironmentState(environmentId)).rejects.toThrow(ResourceNotFoundError);
});
test("should throw ResourceNotFoundError if organization not found", async () => {
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(null);
await expect(getEnvironmentState(environmentId)).rejects.toThrow(ResourceNotFoundError);
});
test("should throw ResourceNotFoundError if project not found", async () => {
vi.mocked(getProjectForEnvironmentState).mockResolvedValue(null);
await expect(getEnvironmentState(environmentId)).rejects.toThrow(ResourceNotFoundError);
});
test("should update environment and capture event if app setup not completed", async () => {
const incompleteEnv = { ...mockEnvironment, appSetupCompleted: false };
vi.mocked(getEnvironment).mockResolvedValue(incompleteEnv);
const result = await getEnvironmentState(environmentId);
expect(prisma.environment.update).toHaveBeenCalledWith({
where: { id: environmentId },
data: { appSetupCompleted: true },
});
expect(capturePosthogEnvironmentEvent).toHaveBeenCalledWith(environmentId, "app setup completed");
expect(result.revalidateEnvironment).toBe(true);
});
test("should return empty surveys if monthly response limit reached (Cloud)", async () => {
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(100); // Exactly at limit
vi.mocked(getSurveysForEnvironmentState).mockResolvedValue(mockSurveys);
const result = await getEnvironmentState(environmentId);
expect(result.data.surveys).toEqual([]);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(mockOrganization.id);
expect(sendPlanLimitsReachedEventToPosthogWeekly).toHaveBeenCalledWith(environmentId, {
plan: mockOrganization.billing.plan,
limits: {
projects: null,
monthly: {
miu: null,
responses: mockOrganization.billing.limits.monthly.responses,
},
},
});
});
test("should return surveys if monthly response limit not reached (Cloud)", async () => {
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(99); // Below limit
const result = await getEnvironmentState(environmentId);
expect(result.data.surveys).toEqual([mockSurveys[0]]);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(mockOrganization.id);
expect(sendPlanLimitsReachedEventToPosthogWeekly).not.toHaveBeenCalled();
});
test("should handle error when sending Posthog limit reached event", async () => {
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(100);
const posthogError = new Error("Posthog failed");
vi.mocked(sendPlanLimitsReachedEventToPosthogWeekly).mockRejectedValue(posthogError);
const result = await getEnvironmentState(environmentId);
expect(result.data.surveys).toEqual([]);
expect(logger.error).toHaveBeenCalledWith(
posthogError,
"Error sending plan limits reached event to Posthog"
);
});
test("should include recaptchaSiteKey if recaptcha variables are set", async () => {
const result = await getEnvironmentState(environmentId);
expect(result.data.recaptchaSiteKey).toBe("mock_recaptcha_site_key");
});
test("should filter surveys correctly (only app type and inProgress status)", async () => {
const result = await getEnvironmentState(environmentId);
expect(result.data.surveys).toHaveLength(1);
expect(result.data.surveys[0].id).toBe("survey-app-inProgress");
});
});

View File

@@ -1,6 +1,6 @@
import { actionClassCache } from "@/lib/actionClass/cache";
import { cache } from "@/lib/cache";
import { IS_FORMBRICKS_CLOUD } from "@/lib/constants";
import { IS_FORMBRICKS_CLOUD, IS_RECAPTCHA_CONFIGURED, RECAPTCHA_SITE_KEY } from "@/lib/constants";
import { environmentCache } from "@/lib/environment/cache";
import { getEnvironment } from "@/lib/environment/service";
import { organizationCache } from "@/lib/organization/cache";
@@ -107,6 +107,7 @@ export const getEnvironmentState = async (
surveys: !isMonthlyResponsesLimitReached ? filteredSurveys : [],
actionClasses,
project: project,
...(IS_RECAPTCHA_CONFIGURED ? { recaptchaSiteKey: RECAPTCHA_SITE_KEY } : {}),
};
return {

View File

@@ -0,0 +1,120 @@
import { cache } from "@/lib/cache";
import { projectCache } from "@/lib/project/cache";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { TJsEnvironmentStateProject } from "@formbricks/types/js";
import { getProjectForEnvironmentState } from "./project";
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/project/cache");
vi.mock("@formbricks/database", () => ({
prisma: {
project: {
findFirst: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
vi.mock("@/lib/utils/validate"); // Mock validateInputs if needed, though it's often tested elsewhere
const environmentId = "test-environment-id";
const mockProject: TJsEnvironmentStateProject = {
id: "test-project-id",
recontactDays: 30,
clickOutsideClose: true,
darkOverlay: false,
placement: "bottomRight",
inAppSurveyBranding: true,
styling: { allowStyleOverwrite: false },
};
describe("getProjectForEnvironmentState", () => {
beforeEach(() => {
vi.resetAllMocks();
// Mock cache implementation
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
// Mock projectCache tags
vi.mocked(projectCache.tag.byEnvironmentId).mockReturnValue(`project-env-${environmentId}`);
});
afterEach(() => {
vi.resetAllMocks();
});
test("should return project state successfully", async () => {
vi.mocked(prisma.project.findFirst).mockResolvedValue(mockProject);
const result = await getProjectForEnvironmentState(environmentId);
expect(result).toEqual(mockProject);
expect(prisma.project.findFirst).toHaveBeenCalledWith({
where: {
environments: {
some: {
id: environmentId,
},
},
},
select: {
id: true,
recontactDays: true,
clickOutsideClose: true,
darkOverlay: true,
placement: true,
inAppSurveyBranding: true,
styling: true,
},
});
expect(cache).toHaveBeenCalledTimes(1);
expect(cache).toHaveBeenCalledWith(
expect.any(Function),
[`getProjectForEnvironmentState-${environmentId}`],
{
tags: [`project-env-${environmentId}`],
}
);
});
test("should return null if project not found", async () => {
vi.mocked(prisma.project.findFirst).mockResolvedValue(null);
const result = await getProjectForEnvironmentState(environmentId);
expect(result).toBeNull();
expect(prisma.project.findFirst).toHaveBeenCalledTimes(1);
expect(cache).toHaveBeenCalledTimes(1);
});
test("should throw DatabaseError on PrismaClientKnownRequestError", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Test error", {
code: "P2001",
clientVersion: "test",
});
vi.mocked(prisma.project.findFirst).mockRejectedValue(prismaError);
await expect(getProjectForEnvironmentState(environmentId)).rejects.toThrow(DatabaseError);
expect(logger.error).toHaveBeenCalledWith(prismaError, "Error getting project for environment state");
expect(cache).toHaveBeenCalledTimes(1);
});
test("should re-throw unknown errors", async () => {
const unknownError = new Error("Something went wrong");
vi.mocked(prisma.project.findFirst).mockRejectedValue(unknownError);
await expect(getProjectForEnvironmentState(environmentId)).rejects.toThrow(unknownError);
expect(logger.error).not.toHaveBeenCalled(); // Should not log unknown errors here
expect(cache).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,143 @@
import { cache } from "@/lib/cache";
import { validateInputs } from "@/lib/utils/validate";
import { transformPrismaSurvey } from "@/modules/survey/lib/utils";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { TJsEnvironmentStateSurvey } from "@formbricks/types/js";
import { getSurveysForEnvironmentState } from "./survey";
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/utils/validate");
vi.mock("@/modules/survey/lib/utils");
vi.mock("@formbricks/database", () => ({
prisma: {
survey: {
findMany: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
const environmentId = "test-environment-id";
const mockPrismaSurvey = {
id: "survey-1",
welcomeCard: { enabled: false },
name: "Test Survey",
questions: [],
variables: [],
type: "app",
showLanguageSwitch: false,
languages: [],
endings: [],
autoClose: null,
styling: null,
status: "inProgress",
recaptcha: null,
segment: null,
recontactDays: null,
displayLimit: null,
displayOption: "displayOnce",
hiddenFields: { enabled: false },
isBackButtonHidden: false,
triggers: [],
displayPercentage: null,
delay: 0,
projectOverwrites: null,
};
const mockTransformedSurvey: TJsEnvironmentStateSurvey = {
id: "survey-1",
welcomeCard: { enabled: false } as TJsEnvironmentStateSurvey["welcomeCard"],
name: "Test Survey",
questions: [],
variables: [],
type: "app",
showLanguageSwitch: false,
languages: [],
endings: [],
autoClose: null,
styling: null,
status: "inProgress",
recaptcha: null,
segment: null,
recontactDays: null,
displayLimit: null,
displayOption: "displayOnce",
hiddenFields: { enabled: false },
isBackButtonHidden: false,
triggers: [],
displayPercentage: null,
delay: 0,
projectOverwrites: null,
};
describe("getSurveysForEnvironmentState", () => {
beforeEach(() => {
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
vi.mocked(validateInputs).mockReturnValue([environmentId]); // Assume validation passes
vi.mocked(transformPrismaSurvey).mockReturnValue(mockTransformedSurvey);
});
afterEach(() => {
vi.resetAllMocks();
});
test("should return transformed surveys on successful fetch", async () => {
vi.mocked(prisma.survey.findMany).mockResolvedValue([mockPrismaSurvey]);
const result = await getSurveysForEnvironmentState(environmentId);
expect(validateInputs).toHaveBeenCalledWith([environmentId, expect.any(Object)]);
expect(prisma.survey.findMany).toHaveBeenCalledWith({
where: { environmentId },
select: expect.any(Object), // Check if select is called, specific fields are in the original code
});
expect(transformPrismaSurvey).toHaveBeenCalledWith(mockPrismaSurvey);
expect(result).toEqual([mockTransformedSurvey]);
expect(logger.error).not.toHaveBeenCalled();
});
test("should return an empty array if no surveys are found", async () => {
vi.mocked(prisma.survey.findMany).mockResolvedValue([]);
const result = await getSurveysForEnvironmentState(environmentId);
expect(prisma.survey.findMany).toHaveBeenCalledWith({
where: { environmentId },
select: expect.any(Object),
});
expect(transformPrismaSurvey).not.toHaveBeenCalled();
expect(result).toEqual([]);
expect(logger.error).not.toHaveBeenCalled();
});
test("should throw DatabaseError on Prisma known request error", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Test Prisma Error", {
code: "P2025",
clientVersion: "5.0.0",
});
vi.mocked(prisma.survey.findMany).mockRejectedValue(prismaError);
await expect(getSurveysForEnvironmentState(environmentId)).rejects.toThrow(DatabaseError);
expect(logger.error).toHaveBeenCalledWith(prismaError, "Error getting surveys for environment state");
});
test("should rethrow unknown errors", async () => {
const unknownError = new Error("Something went wrong");
vi.mocked(prisma.survey.findMany).mockRejectedValue(unknownError);
await expect(getSurveysForEnvironmentState(environmentId)).rejects.toThrow(unknownError);
expect(logger.error).not.toHaveBeenCalled();
});
});

View File

@@ -49,6 +49,7 @@ export const getSurveysForEnvironmentState = reactCache(
autoClose: true,
styling: true,
status: true,
recaptcha: true,
segment: {
include: {
surveys: {

View File

@@ -0,0 +1,160 @@
import { cache } from "@/lib/cache";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { DatabaseError } from "@formbricks/types/errors";
import { getContact, getContactByUserId } from "./contact";
// Mock prisma
vi.mock("@formbricks/database", () => ({
prisma: {
contact: {
findUnique: vi.fn(),
findFirst: vi.fn(),
},
},
}));
// Mock cache module
vi.mock("@/lib/cache");
// Mock react cache
vi.mock("react", async () => {
const actual = await vi.importActual("react");
return {
...actual,
cache: vi.fn((fn) => fn), // Mock react's cache to just return the function
};
});
const mockContactId = "test-contact-id";
const mockEnvironmentId = "test-env-id";
const mockUserId = "test-user-id";
describe("Contact API Lib", () => {
beforeEach(() => {
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
});
afterEach(() => {
vi.resetAllMocks();
});
describe("getContact", () => {
test("should return contact if found", async () => {
const mockContactData = { id: mockContactId };
vi.mocked(prisma.contact.findUnique).mockResolvedValue(mockContactData);
const contact = await getContact(mockContactId);
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: { id: mockContactId },
select: { id: true },
});
expect(contact).toEqual(mockContactData);
});
test("should return null if contact not found", async () => {
vi.mocked(prisma.contact.findUnique).mockResolvedValue(null);
const contact = await getContact(mockContactId);
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: { id: mockContactId },
select: { id: true },
});
expect(contact).toBeNull();
});
test("should throw DatabaseError on Prisma error", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Test Prisma Error", {
code: "P2025",
clientVersion: "5.0.0",
});
vi.mocked(prisma.contact.findUnique).mockRejectedValue(prismaError);
await expect(getContact(mockContactId)).rejects.toThrow(DatabaseError);
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: { id: mockContactId },
select: { id: true },
});
});
});
describe("getContactByUserId", () => {
test("should return contact with formatted attributes if found", async () => {
const mockContactData = {
id: mockContactId,
attributes: [
{ attributeKey: { key: "userId" }, value: mockUserId },
{ attributeKey: { key: "email" }, value: "test@example.com" },
],
};
vi.mocked(prisma.contact.findFirst).mockResolvedValue(mockContactData);
const contact = await getContactByUserId(mockEnvironmentId, mockUserId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId: mockEnvironmentId,
},
value: mockUserId,
},
},
},
select: {
id: true,
attributes: {
select: {
attributeKey: { select: { key: true } },
value: true,
},
},
},
});
expect(contact).toEqual({
id: mockContactId,
attributes: {
userId: mockUserId,
email: "test@example.com",
},
});
});
test("should return null if contact not found by userId", async () => {
vi.mocked(prisma.contact.findFirst).mockResolvedValue(null);
const contact = await getContactByUserId(mockEnvironmentId, mockUserId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId: mockEnvironmentId,
},
value: mockUserId,
},
},
},
select: {
id: true,
attributes: {
select: {
attributeKey: { select: { key: true } },
value: true,
},
},
},
});
expect(contact).toBeNull();
});
});
});

View File

@@ -0,0 +1,201 @@
import {
getMonthlyOrganizationResponseCount,
getOrganizationByEnvironmentId,
} from "@/lib/organization/service";
import { sendPlanLimitsReachedEventToPosthogWeekly } from "@/lib/posthogServer";
import { calculateTtcTotal } from "@/lib/response/utils";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/errors";
import { TResponseInput } from "@formbricks/types/responses";
import { createResponse } from "./response";
let mockIsFormbricksCloud = false;
vi.mock("@/lib/constants", () => ({
get IS_FORMBRICKS_CLOUD() {
return mockIsFormbricksCloud;
},
}));
vi.mock("@/lib/organization/service", () => ({
getMonthlyOrganizationResponseCount: vi.fn(),
getOrganizationByEnvironmentId: vi.fn(),
}));
vi.mock("@/lib/posthogServer", () => ({
sendPlanLimitsReachedEventToPosthogWeekly: vi.fn(),
}));
vi.mock("@/lib/response/cache", () => ({
responseCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/response/utils", () => ({
calculateTtcTotal: vi.fn((ttc) => ttc),
}));
vi.mock("@/lib/responseNote/cache", () => ({
responseNoteCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/telemetry", () => ({
captureTelemetry: vi.fn(),
}));
vi.mock("@/lib/utils/validate", () => ({
validateInputs: vi.fn(),
}));
vi.mock("@formbricks/database", () => ({
prisma: {
response: {
create: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
vi.mock("./contact", () => ({
getContactByUserId: vi.fn(),
}));
const environmentId = "test-environment-id";
const surveyId = "test-survey-id";
const organizationId = "test-organization-id";
const responseId = "test-response-id";
const mockOrganization = {
id: organizationId,
name: "Test Org",
billing: {
limits: { monthly: { responses: 100 } },
plan: "free",
},
};
const mockResponseInput: TResponseInput = {
environmentId,
surveyId,
userId: null,
finished: false,
data: { question1: "answer1" },
meta: { source: "web" },
ttc: { question1: 1000 },
};
const mockResponsePrisma = {
id: responseId,
createdAt: new Date(),
updatedAt: new Date(),
surveyId,
finished: false,
data: { question1: "answer1" },
meta: { source: "web" },
ttc: { question1: 1000 },
variables: {},
contactAttributes: {},
singleUseId: null,
language: null,
displayId: null,
tags: [],
notes: [],
};
describe("createResponse", () => {
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization as any);
vi.mocked(prisma.response.create).mockResolvedValue(mockResponsePrisma as any);
vi.mocked(calculateTtcTotal).mockImplementation((ttc) => ttc);
});
afterEach(() => {
mockIsFormbricksCloud = false;
});
test("should handle finished response and calculate TTC", async () => {
const finishedInput = { ...mockResponseInput, finished: true };
await createResponse(finishedInput);
expect(calculateTtcTotal).toHaveBeenCalledWith(mockResponseInput.ttc);
expect(prisma.response.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ finished: true }),
})
);
});
test("should check response limits if IS_FORMBRICKS_CLOUD is true", async () => {
mockIsFormbricksCloud = true;
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(50);
await createResponse(mockResponseInput);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(organizationId);
expect(sendPlanLimitsReachedEventToPosthogWeekly).not.toHaveBeenCalled();
});
test("should send limit reached event if IS_FORMBRICKS_CLOUD is true and limit reached", async () => {
mockIsFormbricksCloud = true;
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(100);
await createResponse(mockResponseInput);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(organizationId);
expect(sendPlanLimitsReachedEventToPosthogWeekly).toHaveBeenCalledWith(environmentId, {
plan: "free",
limits: {
projects: null,
monthly: {
responses: 100,
miu: null,
},
},
});
});
test("should throw ResourceNotFoundError if organization not found", async () => {
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(null);
await expect(createResponse(mockResponseInput)).rejects.toThrow(ResourceNotFoundError);
});
test("should throw DatabaseError on Prisma known request error", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Test Prisma Error", {
code: "P2002",
clientVersion: "test",
});
vi.mocked(prisma.response.create).mockRejectedValue(prismaError);
await expect(createResponse(mockResponseInput)).rejects.toThrow(DatabaseError);
});
test("should throw original error on other Prisma errors", async () => {
const genericError = new Error("Generic database error");
vi.mocked(prisma.response.create).mockRejectedValue(genericError);
await expect(createResponse(mockResponseInput)).rejects.toThrow(genericError);
});
test("should log error but not throw if sendPlanLimitsReachedEventToPosthogWeekly fails", async () => {
mockIsFormbricksCloud = true;
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(100);
const posthogError = new Error("PostHog error");
vi.mocked(sendPlanLimitsReachedEventToPosthogWeekly).mockRejectedValue(posthogError);
await createResponse(mockResponseInput);
expect(logger.error).toHaveBeenCalledWith(
posthogError,
"Error sending plan limits reached event to Posthog"
);
});
});

View File

@@ -0,0 +1,103 @@
import { getUploadSignedUrl } from "@/lib/storage/service";
import { afterEach, describe, expect, test, vi } from "vitest";
import { uploadPrivateFile } from "./uploadPrivateFile";
vi.mock("@/lib/storage/service", () => ({
getUploadSignedUrl: vi.fn(),
}));
describe("uploadPrivateFile", () => {
afterEach(() => {
vi.resetAllMocks();
});
test("should return a success response with signed URL details when getUploadSignedUrl successfully generates a signed URL", async () => {
const mockSignedUrlResponse = {
signedUrl: "mocked-signed-url",
presignedFields: { field1: "value1" },
fileUrl: "mocked-file-url",
};
vi.mocked(getUploadSignedUrl).mockResolvedValue(mockSignedUrlResponse);
const fileName = "test-file.txt";
const environmentId = "test-env-id";
const fileType = "text/plain";
const result = await uploadPrivateFile(fileName, environmentId, fileType);
const resultData = await result.json();
expect(getUploadSignedUrl).toHaveBeenCalledWith(fileName, environmentId, fileType, "private", false);
expect(resultData).toEqual({
data: mockSignedUrlResponse,
});
});
test("should return a success response when isBiggerFileUploadAllowed is true and getUploadSignedUrl successfully generates a signed URL", async () => {
const mockSignedUrlResponse = {
signedUrl: "mocked-signed-url",
presignedFields: { field1: "value1" },
fileUrl: "mocked-file-url",
};
vi.mocked(getUploadSignedUrl).mockResolvedValue(mockSignedUrlResponse);
const fileName = "test-file.txt";
const environmentId = "test-env-id";
const fileType = "text/plain";
const isBiggerFileUploadAllowed = true;
const result = await uploadPrivateFile(fileName, environmentId, fileType, isBiggerFileUploadAllowed);
const resultData = await result.json();
expect(getUploadSignedUrl).toHaveBeenCalledWith(
fileName,
environmentId,
fileType,
"private",
isBiggerFileUploadAllowed
);
expect(resultData).toEqual({
data: mockSignedUrlResponse,
});
});
test("should return an internal server error response when getUploadSignedUrl throws an error", async () => {
vi.mocked(getUploadSignedUrl).mockRejectedValue(new Error("S3 unavailable"));
const fileName = "test-file.txt";
const environmentId = "test-env-id";
const fileType = "text/plain";
const result = await uploadPrivateFile(fileName, environmentId, fileType);
expect(result.status).toBe(500);
const resultData = await result.json();
expect(resultData).toEqual({
code: "internal_server_error",
details: {},
message: "Internal server error",
});
});
test("should return an internal server error response when fileName has no extension", async () => {
vi.mocked(getUploadSignedUrl).mockRejectedValue(new Error("File extension not found"));
const fileName = "test-file";
const environmentId = "test-env-id";
const fileType = "text/plain";
const result = await uploadPrivateFile(fileName, environmentId, fileType);
const resultData = await result.json();
expect(getUploadSignedUrl).toHaveBeenCalledWith(fileName, environmentId, fileType, "private", false);
expect(result.status).toBe(500);
expect(resultData).toEqual({
code: "internal_server_error",
details: {},
message: "Internal server error",
});
});
});

View File

@@ -0,0 +1,62 @@
import { getSessionUser } from "@/app/api/v1/management/me/lib/utils";
import { authOptions } from "@/modules/auth/lib/authOptions";
import { mockUser } from "@/modules/auth/lib/mock-data";
import { cleanup } from "@testing-library/react";
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { afterEach, describe, expect, test, vi } from "vitest";
vi.mock("next-auth", () => ({
getServerSession: vi.fn(),
}));
vi.mock("@/modules/auth/lib/authOptions", () => ({
authOptions: {},
}));
describe("getSessionUser", () => {
afterEach(() => {
cleanup();
});
test("should return the user object when valid req and res are provided", async () => {
const mockReq = {} as NextApiRequest;
const mockRes = {} as NextApiResponse;
vi.mocked(getServerSession).mockResolvedValue({ user: mockUser });
const user = await getSessionUser(mockReq, mockRes);
expect(user).toEqual(mockUser);
expect(getServerSession).toHaveBeenCalledWith(mockReq, mockRes, authOptions);
});
test("should return the user object when neither req nor res are provided", async () => {
vi.mocked(getServerSession).mockResolvedValue({ user: mockUser });
const user = await getSessionUser();
expect(user).toEqual(mockUser);
expect(getServerSession).toHaveBeenCalledWith(authOptions);
});
test("should return undefined if no session exists", async () => {
vi.mocked(getServerSession).mockResolvedValue(null);
const user = await getSessionUser();
expect(user).toBeUndefined();
});
test("should return null when session exists and user property is null", async () => {
const mockReq = {} as NextApiRequest;
const mockRes = {} as NextApiResponse;
vi.mocked(getServerSession).mockResolvedValue({ user: null });
const user = await getSessionUser(mockReq, mockRes);
expect(user).toBeNull();
expect(getServerSession).toHaveBeenCalledWith(mockReq, mockRes, authOptions);
});
});

View File

@@ -0,0 +1,121 @@
import { cache } from "@/lib/cache";
import { contactCache } from "@/lib/cache/contact";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { TContactAttributes } from "@formbricks/types/contact-attribute";
import { getContactByUserId } from "./contact";
// Mock prisma
vi.mock("@formbricks/database", () => ({
prisma: {
contact: {
findFirst: vi.fn(),
},
},
}));
vi.mock("@/lib/cache");
const environmentId = "test-env-id";
const userId = "test-user-id";
const contactId = "test-contact-id";
const mockContactDbData = {
id: contactId,
attributes: [
{ attributeKey: { key: "userId" }, value: userId },
{ attributeKey: { key: "email" }, value: "test@example.com" },
{ attributeKey: { key: "plan" }, value: "premium" },
],
};
const expectedContactAttributes: TContactAttributes = {
userId: userId,
email: "test@example.com",
plan: "premium",
};
describe("getContactByUserId", () => {
beforeEach(() => {
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
});
test("should return contact with attributes when found", async () => {
vi.mocked(prisma.contact.findFirst).mockResolvedValue(mockContactDbData);
const contact = await getContactByUserId(environmentId, userId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId,
},
value: userId,
},
},
},
select: {
id: true,
attributes: {
select: {
attributeKey: { select: { key: true } },
value: true,
},
},
},
});
expect(contact).toEqual({
id: contactId,
attributes: expectedContactAttributes,
});
expect(cache).toHaveBeenCalledWith(
expect.any(Function),
[`getContactByUserIdForResponsesApi-${environmentId}-${userId}`],
{
tags: [contactCache.tag.byEnvironmentIdAndUserId(environmentId, userId)],
}
);
});
test("should return null when contact is not found", async () => {
vi.mocked(prisma.contact.findFirst).mockResolvedValue(null);
const contact = await getContactByUserId(environmentId, userId);
expect(prisma.contact.findFirst).toHaveBeenCalledWith({
where: {
attributes: {
some: {
attributeKey: {
key: "userId",
environmentId,
},
value: userId,
},
},
},
select: {
id: true,
attributes: {
select: {
attributeKey: { select: { key: true } },
value: true,
},
},
},
});
expect(contact).toBeNull();
expect(cache).toHaveBeenCalledWith(
expect.any(Function),
[`getContactByUserIdForResponsesApi-${environmentId}-${userId}`],
{
tags: [contactCache.tag.byEnvironmentIdAndUserId(environmentId, userId)],
}
);
});
});

View File

@@ -0,0 +1,347 @@
import { cache } from "@/lib/cache";
import {
getMonthlyOrganizationResponseCount,
getOrganizationByEnvironmentId,
} from "@/lib/organization/service";
import { sendPlanLimitsReachedEventToPosthogWeekly } from "@/lib/posthogServer";
import { responseCache } from "@/lib/response/cache";
import { getResponseContact } from "@/lib/response/service";
import { calculateTtcTotal } from "@/lib/response/utils";
import { responseNoteCache } from "@/lib/responseNote/cache";
import { validateInputs } from "@/lib/utils/validate";
import { Organization, Prisma, Response as ResponsePrisma } from "@prisma/client";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/errors";
import { TResponse, TResponseInput } from "@formbricks/types/responses";
import { getContactByUserId } from "./contact";
import { createResponse, getResponsesByEnvironmentIds } from "./response";
// Mock Data
const environmentId = "test-environment-id";
const organizationId = "test-organization-id";
const mockUserId = "test-user-id";
const surveyId = "test-survey-id";
const displayId = "test-display-id";
const responseId = "test-response-id";
const mockOrganization = {
id: organizationId,
name: "Test Org",
createdAt: new Date(),
updatedAt: new Date(),
billing: { plan: "free", limits: { monthly: { responses: null } } } as any, // Default no limit
} as unknown as Organization;
const mockResponseInput: TResponseInput = {
environmentId,
surveyId,
displayId,
finished: true,
data: { q1: "answer1" },
meta: { userAgent: { browser: "test-browser" } },
ttc: { q1: 5 },
language: "en",
};
const mockResponseInputWithUserId: TResponseInput = {
...mockResponseInput,
userId: mockUserId,
};
// Prisma response structure (simplified)
const mockResponsePrisma = {
id: responseId,
createdAt: new Date(),
updatedAt: new Date(),
surveyId,
finished: true,
endingId: null,
data: { q1: "answer1" },
meta: { userAgent: { browser: "test-browser" } },
ttc: { q1: 5, total: 10 }, // Assume calculateTtcTotal adds 'total'
variables: {},
contactAttributes: {},
singleUseId: null,
language: "en",
displayId,
contact: null, // Prisma relation
tags: [], // Prisma relation
notes: [], // Prisma relation
} as unknown as ResponsePrisma & { contact: any; tags: any[]; notes: any[] }; // Adjust type as needed
const mockResponse: TResponse = {
id: responseId,
createdAt: mockResponsePrisma.createdAt,
updatedAt: mockResponsePrisma.updatedAt,
surveyId,
finished: true,
endingId: null,
data: { q1: "answer1" },
meta: { userAgent: { browser: "test-browser" } },
ttc: { q1: 5, total: 10 },
variables: {},
contactAttributes: {},
singleUseId: null,
language: "en",
displayId,
contact: null, // Transformed structure
tags: [], // Transformed structure
notes: [], // Transformed structure
};
const mockEnvironmentIds = [environmentId, "env-2"];
const mockLimit = 10;
const mockOffset = 5;
const mockResponsesPrisma = [mockResponsePrisma, { ...mockResponsePrisma, id: "response-2" }];
const mockTransformedResponses = [mockResponse, { ...mockResponse, id: "response-2" }];
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/constants", () => ({
IS_FORMBRICKS_CLOUD: true,
POSTHOG_API_KEY: "mock-posthog-api-key",
POSTHOG_HOST: "mock-posthog-host",
IS_POSTHOG_CONFIGURED: true,
ENCRYPTION_KEY: "mock-encryption-key",
ENTERPRISE_LICENSE_KEY: "mock-enterprise-license-key",
GITHUB_ID: "mock-github-id",
GITHUB_SECRET: "test-githubID",
GOOGLE_CLIENT_ID: "test-google-client-id",
GOOGLE_CLIENT_SECRET: "test-google-client-secret",
AZUREAD_CLIENT_ID: "test-azuread-client-id",
AZUREAD_CLIENT_SECRET: "test-azure",
AZUREAD_TENANT_ID: "test-azuread-tenant-id",
OIDC_DISPLAY_NAME: "test-oidc-display-name",
OIDC_CLIENT_ID: "test-oidc-client-id",
OIDC_ISSUER: "test-oidc-issuer",
OIDC_CLIENT_SECRET: "test-oidc-client-secret",
OIDC_SIGNING_ALGORITHM: "test-oidc-signing-algorithm",
WEBAPP_URL: "test-webapp-url",
IS_PRODUCTION: false,
SENTRY_DSN: "mock-sentry-dsn",
}));
vi.mock("@/lib/organization/service");
vi.mock("@/lib/posthogServer");
vi.mock("@/lib/response/cache");
vi.mock("@/lib/response/service");
vi.mock("@/lib/response/utils");
vi.mock("@/lib/responseNote/cache");
vi.mock("@/lib/telemetry");
vi.mock("@/lib/utils/validate");
vi.mock("@formbricks/database", () => ({
prisma: {
response: {
create: vi.fn(),
findMany: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger");
vi.mock("./contact");
describe("Response Lib Tests", () => {
beforeEach(() => {
vi.clearAllMocks();
// No need to mock IS_FORMBRICKS_CLOUD here anymore unless specifically changing it from the default
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
});
describe("createResponse", () => {
test("should create a response successfully with userId", async () => {
const mockContact = { id: "contact1", attributes: { userId: mockUserId } };
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization);
vi.mocked(getContactByUserId).mockResolvedValue(mockContact);
vi.mocked(calculateTtcTotal).mockReturnValue({ total: 10 });
vi.mocked(prisma.response.create).mockResolvedValue({
...mockResponsePrisma,
});
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(50);
const response = await createResponse(mockResponseInputWithUserId);
expect(getOrganizationByEnvironmentId).toHaveBeenCalledWith(environmentId);
expect(getContactByUserId).toHaveBeenCalledWith(environmentId, mockUserId);
expect(prisma.response.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
contact: { connect: { id: mockContact.id } },
contactAttributes: mockContact.attributes,
}),
})
);
expect(responseCache.revalidate).toHaveBeenCalledWith(
expect.objectContaining({
contactId: mockContact.id,
userId: mockUserId,
})
);
expect(responseNoteCache.revalidate).toHaveBeenCalled();
expect(response.contact).toEqual({ id: mockContact.id, userId: mockUserId });
});
test("should throw ResourceNotFoundError if organization not found", async () => {
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(null);
await expect(createResponse(mockResponseInput)).rejects.toThrow(ResourceNotFoundError);
expect(getOrganizationByEnvironmentId).toHaveBeenCalledWith(environmentId);
expect(prisma.response.create).not.toHaveBeenCalled();
});
test("should handle PrismaClientKnownRequestError", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("DB error", {
code: "P2002",
clientVersion: "2.0",
});
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization);
vi.mocked(prisma.response.create).mockRejectedValue(prismaError);
await expect(createResponse(mockResponseInput)).rejects.toThrow(DatabaseError);
expect(logger.error).not.toHaveBeenCalled(); // Should be caught and re-thrown as DatabaseError
});
test("should handle generic errors", async () => {
const genericError = new Error("Something went wrong");
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization);
vi.mocked(prisma.response.create).mockRejectedValue(genericError);
await expect(createResponse(mockResponseInput)).rejects.toThrow(genericError);
});
describe("Cloud specific tests", () => {
test("should check response limit and send event if limit reached", async () => {
// IS_FORMBRICKS_CLOUD is true by default from the top-level mock
const limit = 100;
const mockOrgWithBilling = {
...mockOrganization,
billing: { limits: { monthly: { responses: limit } } },
} as any;
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrgWithBilling);
vi.mocked(calculateTtcTotal).mockReturnValue({ total: 10 });
vi.mocked(prisma.response.create).mockResolvedValue(mockResponsePrisma);
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(limit); // Limit reached
await createResponse(mockResponseInput);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(organizationId);
expect(sendPlanLimitsReachedEventToPosthogWeekly).toHaveBeenCalled();
});
test("should check response limit and not send event if limit not reached", async () => {
const limit = 100;
const mockOrgWithBilling = {
...mockOrganization,
billing: { limits: { monthly: { responses: limit } } },
} as any;
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrgWithBilling);
vi.mocked(calculateTtcTotal).mockReturnValue({ total: 10 });
vi.mocked(prisma.response.create).mockResolvedValue(mockResponsePrisma);
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(limit - 1); // Limit not reached
await createResponse(mockResponseInput);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(organizationId);
expect(sendPlanLimitsReachedEventToPosthogWeekly).not.toHaveBeenCalled();
});
test("should log error if sendPlanLimitsReachedEventToPosthogWeekly fails", async () => {
const limit = 100;
const mockOrgWithBilling = {
...mockOrganization,
billing: { limits: { monthly: { responses: limit } } },
} as any;
const posthogError = new Error("Posthog error");
vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrgWithBilling);
vi.mocked(calculateTtcTotal).mockReturnValue({ total: 10 });
vi.mocked(prisma.response.create).mockResolvedValue(mockResponsePrisma);
vi.mocked(getMonthlyOrganizationResponseCount).mockResolvedValue(limit); // Limit reached
vi.mocked(sendPlanLimitsReachedEventToPosthogWeekly).mockRejectedValue(posthogError);
// Expecting successful response creation despite PostHog error
const response = await createResponse(mockResponseInput);
expect(getMonthlyOrganizationResponseCount).toHaveBeenCalledWith(organizationId);
expect(sendPlanLimitsReachedEventToPosthogWeekly).toHaveBeenCalled();
expect(logger.error).toHaveBeenCalledWith(
posthogError,
"Error sending plan limits reached event to Posthog"
);
expect(response).toEqual(mockResponse); // Should still return the created response
});
});
});
describe("getResponsesByEnvironmentIds", () => {
test("should return responses successfully", async () => {
vi.mocked(prisma.response.findMany).mockResolvedValue(mockResponsesPrisma);
vi.mocked(getResponseContact).mockReturnValue(null); // Assume no contact for simplicity
const responses = await getResponsesByEnvironmentIds(mockEnvironmentIds);
expect(validateInputs).toHaveBeenCalledTimes(1);
expect(prisma.response.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: {
survey: {
environmentId: { in: mockEnvironmentIds },
},
},
orderBy: [{ createdAt: "desc" }],
take: undefined,
skip: undefined,
})
);
expect(getResponseContact).toHaveBeenCalledTimes(mockResponsesPrisma.length);
expect(responses).toEqual(mockTransformedResponses);
expect(cache).toHaveBeenCalled();
});
test("should return responses with limit and offset", async () => {
vi.mocked(prisma.response.findMany).mockResolvedValue(mockResponsesPrisma);
vi.mocked(getResponseContact).mockReturnValue(null);
await getResponsesByEnvironmentIds(mockEnvironmentIds, mockLimit, mockOffset);
expect(prisma.response.findMany).toHaveBeenCalledWith(
expect.objectContaining({
take: mockLimit,
skip: mockOffset,
})
);
expect(cache).toHaveBeenCalled();
});
test("should return empty array if no responses found", async () => {
vi.mocked(prisma.response.findMany).mockResolvedValue([]);
const responses = await getResponsesByEnvironmentIds(mockEnvironmentIds);
expect(responses).toEqual([]);
expect(prisma.response.findMany).toHaveBeenCalled();
expect(getResponseContact).not.toHaveBeenCalled();
expect(cache).toHaveBeenCalled();
});
test("should handle PrismaClientKnownRequestError", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("DB error", {
code: "P2002",
clientVersion: "2.0",
});
vi.mocked(prisma.response.findMany).mockRejectedValue(prismaError);
await expect(getResponsesByEnvironmentIds(mockEnvironmentIds)).rejects.toThrow(DatabaseError);
expect(cache).toHaveBeenCalled();
});
test("should handle generic errors", async () => {
const genericError = new Error("Something went wrong");
vi.mocked(prisma.response.findMany).mockRejectedValue(genericError);
await expect(getResponsesByEnvironmentIds(mockEnvironmentIds)).rejects.toThrow(genericError);
expect(cache).toHaveBeenCalled();
});
});
});

View File

@@ -0,0 +1,58 @@
import { responses } from "@/app/lib/api/response";
import { getUploadSignedUrl } from "@/lib/storage/service";
import { cleanup } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { getSignedUrlForPublicFile } from "./getSignedUrl";
vi.mock("@/app/lib/api/response", () => ({
responses: {
successResponse: vi.fn((data) => ({ data })),
internalServerErrorResponse: vi.fn((message) => ({ message })),
},
}));
vi.mock("@/lib/storage/service", () => ({
getUploadSignedUrl: vi.fn(),
}));
describe("getSignedUrlForPublicFile", () => {
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
test("should return success response with signed URL data", async () => {
const mockFileName = "test.jpg";
const mockEnvironmentId = "env123";
const mockFileType = "image/jpeg";
const mockSignedUrlResponse = {
signedUrl: "http://example.com/signed-url",
signingData: { signature: "sig", timestamp: 123, uuid: "uuid" },
updatedFileName: "test--fid--uuid.jpg",
fileUrl: "http://example.com/file-url",
};
vi.mocked(getUploadSignedUrl).mockResolvedValue(mockSignedUrlResponse);
const result = await getSignedUrlForPublicFile(mockFileName, mockEnvironmentId, mockFileType);
expect(getUploadSignedUrl).toHaveBeenCalledWith(mockFileName, mockEnvironmentId, mockFileType, "public");
expect(responses.successResponse).toHaveBeenCalledWith(mockSignedUrlResponse);
expect(result).toEqual({ data: mockSignedUrlResponse });
});
test("should return internal server error response when getUploadSignedUrl throws an error", async () => {
const mockFileName = "test.png";
const mockEnvironmentId = "env456";
const mockFileType = "image/png";
const mockError = new Error("Failed to get signed URL");
vi.mocked(getUploadSignedUrl).mockRejectedValue(mockError);
const result = await getSignedUrlForPublicFile(mockFileName, mockEnvironmentId, mockFileType);
expect(getUploadSignedUrl).toHaveBeenCalledWith(mockFileName, mockEnvironmentId, mockFileType, "public");
expect(responses.internalServerErrorResponse).toHaveBeenCalledWith("Internal server error");
expect(result).toEqual({ message: "Internal server error" });
});
});

View File

@@ -0,0 +1,153 @@
import { segmentCache } from "@/lib/cache/segment";
import { responseCache } from "@/lib/response/cache";
import { surveyCache } from "@/lib/survey/cache";
import { validateInputs } from "@/lib/utils/validate";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { deleteSurvey } from "./surveys";
// Mock dependencies
vi.mock("@/lib/cache/segment", () => ({
segmentCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/response/cache", () => ({
responseCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/survey/cache", () => ({
surveyCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/utils/validate", () => ({
validateInputs: vi.fn(),
}));
vi.mock("@formbricks/database", () => ({
prisma: {
survey: {
delete: vi.fn(),
},
segment: {
delete: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
const surveyId = "clq5n7p1q0000m7z0h5p6g3r2";
const environmentId = "clq5n7p1q0000m7z0h5p6g3r3";
const segmentId = "clq5n7p1q0000m7z0h5p6g3r4";
const actionClassId1 = "clq5n7p1q0000m7z0h5p6g3r5";
const actionClassId2 = "clq5n7p1q0000m7z0h5p6g3r6";
const mockDeletedSurveyAppPrivateSegment = {
id: surveyId,
environmentId,
type: "app",
segment: { id: segmentId, isPrivate: true },
triggers: [{ actionClass: { id: actionClassId1 } }, { actionClass: { id: actionClassId2 } }],
resultShareKey: "shareKey123",
};
const mockDeletedSurveyLink = {
id: surveyId,
environmentId,
type: "link",
segment: null,
triggers: [],
resultShareKey: null,
};
describe("deleteSurvey", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.clearAllMocks();
});
test("should delete a link survey without a segment and revalidate caches", async () => {
vi.mocked(prisma.survey.delete).mockResolvedValue(mockDeletedSurveyLink as any);
const deletedSurvey = await deleteSurvey(surveyId);
expect(validateInputs).toHaveBeenCalledWith([surveyId, expect.any(Object)]);
expect(prisma.survey.delete).toHaveBeenCalledWith({
where: { id: surveyId },
include: {
segment: true,
triggers: { include: { actionClass: true } },
},
});
expect(prisma.segment.delete).not.toHaveBeenCalled();
expect(segmentCache.revalidate).not.toHaveBeenCalled(); // No segment to revalidate
expect(responseCache.revalidate).toHaveBeenCalledWith({ surveyId, environmentId });
expect(surveyCache.revalidate).toHaveBeenCalledTimes(1); // Only for surveyId
expect(surveyCache.revalidate).toHaveBeenCalledWith({
id: surveyId,
environmentId,
resultShareKey: undefined,
});
expect(deletedSurvey).toEqual(mockDeletedSurveyLink);
});
test("should handle PrismaClientKnownRequestError during survey deletion", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Record not found", {
code: "P2025",
clientVersion: "4.0.0",
});
vi.mocked(prisma.survey.delete).mockRejectedValue(prismaError);
await expect(deleteSurvey(surveyId)).rejects.toThrow(DatabaseError);
expect(logger.error).toHaveBeenCalledWith({ error: prismaError, surveyId }, "Error deleting survey");
expect(prisma.segment.delete).not.toHaveBeenCalled();
expect(segmentCache.revalidate).not.toHaveBeenCalled();
expect(responseCache.revalidate).not.toHaveBeenCalled();
expect(surveyCache.revalidate).not.toHaveBeenCalled();
});
test("should handle PrismaClientKnownRequestError during segment deletion", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("Foreign key constraint failed", {
code: "P2003",
clientVersion: "4.0.0",
});
vi.mocked(prisma.survey.delete).mockResolvedValue(mockDeletedSurveyAppPrivateSegment as any);
vi.mocked(prisma.segment.delete).mockRejectedValue(prismaError);
await expect(deleteSurvey(surveyId)).rejects.toThrow(DatabaseError);
expect(logger.error).toHaveBeenCalledWith({ error: prismaError, surveyId }, "Error deleting survey");
expect(prisma.segment.delete).toHaveBeenCalledWith({ where: { id: segmentId } });
// Caches might have been partially revalidated before the error
});
test("should handle generic errors during deletion", async () => {
const genericError = new Error("Something went wrong");
vi.mocked(prisma.survey.delete).mockRejectedValue(genericError);
await expect(deleteSurvey(surveyId)).rejects.toThrow(genericError);
expect(logger.error).not.toHaveBeenCalled(); // Should not log generic errors here
expect(prisma.segment.delete).not.toHaveBeenCalled();
});
test("should throw validation error for invalid surveyId", async () => {
const invalidSurveyId = "invalid-id";
const validationError = new Error("Validation failed");
vi.mocked(validateInputs).mockImplementation(() => {
throw validationError;
});
await expect(deleteSurvey(invalidSurveyId)).rejects.toThrow(validationError);
expect(prisma.survey.delete).not.toHaveBeenCalled();
});
});

View File

@@ -1,12 +1,11 @@
import { authenticateRequest, handleErrorResponse } from "@/app/api/v1/auth";
import { deleteSurvey } from "@/app/api/v1/management/surveys/[surveyId]/lib/surveys";
import { checkFeaturePermissions } from "@/app/api/v1/management/surveys/lib/utils";
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { getOrganizationByEnvironmentId } from "@/lib/organization/service";
import { getSurvey, updateSurvey } from "@/lib/survey/service";
import { getMultiLanguagePermission } from "@/modules/ee/license-check/lib/utils";
import { hasPermission } from "@/modules/organization/settings/api-keys/lib/utils";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { logger } from "@formbricks/logger";
import { TAuthenticationApiKey } from "@formbricks/types/auth";
import { ZSurveyUpdateInput } from "@formbricks/types/surveys/types";
@@ -96,19 +95,8 @@ export const PUT = async (
);
}
if (surveyUpdate.followUps && surveyUpdate.followUps.length) {
const isSurveyFollowUpsEnabled = await getSurveyFollowUpsPermission(organization.billing.plan);
if (!isSurveyFollowUpsEnabled) {
return responses.forbiddenResponse("Survey follow ups are not enabled for this organization");
}
}
if (surveyUpdate.languages && surveyUpdate.languages.length) {
const isMultiLanguageEnabled = await getMultiLanguagePermission(organization.billing.plan);
if (!isMultiLanguageEnabled) {
return responses.forbiddenResponse("Multi language is not enabled for this organization");
}
}
const featureCheckResult = await checkFeaturePermissions(surveyUpdate, organization);
if (featureCheckResult) return featureCheckResult;
return responses.successResponse(await updateSurvey({ ...inputValidation.data, id: params.surveyId }));
} catch (error) {

View File

@@ -0,0 +1,187 @@
import { cache } from "@/lib/cache";
import { selectSurvey } from "@/lib/survey/service";
import { transformPrismaSurvey } from "@/lib/survey/utils";
import { validateInputs } from "@/lib/utils/validate";
import { Prisma } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { TSurvey } from "@formbricks/types/surveys/types";
import { getSurveys } from "./surveys";
// Mock dependencies
vi.mock("@/lib/cache");
vi.mock("@/lib/survey/cache");
vi.mock("@/lib/survey/utils");
vi.mock("@/lib/utils/validate");
vi.mock("@formbricks/database", () => ({
prisma: {
survey: {
findMany: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger");
vi.mock("react", async () => {
const actual = await vi.importActual("react");
return {
...actual,
cache: vi.fn((fn) => fn), // Mock reactCache to just execute the function
};
});
const environmentId1 = "env1";
const environmentId2 = "env2";
const surveyId1 = "survey1";
const surveyId2 = "survey2";
const surveyId3 = "survey3";
const mockSurveyPrisma1 = {
id: surveyId1,
environmentId: environmentId1,
name: "Survey 1",
updatedAt: new Date(),
};
const mockSurveyPrisma2 = {
id: surveyId2,
environmentId: environmentId1,
name: "Survey 2",
updatedAt: new Date(),
};
const mockSurveyPrisma3 = {
id: surveyId3,
environmentId: environmentId2,
name: "Survey 3",
updatedAt: new Date(),
};
const mockSurveyTransformed1: TSurvey = {
...mockSurveyPrisma1,
displayPercentage: null,
segment: null,
} as TSurvey;
const mockSurveyTransformed2: TSurvey = {
...mockSurveyPrisma2,
displayPercentage: null,
segment: null,
} as TSurvey;
const mockSurveyTransformed3: TSurvey = {
...mockSurveyPrisma3,
displayPercentage: null,
segment: null,
} as TSurvey;
describe("getSurveys (Management API)", () => {
beforeEach(() => {
vi.resetAllMocks();
// Mock the cache function to simply execute the underlying function
vi.mocked(cache).mockImplementation((fn) => async () => {
return fn();
});
vi.mocked(transformPrismaSurvey).mockImplementation((survey) => ({
...survey,
displayPercentage: null,
segment: null,
}));
});
afterEach(() => {
vi.resetAllMocks();
});
test("should return surveys for a single environment ID with limit and offset", async () => {
const limit = 1;
const offset = 1;
vi.mocked(prisma.survey.findMany).mockResolvedValue([mockSurveyPrisma2]);
const surveys = await getSurveys([environmentId1], limit, offset);
expect(validateInputs).toHaveBeenCalledWith(
[[environmentId1], expect.any(Object)],
[limit, expect.any(Object)],
[offset, expect.any(Object)]
);
expect(prisma.survey.findMany).toHaveBeenCalledWith({
where: { environmentId: { in: [environmentId1] } },
select: selectSurvey,
orderBy: { updatedAt: "desc" },
take: limit,
skip: offset,
});
expect(transformPrismaSurvey).toHaveBeenCalledTimes(1);
expect(transformPrismaSurvey).toHaveBeenCalledWith(mockSurveyPrisma2);
expect(surveys).toEqual([mockSurveyTransformed2]);
expect(cache).toHaveBeenCalledTimes(1);
});
test("should return surveys for multiple environment IDs without limit and offset", async () => {
vi.mocked(prisma.survey.findMany).mockResolvedValue([
mockSurveyPrisma1,
mockSurveyPrisma2,
mockSurveyPrisma3,
]);
const surveys = await getSurveys([environmentId1, environmentId2]);
expect(validateInputs).toHaveBeenCalledWith(
[[environmentId1, environmentId2], expect.any(Object)],
[undefined, expect.any(Object)],
[undefined, expect.any(Object)]
);
expect(prisma.survey.findMany).toHaveBeenCalledWith({
where: { environmentId: { in: [environmentId1, environmentId2] } },
select: selectSurvey,
orderBy: { updatedAt: "desc" },
take: undefined,
skip: undefined,
});
expect(transformPrismaSurvey).toHaveBeenCalledTimes(3);
expect(surveys).toEqual([mockSurveyTransformed1, mockSurveyTransformed2, mockSurveyTransformed3]);
expect(cache).toHaveBeenCalledTimes(1);
});
test("should return an empty array if no surveys are found", async () => {
vi.mocked(prisma.survey.findMany).mockResolvedValue([]);
const surveys = await getSurveys([environmentId1]);
expect(prisma.survey.findMany).toHaveBeenCalled();
expect(transformPrismaSurvey).not.toHaveBeenCalled();
expect(surveys).toEqual([]);
expect(cache).toHaveBeenCalledTimes(1);
});
test("should handle PrismaClientKnownRequestError", async () => {
const prismaError = new Prisma.PrismaClientKnownRequestError("DB error", {
code: "P2021",
clientVersion: "4.0.0",
});
vi.mocked(prisma.survey.findMany).mockRejectedValue(prismaError);
await expect(getSurveys([environmentId1])).rejects.toThrow(DatabaseError);
expect(logger.error).toHaveBeenCalledWith(prismaError, "Error getting surveys");
expect(cache).toHaveBeenCalledTimes(1);
});
test("should handle generic errors", async () => {
const genericError = new Error("Something went wrong");
vi.mocked(prisma.survey.findMany).mockRejectedValue(genericError);
await expect(getSurveys([environmentId1])).rejects.toThrow(genericError);
expect(logger.error).not.toHaveBeenCalled();
expect(cache).toHaveBeenCalledTimes(1);
});
test("should throw validation error for invalid input", async () => {
const invalidEnvId = "invalid-env";
const validationError = new Error("Validation failed");
vi.mocked(validateInputs).mockImplementation(() => {
throw validationError;
});
await expect(getSurveys([invalidEnvId])).rejects.toThrow(validationError);
expect(prisma.survey.findMany).not.toHaveBeenCalled();
expect(cache).toHaveBeenCalledTimes(1); // Cache wrapper is still called
});
});

View File

@@ -0,0 +1,231 @@
import { responses } from "@/app/lib/api/response";
import { getIsSpamProtectionEnabled, getMultiLanguagePermission } from "@/modules/ee/license-check/lib/utils";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { describe, expect, test, vi } from "vitest";
import { TOrganization } from "@formbricks/types/organizations";
import {
TSurveyCreateInputWithEnvironmentId,
TSurveyQuestionTypeEnum,
} from "@formbricks/types/surveys/types";
import { checkFeaturePermissions } from "./utils";
// Mock dependencies
vi.mock("@/app/lib/api/response", () => ({
responses: {
forbiddenResponse: vi.fn((message) => new Response(message, { status: 403 })),
},
}));
vi.mock("@/modules/ee/license-check/lib/utils", () => ({
getIsSpamProtectionEnabled: vi.fn(),
getMultiLanguagePermission: vi.fn(),
}));
vi.mock("@/modules/survey/follow-ups/lib/utils", () => ({
getSurveyFollowUpsPermission: vi.fn(),
}));
const mockOrganization: TOrganization = {
id: "test-org",
name: "Test Organization",
createdAt: new Date(),
updatedAt: new Date(),
billing: {
plan: "free",
stripeCustomerId: null,
period: "monthly",
limits: {
projects: 3,
monthly: {
responses: 1500,
miu: 2000,
},
},
periodStart: new Date(),
},
isAIEnabled: false,
};
const mockFollowUp: TSurveyCreateInputWithEnvironmentId["followUps"][number] = {
id: "followup1",
surveyId: "mockSurveyId",
name: "Test Follow-up",
trigger: {
type: "response",
properties: null,
},
action: {
type: "send-email",
properties: {
to: "mockQuestion1Id",
from: "noreply@example.com",
replyTo: [],
subject: "Follow-up Subject",
body: "Follow-up Body",
attachResponseData: false,
},
},
};
const mockLanguage: TSurveyCreateInputWithEnvironmentId["languages"][number] = {
language: {
id: "lang1",
code: "en",
alias: "English",
createdAt: new Date(),
projectId: "mockProjectId",
updatedAt: new Date(),
},
default: true,
enabled: true,
};
const baseSurveyData: TSurveyCreateInputWithEnvironmentId = {
name: "Test Survey",
environmentId: "test-env",
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Q1" },
required: false,
charLimit: {},
inputType: "text",
},
],
endings: [],
languages: [],
type: "link",
welcomeCard: { enabled: false, showResponseCount: false, timeToFinish: false },
followUps: [],
};
describe("checkFeaturePermissions", () => {
test("should return null if no restricted features are used", async () => {
const surveyData = { ...baseSurveyData };
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeNull();
});
// Recaptcha tests
test("should return forbiddenResponse if recaptcha is enabled but permission denied", async () => {
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(false);
const surveyData = { ...baseSurveyData, recaptcha: { enabled: true, threshold: 0.5 } };
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(403);
expect(responses.forbiddenResponse).toHaveBeenCalledWith(
"Spam protection is not enabled for this organization"
);
});
test("should return null if recaptcha is enabled and permission granted", async () => {
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
const surveyData: TSurveyCreateInputWithEnvironmentId = {
...baseSurveyData,
recaptcha: { enabled: true, threshold: 0.5 },
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeNull();
});
// Follow-ups tests
test("should return forbiddenResponse if follow-ups are used but permission denied", async () => {
vi.mocked(getSurveyFollowUpsPermission).mockResolvedValue(false);
const surveyData = {
...baseSurveyData,
followUps: [mockFollowUp],
}; // Add minimal follow-up data
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(403);
expect(responses.forbiddenResponse).toHaveBeenCalledWith(
"Survey follow ups are not allowed for this organization"
);
});
test("should return null if follow-ups are used and permission granted", async () => {
vi.mocked(getSurveyFollowUpsPermission).mockResolvedValue(true);
const surveyData = { ...baseSurveyData, followUps: [mockFollowUp] }; // Add minimal follow-up data
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeNull();
});
// Multi-language tests
test("should return forbiddenResponse if multi-language is used but permission denied", async () => {
vi.mocked(getMultiLanguagePermission).mockResolvedValue(false);
const surveyData: TSurveyCreateInputWithEnvironmentId = {
...baseSurveyData,
languages: [mockLanguage],
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(403);
expect(responses.forbiddenResponse).toHaveBeenCalledWith(
"Multi language is not enabled for this organization"
);
});
test("should return null if multi-language is used and permission granted", async () => {
vi.mocked(getMultiLanguagePermission).mockResolvedValue(true);
const surveyData = {
...baseSurveyData,
languages: [mockLanguage],
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeNull();
});
// Combined tests
test("should return null if multiple features are used and all permissions granted", async () => {
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
vi.mocked(getSurveyFollowUpsPermission).mockResolvedValue(true);
vi.mocked(getMultiLanguagePermission).mockResolvedValue(true);
const surveyData = {
...baseSurveyData,
recaptcha: { enabled: true, threshold: 0.5 },
followUps: [mockFollowUp],
languages: [mockLanguage],
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeNull();
});
test("should return forbiddenResponse for the first denied feature (recaptcha)", async () => {
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(false); // Denied
vi.mocked(getSurveyFollowUpsPermission).mockResolvedValue(true);
vi.mocked(getMultiLanguagePermission).mockResolvedValue(true);
const surveyData = {
...baseSurveyData,
recaptcha: { enabled: true, threshold: 0.5 },
followUps: [mockFollowUp],
languages: [mockLanguage],
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(403);
expect(responses.forbiddenResponse).toHaveBeenCalledWith(
"Spam protection is not enabled for this organization"
);
expect(responses.forbiddenResponse).toHaveBeenCalledTimes(1); // Ensure it stops at the first failure
});
test("should return forbiddenResponse for the first denied feature (follow-ups)", async () => {
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
vi.mocked(getSurveyFollowUpsPermission).mockResolvedValue(false); // Denied
vi.mocked(getMultiLanguagePermission).mockResolvedValue(true);
const surveyData = {
...baseSurveyData,
recaptcha: { enabled: true, threshold: 0.5 },
followUps: [mockFollowUp],
languages: [mockLanguage],
};
const result = await checkFeaturePermissions(surveyData, mockOrganization);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(403);
expect(responses.forbiddenResponse).toHaveBeenCalledWith(
"Survey follow ups are not allowed for this organization"
);
expect(responses.forbiddenResponse).toHaveBeenCalledTimes(1); // Ensure it stops at the first failure
});
});

View File

@@ -0,0 +1,33 @@
import { responses } from "@/app/lib/api/response";
import { getIsSpamProtectionEnabled, getMultiLanguagePermission } from "@/modules/ee/license-check/lib/utils";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { TOrganization } from "@formbricks/types/organizations";
import { TSurveyCreateInputWithEnvironmentId } from "@formbricks/types/surveys/types";
export const checkFeaturePermissions = async (
surveyData: TSurveyCreateInputWithEnvironmentId,
organization: TOrganization
): Promise<Response | null> => {
if (surveyData.recaptcha?.enabled) {
const isSpamProtectionEnabled = await getIsSpamProtectionEnabled(organization.billing.plan);
if (!isSpamProtectionEnabled) {
return responses.forbiddenResponse("Spam protection is not enabled for this organization");
}
}
if (surveyData.followUps?.length) {
const isSurveyFollowUpsEnabled = await getSurveyFollowUpsPermission(organization.billing.plan);
if (!isSurveyFollowUpsEnabled) {
return responses.forbiddenResponse("Survey follow ups are not allowed for this organization");
}
}
if (surveyData.languages?.length) {
const isMultiLanguageEnabled = await getMultiLanguagePermission(organization.billing.plan);
if (!isMultiLanguageEnabled) {
return responses.forbiddenResponse("Multi language is not enabled for this organization");
}
}
return null;
};

View File

@@ -1,11 +1,10 @@
import { authenticateRequest } from "@/app/api/v1/auth";
import { checkFeaturePermissions } from "@/app/api/v1/management/surveys/lib/utils";
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { getOrganizationByEnvironmentId } from "@/lib/organization/service";
import { createSurvey } from "@/lib/survey/service";
import { getMultiLanguagePermission } from "@/modules/ee/license-check/lib/utils";
import { hasPermission } from "@/modules/organization/settings/api-keys/lib/utils";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { logger } from "@formbricks/logger";
import { DatabaseError } from "@formbricks/types/errors";
import { ZSurveyCreateInputWithEnvironmentId } from "@formbricks/types/surveys/types";
@@ -56,7 +55,7 @@ export const POST = async (request: Request): Promise<Response> => {
);
}
const environmentId = inputValidation.data.environmentId;
const { environmentId } = inputValidation.data;
if (!hasPermission(authentication.environmentPermissions, environmentId, "POST")) {
return responses.unauthorizedResponse();
@@ -69,19 +68,8 @@ export const POST = async (request: Request): Promise<Response> => {
const surveyData = { ...inputValidation.data, environmentId };
if (surveyData.followUps?.length) {
const isSurveyFollowUpsEnabled = await getSurveyFollowUpsPermission(organization.billing.plan);
if (!isSurveyFollowUpsEnabled) {
return responses.forbiddenResponse("Survey follow ups are not enabled allowed for this organization");
}
}
if (surveyData.languages && surveyData.languages.length) {
const isMultiLanguageEnabled = await getMultiLanguagePermission(organization.billing.plan);
if (!isMultiLanguageEnabled) {
return responses.forbiddenResponse("Multi language is not enabled for this organization");
}
}
const featureCheckResult = await checkFeaturePermissions(surveyData, organization);
if (featureCheckResult) return featureCheckResult;
const survey = await createSurvey(environmentId, { ...surveyData, environmentId: undefined });
return responses.successResponse(survey);

View File

@@ -0,0 +1,108 @@
import { webhookCache } from "@/lib/cache/webhook";
import { Webhook } from "@prisma/client";
import { cleanup } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { ValidationError } from "@formbricks/types/errors";
import { deleteWebhook } from "./webhook";
vi.mock("@formbricks/database", () => ({
prisma: {
webhook: {
delete: vi.fn(),
},
},
}));
vi.mock("@/lib/cache/webhook", () => ({
webhookCache: {
tag: {
byId: () => "mockTag",
},
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/utils/validate", () => ({
validateInputs: vi.fn(),
ValidationError: class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "ValidationError";
}
},
}));
describe("deleteWebhook", () => {
afterEach(() => {
cleanup();
});
test("should delete the webhook and return the deleted webhook object when provided with a valid webhook ID", async () => {
const mockedWebhook: Webhook = {
id: "test-webhook-id",
url: "https://example.com",
name: "Test Webhook",
createdAt: new Date(),
updatedAt: new Date(),
source: "user",
environmentId: "test-environment-id",
triggers: [],
surveyIds: [],
};
vi.mocked(prisma.webhook.delete).mockResolvedValueOnce(mockedWebhook);
const deletedWebhook = await deleteWebhook("test-webhook-id");
expect(deletedWebhook).toEqual(mockedWebhook);
expect(prisma.webhook.delete).toHaveBeenCalledWith({
where: {
id: "test-webhook-id",
},
});
expect(webhookCache.revalidate).toHaveBeenCalled();
});
test("should delete the webhook and call webhookCache.revalidate with correct parameters", async () => {
const mockedWebhook: Webhook = {
id: "test-webhook-id",
url: "https://example.com",
name: "Test Webhook",
createdAt: new Date(),
updatedAt: new Date(),
source: "user",
environmentId: "test-environment-id",
triggers: [],
surveyIds: [],
};
vi.mocked(prisma.webhook.delete).mockResolvedValueOnce(mockedWebhook);
const deletedWebhook = await deleteWebhook("test-webhook-id");
expect(deletedWebhook).toEqual(mockedWebhook);
expect(prisma.webhook.delete).toHaveBeenCalledWith({
where: {
id: "test-webhook-id",
},
});
expect(webhookCache.revalidate).toHaveBeenCalledWith({
id: mockedWebhook.id,
environmentId: mockedWebhook.environmentId,
source: mockedWebhook.source,
});
});
test("should throw an error when called with an invalid webhook ID format", async () => {
const { validateInputs } = await import("@/lib/utils/validate");
(validateInputs as any).mockImplementation(() => {
throw new ValidationError("Validation failed");
});
await expect(deleteWebhook("invalid-id")).rejects.toThrow(ValidationError);
expect(prisma.webhook.delete).not.toHaveBeenCalled();
expect(webhookCache.revalidate).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,203 @@
import { createWebhook } from "@/app/api/v1/webhooks/lib/webhook";
import { TWebhookInput } from "@/app/api/v1/webhooks/types/webhooks";
import { webhookCache } from "@/lib/cache/webhook";
import { validateInputs } from "@/lib/utils/validate";
import { Prisma, WebhookSource } from "@prisma/client";
import { cleanup } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { DatabaseError, ValidationError } from "@formbricks/types/errors";
vi.mock("@formbricks/database", () => ({
prisma: {
webhook: {
create: vi.fn(),
},
},
}));
vi.mock("@/lib/cache/webhook", () => ({
webhookCache: {
revalidate: vi.fn(),
},
}));
vi.mock("@/lib/utils/validate", () => ({
validateInputs: vi.fn(),
}));
describe("createWebhook", () => {
afterEach(() => {
cleanup();
});
test("should create a webhook and revalidate the cache when provided with valid input data", async () => {
const webhookInput: TWebhookInput = {
environmentId: "test-env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user",
triggers: ["responseCreated"],
surveyIds: ["survey1", "survey2"],
};
const createdWebhook = {
id: "webhook-id",
environmentId: "test-env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user" as WebhookSource,
triggers: ["responseCreated"],
surveyIds: ["survey1", "survey2"],
createdAt: new Date(),
updatedAt: new Date(),
} as any;
vi.mocked(prisma.webhook.create).mockResolvedValueOnce(createdWebhook);
const result = await createWebhook(webhookInput);
expect(validateInputs).toHaveBeenCalled();
expect(prisma.webhook.create).toHaveBeenCalledWith({
data: {
url: webhookInput.url,
name: webhookInput.name,
source: webhookInput.source,
surveyIds: webhookInput.surveyIds,
triggers: webhookInput.triggers,
environment: {
connect: {
id: webhookInput.environmentId,
},
},
},
});
expect(webhookCache.revalidate).toHaveBeenCalledWith({
id: createdWebhook.id,
environmentId: createdWebhook.environmentId,
source: createdWebhook.source,
});
expect(result).toEqual(createdWebhook);
});
test("should throw a ValidationError if the input data does not match the ZWebhookInput schema", async () => {
const invalidWebhookInput = {
environmentId: "test-env-id",
name: "Test Webhook",
url: 123, // Invalid URL
source: "user" as WebhookSource,
triggers: ["responseCreated"],
surveyIds: ["survey1", "survey2"],
};
vi.mocked(validateInputs).mockImplementation(() => {
throw new ValidationError("Validation failed");
});
await expect(createWebhook(invalidWebhookInput as any)).rejects.toThrowError(ValidationError);
});
test("should throw a DatabaseError if a PrismaClientKnownRequestError occurs", async () => {
const webhookInput: TWebhookInput = {
environmentId: "test-env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user",
triggers: ["responseCreated"],
surveyIds: ["survey1", "survey2"],
};
vi.mocked(prisma.webhook.create).mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError("Test error", {
code: "P2002",
clientVersion: "5.0.0",
})
);
await expect(createWebhook(webhookInput)).rejects.toThrowError(DatabaseError);
});
test("should call webhookCache.revalidate with the correct parameters after successfully creating a webhook", async () => {
const webhookInput: TWebhookInput = {
environmentId: "env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user",
triggers: ["responseCreated"],
surveyIds: ["survey1"],
};
const createdWebhook = {
id: "webhook123",
environmentId: "env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user",
triggers: ["responseCreated"],
surveyIds: ["survey1"],
createdAt: new Date(),
updatedAt: new Date(),
} as any;
vi.mocked(prisma.webhook.create).mockResolvedValueOnce(createdWebhook);
await createWebhook(webhookInput);
expect(webhookCache.revalidate).toHaveBeenCalledWith({
id: createdWebhook.id,
environmentId: createdWebhook.environmentId,
source: createdWebhook.source,
});
});
test("should throw a DatabaseError when provided with invalid surveyIds", async () => {
const webhookInput: TWebhookInput = {
environmentId: "test-env-id",
name: "Test Webhook",
url: "https://example.com",
source: "user",
triggers: ["responseCreated"],
surveyIds: ["invalid-survey-id"],
};
vi.mocked(prisma.webhook.create).mockRejectedValueOnce(new Error("Foreign key constraint violation"));
await expect(createWebhook(webhookInput)).rejects.toThrowError(DatabaseError);
});
test("should handle edge case URLs that are technically valid but problematic", async () => {
const webhookInput: TWebhookInput = {
environmentId: "test-env-id",
name: "Test Webhook",
url: "http://localhost:3000", // Example of a potentially problematic URL
source: "user",
triggers: ["responseCreated"],
surveyIds: ["survey1", "survey2"],
};
vi.mocked(prisma.webhook.create).mockRejectedValueOnce(new DatabaseError("Invalid URL"));
await expect(createWebhook(webhookInput)).rejects.toThrowError(DatabaseError);
expect(validateInputs).toHaveBeenCalled();
expect(prisma.webhook.create).toHaveBeenCalledWith({
data: {
url: webhookInput.url,
name: webhookInput.name,
source: webhookInput.source,
surveyIds: webhookInput.surveyIds,
triggers: webhookInput.triggers,
environment: {
connect: {
id: webhookInput.environmentId,
},
},
},
});
expect(webhookCache.revalidate).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,81 @@
import { Organization } from "@prisma/client";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
import { getOrganizationBillingByEnvironmentId } from "./organization";
vi.mock("@formbricks/database", () => ({
prisma: {
organization: {
findFirst: vi.fn(),
},
},
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
vi.mock("@/lib/cache", () => ({
cache: (fn: any) => fn,
}));
vi.mock("@/lib/organization/cache", () => ({
organizationCache: {
tag: {
byEnvironmentId: (id: string) => `tag-${id}`,
},
},
}));
vi.mock("react", () => ({
cache: (fn: any) => fn,
}));
describe("getOrganizationBillingByEnvironmentId", () => {
const environmentId = "env-123";
const mockBillingData: Organization["billing"] = {
limits: {
monthly: { miu: 0, responses: 0 },
projects: 3,
},
period: "monthly",
periodStart: new Date(),
plan: "scale",
stripeCustomerId: "mock-stripe-customer-id",
};
test("returns billing when organization is found", async () => {
vi.mocked(prisma.organization.findFirst).mockResolvedValue({ billing: mockBillingData });
const result = await getOrganizationBillingByEnvironmentId(environmentId);
expect(result).toEqual(mockBillingData);
expect(prisma.organization.findFirst).toHaveBeenCalledWith({
where: {
projects: {
some: {
environments: {
some: {
id: environmentId,
},
},
},
},
},
select: {
billing: true,
},
});
});
test("returns null when organization is not found", async () => {
vi.mocked(prisma.organization.findFirst).mockResolvedValueOnce(null);
const result = await getOrganizationBillingByEnvironmentId(environmentId);
expect(result).toBeNull();
});
test("logs error and returns null on exception", async () => {
const error = new Error("db error");
vi.mocked(prisma.organization.findFirst).mockRejectedValueOnce(error);
const result = await getOrganizationBillingByEnvironmentId(environmentId);
expect(result).toBeNull();
expect(logger.error).toHaveBeenCalledWith(error, "Failed to get organization billing by environment ID");
});
});

View File

@@ -0,0 +1,45 @@
import { cache } from "@/lib/cache";
import { organizationCache } from "@/lib/organization/cache";
import { Organization } from "@prisma/client";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { logger } from "@formbricks/logger";
export const getOrganizationBillingByEnvironmentId = reactCache(
async (environmentId: string): Promise<Organization["billing"] | null> =>
cache(
async () => {
try {
const organization = await prisma.organization.findFirst({
where: {
projects: {
some: {
environments: {
some: {
id: environmentId,
},
},
},
},
},
select: {
billing: true,
},
});
if (!organization) {
return null;
}
return organization.billing;
} catch (error) {
logger.error(error, "Failed to get organization billing by environment ID");
return null;
}
},
[`api-v2-client-getOrganizationBillingByEnvironmentId-${environmentId}`],
{
tags: [organizationCache.tag.byEnvironmentId(environmentId)],
}
)()
);

View File

@@ -0,0 +1,110 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { logger } from "@formbricks/logger";
import { verifyRecaptchaToken } from "./recaptcha";
// Mock constants
vi.mock("@/lib/constants", () => ({
RECAPTCHA_SITE_KEY: "test-site-key",
RECAPTCHA_SECRET_KEY: "test-secret-key",
}));
// Mock logger
vi.mock("@formbricks/logger", () => ({
logger: {
warn: vi.fn(),
error: vi.fn(),
},
}));
describe("verifyRecaptchaToken", () => {
beforeEach(() => {
vi.resetAllMocks();
global.fetch = vi.fn();
});
afterEach(() => {
vi.useRealTimers();
});
test("returns true if site key or secret key is missing", async () => {
vi.doMock("@/lib/constants", () => ({
RECAPTCHA_SITE_KEY: undefined,
RECAPTCHA_SECRET_KEY: undefined,
}));
// Re-import to get new mocked values
const { verifyRecaptchaToken: verifyWithNoKeys } = await import("./recaptcha");
const result = await verifyWithNoKeys("token", 0.5);
expect(result).toBe(true);
expect(logger.warn).toHaveBeenCalledWith("reCAPTCHA verification skipped: keys not configured");
});
test("returns false if fetch response is not ok", async () => {
(global.fetch as any).mockResolvedValue({ ok: false });
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(false);
});
test("returns false if verification fails (data.success is false)", async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: false }),
});
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(false);
expect(logger.error).toHaveBeenCalledWith({ success: false }, "reCAPTCHA verification failed");
});
test("returns false if score is below or equal to threshold", async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, score: 0.3 }),
});
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(false);
expect(logger.error).toHaveBeenCalledWith(
{ success: true, score: 0.3 },
"reCAPTCHA score below threshold"
);
});
test("returns true if verification is successful and score is above threshold", async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, score: 0.9 }),
});
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(true);
});
test("returns true if verification is successful and score is undefined", async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
});
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(true);
});
test("returns false and logs error if fetch throws", async () => {
(global.fetch as any).mockRejectedValue(new Error("network error"));
const result = await verifyRecaptchaToken("token", 0.5);
expect(result).toBe(false);
expect(logger.error).toHaveBeenCalledWith(expect.any(Error), "Error verifying reCAPTCHA token");
});
test("aborts fetch after timeout", async () => {
vi.useFakeTimers();
let abortCalled = false;
const abortController = {
abort: () => {
abortCalled = true;
},
signal: {},
};
vi.spyOn(global, "AbortController").mockImplementation(() => abortController as any);
(global.fetch as any).mockImplementation(() => new Promise(() => {}));
verifyRecaptchaToken("token", 0.5);
vi.advanceTimersByTime(5000);
expect(abortCalled).toBe(true);
});
});

View File

@@ -0,0 +1,62 @@
import { RECAPTCHA_SECRET_KEY, RECAPTCHA_SITE_KEY } from "@/lib/constants";
import { logger } from "@formbricks/logger";
/**
* Verifies a reCAPTCHA token with Google's reCAPTCHA API
* @param token The reCAPTCHA token to verify
* @param threshold The minimum score threshold (0.0 to 1.0)
* @returns A promise that resolves to true if the verification is successful and the score meets the threshold, false otherwise
*/
export const verifyRecaptchaToken = async (token: string, threshold: number): Promise<boolean> => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
try {
// If keys aren't configured, skip verification
if (!RECAPTCHA_SITE_KEY || !RECAPTCHA_SECRET_KEY) {
logger.warn("reCAPTCHA verification skipped: keys not configured");
return true;
}
// Build URL-encoded form data
const params = new URLSearchParams();
params.append("secret", RECAPTCHA_SECRET_KEY);
params.append("response", token);
// POST to Googles siteverify endpoint
const response = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: params.toString(),
signal: controller.signal,
});
if (!response.ok) {
logger.error(`reCAPTCHA HTTP error: ${response.status}`);
return false;
}
const data = await response.json();
// Check if verification was successful
if (!data.success) {
logger.error(data, "reCAPTCHA verification failed");
return false;
}
// Check if the score meets the threshold
if (data.score !== undefined && data.score < threshold) {
logger.error(data, "reCAPTCHA score below threshold");
return false;
}
return true;
} catch (error) {
logger.error(error, "Error verifying reCAPTCHA token");
return false;
} finally {
clearTimeout(timeoutId);
}
};

View File

@@ -0,0 +1,203 @@
import { getOrganizationBillingByEnvironmentId } from "@/app/api/v2/client/[environmentId]/responses/lib/organization";
import { verifyRecaptchaToken } from "@/app/api/v2/client/[environmentId]/responses/lib/recaptcha";
import { checkSurveyValidity } from "@/app/api/v2/client/[environmentId]/responses/lib/utils";
import { TResponseInputV2 } from "@/app/api/v2/client/[environmentId]/responses/types/response";
import { responses } from "@/app/lib/api/response";
import { getIsSpamProtectionEnabled } from "@/modules/ee/license-check/lib/utils";
import { Organization } from "@prisma/client";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { logger } from "@formbricks/logger";
import { TSurvey } from "@formbricks/types/surveys/types";
vi.mock("@/app/api/v2/client/[environmentId]/responses/lib/recaptcha", () => ({
verifyRecaptchaToken: vi.fn(),
}));
vi.mock("@/app/lib/api/response", () => ({
responses: {
badRequestResponse: vi.fn((message) => new Response(message, { status: 400 })),
notFoundResponse: vi.fn((message) => new Response(message, { status: 404 })),
},
}));
vi.mock("@/modules/ee/license-check/lib/utils", () => ({
getIsSpamProtectionEnabled: vi.fn(),
}));
vi.mock("@/app/api/v2/client/[environmentId]/responses/lib/organization", () => ({
getOrganizationBillingByEnvironmentId: vi.fn(),
}));
vi.mock("@formbricks/logger", () => ({
logger: {
error: vi.fn(),
},
}));
const mockSurvey: TSurvey = {
id: "survey-1",
createdAt: new Date(),
updatedAt: new Date(),
name: "Test Survey",
environmentId: "env-1",
type: "link",
status: "inProgress",
questions: [],
displayOption: "displayOnce",
recontactDays: null,
autoClose: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,
autoComplete: null,
singleUse: null,
triggers: [],
languages: [],
pin: null,
resultShareKey: null,
segment: null,
styling: null,
surveyClosedMessage: null,
hiddenFields: { enabled: false },
welcomeCard: { enabled: false, showResponseCount: false, timeToFinish: false },
variables: [],
createdBy: null,
recaptcha: { enabled: false, threshold: 0.5 },
displayLimit: null,
endings: [],
followUps: [],
isBackButtonHidden: false,
isSingleResponsePerEmailEnabled: false,
isVerifyEmailEnabled: false,
projectOverwrites: null,
runOnDate: null,
showLanguageSwitch: false,
};
const mockResponseInput: TResponseInputV2 = {
surveyId: "survey-1",
environmentId: "env-1",
data: {},
finished: false,
ttc: {},
meta: {},
};
const mockBillingData: Organization["billing"] = {
limits: {
monthly: { miu: 0, responses: 0 },
projects: 3,
},
period: "monthly",
periodStart: new Date(),
plan: "scale",
stripeCustomerId: "mock-stripe-customer-id",
};
describe("checkSurveyValidity", () => {
beforeEach(() => {
vi.resetAllMocks();
});
test("should return badRequestResponse if survey environmentId does not match", async () => {
const survey = { ...mockSurvey, environmentId: "env-2" };
const result = await checkSurveyValidity(survey, "env-1", mockResponseInput);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(400);
expect(responses.badRequestResponse).toHaveBeenCalledWith(
"Survey is part of another environment",
{
"survey.environmentId": "env-2",
environmentId: "env-1",
},
true
);
});
test("should return null if recaptcha is not enabled", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: false, threshold: 0.5 } };
const result = await checkSurveyValidity(survey, "env-1", mockResponseInput);
expect(result).toBeNull();
});
test("should return badRequestResponse if recaptcha enabled, spam protection enabled, but token is missing", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: true, threshold: 0.5 } };
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
const responseInputWithoutToken = { ...mockResponseInput };
delete responseInputWithoutToken.recaptchaToken;
const result = await checkSurveyValidity(survey, "env-1", responseInputWithoutToken);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(400);
expect(logger.error).toHaveBeenCalledWith("Missing recaptcha token");
expect(responses.badRequestResponse).toHaveBeenCalledWith(
"Missing recaptcha token",
{ code: "recaptcha_verification_failed" },
true
);
});
test("should return not found response if billing data is not found", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: true, threshold: 0.5 } };
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
vi.mocked(getOrganizationBillingByEnvironmentId).mockResolvedValue(null);
const result = await checkSurveyValidity(survey, "env-1", {
...mockResponseInput,
recaptchaToken: "test-token",
});
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(404);
expect(responses.notFoundResponse).toHaveBeenCalledWith("Organization", null);
expect(getOrganizationBillingByEnvironmentId).toHaveBeenCalledWith("env-1");
});
test("should return null if recaptcha is enabled but spam protection is disabled", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: true, threshold: 0.5 } };
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(false);
vi.mocked(verifyRecaptchaToken).mockResolvedValue(true);
vi.mocked(getOrganizationBillingByEnvironmentId).mockResolvedValue(mockBillingData);
const result = await checkSurveyValidity(survey, "env-1", {
...mockResponseInput,
recaptchaToken: "test-token",
});
expect(result).toBeNull();
expect(logger.error).toHaveBeenCalledWith("Spam protection is not enabled for this organization");
});
test("should return badRequestResponse if recaptcha verification fails", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: true, threshold: 0.5 } };
const responseInputWithToken = { ...mockResponseInput, recaptchaToken: "test-token" };
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
vi.mocked(verifyRecaptchaToken).mockResolvedValue(false);
vi.mocked(getOrganizationBillingByEnvironmentId).mockResolvedValue(mockBillingData);
const result = await checkSurveyValidity(survey, "env-1", responseInputWithToken);
expect(result).toBeInstanceOf(Response);
expect(result?.status).toBe(400);
expect(verifyRecaptchaToken).toHaveBeenCalledWith("test-token", 0.5);
expect(responses.badRequestResponse).toHaveBeenCalledWith(
"reCAPTCHA verification failed",
{ code: "recaptcha_verification_failed" },
true
);
});
test("should return null if recaptcha verification passes", async () => {
const survey = { ...mockSurvey, recaptcha: { enabled: true, threshold: 0.5 } };
const responseInputWithToken = { ...mockResponseInput, recaptchaToken: "test-token" };
vi.mocked(getIsSpamProtectionEnabled).mockResolvedValue(true);
vi.mocked(verifyRecaptchaToken).mockResolvedValue(true);
vi.mocked(getOrganizationBillingByEnvironmentId).mockResolvedValue(mockBillingData);
const result = await checkSurveyValidity(survey, "env-1", responseInputWithToken);
expect(result).toBeNull();
expect(verifyRecaptchaToken).toHaveBeenCalledWith("test-token", 0.5);
});
test("should return null for a valid survey and input", async () => {
const survey = { ...mockSurvey }; // Recaptcha disabled by default in mock
const result = await checkSurveyValidity(survey, "env-1", mockResponseInput);
expect(result).toBeNull();
});
});

View File

@@ -0,0 +1,63 @@
import { getOrganizationBillingByEnvironmentId } from "@/app/api/v2/client/[environmentId]/responses/lib/organization";
import { verifyRecaptchaToken } from "@/app/api/v2/client/[environmentId]/responses/lib/recaptcha";
import { TResponseInputV2 } from "@/app/api/v2/client/[environmentId]/responses/types/response";
import { responses } from "@/app/lib/api/response";
import { getIsSpamProtectionEnabled } from "@/modules/ee/license-check/lib/utils";
import { logger } from "@formbricks/logger";
import { TSurvey } from "@formbricks/types/surveys/types";
export const RECAPTCHA_VERIFICATION_ERROR_CODE = "recaptcha_verification_failed";
export const checkSurveyValidity = async (
survey: TSurvey,
environmentId: string,
responseInput: TResponseInputV2
): Promise<Response | null> => {
if (survey.environmentId !== environmentId) {
return responses.badRequestResponse(
"Survey is part of another environment",
{
"survey.environmentId": survey.environmentId,
environmentId,
},
true
);
}
if (survey.recaptcha?.enabled) {
if (!responseInput.recaptchaToken) {
logger.error("Missing recaptcha token");
return responses.badRequestResponse(
"Missing recaptcha token",
{
code: RECAPTCHA_VERIFICATION_ERROR_CODE,
},
true
);
}
const billing = await getOrganizationBillingByEnvironmentId(environmentId);
if (!billing) {
return responses.notFoundResponse("Organization", null);
}
const isSpamProtectionEnabled = await getIsSpamProtectionEnabled(billing.plan);
if (!isSpamProtectionEnabled) {
logger.error("Spam protection is not enabled for this organization");
}
const isPassed = await verifyRecaptchaToken(responseInput.recaptchaToken, survey.recaptcha.threshold);
if (!isPassed) {
return responses.badRequestResponse(
"reCAPTCHA verification failed",
{
code: RECAPTCHA_VERIFICATION_ERROR_CODE,
},
true
);
}
}
return null;
};

View File

@@ -1,3 +1,4 @@
import { checkSurveyValidity } from "@/app/api/v2/client/[environmentId]/responses/lib/utils";
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { sendToPipeline } from "@/app/lib/pipelines";
@@ -74,18 +75,10 @@ export const POST = async (request: Request, context: Context): Promise<Response
// get and check survey
const survey = await getSurvey(responseInputData.surveyId);
if (!survey) {
return responses.notFoundResponse("Survey", responseInputData.surveyId, true);
}
if (survey.environmentId !== environmentId) {
return responses.badRequestResponse(
"Survey is part of another environment",
{
"survey.environmentId": survey.environmentId,
environmentId,
},
true
);
return responses.notFoundResponse("Survey", responseInput.surveyId, true);
}
const surveyCheckResult = await checkSurveyValidity(survey, environmentId, responseInput);
if (surveyCheckResult) return surveyCheckResult;
let response: TResponse;
try {
@@ -108,15 +101,14 @@ export const POST = async (request: Request, context: Context): Promise<Response
} catch (error) {
if (error instanceof InvalidInputError) {
return responses.badRequestResponse(error.message);
} else {
logger.error({ error, url: request.url }, "Error creating response");
return responses.internalServerErrorResponse(error.message);
}
logger.error({ error, url: request.url }, "Error creating response");
return responses.internalServerErrorResponse(error.message);
}
sendToPipeline({
event: "responseCreated",
environmentId: survey.environmentId,
environmentId,
surveyId: response.surveyId,
response: response,
});
@@ -124,13 +116,13 @@ export const POST = async (request: Request, context: Context): Promise<Response
if (responseInput.finished) {
sendToPipeline({
event: "responseFinished",
environmentId: survey.environmentId,
environmentId,
surveyId: response.surveyId,
response: response,
});
}
await capturePosthogEnvironmentEvent(survey.environmentId, "response created", {
await capturePosthogEnvironmentEvent(environmentId, "response created", {
surveyId: response.surveyId,
surveyType: survey.type,
});

View File

@@ -2,5 +2,8 @@ import { z } from "zod";
import { ZId } from "@formbricks/types/common";
import { ZResponseInput } from "@formbricks/types/responses";
export const ZResponseInputV2 = ZResponseInput.omit({ userId: true }).extend({ contactId: ZId.nullish() });
export const ZResponseInputV2 = ZResponseInput.omit({ userId: true }).extend({
contactId: ZId.nullish(),
recaptchaToken: z.string().nullish(),
});
export type TResponseInputV2 = z.infer<typeof ZResponseInputV2>;

View File

@@ -3567,6 +3567,7 @@ export const previewSurvey = (projectName: string, t: TFnType) => {
displayLimit: null,
autoClose: null,
runOnDate: null,
recaptcha: null,
closeOnDate: null,
delay: 0,
displayPercentage: null,

View File

@@ -273,6 +273,10 @@ export const TURNSTILE_SECRET_KEY = env.TURNSTILE_SECRET_KEY;
export const TURNSTILE_SITE_KEY = env.TURNSTILE_SITE_KEY;
export const IS_TURNSTILE_CONFIGURED = Boolean(env.TURNSTILE_SITE_KEY && TURNSTILE_SECRET_KEY);
export const RECAPTCHA_SITE_KEY = env.RECAPTCHA_SITE_KEY;
export const RECAPTCHA_SECRET_KEY = env.RECAPTCHA_SECRET_KEY;
export const IS_RECAPTCHA_CONFIGURED = Boolean(RECAPTCHA_SITE_KEY && RECAPTCHA_SECRET_KEY);
export const IS_PRODUCTION = env.NODE_ENV === "production";
export const IS_DEVELOPMENT = env.NODE_ENV === "development";

View File

@@ -102,6 +102,8 @@ export const env = createEnv({
.or(z.string().refine((str) => str === "")),
TURNSTILE_SECRET_KEY: z.string().optional(),
TURNSTILE_SITE_KEY: z.string().optional(),
RECAPTCHA_SITE_KEY: z.string().optional(),
RECAPTCHA_SECRET_KEY: z.string().optional(),
UPLOADS_DIR: z.string().min(1).optional(),
VERCEL_URL: z.string().optional(),
WEBAPP_URL: z.string().url().optional(),
@@ -198,6 +200,8 @@ export const env = createEnv({
TELEMETRY_DISABLED: process.env.TELEMETRY_DISABLED,
TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY,
TURNSTILE_SITE_KEY: process.env.TURNSTILE_SITE_KEY,
RECAPTCHA_SITE_KEY: process.env.RECAPTCHA_SITE_KEY,
RECAPTCHA_SECRET_KEY: process.env.RECAPTCHA_SECRET_KEY,
TERMS_URL: process.env.TERMS_URL,
UPLOADS_DIR: process.env.UPLOADS_DIR,
VERCEL_URL: process.env.VERCEL_URL,

View File

@@ -309,6 +309,7 @@ export const mockSurvey: TSurvey = {
isVerifyEmailEnabled: false,
projectOverwrites: null,
styling: null,
recaptcha: null,
surveyClosedMessage: null,
singleUse: {
enabled: false,

View File

@@ -512,6 +512,7 @@ export const mockSurvey: TSurvey = {
autoComplete: null,
isVerifyEmailEnabled: false,
projectOverwrites: null,
recaptcha: null,
styling: null,
surveyClosedMessage: null,
singleUse: {

View File

@@ -69,7 +69,7 @@ export const processResponseData = (
if (Array.isArray(responseData)) {
responseData = responseData
.filter((item) => item !== null && item !== undefined && item !== "")
.join(", ");
.join("; ");
return responseData;
} else {
const formattedString = Object.entries(responseData)

View File

@@ -63,6 +63,7 @@ export const selectSurvey = {
pin: true,
resultShareKey: true,
showLanguageSwitch: true,
recaptcha: true,
languages: {
select: {
default: true,

View File

@@ -254,6 +254,7 @@ export const mockSyncSurveyOutput: SurveyMock = {
projectOverwrites: null,
singleUse: null,
styling: null,
recaptcha: null,
displayPercentage: null,
createdBy: null,
pin: null,
@@ -276,6 +277,7 @@ export const mockSurveyOutput: SurveyMock = {
displayOption: "respondMultiple",
triggers: [{ actionClass: mockActionClass }],
projectOverwrites: null,
recaptcha: null,
singleUse: null,
styling: null,
displayPercentage: null,
@@ -312,6 +314,7 @@ export const updateSurveyInput: TSurvey = {
displayPercentage: null,
createdBy: null,
pin: null,
recaptcha: null,
resultShareKey: null,
segment: null,
languages: [],

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "Login mit Azure",
"continue_with_azure": "Weiter mit Microsoft",
"continue_with_email": "Login mit E-Mail",
"continue_with_github": "Login mit GitHub",
"continue_with_google": "Login mit Google",
@@ -1366,6 +1366,8 @@
"edit_translations": "{lang} -Übersetzungen bearbeiten",
"enable_encryption_of_single_use_id_suid_in_survey_url": "Single Use Id (suId) in der Umfrage-URL verschlüsseln.",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "Teilnehmer können die Umfragesprache jederzeit während der Umfrage ändern.",
"enable_recaptcha_to_protect_your_survey_from_spam": "Spamschutz verwendet reCAPTCHA v3, um Spam-Antworten herauszufiltern.",
"enable_spam_protection": "Spamschutz",
"end_screen_card": "Abschluss-Karte",
"ending_card": "Abschluss-Karte",
"ending_card_used_in_logic": "Diese Abschlusskarte wird in der Logik der Frage {questionIndex} verwendet.",
@@ -1569,8 +1571,12 @@
"simple": "Einfach",
"single_use_survey_links": "Einmalige Umfragelinks",
"single_use_survey_links_description": "Erlaube nur eine Antwort pro Umfragelink.",
"six_points": "6 Punkte",
"skip_button_label": "Überspringen-Button-Beschriftung",
"smiley": "Smiley",
"spam_protection_note": "Spamschutz funktioniert nicht für Umfragen, die mit den iOS-, React Native- und Android-SDKs angezeigt werden. Es wird die Umfrage unterbrechen.",
"spam_protection_threshold_description": "Wert zwischen 0 und 1 festlegen, Antworten unter diesem Wert werden abgelehnt.",
"spam_protection_threshold_heading": "Antwortschwelle",
"star": "Stern",
"starts_with": "Fängt an mit",
"state": "Bundesland",

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "Continue with Azure",
"continue_with_azure": "Continue with Microsoft",
"continue_with_email": "Continue with Email",
"continue_with_github": "Continue with GitHub",
"continue_with_google": "Continue with Google",
@@ -1366,6 +1366,8 @@
"edit_translations": "Edit {lang} translations",
"enable_encryption_of_single_use_id_suid_in_survey_url": "Enable encryption of Single Use Id (suId) in survey URL.",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "Enable participants to switch the survey language at any point during the survey.",
"enable_recaptcha_to_protect_your_survey_from_spam": "Spam protection uses reCAPTCHA v3 to filter out the spam responses.",
"enable_spam_protection": "Spam protection",
"end_screen_card": "End screen card",
"ending_card": "Ending card",
"ending_card_used_in_logic": "This ending card is used in logic of question {questionIndex}.",
@@ -1569,8 +1571,12 @@
"simple": "Simple",
"single_use_survey_links": "Single-use survey links",
"single_use_survey_links_description": "Allow only 1 response per survey link.",
"six_points": "6 points",
"skip_button_label": "Skip Button Label",
"smiley": "Smiley",
"spam_protection_note": "Spam protection does not work for surveys displayed with the iOS, React Native, and Android SDKs. It will break the survey.",
"spam_protection_threshold_description": "Set value between 0 and 1, responses below this value will be rejected.",
"spam_protection_threshold_heading": "Response threshold",
"star": "Star",
"starts_with": "Starts with",
"state": "State",

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "Continuer avec Azure",
"continue_with_azure": "Continuer avec Microsoft",
"continue_with_email": "Continuer avec l'e-mail",
"continue_with_github": "Continuer avec GitHub",
"continue_with_google": "Continuer avec Google",
@@ -1366,6 +1366,8 @@
"edit_translations": "Modifier les traductions {lang}",
"enable_encryption_of_single_use_id_suid_in_survey_url": "Activer le chiffrement de l'identifiant à usage unique (suId) dans l'URL de l'enquête.",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "Permettre aux participants de changer la langue de l'enquête à tout moment pendant celle-ci.",
"enable_recaptcha_to_protect_your_survey_from_spam": "La protection contre le spam utilise reCAPTCHA v3 pour filtrer les réponses indésirables.",
"enable_spam_protection": "Protection contre le spam",
"end_screen_card": "Carte de fin d'écran",
"ending_card": "Carte de fin",
"ending_card_used_in_logic": "Cette carte de fin est utilisée dans la logique de la question '{'questionIndex'}'.",
@@ -1569,8 +1571,12 @@
"simple": "Simple",
"single_use_survey_links": "Liens d'enquête à usage unique",
"single_use_survey_links_description": "Autoriser uniquement 1 réponse par lien d'enquête.",
"six_points": "6 points",
"skip_button_label": "Étiquette du bouton Ignorer",
"smiley": "Sourire",
"spam_protection_note": "La protection contre le spam ne fonctionne pas pour les enquêtes affichées avec les SDK iOS, React Native et Android. Cela cassera l'enquête.",
"spam_protection_threshold_description": "Définir une valeur entre 0 et 1, les réponses en dessous de cette valeur seront rejetées.",
"spam_protection_threshold_heading": "Seuil de réponse",
"star": "Étoile",
"starts_with": "Commence par",
"state": "État",

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "Continuar com Azure",
"continue_with_azure": "Continuar com Microsoft",
"continue_with_email": "Continuar com o Email",
"continue_with_github": "Continuar com o GitHub",
"continue_with_google": "Continuar com o Google",
@@ -1366,6 +1366,8 @@
"edit_translations": "Editar traduções de {lang}",
"enable_encryption_of_single_use_id_suid_in_survey_url": "Habilitar criptografia do Id de Uso Único (suId) na URL da pesquisa.",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "Permitir que os participantes mudem o idioma da pesquisa a qualquer momento durante a pesquisa.",
"enable_recaptcha_to_protect_your_survey_from_spam": "A proteção contra spam usa o reCAPTCHA v3 para filtrar as respostas de spam.",
"enable_spam_protection": "Proteção contra spam",
"end_screen_card": "cartão de tela final",
"ending_card": "Cartão de encerramento",
"ending_card_used_in_logic": "Esse cartão de encerramento é usado na lógica da pergunta {questionIndex}.",
@@ -1569,8 +1571,12 @@
"simple": "Simples",
"single_use_survey_links": "Links de pesquisa de uso único",
"single_use_survey_links_description": "Permitir apenas 1 resposta por link da pesquisa.",
"six_points": "6 pontos",
"skip_button_label": "Botão de Pular",
"smiley": "Sorridente",
"spam_protection_note": "A proteção contra spam não funciona para pesquisas exibidas com os SDKs iOS, React Native e Android. Isso vai quebrar a pesquisa.",
"spam_protection_threshold_description": "Defina um valor entre 0 e 1, respostas abaixo desse valor serão rejeitadas.",
"spam_protection_threshold_heading": "Limite de resposta",
"star": "Estrela",
"starts_with": "Começa com",
"state": "Estado",

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "Continuar com Azure",
"continue_with_azure": "Continuar com Microsoft",
"continue_with_email": "Continuar com Email",
"continue_with_github": "Continuar com GitHub",
"continue_with_google": "Continuar com Google",
@@ -1366,6 +1366,8 @@
"edit_translations": "Editar traduções {lang}",
"enable_encryption_of_single_use_id_suid_in_survey_url": "Ativar encriptação do Id de Uso Único (suId) no URL do inquérito.",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "Permitir aos participantes mudar a língua do inquérito a qualquer momento durante o inquérito.",
"enable_recaptcha_to_protect_your_survey_from_spam": "A proteção contra spam usa o reCAPTCHA v3 para filtrar as respostas de spam.",
"enable_spam_protection": "Proteção contra spam",
"end_screen_card": "Cartão de ecrã final",
"ending_card": "Cartão de encerramento",
"ending_card_used_in_logic": "Este cartão final é usado na lógica da pergunta {questionIndex}.",
@@ -1569,8 +1571,12 @@
"simple": "Simples",
"single_use_survey_links": "Links de inquérito de uso único",
"single_use_survey_links_description": "Permitir apenas 1 resposta por link de inquérito.",
"six_points": "6 pontos",
"skip_button_label": "Rótulo do botão Ignorar",
"smiley": "Sorridente",
"spam_protection_note": "A proteção contra spam não funciona para inquéritos exibidos com os SDKs iOS, React Native e Android. Isso irá quebrar o inquérito.",
"spam_protection_threshold_description": "Defina um valor entre 0 e 1, respostas abaixo deste valor serão rejeitadas.",
"spam_protection_threshold_heading": "Limite de resposta",
"star": "Estrela",
"starts_with": "Começa com",
"state": "Estado",

View File

@@ -1,6 +1,6 @@
{
"auth": {
"continue_with_azure": "使用 Azure 繼續",
"continue_with_azure": "繼續使用 Microsoft",
"continue_with_email": "使用電子郵件繼續",
"continue_with_github": "使用 GitHub 繼續",
"continue_with_google": "使用 Google 繼續",
@@ -1366,6 +1366,8 @@
"edit_translations": "編輯 '{'language'}' 翻譯",
"enable_encryption_of_single_use_id_suid_in_survey_url": "啟用問卷網址中單次使用 ID (suId) 的加密。",
"enable_participants_to_switch_the_survey_language_at_any_point_during_the_survey": "允許參與者在問卷中的任何時間點切換問卷語言。",
"enable_recaptcha_to_protect_your_survey_from_spam": "垃圾郵件保護使用 reCAPTCHA v3 過濾垃圾回應。",
"enable_spam_protection": "垃圾郵件保護",
"end_screen_card": "結束畫面卡片",
"ending_card": "結尾卡片",
"ending_card_used_in_logic": "此結尾卡片用於問題 '{'questionIndex'}' 的邏輯中。",
@@ -1569,8 +1571,12 @@
"simple": "簡單",
"single_use_survey_links": "單次使用問卷連結",
"single_use_survey_links_description": "每個問卷連結只允許 1 個回應。",
"six_points": "6 分",
"skip_button_label": "「跳過」按鈕標籤",
"smiley": "表情符號",
"spam_protection_note": "垃圾郵件保護不適用於使用 iOS、React Native 和 Android SDK 顯示的問卷。它會破壞問卷。",
"spam_protection_threshold_description": "設置值在 0 和 1 之間,低於此值的回應將被拒絕。",
"spam_protection_threshold_heading": "回應閾值",
"star": "星形",
"starts_with": "開頭為",
"state": "州/省",

View File

@@ -40,27 +40,47 @@ const getSmiley = (iconIdx: number, idx: number, range: number, active: boolean,
const inactiveColor = addColors ? getSmileyColor(range, idx) : "fill-none";
const icons = [
<TiredFace className={active ? activeColor : inactiveColor} data-testid="TiredFace" />,
<WearyFace className={active ? activeColor : inactiveColor} data-testid="WearyFace" />,
<PerseveringFace className={active ? activeColor : inactiveColor} data-testid="PerseveringFace" />,
<FrowningFace className={active ? activeColor : inactiveColor} data-testid="FrowningFace" />,
<ConfusedFace className={active ? activeColor : inactiveColor} data-testid="ConfusedFace" />,
<NeutralFace className={active ? activeColor : inactiveColor} data-testid="NeutralFace" />,
<TiredFace className={active ? activeColor : inactiveColor} data-testid="TiredFace" key="tired-face" />,
<WearyFace className={active ? activeColor : inactiveColor} data-testid="WearyFace" key="weary-face" />,
<PerseveringFace
className={active ? activeColor : inactiveColor}
data-testid="PerseveringFace"
key="perserving-face"
/>,
<FrowningFace
className={active ? activeColor : inactiveColor}
data-testid="FrowningFace"
key="frowning-face"
/>,
<ConfusedFace
className={active ? activeColor : inactiveColor}
data-testid="ConfusedFace"
key="confused-face"
/>,
<NeutralFace
className={active ? activeColor : inactiveColor}
data-testid="NeutralFace"
key="neutral-face"
/>,
<SlightlySmilingFace
className={active ? activeColor : inactiveColor}
data-testid="SlightlySmilingFace"
key="slightly-smiling-face"
/>,
<SmilingFaceWithSmilingEyes
className={active ? activeColor : inactiveColor}
data-testid="SmilingFaceWithSmilingEyes"
key="smiling-face-with-smiling-eyes"
/>,
<GrinningFaceWithSmilingEyes
className={active ? activeColor : inactiveColor}
data-testid="GrinningFaceWithSmilingEyes"
key="grinning-face-with-smiling-eyes"
/>,
<GrinningSquintingFace
className={active ? activeColor : inactiveColor}
data-testid="GrinningSquintingFace"
key="grinning-squinting-face"
/>,
];
@@ -71,6 +91,7 @@ export const RatingSmiley = ({ active, idx, range, addColors = false }: RatingSm
let iconsIdx: number[] = [];
if (range === 10) iconsIdx = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
else if (range === 7) iconsIdx = [1, 3, 4, 5, 6, 8, 9];
else if (range === 6) iconsIdx = [0, 2, 4, 5, 7, 9];
else if (range === 5) iconsIdx = [3, 4, 5, 6, 7];
else if (range === 4) iconsIdx = [4, 5, 6, 7];
else if (range === 3) iconsIdx = [4, 5, 7];

View File

@@ -466,7 +466,6 @@ describe("SegmentSettings", () => {
expect(updatedSaveButton.getAttribute("data-loading")).not.toBe("true");
});
// [Tusk] FAILING TEST
test("should add a filter to the segment when a valid filter is selected in the filter modal", async () => {
// Render component
render(<SegmentSettings {...mockProps} />);

View File

@@ -4,6 +4,7 @@ import {
E2E_TESTING,
ENTERPRISE_LICENSE_KEY,
IS_FORMBRICKS_CLOUD,
IS_RECAPTCHA_CONFIGURED,
PROJECT_FEATURE_KEYS,
} from "@/lib/constants";
import { env } from "@/lib/env";
@@ -89,6 +90,7 @@ const fetchLicenseForE2ETesting = async (): Promise<{
projects: 3,
whitelabel: true,
removeBranding: true,
spamProtection: true,
ai: true,
saml: true,
},
@@ -158,6 +160,7 @@ export const getEnterpriseLicense = async (): Promise<{
contacts: false,
ai: false,
saml: false,
spamProtection: false,
},
lastChecked: new Date(),
};
@@ -388,6 +391,23 @@ export const getIsSamlSsoEnabled = async (): Promise<boolean> => {
return licenseFeatures.sso && licenseFeatures.saml;
};
export const getIsSpamProtectionEnabled = async (
billingPlan: Organization["billing"]["plan"]
): Promise<boolean> => {
if (!IS_RECAPTCHA_CONFIGURED) return false;
if (E2E_TESTING) {
const previousResult = await fetchLicenseForE2ETesting();
return previousResult?.features ? previousResult.features.spamProtection : false;
}
if (IS_FORMBRICKS_CLOUD)
return billingPlan === PROJECT_FEATURE_KEYS.SCALE || billingPlan === PROJECT_FEATURE_KEYS.ENTERPRISE;
const licenseFeatures = await getLicenseFeatures();
if (!licenseFeatures) return false;
return licenseFeatures.spamProtection;
};
export const getOrganizationProjectsLimit = async (
limits: Organization["billing"]["limits"]
): Promise<number> => {

View File

@@ -13,6 +13,7 @@ const ZEnterpriseLicenseFeatures = z.object({
twoFactorAuth: z.boolean(),
sso: z.boolean(),
saml: z.boolean(),
spamProtection: z.boolean(),
ai: z.boolean(),
});

View File

@@ -4,7 +4,6 @@ import { checkAuthorizationUpdated } from "@/lib/utils/action-client-middleware"
import { getRoleManagementPermission } from "@/modules/ee/license-check/lib/utils";
import {
TUpdateInviteAction,
TUpdateMembershipAction,
checkRoleManagementPermission,
updateInviteAction,
updateMembershipAction,
@@ -215,7 +214,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "member" },
},
} as unknown as TUpdateMembershipAction)
} as any)
).rejects.toThrow(new AuthenticationError("User not a member of this organization"));
});
@@ -231,7 +230,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "member" },
},
} as unknown as TUpdateMembershipAction)
} as any)
).rejects.toThrow(new OperationNotAllowedError("User management is disabled"));
});
@@ -248,7 +247,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "billing" },
},
} as unknown as TUpdateMembershipAction)
} as any)
).rejects.toThrow(new ValidationError("Billing role is not allowed"));
});
@@ -268,7 +267,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "billing" },
},
} as unknown as TUpdateMembershipAction);
} as any);
expect(result).toEqual({ id: "membership-123", role: "billing" });
});
@@ -286,7 +285,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "owner" },
},
} as unknown as TUpdateMembershipAction)
} as any)
).rejects.toThrow(new OperationNotAllowedError("Managers can only assign users to the member role"));
});
@@ -305,7 +304,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "member" },
},
} as unknown as TUpdateMembershipAction);
} as any);
expect(result).toEqual({ id: "membership-123", role: "member" });
expect(updateMembership).toHaveBeenCalledWith("user-456", "org-123", { role: "member" });
@@ -326,7 +325,7 @@ describe("Role Management Actions", () => {
organizationId: "org-123",
data: { role: "member" },
},
} as unknown as TUpdateMembershipAction);
} as any);
expect(result).toEqual({ id: "membership-123", role: "member" });
expect(updateMembership).toHaveBeenCalledWith("user-456", "org-123", { role: "member" });

View File

@@ -11,8 +11,7 @@ import { updateMembership } from "@/modules/ee/role-management/lib/membership";
import { ZInviteUpdateInput } from "@/modules/ee/role-management/types/invites";
import { z } from "zod";
import { ZId, ZUuid } from "@formbricks/types/common";
import { OperationNotAllowedError, ValidationError } from "@formbricks/types/errors";
import { AuthenticationError } from "@formbricks/types/errors";
import { AuthenticationError, OperationNotAllowedError, ValidationError } from "@formbricks/types/errors";
import { ZMembershipUpdateInput } from "@formbricks/types/memberships";
export const checkRoleManagementPermission = async (organizationId: string) => {

View File

@@ -6,6 +6,7 @@ import { getOrganizationIdFromEnvironmentId, getProjectIdFromEnvironmentId } fro
import { checkMultiLanguagePermission } from "@/modules/ee/multi-language-surveys/lib/actions";
import { createSurvey } from "@/modules/survey/components/template-list/lib/survey";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { checkSpamProtectionPermission } from "@/modules/survey/lib/permission";
import { getOrganizationBilling } from "@/modules/survey/lib/survey";
import { z } from "zod";
import { OperationNotAllowedError, ResourceNotFoundError } from "@formbricks/types/errors";
@@ -56,6 +57,10 @@ export const createSurveyAction = authenticatedActionClient
],
});
if (parsedInput.surveyBody.recaptcha?.enabled) {
await checkSpamProtectionPermission(organizationId);
}
if (parsedInput.surveyBody.followUps?.length) {
await checkSurveyFollowUpsPermission(organizationId);
}

View File

@@ -14,6 +14,7 @@ import { checkMultiLanguagePermission } from "@/modules/ee/multi-language-survey
import { createActionClass } from "@/modules/survey/editor/lib/action-class";
import { updateSurvey } from "@/modules/survey/editor/lib/survey";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { checkSpamProtectionPermission } from "@/modules/survey/lib/permission";
import { getOrganizationBilling } from "@/modules/survey/lib/survey";
import { z } from "zod";
import { ZActionClassInput } from "@formbricks/types/action-classes";
@@ -61,6 +62,10 @@ export const updateSurveyAction = authenticatedActionClient
],
});
if (parsedInput.recaptcha?.enabled) {
await checkSpamProtectionPermission(organizationId);
}
if (parsedInput.followUps?.length) {
await checkSurveyFollowUpsPermission(organizationId);
}

View File

@@ -0,0 +1,195 @@
import { AddActionModal } from "@/modules/survey/editor/components/add-action-modal";
import { CreateNewActionTab } from "@/modules/survey/editor/components/create-new-action-tab";
import { SavedActionsTab } from "@/modules/survey/editor/components/saved-actions-tab";
import { ModalWithTabs } from "@/modules/ui/components/modal-with-tabs";
import { ActionClass } from "@prisma/client";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey } from "@formbricks/types/surveys/types";
// Mock child components
vi.mock("@/modules/survey/editor/components/create-new-action-tab", () => ({
CreateNewActionTab: vi.fn(() => <div>CreateNewActionTab Mock</div>),
}));
vi.mock("@/modules/survey/editor/components/saved-actions-tab", () => ({
SavedActionsTab: vi.fn(() => <div>SavedActionsTab Mock</div>),
}));
vi.mock("@/modules/ui/components/modal-with-tabs", () => ({
ModalWithTabs: vi.fn(
({ label, description, open, setOpen, tabs, size, closeOnOutsideClick, restrictOverflow }) => (
<div data-testid="modal-with-tabs">
<h1>{label}</h1>
<p>{description}</p>
<div>Open: {open.toString()}</div>
<button onClick={() => setOpen(false)}>Close</button>
<div>Size: {size}</div>
<div>Close on outside click: {closeOnOutsideClick.toString()}</div>
<div>Restrict overflow: {restrictOverflow.toString()}</div>
{tabs.map((tab) => (
<div key={tab.title}>
<h2>{tab.title}</h2>
<div>{tab.children}</div>
</div>
))}
</div>
)
),
}));
// Mock useTranslate hook
vi.mock("@tolgee/react", () => ({
useTranslate: () => ({
t: (key: string) => {
const translations = {
"environments.surveys.edit.select_saved_action": "Select Saved Action",
"environments.surveys.edit.capture_new_action": "Capture New Action",
"common.add_action": "Add Action",
"environments.surveys.edit.capture_a_new_action_to_trigger_a_survey_on": "Capture a new action...",
};
return translations[key] || key;
},
}),
}));
const mockSetOpen = vi.fn();
const mockSetActionClasses = vi.fn();
const mockSetLocalSurvey = vi.fn();
const mockActionClasses: ActionClass[] = [
// Add mock action classes if needed for SavedActionsTab testing
];
const mockSurvey: TSurvey = {
id: "survey1",
name: "Test Survey",
type: "app",
environmentId: "env1",
status: "draft",
questions: [],
triggers: [],
recontactDays: null,
displayOption: "displayOnce",
autoClose: null,
delay: 0,
autoComplete: null,
surveyClosedMessage: null,
singleUse: null,
styling: null,
languages: [],
variables: [],
welcomeCard: { enabled: false } as unknown as TSurvey["welcomeCard"],
endings: [],
hiddenFields: { enabled: false },
createdAt: new Date(),
updatedAt: new Date(),
pin: null,
resultShareKey: null,
displayPercentage: null,
segment: null,
closeOnDate: null,
createdBy: null,
} as unknown as TSurvey;
const defaultProps = {
open: true,
setOpen: mockSetOpen,
environmentId: "env1",
actionClasses: mockActionClasses,
setActionClasses: mockSetActionClasses,
isReadOnly: false,
localSurvey: mockSurvey,
setLocalSurvey: mockSetLocalSurvey,
};
const ModalWithTabsMock = vi.mocked(ModalWithTabs);
const SavedActionsTabMock = vi.mocked(SavedActionsTab);
const CreateNewActionTabMock = vi.mocked(CreateNewActionTab);
describe("AddActionModal", () => {
afterEach(() => {
cleanup();
vi.clearAllMocks(); // Clear mocks after each test
});
test("renders correctly when open", () => {
render(<AddActionModal {...defaultProps} />);
expect(screen.getByTestId("modal-with-tabs")).toBeInTheDocument();
// Check for translated text
expect(screen.getByText("Add Action")).toBeInTheDocument();
expect(screen.getByText("Capture a new action...")).toBeInTheDocument();
expect(screen.getByText("Select Saved Action")).toBeInTheDocument(); // Check translated tab title
expect(screen.getByText("Capture New Action")).toBeInTheDocument(); // Check translated tab title
expect(screen.getByText("SavedActionsTab Mock")).toBeInTheDocument();
expect(screen.getByText("CreateNewActionTab Mock")).toBeInTheDocument();
});
test("passes correct props to ModalWithTabs", () => {
render(<AddActionModal {...defaultProps} />);
expect(ModalWithTabsMock).toHaveBeenCalledWith(
expect.objectContaining({
// Check for translated props
label: "Add Action",
description: "Capture a new action...",
open: true,
setOpen: mockSetOpen,
tabs: expect.any(Array),
size: "md",
closeOnOutsideClick: false,
restrictOverflow: true,
}),
undefined
);
expect(ModalWithTabsMock.mock.calls[0][0].tabs).toHaveLength(2);
// Check for translated tab titles in the tabs array
expect(ModalWithTabsMock.mock.calls[0][0].tabs[0].title).toBe("Select Saved Action");
expect(ModalWithTabsMock.mock.calls[0][0].tabs[1].title).toBe("Capture New Action");
});
test("passes correct props to SavedActionsTab", () => {
render(<AddActionModal {...defaultProps} />);
expect(SavedActionsTabMock).toHaveBeenCalledWith(
{
actionClasses: mockActionClasses,
localSurvey: mockSurvey,
setLocalSurvey: mockSetLocalSurvey,
setOpen: mockSetOpen,
},
undefined
);
});
test("passes correct props to CreateNewActionTab", () => {
render(<AddActionModal {...defaultProps} />);
expect(CreateNewActionTabMock).toHaveBeenCalledWith(
{
actionClasses: mockActionClasses,
setActionClasses: mockSetActionClasses,
setOpen: mockSetOpen,
isReadOnly: false,
setLocalSurvey: mockSetLocalSurvey,
environmentId: "env1",
},
undefined
);
});
test("does not render when open is false", () => {
render(<AddActionModal {...defaultProps} open={false} />);
// Check the full props object passed to the mock, ensuring 'open' is false
expect(ModalWithTabsMock).toHaveBeenCalledWith(
expect.objectContaining({
label: "Add Action", // Expect translated label even when closed
description: "Capture a new action...", // Expect translated description
open: false, // Check that open is false
setOpen: mockSetOpen,
tabs: expect.any(Array),
size: "md",
closeOnOutsideClick: false,
restrictOverflow: true,
}),
undefined
);
});
});

View File

@@ -0,0 +1,103 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey } from "@formbricks/types/surveys/types";
import { AddEndingCardButton } from "./add-ending-card-button";
const mockAddEndingCard = vi.fn();
const mockSetLocalSurvey = vi.fn(); // Although not used in the button click, it's a prop
const mockSurvey: TSurvey = {
id: "survey1",
name: "Test Survey",
type: "app",
environmentId: "env1",
status: "draft",
questions: [],
triggers: [],
recontactDays: null,
displayOption: "displayOnce",
autoClose: null,
delay: 0,
autoComplete: null,
surveyClosedMessage: null,
singleUse: null,
languages: [],
styling: null,
variables: [],
welcomeCard: { enabled: false } as unknown as TSurvey["welcomeCard"],
endings: [], // Start with an empty endings array
hiddenFields: { enabled: false },
createdAt: new Date(),
updatedAt: new Date(),
createdBy: null,
segment: null,
resultShareKey: null,
displayPercentage: null,
closeOnDate: null,
runOnDate: null,
} as unknown as TSurvey;
describe("AddEndingCardButton", () => {
afterEach(() => {
cleanup();
vi.clearAllMocks(); // Clear mocks after each test
});
test("renders the button correctly", () => {
render(
<AddEndingCardButton
localSurvey={mockSurvey}
setLocalSurvey={mockSetLocalSurvey}
addEndingCard={mockAddEndingCard}
/>
);
// Check for the Tolgee translated text
expect(screen.getByText("environments.surveys.edit.add_ending")).toBeInTheDocument();
});
test("calls addEndingCard with the correct index when clicked", async () => {
const user = userEvent.setup();
const surveyWithEndings = { ...mockSurvey, endings: [{}, {}] } as unknown as TSurvey; // Survey with 2 endings
render(
<AddEndingCardButton
localSurvey={surveyWithEndings}
setLocalSurvey={mockSetLocalSurvey}
addEndingCard={mockAddEndingCard}
/>
);
const button = screen.getByText("environments.surveys.edit.add_ending").closest("div.group");
expect(button).toBeInTheDocument();
if (button) {
await user.click(button);
// Should be called with the current length of the endings array
expect(mockAddEndingCard).toHaveBeenCalledTimes(1);
expect(mockAddEndingCard).toHaveBeenCalledWith(2);
}
});
test("calls addEndingCard with index 0 when no endings exist", async () => {
const user = userEvent.setup();
render(
<AddEndingCardButton
localSurvey={mockSurvey} // Survey with 0 endings
setLocalSurvey={mockSetLocalSurvey}
addEndingCard={mockAddEndingCard}
/>
);
const button = screen.getByText("environments.surveys.edit.add_ending").closest("div.group");
expect(button).toBeInTheDocument();
if (button) {
await user.click(button);
// Should be called with index 0
expect(mockAddEndingCard).toHaveBeenCalledTimes(1);
expect(mockAddEndingCard).toHaveBeenCalledWith(0);
}
});
});

View File

@@ -0,0 +1,159 @@
import { AddQuestionButton } from "@/modules/survey/editor/components/add-question-button";
import {
TQuestion,
getCXQuestionTypes,
getQuestionDefaults,
getQuestionTypes,
} from "@/modules/survey/lib/questions";
import { createId } from "@paralleldrive/cuid2";
import { Project } from "@prisma/client";
// Import React for the mock
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
// Mock dependencies
vi.mock("@/lib/cn", () => ({
cn: (...args: any[]) => args.filter(Boolean).join(" "),
}));
vi.mock("@/modules/survey/lib/questions", () => ({
getCXQuestionTypes: vi.fn(),
getQuestionDefaults: vi.fn(),
getQuestionTypes: vi.fn(),
universalQuestionPresets: { presetKey: "presetValue" },
}));
vi.mock("@formkit/auto-animate/react", () => ({
useAutoAnimate: vi.fn(() => [vi.fn()]),
}));
vi.mock("@paralleldrive/cuid2", () => ({
createId: vi.fn(),
}));
vi.mock("@radix-ui/react-collapsible", async () => {
const original = await vi.importActual("@radix-ui/react-collapsible");
return {
...original,
Root: ({ children, open, onOpenChange }: any) => (
<div data-state={open ? "open" : "closed"} onClick={onOpenChange}>
{children}
</div>
),
CollapsibleTrigger: ({ children, asChild }: any) => (asChild ? children : <button>{children}</button>),
CollapsibleContent: ({ children }: any) => <div>{children}</div>,
};
});
vi.mock("lucide-react", () => ({
PlusIcon: () => <div>PlusIcon</div>,
}));
const mockProject = { id: "test-project-id" } as Project;
const mockAddQuestion = vi.fn();
const mockQuestionType1 = {
id: "type1",
label: "Type 1",
description: "Desc 1",
icon: () => <div>Icon1</div>,
} as TQuestion;
const mockQuestionType2 = {
id: "type2",
label: "Type 2",
description: "Desc 2",
icon: () => <div>Icon2</div>,
} as TQuestion;
const mockCXQuestionType = {
id: "cxType",
label: "CX Type",
description: "CX Desc",
icon: () => <div>CXIcon</div>,
} as TQuestion;
describe("AddQuestionButton", () => {
beforeEach(() => {
vi.mocked(getQuestionTypes).mockReturnValue([mockQuestionType1, mockQuestionType2]);
vi.mocked(getCXQuestionTypes).mockReturnValue([mockCXQuestionType]);
vi.mocked(getQuestionDefaults).mockReturnValue({ defaultKey: "defaultValue" } as any);
vi.mocked(createId).mockReturnValue("test-cuid");
});
afterEach(() => {
cleanup();
});
test("opens and shows question types on click", async () => {
render(<AddQuestionButton addQuestion={mockAddQuestion} project={mockProject} isCxMode={false} />);
const trigger = screen.getByText("environments.surveys.edit.add_question").closest("div")?.parentElement;
expect(trigger).toBeInTheDocument();
if (trigger) {
await userEvent.click(trigger);
}
expect(screen.getByText(mockQuestionType1.label)).toBeInTheDocument();
expect(screen.getByText(mockQuestionType2.label)).toBeInTheDocument();
});
test("calls getQuestionTypes when isCxMode is false", () => {
render(<AddQuestionButton addQuestion={mockAddQuestion} project={mockProject} isCxMode={false} />);
expect(getQuestionTypes).toHaveBeenCalled();
expect(getCXQuestionTypes).not.toHaveBeenCalled();
});
test("calls getCXQuestionTypes when isCxMode is true", async () => {
render(<AddQuestionButton addQuestion={mockAddQuestion} project={mockProject} isCxMode={true} />);
const trigger = screen.getByText("environments.surveys.edit.add_question").closest("div")?.parentElement;
expect(trigger).toBeInTheDocument();
if (trigger) {
await userEvent.click(trigger);
}
expect(getCXQuestionTypes).toHaveBeenCalled();
expect(getQuestionTypes).not.toHaveBeenCalled();
expect(screen.getByText(mockCXQuestionType.label)).toBeInTheDocument();
});
test("shows description on hover", async () => {
render(<AddQuestionButton addQuestion={mockAddQuestion} project={mockProject} isCxMode={false} />);
const trigger = screen.getByText("environments.surveys.edit.add_question").closest("div")?.parentElement;
expect(trigger).toBeInTheDocument();
if (trigger) {
await userEvent.click(trigger); // Open the collapsible
}
const questionButton = screen.getByText(mockQuestionType1.label).closest("button");
expect(questionButton).toBeInTheDocument();
if (questionButton) {
fireEvent.mouseEnter(questionButton);
// Description might be visually hidden/styled based on opacity, check if it's in the DOM
expect(screen.getByText(mockQuestionType1.description)).toBeInTheDocument();
fireEvent.mouseLeave(questionButton);
}
});
test("closes the collapsible after adding a question", async () => {
render(<AddQuestionButton addQuestion={mockAddQuestion} project={mockProject} isCxMode={false} />);
const rootElement = screen.getByText("environments.surveys.edit.add_question").closest("[data-state]");
expect(rootElement).toHaveAttribute("data-state", "closed");
// Open
const trigger = screen.getByText("environments.surveys.edit.add_question").closest("div")?.parentElement;
expect(trigger).toBeInTheDocument();
if (trigger) {
await userEvent.click(trigger);
}
expect(rootElement).toHaveAttribute("data-state", "open");
// Click a question type
const questionButton = screen.getByText(mockQuestionType1.label).closest("button");
expect(questionButton).toBeInTheDocument();
if (questionButton) {
await userEvent.click(questionButton);
}
// Check if it closed (state should change back to closed)
// Note: The mock implementation might not perfectly replicate Radix's state management on click inside content
// We verified addQuestion is called, which includes setOpen(false)
expect(mockAddQuestion).toHaveBeenCalled();
// We can't directly test setOpen(false) state change easily with this mock structure,
// but we know the onClick handler calls it.
});
});

View File

@@ -16,7 +16,6 @@ interface AddressQuestionFormProps {
question: TSurveyAddressQuestion;
questionIdx: number;
updateQuestion: (questionIdx: number, updatedAttributes: Partial<TSurveyAddressQuestion>) => void;
lastQuestion: boolean;
isInvalid: boolean;
selectedLanguageCode: string;
setSelectedLanguageCode: (language: string) => void;

View File

@@ -0,0 +1,412 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey, TSurveyQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types";
import { AdvancedSettings } from "./advanced-settings";
// Mock the child components
vi.mock("@/modules/survey/editor/components/conditional-logic", () => ({
ConditionalLogic: ({ question, questionIdx, localSurvey, updateQuestion }: any) => (
<div data-testid="conditional-logic">
<span data-testid="conditional-logic-question-id">{question.id}</span>
<span data-testid="conditional-logic-question-type">{question.type}</span>
<span data-testid="conditional-logic-question-idx">{questionIdx}</span>
<span data-testid="conditional-logic-survey-id">{localSurvey.id}</span>
<span data-testid="conditional-logic-logic-conditions">
{question.logic && JSON.stringify(question.logic)}
</span>
<span data-testid="conditional-logic-survey-questions">
{JSON.stringify(localSurvey.questions.map((q) => q.id))}
</span>
<button
data-testid="conditional-logic-update-button"
onClick={() => updateQuestion(questionIdx, { test: "value" })}>
Update
</button>
{question.logic && question.logic.length > 0 ? (
<button
data-testid="remove-logic-button"
onClick={() => {
updateQuestion(questionIdx, { logic: [] });
}}>
Remove All Logic
</button>
) : (
<span data-testid="no-logic-message">No logic conditions</span>
)}
{question.logic?.map((logicItem: any, index: number) => (
<div key={logicItem.id} data-testid={`logic-item-${index}`}>
Referenced Question ID: {logicItem.conditions.conditions[0].leftOperand.value}
</div>
))}
</div>
),
}));
vi.mock("@/modules/survey/editor/components/update-question-id", () => ({
UpdateQuestionId: ({ question, questionIdx, localSurvey, updateQuestion }: any) => (
<div data-testid="update-question-id">
<span data-testid="update-question-id-question-id">{question.id}</span>
<span data-testid="update-question-id-question-type">{question.type}</span>
<span data-testid="update-question-id-question-idx">{questionIdx}</span>
<span data-testid="update-question-id-survey-id">{localSurvey.id}</span>
<button
data-testid="update-question-id-update-button"
onClick={() => updateQuestion(questionIdx, { id: "new-id" })}>
Update
</button>
<input
data-testid="question-id-input"
defaultValue={question.id}
onChange={(e) => updateQuestion(questionIdx, { id: e.target.value })}
/>
<button
data-testid="save-question-id"
onClick={() => updateQuestion(questionIdx, { id: "q2-updated" })}>
Save
</button>
</div>
),
}));
describe("AdvancedSettings", () => {
afterEach(() => {
cleanup();
});
test("should render ConditionalLogic and UpdateQuestionId components when provided with valid props", () => {
// Arrange
const mockQuestion = {
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
} as unknown as TSurveyQuestion;
const mockSurvey = {
id: "survey1",
name: "Test Survey",
questions: [mockQuestion],
welcomeCard: {
enabled: false,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
fieldIds: [],
},
status: "draft",
createdAt: new Date(),
updatedAt: new Date(),
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const questionIdx = 0;
// Act
render(
<AdvancedSettings
question={mockQuestion}
questionIdx={questionIdx}
localSurvey={mockSurvey}
updateQuestion={mockUpdateQuestion}
/>
);
// Assert
// Check if both components are rendered
expect(screen.getByTestId("conditional-logic")).toBeInTheDocument();
expect(screen.getByTestId("update-question-id")).toBeInTheDocument();
// Check if props are correctly passed to ConditionalLogic
expect(screen.getByTestId("conditional-logic-question-id")).toHaveTextContent("q1");
expect(screen.getByTestId("conditional-logic-question-idx")).toHaveTextContent("0");
expect(screen.getByTestId("conditional-logic-survey-id")).toHaveTextContent("survey1");
// Check if props are correctly passed to UpdateQuestionId
expect(screen.getByTestId("update-question-id-question-id")).toHaveTextContent("q1");
expect(screen.getByTestId("update-question-id-question-idx")).toHaveTextContent("0");
expect(screen.getByTestId("update-question-id-survey-id")).toHaveTextContent("survey1");
// Verify that updateQuestion function is passed and can be called
const conditionalLogicUpdateButton = screen.getByTestId("conditional-logic-update-button");
conditionalLogicUpdateButton.click();
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, { test: "value" });
const updateQuestionIdUpdateButton = screen.getByTestId("update-question-id-update-button");
updateQuestionIdUpdateButton.click();
expect(mockUpdateQuestion).toHaveBeenCalledTimes(2);
expect(mockUpdateQuestion).toHaveBeenLastCalledWith(0, { id: "new-id" });
});
test("should pass the correct props to ConditionalLogic and UpdateQuestionId components", () => {
// Arrange
const mockQuestion: TSurveyQuestion = {
id: "question-123",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
} as unknown as TSurveyQuestion;
const mockSurvey = {
id: "survey-456",
name: "Test Survey",
questions: [mockQuestion],
welcomeCard: {
enabled: false,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
fieldIds: [],
},
status: "draft",
createdAt: new Date(),
updatedAt: new Date(),
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const questionIdx = 2; // Using a non-zero index to ensure it's passed correctly
// Act
render(
<AdvancedSettings
question={mockQuestion}
questionIdx={questionIdx}
localSurvey={mockSurvey}
updateQuestion={mockUpdateQuestion}
/>
);
// Assert
// Check if both components are rendered
expect(screen.getByTestId("conditional-logic")).toBeInTheDocument();
expect(screen.getByTestId("update-question-id")).toBeInTheDocument();
// Check if props are correctly passed to ConditionalLogic
expect(screen.getByTestId("conditional-logic-question-id")).toHaveTextContent("question-123");
expect(screen.getByTestId("conditional-logic-question-idx")).toHaveTextContent("2");
expect(screen.getByTestId("conditional-logic-survey-id")).toHaveTextContent("survey-456");
// Check if props are correctly passed to UpdateQuestionId
expect(screen.getByTestId("update-question-id-question-id")).toHaveTextContent("question-123");
expect(screen.getByTestId("update-question-id-question-idx")).toHaveTextContent("2");
expect(screen.getByTestId("update-question-id-survey-id")).toHaveTextContent("survey-456");
// Verify that updateQuestion function is passed and can be called from ConditionalLogic
const conditionalLogicUpdateButton = screen.getByTestId("conditional-logic-update-button");
conditionalLogicUpdateButton.click();
expect(mockUpdateQuestion).toHaveBeenCalledWith(2, { test: "value" });
// Verify that updateQuestion function is passed and can be called from UpdateQuestionId
const updateQuestionIdUpdateButton = screen.getByTestId("update-question-id-update-button");
updateQuestionIdUpdateButton.click();
expect(mockUpdateQuestion).toHaveBeenCalledWith(2, { id: "new-id" });
// Verify the function was called exactly twice
expect(mockUpdateQuestion).toHaveBeenCalledTimes(2);
});
test("should render correctly when dynamically rendered after being initially hidden", async () => {
// Arrange
const mockQuestion: TSurveyQuestion = {
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
} as unknown as TSurveyQuestion;
const mockSurvey = {
id: "survey1",
name: "Test Survey",
questions: [mockQuestion],
welcomeCard: {
enabled: false,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
fieldIds: [],
},
status: "draft",
createdAt: new Date(),
updatedAt: new Date(),
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const questionIdx = 0;
// Act
const { rerender } = render(
<div>
{/* Simulate AdvancedSettings being initially hidden */}
{false && ( // NOSONAR typescript:1125 typescript:6638 // This is a simulation of a condition
<AdvancedSettings
question={mockQuestion}
questionIdx={questionIdx}
localSurvey={mockSurvey}
updateQuestion={mockUpdateQuestion}
/>
)}
</div>
);
// Simulate AdvancedSettings being dynamically rendered
rerender(
<div>
<AdvancedSettings
question={mockQuestion}
questionIdx={questionIdx}
localSurvey={mockSurvey}
updateQuestion={mockUpdateQuestion}
/>
</div>
);
// Assert
// Check if both components are rendered
expect(screen.getByTestId("conditional-logic")).toBeInTheDocument();
expect(screen.getByTestId("update-question-id")).toBeInTheDocument();
// Check if props are correctly passed to ConditionalLogic
expect(screen.getByTestId("conditional-logic-question-id")).toHaveTextContent("q1");
expect(screen.getByTestId("conditional-logic-question-idx")).toHaveTextContent("0");
expect(screen.getByTestId("conditional-logic-survey-id")).toHaveTextContent("survey1");
// Check if props are correctly passed to UpdateQuestionId
expect(screen.getByTestId("update-question-id-question-id")).toHaveTextContent("q1");
expect(screen.getByTestId("update-question-id-question-idx")).toHaveTextContent("0");
expect(screen.getByTestId("update-question-id-survey-id")).toHaveTextContent("survey1");
// Verify that updateQuestion function is passed and can be called
const conditionalLogicUpdateButton = screen.getByTestId("conditional-logic-update-button");
await userEvent.click(conditionalLogicUpdateButton);
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, { test: "value" });
const updateQuestionIdUpdateButton = screen.getByTestId("update-question-id-update-button");
await userEvent.click(updateQuestionIdUpdateButton);
expect(mockUpdateQuestion).toHaveBeenCalledTimes(2);
expect(mockUpdateQuestion).toHaveBeenLastCalledWith(0, { id: "new-id" });
});
test("should update conditional logic when question ID is changed", async () => {
// Arrange
const mockQuestion1 = {
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Question 1" },
logic: [
{
id: "logic1",
conditions: {
id: "cond1",
connector: "and",
conditions: [
{
id: "subcond1",
leftOperand: { value: "q2", type: "question" },
operator: "equals",
},
],
},
actions: [
{
id: "action1",
objective: "jumpToQuestion",
target: "q3",
},
],
},
],
} as unknown as TSurveyQuestion;
const mockQuestion2 = {
id: "q2",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Question 2" },
} as unknown as TSurveyQuestion;
const mockSurvey = {
id: "survey1",
name: "Test Survey",
questions: [mockQuestion1, mockQuestion2],
welcomeCard: {
enabled: false,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
endings: [],
hiddenFields: { enabled: false, fieldIds: [] },
status: "draft",
createdAt: new Date(),
updatedAt: new Date(),
} as unknown as TSurvey;
// Create a mock function that simulates updating the question ID and updating any logic that references it
const mockUpdateQuestion = vi.fn((questionIdx, updatedAttributes) => {
// If we're updating a question ID
if (updatedAttributes.id) {
const oldId = mockSurvey.questions[questionIdx].id;
const newId = updatedAttributes.id;
// Update the question ID
mockSurvey.questions[questionIdx] = {
...mockSurvey.questions[questionIdx],
...updatedAttributes,
};
// Update any logic that references this question ID
mockSurvey.questions.forEach((q) => {
if (q.logic) {
q.logic.forEach((logicItem) => {
// NOSONAR typescript:S2004 // This is ok for testing
logicItem.conditions.conditions.forEach((condition) => {
// Check if it's a TSingleCondition (not a TConditionGroup)
if ("leftOperand" in condition) {
if (condition.leftOperand.type === "question" && condition.leftOperand.value === oldId) {
condition.leftOperand.value = newId;
}
}
});
});
}
});
}
});
// Act
render(
<AdvancedSettings
question={mockQuestion2}
questionIdx={1}
localSurvey={mockSurvey}
updateQuestion={mockUpdateQuestion}
/>
);
// Assert
// Check if both components are rendered
expect(screen.getByTestId("conditional-logic")).toBeInTheDocument();
expect(screen.getByTestId("update-question-id")).toBeInTheDocument();
// Check if props are correctly passed to ConditionalLogic
expect(screen.getByTestId("conditional-logic-question-id")).toHaveTextContent("q2");
expect(screen.getByTestId("conditional-logic-question-idx")).toHaveTextContent("1");
expect(screen.getByTestId("conditional-logic-survey-id")).toHaveTextContent("survey1");
// Check if props are correctly passed to UpdateQuestionId
expect(screen.getByTestId("update-question-id-question-id")).toHaveTextContent("q2");
expect(screen.getByTestId("update-question-id-question-idx")).toHaveTextContent("1");
expect(screen.getByTestId("update-question-id-survey-id")).toHaveTextContent("survey1");
// Verify that updateQuestion function is passed and can be called
const conditionalLogicUpdateButton = screen.getByTestId("conditional-logic-update-button");
await userEvent.click(conditionalLogicUpdateButton);
expect(mockUpdateQuestion).toHaveBeenCalledWith(1, { test: "value" });
const updateQuestionIdUpdateButton = screen.getByTestId("update-question-id-update-button");
await userEvent.click(updateQuestionIdUpdateButton);
expect(mockUpdateQuestion).toHaveBeenCalledTimes(2);
expect(mockUpdateQuestion).toHaveBeenLastCalledWith(1, { id: "new-id" });
});
});

View File

@@ -88,7 +88,7 @@ export const AnimatedSurveyBg = ({ handleBgChange, background }: AnimatedSurveyB
<source src={`${key}`} type="video/mp4" />
</video>
<input
className="absolute right-2 top-2 h-4 w-4 rounded-sm bg-white"
className="absolute top-2 right-2 h-4 w-4 rounded-sm bg-white"
type="checkbox"
checked={animation === value}
onChange={() => handleBg(value)}

View File

@@ -0,0 +1,335 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey, TSurveyCalQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types";
import { CalQuestionForm } from "./cal-question-form";
// Mock necessary modules and components
vi.mock("@/modules/ui/components/advanced-option-toggle", () => ({
AdvancedOptionToggle: ({
isChecked,
onToggle,
htmlId,
children,
title,
}: {
isChecked: boolean;
onToggle?: (checked: boolean) => void;
htmlId?: string;
children?: React.ReactNode;
title?: string;
}) => {
let content;
if (onToggle && htmlId) {
content = (
<input
type="checkbox"
id={htmlId}
checked={isChecked}
onChange={() => onToggle(!isChecked)}
data-testid="cal-host-toggle"
/>
);
} else {
content = isChecked ? "Enabled" : "Disabled";
}
return (
<div data-testid="advanced-option-toggle">
{htmlId && title ? <label htmlFor={htmlId}>{title}</label> : null}
{content}
{isChecked && children}
</div>
);
},
}));
// Updated Input mock to use id prop correctly
vi.mock("@/modules/ui/components/input", () => ({
Input: ({
id,
onChange,
value,
}: {
id: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
value: string;
}) => (
<input
id={id} // Ensure the input has the ID the label points to
value={value}
onChange={onChange}
/>
),
}));
vi.mock("@/modules/survey/components/question-form-input", () => ({
QuestionFormInput: ({
id,
value,
label,
localSurvey,
questionIdx,
isInvalid,
selectedLanguageCode,
locale,
}: any) => (
<div data-testid="question-form-input">
{id
? `${id} - ${value?.default} - ${label} - ${localSurvey.id} - ${questionIdx} - ${isInvalid.toString()} - ${selectedLanguageCode} - ${locale}`
: ""}
</div>
),
}));
describe("CalQuestionForm", () => {
afterEach(() => {
cleanup();
});
test("should initialize isCalHostEnabled to true if question.calHost is defined", () => {
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const mockQuestion = {
id: "cal_question_1",
type: TSurveyQuestionTypeEnum.Cal,
headline: { default: "Book a meeting" },
calUserName: "testuser",
calHost: "cal.com",
} as unknown as TSurveyCalQuestion;
const mockLocalSurvey: TSurvey = {
id: "survey_123",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "env_123",
status: "draft",
questions: [],
languages: [
{
id: "lang_1",
default: true,
enabled: true,
language: {
id: "en",
code: "en",
name: "English",
createdAt: new Date(),
updatedAt: new Date(),
alias: null,
projectId: "project_123",
},
},
],
endings: [],
} as unknown as TSurvey;
render(
<CalQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
lastQuestion={false}
/>
);
// Assert that the AdvancedOptionToggle component is rendered with isChecked prop set to true
expect(screen.getByTestId("advanced-option-toggle")).toHaveTextContent(
"environments.surveys.edit.custom_hostname"
);
});
test("should set calHost to undefined when isCalHostEnabled is toggled off", async () => {
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const user = userEvent.setup();
const mockQuestion = {
id: "cal_question_1",
type: TSurveyQuestionTypeEnum.Cal,
headline: { default: "Book a meeting" },
calUserName: "testuser",
calHost: "cal.com",
} as unknown as TSurveyCalQuestion;
const mockLocalSurvey: TSurvey = {
id: "survey_123",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "env_123",
status: "draft",
questions: [],
languages: [
{
id: "lang_1",
default: true,
enabled: true,
language: {
id: "en",
code: "en",
name: "English",
createdAt: new Date(),
updatedAt: new Date(),
alias: null,
projectId: "project_123",
},
},
],
endings: [],
} as unknown as TSurvey;
render(
<CalQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
lastQuestion={false}
/>
);
// Find the toggle and click it to disable calHost
const toggle = screen.getByTestId("cal-host-toggle");
await user.click(toggle);
// Assert that updateQuestion is called with calHost: undefined
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, { calHost: undefined });
});
test("should render QuestionFormInput for the headline field with the correct props", () => {
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const mockQuestion = {
id: "cal_question_1",
type: TSurveyQuestionTypeEnum.Cal,
headline: { default: "Book a meeting" },
calUserName: "testuser",
calHost: "cal.com",
} as unknown as TSurveyCalQuestion;
const mockLocalSurvey = {
id: "survey_123",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "env_123",
status: "draft",
questions: [],
languages: [
{
id: "lang_1",
default: true,
enabled: true,
language: {
id: "en",
code: "en",
name: "English",
createdAt: new Date(),
updatedAt: new Date(),
alias: null,
projectId: "project_123",
},
},
],
endings: [],
} as unknown as TSurvey;
render(
<CalQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
lastQuestion={false}
/>
);
// Assert that the QuestionFormInput component is rendered with the correct props
expect(screen.getByTestId("question-form-input")).toHaveTextContent(
"headline - Book a meeting - environments.surveys.edit.question* - survey_123 - 0 - false - en - en-US"
);
});
test("should call updateQuestion with an empty calUserName when the input is cleared", async () => {
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const user = userEvent.setup();
const mockQuestion = {
id: "cal_question_1",
type: TSurveyQuestionTypeEnum.Cal,
headline: { default: "Book a meeting" },
calUserName: "testuser",
calHost: "cal.com",
} as unknown as TSurveyCalQuestion;
const mockLocalSurvey = {
id: "survey_123",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "env_123",
status: "draft",
questions: [],
languages: [
{
id: "lang_1",
default: true,
enabled: true,
language: {
id: "en",
code: "en",
name: "English",
createdAt: new Date(),
updatedAt: new Date(),
alias: null,
projectId: "project_123",
},
},
],
endings: [],
} as unknown as TSurvey;
render(
<CalQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
lastQuestion={false}
/>
);
const calUserNameInput = screen.getByLabelText("environments.surveys.edit.cal_username", {
selector: "input",
});
await user.clear(calUserNameInput);
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, { calUserName: "" });
});
});

View File

@@ -0,0 +1,194 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { ColorSurveyBg } from "./color-survey-bg";
// Mock the ColorPicker component
vi.mock("@/modules/ui/components/color-picker", () => ({
ColorPicker: ({ color, onChange }: { color: string; onChange?: (color: string) => void }) => (
<div data-testid="color-picker" data-color={color}>
Mocked ColorPicker
{onChange && (
<button data-testid="color-picker-change" onClick={() => onChange("#ABCDEF")}>
Change Color
</button>
)}
{onChange && (
<button data-testid="simulate-color-change" onClick={() => onChange("invalid-color")}>
Change Invalid Color
</button>
)}
</div>
),
}));
describe("ColorSurveyBg", () => {
const mockHandleBgChange = vi.fn();
const mockColors = ["#FF0000", "#00FF00", "#0000FF"];
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
test("initializes color state with provided background prop", () => {
const testBackground = "#123456";
render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={mockColors} background={testBackground} />
);
// Check if ColorPicker received the correct color prop
const colorPicker = screen.getByTestId("color-picker");
expect(colorPicker).toHaveAttribute("data-color", testBackground);
});
test("initializes color state with default #FFFFFF when background prop is not provided", () => {
render(
<ColorSurveyBg
handleBgChange={mockHandleBgChange}
colors={mockColors}
background={undefined as unknown as string}
/>
);
// Check if ColorPicker received the default color
const colorPicker = screen.getByTestId("color-picker");
expect(colorPicker).toHaveAttribute("data-color", "#FFFFFF");
});
test("should update color state and call handleBgChange when a color is selected from ColorPicker", async () => {
const user = userEvent.setup();
const initialBackground = "#123456";
render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={mockColors} background={initialBackground} />
);
// Verify initial state
const colorPicker = screen.getByTestId("color-picker");
expect(colorPicker).toHaveAttribute("data-color", initialBackground);
// Simulate color change from ColorPicker
const changeButton = screen.getByTestId("color-picker-change");
await user.click(changeButton);
// Verify handleBgChange was called with the new color and 'color' type
expect(mockHandleBgChange).toHaveBeenCalledWith("#ABCDEF", "color");
// Verify color state was updated (ColorPicker should receive the new color)
expect(colorPicker).toHaveAttribute("data-color", "#ABCDEF");
});
test("applies border style to the currently selected color box", () => {
const selectedColor = "#00FF00"; // Second color in the mockColors array
const { container } = render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={mockColors} background={selectedColor} />
);
// Get all color boxes using CSS selector
const colorBoxes = container.querySelectorAll(".h-16.w-16.cursor-pointer");
expect(colorBoxes).toHaveLength(mockColors.length);
// Find the selected color box (should be the second one)
const selectedColorBox = colorBoxes[1];
// Check that the selected color box has the border classes
expect(selectedColorBox.className).toContain("border-4");
expect(selectedColorBox.className).toContain("border-slate-500");
// Check that other color boxes don't have these classes
expect(colorBoxes[0].className).not.toContain("border-4");
expect(colorBoxes[0].className).not.toContain("border-slate-500");
expect(colorBoxes[2].className).not.toContain("border-4");
expect(colorBoxes[2].className).not.toContain("border-slate-500");
});
test("renders all color boxes provided in the colors prop", () => {
const testBackground = "#FF0000";
const { container } = render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={mockColors} background={testBackground} />
);
// Check if all color boxes are rendered using class selectors
const colorBoxes = container.querySelectorAll(".h-16.w-16.cursor-pointer.rounded-lg");
expect(colorBoxes).toHaveLength(mockColors.length);
// Verify each color box has the correct background color
mockColors.forEach((color, index) => {
expect(colorBoxes[index]).toHaveStyle({ backgroundColor: color });
});
// Check that the selected color has the special border styling
const selectedColorBox = colorBoxes[0]; // First color (#FF0000) should be selected
expect(selectedColorBox.className).toContain("border-4 border-slate-500");
// Check that non-selected colors don't have the special border styling
const nonSelectedColorBoxes = Array.from(colorBoxes).slice(1);
nonSelectedColorBoxes.forEach((box) => {
expect(box.className).not.toContain("border-4 border-slate-500");
});
});
test("renders without crashing when an invalid color format is provided", () => {
const invalidColor = "invalid-color";
const invalidColorsMock = ["#FF0000", "#00FF00", "invalid-color"];
const { container } = render(
<ColorSurveyBg
handleBgChange={mockHandleBgChange}
colors={invalidColorsMock}
background={invalidColor}
/>
);
// Check if component renders without crashing
expect(screen.getByTestId("color-picker")).toBeInTheDocument();
// Check if ColorPicker received the invalid color
expect(screen.getByTestId("color-picker")).toHaveAttribute("data-color", invalidColor);
// Check if the color boxes render
const colorBoxes = container.querySelectorAll(".h-16.w-16.cursor-pointer");
expect(colorBoxes.length).toBe(3);
});
test("passes invalid color to handleBgChange when selected through ColorPicker", async () => {
const user = userEvent.setup();
const invalidColorsMock = ["#FF0000", "#00FF00", "invalid-color"];
render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={invalidColorsMock} background="#FFFFFF" />
);
// Simulate color change in ColorPicker with invalid color
await user.click(screen.getByTestId("simulate-color-change"));
// Verify handleBgChange was called with the invalid color
expect(mockHandleBgChange).toHaveBeenCalledWith("invalid-color", "color");
});
test("passes invalid color to handleBgChange when clicking a color box with invalid color", async () => {
const user = userEvent.setup();
const invalidColorsMock = ["#FF0000", "#00FF00", "invalid-color"];
const { container } = render(
<ColorSurveyBg handleBgChange={mockHandleBgChange} colors={invalidColorsMock} background="#FFFFFF" />
);
// Find all color boxes
const colorBoxes = container.querySelectorAll(".h-16.w-16.cursor-pointer");
// The third box corresponds to our invalid color (from invalidColorsMock)
const invalidColorBox = colorBoxes[2];
expect(invalidColorBox).toBeInTheDocument();
// Click the invalid color box
await user.click(invalidColorBox);
// Verify handleBgChange was called with the invalid color
expect(mockHandleBgChange).toHaveBeenCalledWith("invalid-color", "color");
});
});

View File

@@ -0,0 +1,233 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeAll, describe, expect, test, vi } from "vitest";
import {
TSurvey,
TSurveyLogic,
TSurveyQuestion,
TSurveyQuestionTypeEnum,
} from "@formbricks/types/surveys/types";
import { ConditionalLogic } from "./conditional-logic";
// Mock @formkit/auto-animate - simplify implementation
vi.mock("@formkit/auto-animate/react", () => ({
useAutoAnimate: () => [null],
}));
vi.mock("@/lib/surveyLogic/utils", () => ({
duplicateLogicItem: (logicItem: TSurveyLogic) => ({
...logicItem,
id: "new-duplicated-id",
}),
}));
vi.mock("./logic-editor", () => ({
LogicEditor: () => <div data-testid="logic-editor">LogicEditor</div>,
}));
describe("ConditionalLogic", () => {
beforeAll(() => {
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
afterEach(() => {
cleanup();
});
test("should add a new logic condition to the question's logic array when the add logic button is clicked", async () => {
const mockUpdateQuestion = vi.fn();
const mockQuestion: TSurveyQuestion = {
id: "testQuestionId",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
required: false,
inputType: "text",
charLimit: {
enabled: false,
},
};
const mockSurvey = {
id: "testSurveyId",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "testEnvId",
status: "inProgress",
questions: [mockQuestion],
endings: [],
} as unknown as TSurvey;
render(
<ConditionalLogic
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
/>
);
const addLogicButton = screen.getByRole("button", { name: "environments.surveys.edit.add_logic" });
await userEvent.click(addLogicButton);
expect(mockUpdateQuestion).toHaveBeenCalledTimes(1);
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, {
logic: expect.arrayContaining([
expect.objectContaining({
conditions: expect.objectContaining({
connector: "and",
conditions: expect.arrayContaining([
expect.objectContaining({
leftOperand: expect.objectContaining({
value: "testQuestionId",
type: "question",
}),
}),
]),
}),
actions: expect.arrayContaining([
expect.objectContaining({
objective: "jumpToQuestion",
target: "",
}),
]),
}),
]),
});
});
test("should duplicate the specified logic condition and insert it into the logic array", async () => {
const mockUpdateQuestion = vi.fn();
const initialLogic: TSurveyLogic = {
id: "initialLogicId",
conditions: {
id: "conditionGroupId",
connector: "and",
conditions: [],
},
actions: [],
};
const mockQuestion: TSurveyQuestion = {
id: "testQuestionId",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
required: false,
inputType: "text",
charLimit: {
enabled: false,
},
logic: [initialLogic],
};
const mockSurvey = {
id: "testSurveyId",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "testEnvId",
status: "inProgress",
questions: [mockQuestion],
endings: [],
} as unknown as TSurvey;
render(
<ConditionalLogic
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
/>
);
// First click the ellipsis menu button to open the dropdown
const menuButton = screen.getByRole("button", {
name: "", // The button has no text content, just an icon
});
await userEvent.click(menuButton);
// Now look for the duplicate option in the dropdown menu that appears
const duplicateButton = await screen.findByText("common.duplicate");
await userEvent.click(duplicateButton);
expect(mockUpdateQuestion).toHaveBeenCalledTimes(1);
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, {
logic: expect.arrayContaining([
initialLogic,
expect.objectContaining({
id: "new-duplicated-id",
conditions: initialLogic.conditions,
actions: initialLogic.actions,
}),
]),
});
});
test("should render the list of logic conditions and their associated actions based on the question's logic data", () => {
const mockUpdateQuestion = vi.fn();
const mockLogic: TSurveyLogic[] = [
{
id: "logic1",
conditions: {
id: "cond1",
connector: "and",
conditions: [],
},
actions: [],
},
{
id: "logic2",
conditions: {
id: "cond2",
connector: "or",
conditions: [],
},
actions: [],
},
];
const mockQuestion: TSurveyQuestion = {
id: "testQuestionId",
type: TSurveyQuestionTypeEnum.OpenText,
headline: { default: "Test Question" },
required: false,
inputType: "text",
charLimit: {
enabled: false,
},
logic: mockLogic,
};
const mockSurvey = {
id: "testSurveyId",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "testEnvId",
status: "inProgress",
questions: [mockQuestion],
endings: [],
} as unknown as TSurvey;
render(
<ConditionalLogic
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
/>
);
expect(screen.getAllByTestId("logic-editor").length).toBe(2);
});
});

View File

@@ -0,0 +1,68 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey, TSurveyConsentQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { ConsentQuestionForm } from "./consent-question-form";
vi.mock("@/modules/survey/components/question-form-input", () => ({
QuestionFormInput: ({ label }: { label: string }) => <div data-testid="question-form-input">{label}</div>,
}));
vi.mock("@/modules/ee/multi-language-surveys/components/localized-editor", () => ({
LocalizedEditor: ({ id }: { id: string }) => <div data-testid="localized-editor">{id}</div>,
}));
vi.mock("@/modules/ui/components/label", () => ({
Label: ({ children }: { children: string }) => <div data-testid="label">{children}</div>,
}));
describe("ConsentQuestionForm", () => {
afterEach(() => {
cleanup();
});
test("renders the form with headline, description, and checkbox label when provided valid props", () => {
const mockQuestion = {
id: "consent1",
type: TSurveyQuestionTypeEnum.Consent,
headline: { en: "Consent Headline" },
html: { en: "Consent Description" },
label: { en: "Consent Checkbox Label" },
} as unknown as TSurveyConsentQuestion;
const mockLocalSurvey = {
id: "survey1",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "env1",
status: "draft",
questions: [],
languages: [],
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const mockLocale: TUserLocale = "en-US";
render(
<ConsentQuestionForm
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
isInvalid={false}
localSurvey={mockLocalSurvey}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
locale={mockLocale}
/>
);
const questionFormInputs = screen.getAllByTestId("question-form-input");
expect(questionFormInputs[0]).toHaveTextContent("environments.surveys.edit.question*");
expect(screen.getByTestId("label")).toHaveTextContent("common.description");
expect(screen.getByTestId("localized-editor")).toHaveTextContent("subheader");
expect(questionFormInputs[1]).toHaveTextContent("environments.surveys.edit.checkbox_label*");
});
});

View File

@@ -0,0 +1,262 @@
import { createI18nString } from "@/lib/i18n/utils";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
TSurvey,
TSurveyContactInfoQuestion,
TSurveyQuestionTypeEnum,
} from "@formbricks/types/surveys/types";
import { ContactInfoQuestionForm } from "./contact-info-question-form";
// Mock QuestionFormInput component
vi.mock("@/modules/survey/components/question-form-input", () => ({
QuestionFormInput: vi.fn(({ id, label, value, selectedLanguageCode }) => (
<div data-testid="question-form-input">
<label data-testid="question-form-input-label">{label}</label>
<div data-testid={`question-form-input-${id}`}>
{selectedLanguageCode ? value?.[selectedLanguageCode] || "" : value?.default || ""}
</div>
</div>
)),
}));
// Mock QuestionToggleTable component
vi.mock("@/modules/ui/components/question-toggle-table", () => ({
QuestionToggleTable: vi.fn(({ fields }) => (
<div data-testid="question-toggle-table">
{fields?.map((field) => (
<div key={field.id} data-testid={`question-toggle-table-field-${field.id}`}>
{field.label}
</div>
))}
</div>
)),
}));
// Mock the Button component
vi.mock("@/modules/ui/components/button", () => ({
Button: ({ children, onClick }) => (
<button data-testid="add-description-button" onClick={onClick}>
{children}
</button>
),
}));
// Mock @formkit/auto-animate - simplify implementation
vi.mock("@formkit/auto-animate/react", () => ({
useAutoAnimate: () => [null],
}));
describe("ContactInfoQuestionForm", () => {
let mockSurvey: TSurvey;
let mockQuestion: TSurveyContactInfoQuestion;
let updateQuestionMock: any;
beforeEach(() => {
// Mock window.matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
mockSurvey = {
id: "survey-1",
name: "Test Survey",
questions: [],
languages: [],
} as unknown as TSurvey;
mockQuestion = {
id: "contact-info-1",
type: TSurveyQuestionTypeEnum.ContactInfo,
headline: createI18nString("Headline Text", ["en"]),
required: true,
firstName: { show: true, required: false, placeholder: createI18nString("", ["en"]) },
lastName: { show: true, required: false, placeholder: createI18nString("", ["en"]) },
email: { show: true, required: false, placeholder: createI18nString("", ["en"]) },
phone: { show: true, required: false, placeholder: createI18nString("", ["en"]) },
company: { show: true, required: false, placeholder: createI18nString("", ["en"]) },
} as unknown as TSurveyContactInfoQuestion;
updateQuestionMock = vi.fn();
});
afterEach(() => {
cleanup();
});
test("should update required to false when all fields are visible but optional", () => {
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={updateQuestionMock}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
expect(updateQuestionMock).toHaveBeenCalledWith(0, { required: false });
});
test("should update required to true when all fields are visible and at least one is required", () => {
mockQuestion = {
...mockQuestion,
firstName: { show: true, required: true, placeholder: createI18nString("", ["en"]) },
} as unknown as TSurveyContactInfoQuestion;
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={updateQuestionMock}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
expect(updateQuestionMock).toHaveBeenCalledWith(0, { required: true });
});
test("should update required to false when all fields are hidden", () => {
mockQuestion = {
...mockQuestion,
firstName: { show: false, required: false, placeholder: createI18nString("", ["en"]) },
lastName: { show: false, required: false, placeholder: createI18nString("", ["en"]) },
email: { show: false, required: false, placeholder: createI18nString("", ["en"]) },
phone: { show: false, required: false, placeholder: createI18nString("", ["en"]) },
company: { show: false, required: false, placeholder: createI18nString("", ["en"]) },
} as unknown as TSurveyContactInfoQuestion;
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={updateQuestionMock}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
expect(updateQuestionMock).toHaveBeenCalledWith(0, { required: false });
});
test("should display the subheader input field when the subheader property is defined", () => {
const mockQuestionWithSubheader: TSurveyContactInfoQuestion = {
...mockQuestion,
subheader: createI18nString("Subheader Text", ["en"]), // Define subheader
} as unknown as TSurveyContactInfoQuestion;
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestionWithSubheader}
questionIdx={0}
updateQuestion={vi.fn()}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
const subheaderInput = screen.getByTestId("question-form-input-subheader");
expect(subheaderInput).toBeInTheDocument();
});
test("should display the 'Add Description' button when subheader is undefined", () => {
const mockQuestionWithoutSubheader: TSurveyContactInfoQuestion = {
...mockQuestion,
subheader: undefined,
} as unknown as TSurveyContactInfoQuestion;
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestionWithoutSubheader}
questionIdx={0}
updateQuestion={updateQuestionMock}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
const addButton = screen.getByTestId("add-description-button");
expect(addButton).toBeInTheDocument();
});
test("should handle gracefully when selectedLanguageCode is not in translations", () => {
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={vi.fn()}
isInvalid={false}
selectedLanguageCode="fr"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
const headlineValue = screen.getByTestId("question-form-input-headline");
expect(headlineValue).toBeInTheDocument();
expect(headlineValue).toHaveTextContent(""); // Expect empty string since "fr" is not in headline translations
});
test("should handle a question object with a new or custom field", () => {
const mockQuestionWithCustomField: TSurveyContactInfoQuestion = {
...mockQuestion,
// Add a custom field with an unexpected structure
customField: { value: "Custom Value" },
} as unknown as TSurveyContactInfoQuestion;
render(
<ContactInfoQuestionForm
localSurvey={mockSurvey}
question={mockQuestionWithCustomField}
questionIdx={0}
updateQuestion={vi.fn()}
isInvalid={false}
selectedLanguageCode="en"
setSelectedLanguageCode={vi.fn()}
locale="en-US"
lastQuestion={false}
/>
);
// Assert that the component renders without errors
const headlineValue = screen.getByTestId("question-form-input-headline");
expect(headlineValue).toBeInTheDocument();
// Assert that the QuestionToggleTable is rendered
const toggleTable = screen.getByTestId("question-toggle-table");
expect(toggleTable).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,60 @@
import { ActionClass } from "@prisma/client";
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { CreateNewActionTab } from "./create-new-action-tab";
// Mock the NoCodeActionForm and CodeActionForm components
vi.mock("@/modules/ui/components/no-code-action-form", () => ({
NoCodeActionForm: () => <div data-testid="no-code-action-form">NoCodeActionForm</div>,
}));
vi.mock("@/modules/ui/components/code-action-form", () => ({
CodeActionForm: () => <div data-testid="code-action-form">CodeActionForm</div>,
}));
// Mock constants
vi.mock("@/lib/constants", () => ({
IS_FORMBRICKS_CLOUD: false,
FORMBRICKS_API_HOST: "http://localhost:3000",
FORMBRICKS_ENVIRONMENT_ID: "test-env-id",
}));
// Mock the createActionClassAction function
vi.mock("../actions", () => ({
createActionClassAction: vi.fn(),
}));
describe("CreateNewActionTab", () => {
afterEach(() => {
cleanup();
});
test("renders all expected fields and UI elements when provided with valid props", () => {
const actionClasses: ActionClass[] = [];
const setActionClasses = vi.fn();
const setOpen = vi.fn();
const isReadOnly = false;
const setLocalSurvey = vi.fn();
const environmentId = "test-env-id";
render(
<CreateNewActionTab
actionClasses={actionClasses}
setActionClasses={setActionClasses}
setOpen={setOpen}
isReadOnly={isReadOnly}
setLocalSurvey={setLocalSurvey}
environmentId={environmentId}
/>
);
// Check for the presence of key UI elements
expect(screen.getByText("environments.actions.action_type")).toBeInTheDocument();
expect(screen.getByRole("radio", { name: "common.no_code" })).toBeInTheDocument();
expect(screen.getByRole("radio", { name: "common.code" })).toBeInTheDocument();
expect(screen.getByLabelText("environments.actions.what_did_your_user_do")).toBeInTheDocument();
expect(screen.getByLabelText("common.description")).toBeInTheDocument();
expect(screen.getByTestId("no-code-action-form")).toBeInTheDocument(); // Ensure NoCodeActionForm is rendered by default
});
});

View File

@@ -0,0 +1,75 @@
import { createI18nString } from "@/lib/i18n/utils";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TSurvey, TSurveyCTAQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types";
import { CTAQuestionForm } from "./cta-question-form";
vi.mock("@formkit/auto-animate/react", () => ({
useAutoAnimate: () => [null],
}));
vi.mock("@/modules/survey/components/question-form-input", () => ({
QuestionFormInput: () => <div data-testid="question-form-input">QuestionFormInput</div>,
}));
vi.mock("@/modules/ee/multi-language-surveys/components/localized-editor", () => ({
LocalizedEditor: () => <div data-testid="localized-editor">LocalizedEditor</div>,
}));
vi.mock("@/modules/ui/components/options-switch", () => ({
OptionsSwitch: () => <div data-testid="options-switch">OptionsSwitch</div>,
}));
describe("CTAQuestionForm", () => {
afterEach(() => {
cleanup();
});
test("renders all required fields and components when provided with valid props", () => {
const mockQuestion: TSurveyCTAQuestion = {
id: "test-question",
type: TSurveyQuestionTypeEnum.CTA,
headline: createI18nString("Test Headline", ["en"]),
buttonLabel: createI18nString("Next", ["en"]),
backButtonLabel: createI18nString("Back", ["en"]),
buttonExternal: false,
buttonUrl: "",
required: true,
};
const mockLocalSurvey = {
id: "test-survey",
name: "Test Survey",
type: "link",
createdAt: new Date(),
updatedAt: new Date(),
environmentId: "test-env",
status: "draft",
questions: [],
languages: [],
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
const mockLocale = "en-US";
render(
<CTAQuestionForm
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
lastQuestion={false}
isInvalid={false}
localSurvey={mockLocalSurvey}
selectedLanguageCode="en"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
locale={mockLocale}
/>
);
const questionFormInputs = screen.getAllByTestId("question-form-input");
expect(questionFormInputs.length).toBe(2);
expect(screen.getByTestId("localized-editor")).toBeInTheDocument();
expect(screen.getByTestId("options-switch")).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,400 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TLanguage } from "@formbricks/types/project";
import {
TSurvey,
TSurveyDateQuestion,
TSurveyLanguage,
TSurveyQuestionTypeEnum,
} from "@formbricks/types/surveys/types";
import { DateQuestionForm } from "./date-question-form";
// Mock dependencies
vi.mock("@formkit/auto-animate/react", () => ({
useAutoAnimate: () => [null],
}));
vi.mock("@/modules/survey/components/question-form-input", () => ({
QuestionFormInput: ({ id, value, label, locale, selectedLanguageCode }: any) => (
<div
data-testid={`question-form-input-${id}`}
data-value={value?.default}
data-label={label}
data-locale={locale}
data-language={selectedLanguageCode}>
{label}: {value?.[selectedLanguageCode] ?? value?.default}
</div>
),
}));
vi.mock("@/modules/ui/components/button", () => ({
Button: ({ children, onClick, className, size, variant, type }: any) => (
<button
data-testid="add-description-button"
onClick={onClick}
className={className}
data-size={size}
data-variant={variant}
type={type}>
{children}
</button>
),
}));
vi.mock("@/modules/ui/components/label", () => ({
Label: ({ children, htmlFor }: any) => (
<label data-testid={`label-${htmlFor}`} htmlFor={htmlFor}>
{children}
</label>
),
}));
vi.mock("@/modules/ui/components/options-switch", () => ({
OptionsSwitch: ({ options, currentOption, handleOptionChange }: any) => (
<div data-testid="options-switch" data-current-option={currentOption}>
{options.map((option: any) => (
<button
key={option.value}
data-testid={`option-${option.value}`}
onClick={() => handleOptionChange(option.value)}>
{option.label}
</button>
))}
</div>
),
}));
vi.mock("lucide-react", () => ({
PlusIcon: () => <div data-testid="plus-icon">PlusIcon</div>,
}));
// Mock with implementation to track calls and arguments
const extractLanguageCodesMock = vi.fn().mockReturnValue(["default", "en", "fr"]);
const createI18nStringMock = vi.fn().mockImplementation((text, _) => ({
default: text,
en: "",
fr: "",
}));
vi.mock("@/lib/i18n/utils", () => ({
extractLanguageCodes: (languages: any) => extractLanguageCodesMock(languages),
createI18nString: (text: string, languages: string[]) => createI18nStringMock(text, languages),
}));
describe("DateQuestionForm", () => {
afterEach(() => {
cleanup();
});
const mockQuestion: TSurveyDateQuestion = {
id: "q1",
type: TSurveyQuestionTypeEnum.Date,
headline: {
default: "Select a date",
en: "Select a date",
fr: "Sélectionnez une date",
},
required: true,
format: "M-d-y",
// Note: subheader is intentionally undefined for this test
};
const mockLocalSurvey = {
id: "survey1",
environmentId: "env1",
name: "Test Survey",
createdAt: new Date(),
updatedAt: new Date(),
status: "draft",
questions: [mockQuestion],
welcomeCard: {
enabled: true,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
},
languages: [
{
default: true,
language: {
code: "en",
} as unknown as TLanguage,
} as TSurveyLanguage,
{
default: false,
language: {
code: "fr",
} as unknown as TLanguage,
} as TSurveyLanguage,
],
endings: [],
} as unknown as TSurvey;
const mockUpdateQuestion = vi.fn();
const mockSetSelectedLanguageCode = vi.fn();
test("should render the headline input field with the correct label and value", () => {
render(
<DateQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="default"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
/>
);
// Check if the headline input field is rendered with the correct label and value
const headlineInput = screen.getByTestId("question-form-input-headline");
expect(headlineInput).toBeInTheDocument();
expect(headlineInput).toHaveAttribute("data-label", "environments.surveys.edit.question*");
expect(headlineInput).toHaveAttribute("data-value", "Select a date");
});
test("should display the 'Add Description' button when the question object has an undefined subheader property", async () => {
// Reset mocks to ensure clean state
mockUpdateQuestion.mockReset();
// Set up mocks for this specific test
extractLanguageCodesMock.mockReturnValueOnce(["default", "en", "fr"]);
createI18nStringMock.mockReturnValueOnce({
default: "",
en: "",
fr: "",
});
const user = userEvent.setup();
render(
<DateQuestionForm
localSurvey={mockLocalSurvey}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="default"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
/>
);
// Check if the 'Add Description' button is rendered
const addDescriptionButton = screen.getByTestId("add-description-button");
expect(addDescriptionButton).toBeInTheDocument();
expect(addDescriptionButton).toHaveTextContent("environments.surveys.edit.add_description");
// Check if the button has the correct properties
expect(addDescriptionButton).toHaveAttribute("data-size", "sm");
expect(addDescriptionButton).toHaveAttribute("data-variant", "secondary");
expect(addDescriptionButton).toHaveAttribute("type", "button");
// Check if the PlusIcon is rendered inside the button
const plusIcon = screen.getByTestId("plus-icon");
expect(plusIcon).toBeInTheDocument();
// Click the button and verify that updateQuestion is called with the correct parameters
await user.click(addDescriptionButton);
// Verify the mock was called correctly
expect(mockUpdateQuestion).toHaveBeenCalledTimes(1);
// Use a more flexible assertion that doesn't rely on exact structure matching
expect(mockUpdateQuestion).toHaveBeenCalledWith(
0,
expect.objectContaining({
subheader: expect.anything(),
})
);
});
test("should handle empty language configuration when adding a subheader", async () => {
// Create a survey with empty languages array
const mockLocalSurveyWithEmptyLanguages = {
id: "survey1",
environmentId: "env1",
name: "Test Survey",
createdAt: new Date(),
updatedAt: new Date(),
status: "draft",
questions: [mockQuestion],
welcomeCard: {
enabled: true,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
},
languages: [], // Empty languages array
endings: [],
} as unknown as TSurvey;
// Set up the mock to return an empty array when extractLanguageCodes is called with empty languages
extractLanguageCodesMock.mockReturnValueOnce([]);
// Set up createI18nString mock to return an empty i18n object
createI18nStringMock.mockReturnValueOnce({ default: "" });
render(
<DateQuestionForm
localSurvey={mockLocalSurveyWithEmptyLanguages}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="default"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
/>
);
// Verify the "Add Description" button is rendered since subheader is undefined
const addDescriptionButton = screen.getByTestId("add-description-button");
expect(addDescriptionButton).toBeInTheDocument();
expect(addDescriptionButton).toHaveTextContent("environments.surveys.edit.add_description");
// Click the "Add Description" button
const user = userEvent.setup();
await user.click(addDescriptionButton);
// Verify extractLanguageCodes was called with the empty languages array
expect(extractLanguageCodesMock).toHaveBeenCalledWith([]);
// Verify createI18nString was called with empty string and empty array
expect(createI18nStringMock).toHaveBeenCalledWith("", []);
// Verify updateQuestion was called with the correct parameters
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, {
subheader: { default: "" },
});
});
test("should handle malformed language configuration when adding a subheader", async () => {
// Create a survey with malformed languages array (missing required properties)
const mockLocalSurveyWithMalformedLanguages: TSurvey = {
id: "survey1",
environmentId: "env1",
name: "Test Survey",
createdAt: new Date(),
updatedAt: new Date(),
status: "draft",
questions: [mockQuestion],
welcomeCard: {
enabled: true,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
},
// @ts-ignore - Intentionally malformed for testing
languages: [{ default: true }], // Missing language object
endings: [],
};
// Set up the mock to return a fallback array when extractLanguageCodes is called with malformed languages
extractLanguageCodesMock.mockReturnValueOnce(["default"]);
// Set up createI18nString mock to return an i18n object with default language
createI18nStringMock.mockReturnValueOnce({ default: "" });
render(
<DateQuestionForm
localSurvey={mockLocalSurveyWithMalformedLanguages}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="default"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
/>
);
// Verify the "Add Description" button is rendered
const addDescriptionButton = screen.getByTestId("add-description-button");
expect(addDescriptionButton).toBeInTheDocument();
// Click the "Add Description" button
const user = userEvent.setup();
await user.click(addDescriptionButton);
// Verify extractLanguageCodes was called with the malformed languages array
expect(extractLanguageCodesMock).toHaveBeenCalledWith([{ default: true }]);
// Verify createI18nString was called with empty string and the extracted language codes
expect(createI18nStringMock).toHaveBeenCalledWith("", ["default"]);
// Verify updateQuestion was called with the correct parameters
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, {
subheader: { default: "" },
});
});
test("should handle null language configuration when adding a subheader", async () => {
// Create a survey with null languages property
const mockLocalSurveyWithNullLanguages: TSurvey = {
id: "survey1",
environmentId: "env1",
name: "Test Survey",
createdAt: new Date(),
updatedAt: new Date(),
status: "draft",
questions: [mockQuestion],
welcomeCard: {
enabled: true,
headline: { default: "Welcome" },
html: { default: "" },
} as unknown as TSurvey["welcomeCard"],
hiddenFields: {
enabled: false,
},
// @ts-ignore - Intentionally set to null for testing
languages: null,
endings: [],
};
// Set up the mock to return an empty array when extractLanguageCodes is called with null
extractLanguageCodesMock.mockReturnValueOnce([]);
// Set up createI18nString mock to return an empty i18n object
createI18nStringMock.mockReturnValueOnce({ default: "" });
render(
<DateQuestionForm
localSurvey={mockLocalSurveyWithNullLanguages}
question={mockQuestion}
questionIdx={0}
updateQuestion={mockUpdateQuestion}
selectedLanguageCode="default"
setSelectedLanguageCode={mockSetSelectedLanguageCode}
isInvalid={false}
locale="en-US"
/>
);
// Verify the "Add Description" button is rendered
const addDescriptionButton = screen.getByTestId("add-description-button");
expect(addDescriptionButton).toBeInTheDocument();
// Click the "Add Description" button
const user = userEvent.setup();
await user.click(addDescriptionButton);
// Verify extractLanguageCodes was called with null
expect(extractLanguageCodesMock).toHaveBeenCalledWith(null);
// Verify createI18nString was called with empty string and empty array
expect(createI18nStringMock).toHaveBeenCalledWith("", []);
// Verify updateQuestion was called with the correct parameters
expect(mockUpdateQuestion).toHaveBeenCalledWith(0, {
subheader: { default: "" },
});
});
});

View File

@@ -17,7 +17,6 @@ interface IDateQuestionFormProps {
question: TSurveyDateQuestion;
questionIdx: number;
updateQuestion: (questionIdx: number, updatedAttributes: Partial<TSurveyDateQuestion>) => void;
lastQuestion: boolean;
selectedLanguageCode: string;
setSelectedLanguageCode: (language: string) => void;
isInvalid: boolean;

View File

@@ -0,0 +1,69 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { TOrganizationBillingPlan } from "@formbricks/types/organizations";
import { TLanguage } from "@formbricks/types/project";
import { TSurvey, TSurveyEndScreenCard, TSurveyLanguage } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { EditEndingCard } from "./edit-ending-card";
vi.mock("./end-screen-form", () => ({
EndScreenForm: vi.fn(() => <div data-testid="end-screen-form">EndScreenForm</div>),
}));
describe("EditEndingCard", () => {
afterEach(() => {
cleanup();
});
test("should render the EndScreenForm when the ending card type is 'endScreen'", () => {
const endingCardId = "ending1";
const localSurvey = {
id: "testSurvey",
name: "Test Survey",
languages: [
{ language: { code: "en", name: "English" } as unknown as TLanguage } as unknown as TSurveyLanguage,
],
createdAt: new Date(),
updatedAt: new Date(),
type: "link",
questions: [],
endings: [
{
id: endingCardId,
type: "endScreen",
headline: { en: "Thank you!" },
} as TSurveyEndScreenCard,
],
followUps: [],
welcomeCard: { enabled: false, headline: { en: "" } } as unknown as TSurvey["welcomeCard"],
} as unknown as TSurvey;
const setLocalSurvey = vi.fn();
const setActiveQuestionId = vi.fn();
const selectedLanguageCode = "en";
const setSelectedLanguageCode = vi.fn();
const plan: TOrganizationBillingPlan = "free";
const addEndingCard = vi.fn();
const isFormbricksCloud = false;
const locale: TUserLocale = "en-US";
render(
<EditEndingCard
localSurvey={localSurvey}
endingCardIndex={0}
setLocalSurvey={setLocalSurvey}
setActiveQuestionId={setActiveQuestionId}
activeQuestionId={endingCardId}
isInvalid={false}
selectedLanguageCode={selectedLanguageCode}
setSelectedLanguageCode={setSelectedLanguageCode}
plan={plan}
addEndingCard={addEndingCard}
isFormbricksCloud={isFormbricksCloud}
locale={locale}
/>
);
expect(screen.getByTestId("end-screen-form")).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,159 @@
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, test, vi } from "vitest";
import { EditorCardMenu } from "./editor-card-menu";
describe("EditorCardMenu", () => {
afterEach(() => {
cleanup();
});
test("should move the card up when the 'Move Up' button is clicked and the card is not the first one", async () => {
const moveCard = vi.fn();
const cardIdx = 1;
render(
<EditorCardMenu
survey={{ questions: [] } as any}
cardIdx={cardIdx}
lastCard={false}
duplicateCard={vi.fn()}
deleteCard={vi.fn()}
moveCard={moveCard}
card={{ type: "openText" } as any}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
const moveUpButton = screen.getAllByRole("button")[0];
await userEvent.click(moveUpButton);
expect(moveCard).toHaveBeenCalledWith(cardIdx, true);
});
test("should move the card down when the 'Move Down' button is clicked and the card is not the last one", async () => {
const moveCard = vi.fn();
const cardIdx = 0;
render(
<EditorCardMenu
survey={{ questions: [] } as any}
cardIdx={cardIdx}
lastCard={false}
duplicateCard={vi.fn()}
deleteCard={vi.fn()}
moveCard={moveCard}
card={{ type: "openText" } as any}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
const moveDownButton = screen.getAllByRole("button")[1];
await userEvent.click(moveDownButton);
expect(moveCard).toHaveBeenCalledWith(cardIdx, false);
});
test("should duplicate the card when the 'Duplicate' button is clicked", async () => {
const duplicateCard = vi.fn();
const cardIdx = 1;
render(
<EditorCardMenu
survey={{ questions: [] } as any}
cardIdx={cardIdx}
lastCard={false}
duplicateCard={duplicateCard}
deleteCard={vi.fn()}
moveCard={vi.fn()}
card={{ type: "openText" } as any}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
const duplicateButton = screen.getAllByRole("button")[2];
await userEvent.click(duplicateButton);
expect(duplicateCard).toHaveBeenCalledWith(cardIdx);
});
test("should disable the delete button when the card is the only one left in the survey", () => {
const survey = {
questions: [{ id: "1", type: "openText" }],
type: "link",
endings: [],
} as any;
render(
<EditorCardMenu
survey={survey}
cardIdx={0}
lastCard={true}
duplicateCard={vi.fn()}
deleteCard={vi.fn()}
moveCard={vi.fn()}
card={survey.questions[0]}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
// Find the button with the trash icon (4th button in the menu)
const deleteButton = screen.getAllByRole("button")[3];
expect(deleteButton).toBeDisabled();
});
test("should disable 'Move Up' button when the card is the first card", () => {
const moveCard = vi.fn();
const cardIdx = 0;
render(
<EditorCardMenu
survey={{ questions: [] } as any}
cardIdx={cardIdx}
lastCard={false}
duplicateCard={vi.fn()}
deleteCard={vi.fn()}
moveCard={moveCard}
card={{ type: "openText" } as any}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
const moveUpButton = screen.getAllByRole("button")[0];
expect(moveUpButton).toBeDisabled();
});
test("should disable 'Move Down' button when the card is the last card", () => {
const moveCard = vi.fn();
const cardIdx = 1;
const lastCard = true;
render(
<EditorCardMenu
survey={{ questions: [] } as any}
cardIdx={cardIdx}
lastCard={lastCard}
duplicateCard={vi.fn()}
deleteCard={vi.fn()}
moveCard={moveCard}
card={{ type: "openText" } as any}
updateCard={vi.fn()}
addCard={vi.fn()}
cardType="question"
/>
);
const moveDownButton = screen.getAllByRole("button")[1];
expect(moveDownButton).toBeDisabled();
});
});

Some files were not shown because too many files have changed in this diff Show More