Merge pull request 'metrics' (#375) from stats into develop
[akkoma] / test / pleroma / web / akkoma_api / metrics_controller_test.exs
diff --git a/test/pleroma/web/akkoma_api/metrics_controller_test.exs b/test/pleroma/web/akkoma_api/metrics_controller_test.exs
new file mode 100644 (file)
index 0000000..9482f13
--- /dev/null
@@ -0,0 +1,33 @@
+defmodule Pleroma.Web.AkkomaAPI.MetricsControllerTest do
+  use Pleroma.Web.ConnCase, async: true
+
+  describe "GET /api/v1/akkoma/metrics" do
+    test "should return metrics when the user has admin:metrics" do
+      %{conn: conn} = oauth_access(["admin:metrics"])
+
+      resp =
+        conn
+        |> get("/api/v1/akkoma/metrics")
+        |> text_response(200)
+
+      assert resp =~ "# HELP"
+    end
+
+    test "should not allow users that do not have the admin:metrics scope" do
+      %{conn: conn} = oauth_access(["read:metrics"])
+
+      conn
+      |> get("/api/v1/akkoma/metrics")
+      |> json_response(403)
+    end
+
+    test "should be disabled by export_prometheus_metrics" do
+      clear_config([:instance, :export_prometheus_metrics], false)
+      %{conn: conn} = oauth_access(["admin:metrics"])
+
+      conn
+      |> get("/api/v1/akkoma/metrics")
+      |> response(404)
+    end
+  end
+end