Files
triggerdotdev--trigger.dev/apps/webapp/test/environmentVariableRules.test.ts
Matt Aitken 068c024477 Preview branches (#2086)
* Initial preview migrations

* Modified the staging endpoint to create preview environments

* Added isBranchableEnvironment to RuntimeEnvironment

* Staging = yellow Preview = orange

* Changed the env sort order

* Set isBranchableEnvironment correctly. Create preview for new projects

* Very basic branch menu

* Creating branches from the dashboard

* Fix for string icons on project delete page

* Don’t show branch API keys

* WIP on the manage branches page

* RuntimeEnvironment added projectId index

* Only create the parentEnvironmentId column if it doesn’t exist already

* Improved the limit wording

* Add search to the branch list

* contains in both places

* Many style improvements

* Branch dropdown and v4 badge

* Arching/unarchive branches working in the dashboard

* Tidied imports

* Change preview slug from `prev` to `preview`

* Use correct color for side menu preview branch icon

* Upsert the branch and use the shortcode as a unique constraint

* Upserting working with nice messages in the dashboard

* Better errors when upserting branches

* Button shortcut, don’t allow event to propagate

* Better duplicate error message

* Filter out archived branches from the env selector

* Archiving/creating tweaked some more

* Add an archived banner to the app, fixes for archived branches and upsells

* Fixed pagination

* Disable editing schedules, pausing queues, testing tasks

* Don’t allow replaying if the env is archived

* When deploying detect the correct environment

* Get the projectClient when there’s a branch

* createGitMeta function, most code from the vercel CLI repo

* Deploy, getting the correct environment client

* Added git column to WorkerDeployment

* Add GitMeta to core schemas

* Create branch when deploying

* WIP on branch support in the API

* Delete old createTaskRunAttempt fn

* apiAuth remove export from internal functions

* Rename env var to “TRIGGER_PREVIEW_BRANCH”

* Add TRIGGER_PREVIEW_BRANCH to resolved env vars for runs

* First preview deploy and run working

* Set the preview branch in the main SDK

* Added git links to the preview branches table

* Better errors when replaying/testing archived branches

* Don’t dequeue archived environments

* Env var resolution with parent environment

* Hello world default machine small-2x to save my memory

* Fix for more env var functions

* Only return non-archived envs

* Switch to controlled state for the checkboxes

* Uncheck everything when PREVIEW is checked

* WIP on branch UI

* Show the preview branch label on the env vars list

* Fix for overriding env vars

* Adding preview branch env vars working

* Progress on new env vars

* Only allow selecting a single branch

* Layout fix when there are errors

* Set the defaultValue so there are some fields

* Conform fix for team invite page

* Archived environments don’t run scheduled tasks

* Added Git data to deployments

* Added git data to the deployment inspector

* Don’t allow upserting schedules when archived

* Deduplicate and blacklist some env vars

* Fix for wrong conform function being used

* Show a better error if all vars were blacklisted

* Added environment variable search (by key and value)

* Improved preview branch icon

* Replay now supports branches

* Schedule page render branches properly

* Show the env icon in bottom-left of the test page

* When editing older schedules (that have multi-env) show preview branches correctly

* Fix for incorrect disallowed branch name character

* Extract and improve the directory verification code

* WIP for CLI preview archive command

* Improved the preview branch action buttons

* Redirect to the project if we don’t find a matching env

* Archiving branch via the CLI working

* Fix for archiving branches

* Public access token test task

* JWTs working are with preview branches

* Add branch and git data to the Run ctx

* Updated GitMeta functions to work in CI

* Added pullRequestState

* Archive when deploying if the PR is closed/merged

* Fix for the changesets guide

* Fix for CLI dev bug introduced

* CLI promote now supports preview branches

* Add PR title. Reordered them and added tooltips

* syncEnvVars working with branches

* Added preview branch support to syncVercelEnvVars()

* Detect the branch from Vercel env var (set during build)

* Allow passing a branch in

* Use process.env.VERCEL_TOKEN as well… this used in Vercel CI

* Temp delete

* Improved regenerate api key modal

* Added Accordion component (with styles)

* Redesigned the API keys page

* Revert "Temp delete"

This reverts commit 177b92cd935a6161456bde65d01294e23ecfd47f.

* Changeset

* Fixed docs link

* The new branch panel closes when a branch is created

* Update apps/webapp/app/services/upsertBranch.server.ts

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Removed findUniques from WorkerGroupTokenService

* Made the parentEnvironmentId migrations safe

* Latest lockfile

* Update packages/cli-v3/src/commands/workers/build.ts

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Move isValidGitBranchName to a separate file

* Move the sanitize fn too

* removeBlacklistedVariables moved to a separate file

* Moved deduplicateVariableArray to a separate file…

* Fix broken sanitizeBranchName import

* Another import fix…

* Improved blacklisted error message

* SImplified migration to use `ADD COLUMN IF NOT EXISTS "parentEnvironmentId" TEXT`

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-05-28 11:02:56 +01:00

113 lines
4.0 KiB
TypeScript

import { describe, it, expect } from "vitest";
import type { EnvironmentVariable } from "../app/v3/environmentVariables/repository";
import { removeBlacklistedVariables } from "~/v3/environmentVariableRules.server";
describe("removeBlacklistedVariables", () => {
it("should remove exact match blacklisted variables", () => {
const variables: EnvironmentVariable[] = [
{ key: "TRIGGER_SECRET_KEY", value: "secret123" },
{ key: "TRIGGER_API_URL", value: "https://api.example.com" },
{ key: "NORMAL_VAR", value: "normal" },
];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([{ key: "NORMAL_VAR", value: "normal" }]);
});
it("should remove variables with blacklisted prefixes", () => {
const variables: EnvironmentVariable[] = [
{ key: "OTEL_SERVICE_NAME", value: "my-service" },
{ key: "OTEL_TRACE_SAMPLER", value: "always_on" },
{ key: "NORMAL_VAR", value: "normal" },
];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([{ key: "NORMAL_VAR", value: "normal" }]);
});
it("should keep whitelisted variables even if they match a blacklisted prefix", () => {
const variables: EnvironmentVariable[] = [
{ key: "OTEL_LOG_LEVEL", value: "debug" },
{ key: "OTEL_SERVICE_NAME", value: "my-service" },
{ key: "NORMAL_VAR", value: "normal" },
];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([
{ key: "OTEL_LOG_LEVEL", value: "debug" },
{ key: "NORMAL_VAR", value: "normal" },
]);
});
it("should handle empty input array", () => {
const variables: EnvironmentVariable[] = [];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([]);
});
it("should handle mixed case variables", () => {
const variables: EnvironmentVariable[] = [
{ key: "trigger_secret_key", value: "secret123" }, // Different case
{ key: "OTEL_LOG_LEVEL", value: "debug" },
{ key: "otel_service_name", value: "my-service" }, // Different case
{ key: "NORMAL_VAR", value: "normal" },
];
const result = removeBlacklistedVariables(variables);
// Should keep only the whitelisted OTEL_LOG_LEVEL and NORMAL_VAR
// Note: The function is case-sensitive, so different case variables should pass through
expect(result).toEqual([
{ key: "trigger_secret_key", value: "secret123" },
{ key: "OTEL_LOG_LEVEL", value: "debug" },
{ key: "otel_service_name", value: "my-service" },
{ key: "NORMAL_VAR", value: "normal" },
]);
});
it("should handle variables with empty values", () => {
const variables: EnvironmentVariable[] = [
{ key: "TRIGGER_SECRET_KEY", value: "" },
{ key: "OTEL_SERVICE_NAME", value: "" },
{ key: "OTEL_LOG_LEVEL", value: "" },
{ key: "NORMAL_VAR", value: "" },
];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([
{ key: "OTEL_LOG_LEVEL", value: "" },
{ key: "NORMAL_VAR", value: "" },
]);
});
it("should handle all types of rules in a single array", () => {
const variables: EnvironmentVariable[] = [
// Exact matches (should be removed)
{ key: "TRIGGER_SECRET_KEY", value: "secret123" },
{ key: "TRIGGER_API_URL", value: "https://api.example.com" },
// Prefix matches (should be removed)
{ key: "OTEL_SERVICE_NAME", value: "my-service" },
{ key: "OTEL_TRACE_SAMPLER", value: "always_on" },
// Whitelist exception (should be kept)
{ key: "OTEL_LOG_LEVEL", value: "debug" },
// Normal variables (should be kept)
{ key: "NORMAL_VAR", value: "normal" },
{ key: "DATABASE_URL", value: "postgres://..." },
];
const result = removeBlacklistedVariables(variables);
expect(result).toEqual([
{ key: "OTEL_LOG_LEVEL", value: "debug" },
{ key: "NORMAL_VAR", value: "normal" },
{ key: "DATABASE_URL", value: "postgres://..." },
]);
});
});