Merge branch '1668-prometheus-access-restrictions' into 'develop'
[akkoma] / test / pleroma / web / endpoint / metrics_exporter_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Endpoint.MetricsExporterTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Web.Endpoint.MetricsExporter
9
10 defp config do
11 Application.get_env(:prometheus, MetricsExporter)
12 end
13
14 describe "with default config" do
15 test "does NOT expose app metrics", %{conn: conn} do
16 conn
17 |> get(config()[:path])
18 |> json_response(404)
19 end
20 end
21
22 describe "when enabled" do
23 setup do
24 initial_config = config()
25 on_exit(fn -> Application.put_env(:prometheus, MetricsExporter, initial_config) end)
26
27 Application.put_env(
28 :prometheus,
29 MetricsExporter,
30 Keyword.put(initial_config, :enabled, true)
31 )
32 end
33
34 test "serves app metrics", %{conn: conn} do
35 conn = get(conn, config()[:path])
36 assert response = response(conn, 200)
37
38 for metric <- [
39 "http_requests_total",
40 "http_request_duration_microseconds",
41 "phoenix_controller_render_duration",
42 "phoenix_controller_call_duration",
43 "telemetry_scrape_duration",
44 "erlang_vm_memory_atom_bytes_total"
45 ] do
46 assert response =~ ~r/#{metric}/
47 end
48 end
49
50 test "when IP whitelist configured, " <>
51 "serves app metrics only if client IP is whitelisted",
52 %{conn: conn} do
53 Application.put_env(
54 :prometheus,
55 MetricsExporter,
56 Keyword.put(config(), :ip_whitelist, ["127.127.127.127", {1, 1, 1, 1}, '255.255.255.255'])
57 )
58
59 conn
60 |> get(config()[:path])
61 |> json_response(404)
62
63 conn
64 |> Map.put(:remote_ip, {127, 127, 127, 127})
65 |> get(config()[:path])
66 |> response(200)
67 end
68 end
69 end