[#3213] Hashtag-filtering functions in ActivityPub. Mix task for migrating hashtags...
[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_call_duration",
42 "telemetry_scrape_duration",
43 "erlang_vm_memory_atom_bytes_total"
44 ] do
45 assert response =~ ~r/#{metric}/
46 end
47 end
48
49 test "when IP whitelist configured, " <>
50 "serves app metrics only if client IP is whitelisted",
51 %{conn: conn} do
52 Application.put_env(
53 :prometheus,
54 MetricsExporter,
55 Keyword.put(config(), :ip_whitelist, ["127.127.127.127", {1, 1, 1, 1}, '255.255.255.255'])
56 )
57
58 conn
59 |> get(config()[:path])
60 |> json_response(404)
61
62 conn
63 |> Map.put(:remote_ip, {127, 127, 127, 127})
64 |> get(config()[:path])
65 |> response(200)
66 end
67 end
68 end