|
| 1 | +# |
| 2 | +# This file is part of Edgehog. |
| 3 | +# |
| 4 | +# Copyright 2026 SECO Mind Srl |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: Apache-2.0 |
| 19 | +# |
| 20 | + |
| 21 | +defmodule EdgehogWeb.Schema.Subscriptions.CheckOriginConfigTest do |
| 22 | + use ExUnit.Case, async: false |
| 23 | + |
| 24 | + defp read_endpoint_config!(config_file, env) do |
| 25 | + config_file |
| 26 | + |> Config.Reader.read!(env: env, target: :host) |
| 27 | + |> Keyword.get(:edgehog, []) |
| 28 | + |> Keyword.fetch!(EdgehogWeb.Endpoint) |
| 29 | + end |
| 30 | + |
| 31 | + defp with_env(overrides, fun) do |
| 32 | + previous = for {key, _value} <- overrides, into: %{}, do: {key, System.get_env(key)} |
| 33 | + |
| 34 | + Enum.each(overrides, fn |
| 35 | + {key, nil} -> System.delete_env(key) |
| 36 | + {key, value} -> System.put_env(key, value) |
| 37 | + end) |
| 38 | + |
| 39 | + try do |
| 40 | + fun.() |
| 41 | + after |
| 42 | + Enum.each(previous, fn |
| 43 | + {key, nil} -> System.delete_env(key) |
| 44 | + {key, value} -> System.put_env(key, value) |
| 45 | + end) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe "config/dev.exs check_origin" do |
| 50 | + test "defaults to localhost origins when env var is not set" do |
| 51 | + with_env([{"CHECK_ORIGIN_ALLOWED_ORIGINS", nil}], fn -> |
| 52 | + endpoint = read_endpoint_config!("config/dev.exs", :dev) |
| 53 | + |
| 54 | + assert Keyword.fetch!(endpoint, :check_origin) == ["//localhost", "//127.0.0.1"] |
| 55 | + end) |
| 56 | + end |
| 57 | + |
| 58 | + test "uses comma-separated CHECK_ORIGIN_ALLOWED_ORIGINS" do |
| 59 | + with_env( |
| 60 | + [ |
| 61 | + {"CHECK_ORIGIN_ALLOWED_ORIGINS", "http://localhost:5173, http://edgehog.localhost"} |
| 62 | + ], |
| 63 | + fn -> |
| 64 | + endpoint = read_endpoint_config!("config/dev.exs", :dev) |
| 65 | + |
| 66 | + assert Keyword.fetch!(endpoint, :check_origin) == [ |
| 67 | + "http://localhost:5173", |
| 68 | + "http://edgehog.localhost" |
| 69 | + ] |
| 70 | + end |
| 71 | + ) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + describe "config/runtime.exs check_origin" do |
| 76 | + test "defaults to URL_SCHEME://URL_HOST:URL_PORT in prod" do |
| 77 | + with_env( |
| 78 | + [ |
| 79 | + {"DATABASE_USERNAME", "postgres"}, |
| 80 | + {"DATABASE_PASSWORD", "postgres"}, |
| 81 | + {"DATABASE_HOSTNAME", "localhost"}, |
| 82 | + {"DATABASE_NAME", "edgehog"}, |
| 83 | + {"SECRET_KEY_BASE", "test_secret_key_base"}, |
| 84 | + {"URL_HOST", "api.edgehog.localhost"}, |
| 85 | + {"URL_SCHEME", "https"}, |
| 86 | + {"URL_PORT", "443"}, |
| 87 | + {"CHECK_ORIGIN_ALLOWED_ORIGINS", nil} |
| 88 | + ], |
| 89 | + fn -> |
| 90 | + endpoint = read_endpoint_config!("config/runtime.exs", :prod) |
| 91 | + |
| 92 | + assert Keyword.fetch!(endpoint, :check_origin) == ["https://api.edgehog.localhost:443"] |
| 93 | + end |
| 94 | + ) |
| 95 | + end |
| 96 | + |
| 97 | + test "uses CHECK_ORIGIN_ALLOWED_ORIGINS in prod when provided" do |
| 98 | + with_env( |
| 99 | + [ |
| 100 | + {"DATABASE_USERNAME", "postgres"}, |
| 101 | + {"DATABASE_PASSWORD", "postgres"}, |
| 102 | + {"DATABASE_HOSTNAME", "localhost"}, |
| 103 | + {"DATABASE_NAME", "edgehog"}, |
| 104 | + {"SECRET_KEY_BASE", "test_secret_key_base"}, |
| 105 | + {"URL_HOST", "api.edgehog.localhost"}, |
| 106 | + {"URL_SCHEME", "https"}, |
| 107 | + {"URL_PORT", "443"}, |
| 108 | + {"CHECK_ORIGIN_ALLOWED_ORIGINS", "https://ui.edgehog.localhost, https://ops.edgehog.localhost"} |
| 109 | + ], |
| 110 | + fn -> |
| 111 | + endpoint = read_endpoint_config!("config/runtime.exs", :prod) |
| 112 | + |
| 113 | + assert Keyword.fetch!(endpoint, :check_origin) == [ |
| 114 | + "https://ui.edgehog.localhost", |
| 115 | + "https://ops.edgehog.localhost" |
| 116 | + ] |
| 117 | + end |
| 118 | + ) |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments