|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +defmodule FlagdUiWeb.FeatureControllerTest do |
| 5 | + use FlagdUiWeb.ConnCase |
| 6 | + |
| 7 | + setup do |
| 8 | + file_content = GenServer.call(Storage, :read) |
| 9 | + |
| 10 | + on_exit(fn -> |
| 11 | + GenServer.cast(Storage, {:replace, Jason.encode!(file_content)}) |
| 12 | + end) |
| 13 | + end |
| 14 | + |
| 15 | + test "GET /api/read", %{conn: conn} do |
| 16 | + conn = get(conn, ~p"/api/read") |
| 17 | + |
| 18 | + assert %{ |
| 19 | + "flags" => %{ |
| 20 | + "adFailure" => %{ |
| 21 | + "defaultVariant" => "off", |
| 22 | + "description" => "Fail ad service", |
| 23 | + "state" => "ENABLED", |
| 24 | + "variants" => %{"off" => false, "on" => true} |
| 25 | + }, |
| 26 | + "adHighCpu" => %{ |
| 27 | + "defaultVariant" => "off", |
| 28 | + "description" => "Triggers high cpu load in the ad service", |
| 29 | + "state" => "ENABLED", |
| 30 | + "variants" => %{"off" => false, "on" => true} |
| 31 | + } |
| 32 | + } |
| 33 | + } = json_response(conn, 200) |
| 34 | + end |
| 35 | + |
| 36 | + test "GET /api/read-file (legacy API)", %{conn: conn} do |
| 37 | + conn = get(conn, ~p"/api/read-file") |
| 38 | + |
| 39 | + assert %{ |
| 40 | + "flags" => %{ |
| 41 | + "adFailure" => %{ |
| 42 | + "defaultVariant" => "off", |
| 43 | + "description" => "Fail ad service", |
| 44 | + "state" => "ENABLED", |
| 45 | + "variants" => %{"off" => false, "on" => true} |
| 46 | + }, |
| 47 | + "adHighCpu" => %{ |
| 48 | + "defaultVariant" => "off", |
| 49 | + "description" => "Triggers high cpu load in the ad service", |
| 50 | + "state" => "ENABLED", |
| 51 | + "variants" => %{"off" => false, "on" => true} |
| 52 | + } |
| 53 | + } |
| 54 | + } = json_response(conn, 200) |
| 55 | + end |
| 56 | + |
| 57 | + test "POST /api/write", %{conn: conn} do |
| 58 | + data = %{ |
| 59 | + "flags" => %{ |
| 60 | + "adFailure" => %{ |
| 61 | + "defaultVariant" => "off", |
| 62 | + "description" => "Fail ad service", |
| 63 | + "state" => "ENABLED", |
| 64 | + "variants" => %{"off" => true, "on" => false} |
| 65 | + }, |
| 66 | + "adHighCpu" => %{ |
| 67 | + "defaultVariant" => "off", |
| 68 | + "description" => "Triggers high cpu load in the ad service", |
| 69 | + "state" => "ENABLED", |
| 70 | + "variants" => %{"off" => true, "on" => false} |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + conn = post(conn, ~p"/api/write", %{"data" => data}) |
| 76 | + |
| 77 | + conn = get(conn, ~p"/api/read") |
| 78 | + |
| 79 | + assert %{ |
| 80 | + "flags" => %{ |
| 81 | + "adFailure" => %{ |
| 82 | + "defaultVariant" => "off", |
| 83 | + "description" => "Fail ad service", |
| 84 | + "state" => "ENABLED", |
| 85 | + "variants" => %{"off" => true, "on" => false} |
| 86 | + }, |
| 87 | + "adHighCpu" => %{ |
| 88 | + "defaultVariant" => "off", |
| 89 | + "description" => "Triggers high cpu load in the ad service", |
| 90 | + "state" => "ENABLED", |
| 91 | + "variants" => %{"off" => true, "on" => false} |
| 92 | + } |
| 93 | + } |
| 94 | + } = json_response(conn, 200) |
| 95 | + end |
| 96 | + |
| 97 | + test "POST /api/write-to-file (legacy API)", %{conn: conn} do |
| 98 | + data = %{ |
| 99 | + "flags" => %{ |
| 100 | + "adFailure" => %{ |
| 101 | + "defaultVariant" => "off", |
| 102 | + "description" => "Fail ad service", |
| 103 | + "state" => "ENABLED", |
| 104 | + "variants" => %{"off" => true, "on" => false} |
| 105 | + }, |
| 106 | + "adHighCpu" => %{ |
| 107 | + "defaultVariant" => "off", |
| 108 | + "description" => "Triggers high cpu load in the ad service", |
| 109 | + "state" => "ENABLED", |
| 110 | + "variants" => %{"off" => true, "on" => false} |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + conn = post(conn, ~p"/api/write-to-file", %{"data" => data}) |
| 116 | + |
| 117 | + conn = get(conn, ~p"/api/read-file") |
| 118 | + |
| 119 | + assert %{ |
| 120 | + "flags" => %{ |
| 121 | + "adFailure" => %{ |
| 122 | + "defaultVariant" => "off", |
| 123 | + "description" => "Fail ad service", |
| 124 | + "state" => "ENABLED", |
| 125 | + "variants" => %{"off" => true, "on" => false} |
| 126 | + }, |
| 127 | + "adHighCpu" => %{ |
| 128 | + "defaultVariant" => "off", |
| 129 | + "description" => "Triggers high cpu load in the ad service", |
| 130 | + "state" => "ENABLED", |
| 131 | + "variants" => %{"off" => true, "on" => false} |
| 132 | + } |
| 133 | + } |
| 134 | + } = json_response(conn, 200) |
| 135 | + end |
| 136 | +end |
0 commit comments