Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions e2e/tests-dfx/assetscanister.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2108,3 +2108,139 @@ EOF
assert_command dfx canister call e2e_project_frontend list_permitted '(record { permission = variant { ManagePermissions }; })'
assert_match "$(dfx identity get-principal)"
}

@test "ic_env cookie is set for html files" {
install_asset assetscanister
dfx_start

touch src/e2e_project_frontend/assets/index.html
echo "<html><body>Test</body></html>" > src/e2e_project_frontend/assets/index.html

dfx deploy

ID=$(dfx canister id e2e_project_frontend)
PORT=$(get_webserver_port)

IC_ENV_COOKIE_REGEX="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+; SameSite=Lax"

# Request HTML file and verify ic_env cookie is set
assert_command curl -v "http://$ID.localhost:$PORT/index.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX"
}

@test "ic_env cookie contains PUBLIC_ environment variables" {
install_asset assetscanister
dfx_start

touch src/e2e_project_frontend/assets/index.html
echo "<html><body>Test</body></html>" > src/e2e_project_frontend/assets/index.html

dfx canister create --all
dfx build --all
dfx canister install e2e_project_frontend

ID=$(dfx canister id e2e_project_frontend)

set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 PUBLIC_TEST2=value2

dfx deploy

PORT=$(get_webserver_port)

IC_ENV_COOKIE_REGEX="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1%26PUBLIC%5FTEST2%3Dvalue2; SameSite=Lax"

# Request HTML file and verify cookie contains PUBLIC_ variables
assert_command curl -v "http://$ID.localhost:$PORT/index.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX"
}

@test "ic_env cookie is not set for non-html files" {
install_asset assetscanister
dfx_start

touch src/e2e_project_frontend/assets/test.txt
echo "plain text file" > src/e2e_project_frontend/assets/test.txt
touch src/e2e_project_frontend/assets/script.js
echo "console.log('test');" > src/e2e_project_frontend/assets/script.js

dfx deploy

ID=$(dfx canister id e2e_project_frontend)
PORT=$(get_webserver_port)

# Request text file and verify no ic_env cookie is set
assert_command curl -v "http://$ID.localhost:$PORT/test.txt"
assert_not_match "set-cookie: ic_env="

# Request JS file and verify no ic_env cookie is set
assert_command curl -v "http://$ID.localhost:$PORT/script.js"
assert_not_match "set-cookie: ic_env="
}

@test "ic_env cookie is set only for PUBLIC_ prefixed environment variables" {
install_asset assetscanister
dfx_start

touch src/e2e_project_frontend/assets/app.html
echo "<html><body>App</body></html>" > src/e2e_project_frontend/assets/app.html

dfx canister create --all
dfx build --all
dfx canister install e2e_project_frontend

ID=$(dfx canister id e2e_project_frontend)

set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 TEST2=value2

dfx deploy

PORT=$(get_webserver_port)

IC_ENV_COOKIE_REGEX_1="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1; SameSite=Lax"

assert_command curl -v "http://$ID.localhost:$PORT/app.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_1"

# Redeploy with no PUBLIC_ prefixed (case sensitive!) environment variables
set_canister_environment_variables "$ID" public_TEST1=value1 TEST2=value2
dfx deploy

IC_ENV_COOKIE_REGEX_2="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+; SameSite=Lax"

assert_command curl -v "http://$ID.localhost:$PORT/app.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_2"
}

@test "ic_env cookie updates on redeploy with new environment variables" {
install_asset assetscanister
dfx_start

touch src/e2e_project_frontend/assets/app.html
echo "<html><body>App</body></html>" > src/e2e_project_frontend/assets/app.html

dfx canister create --all
dfx build --all
dfx canister install e2e_project_frontend

ID=$(dfx canister id e2e_project_frontend)

set_canister_environment_variables "$ID" PUBLIC_TEST1=value1

dfx deploy

PORT=$(get_webserver_port)

IC_ENV_COOKIE_REGEX_1="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1; SameSite=Lax"

assert_command curl -v "http://$ID.localhost:$PORT/app.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_1"

# Redeploy with additional PUBLIC_ variable
set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 PUBLIC_TEST2=value2
dfx deploy

IC_ENV_COOKIE_REGEX_2="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1%26PUBLIC%5FTEST2%3Dvalue2; SameSite=Lax"

assert_command curl -v "http://$ID.localhost:$PORT/app.html"
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_2"
}
21 changes: 21 additions & 0 deletions e2e/utils/_.bash
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,24 @@ stop_and_delete() {
assert_command dfx canister delete -y --no-withdrawal "$1"
echo "Canister $1 deleted"
}

set_canister_environment_variables() {
local canister_id=$1
shift

# Build the Candid vec of records from the remaining arguments
# Each argument should be in format "NAME=VALUE"
local env_records=""
for env_var in "$@"; do
local name="${env_var%%=*}"
local value="${env_var#*=}"
if [ -n "$env_records" ]; then
env_records="$env_records; "
fi
env_records="${env_records}record { name = \"$name\"; value = \"$value\" }"
done

management_canister_id=aaaaa-aa

dfx canister call $management_canister_id update_settings "(record { canister_id = principal \"$canister_id\"; settings = record { environment_variables = opt vec { $env_records } } })"
}