From 76cb7e0f03279ef8dabb2c9ecb708b52f1bdc8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Niemier?= Date: Thu, 21 Sep 2023 20:54:51 +0200 Subject: [PATCH 1/3] chore: fix typo in test name --- test/logflare_web/controllers/endpoints_controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/logflare_web/controllers/endpoints_controller_test.exs b/test/logflare_web/controllers/endpoints_controller_test.exs index 17fb470b6..e22455f0a 100644 --- a/test/logflare_web/controllers/endpoints_controller_test.exs +++ b/test/logflare_web/controllers/endpoints_controller_test.exs @@ -178,7 +178,7 @@ defmodule LogflareWeb.EndpointsControllerTest do assert conn.halted == false end - test "GET a basic sandboxed query with fromt able", %{conn: initial_conn, user: user} do + test "GET a basic sandboxed query with from table", %{conn: initial_conn, user: user} do for source <- Logflare.Repo.all(Source) do Backends.ingest_logs( [%{"event_message" => "some message", "project" => "default"}], From 643b9d7ce1e5d06fc4153d22be44e0a95ea15c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Niemier?= Date: Fri, 22 Sep 2023 14:17:54 +0200 Subject: [PATCH 2/3] fix: make version loading more foolproof --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index ea015e949..48a7f1a28 100644 --- a/mix.exs +++ b/mix.exs @@ -284,5 +284,5 @@ defmodule Logflare.Mixfile do ] end - defp version, do: File.read!("./VERSION") + defp version, do: File.read!(Path.join(__DIR__, "VERSION")) end From 7446aa7d180e10c0564bae2b6a9617ab35d75438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Niemier?= Date: Fri, 22 Sep 2023 14:18:37 +0200 Subject: [PATCH 3/3] chore: move helper functions to parent module to reduce amount of generated code --- test/support/conn_case.ex | 50 ++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 1c27a9ae5..48b3a405a 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -34,6 +34,8 @@ defmodule LogflareWeb.ConnCase do import Phoenix.LiveViewTest import Plug.Conn import Phoenix.VerifiedRoutes + import unquote(__MODULE__) + alias Logflare.TestUtils alias Logflare.User alias Logflare.Partners.Partner @@ -50,30 +52,6 @@ defmodule LogflareWeb.ConnCase do :ok end - - # for browser use - def login_user(conn, user) do - conn - |> Plug.Test.init_test_session(%{user_id: user.id}) - |> Plug.Conn.assign(:user, user) - end - - # for api use - def add_access_token(conn, user, scopes \\ ~w(public)) - - def add_access_token(conn, %User{} = user, scopes) do - scopes = if is_list(scopes), do: Enum.join(scopes, " "), else: scopes - {:ok, access_token} = Logflare.Auth.create_access_token(user, %{scopes: scopes}) - - put_req_header(conn, "authorization", "Bearer #{access_token.token}") - end - - def add_access_token(conn, %Partner{} = partner, scopes) do - scopes = if is_list(scopes), do: Enum.join(scopes, " "), else: scopes - {:ok, access_token} = Logflare.Auth.create_access_token(partner, %{scopes: scopes}) - - put_req_header(conn, "authorization", "Bearer #{access_token.token}") - end end end @@ -92,4 +70,28 @@ defmodule LogflareWeb.ConnCase do |> Plug.Session.call(@session) |> Plug.Conn.fetch_session()} end + + # for browser use + def login_user(conn, user) do + conn + |> Plug.Test.init_test_session(%{user_id: user.id}) + |> Plug.Conn.assign(:user, user) + end + + # for api use + def add_access_token(conn, user, scopes \\ ~w(public)) + + def add_access_token(conn, %Logflare.User{} = user, scopes) do + scopes = if is_list(scopes), do: Enum.join(scopes, " "), else: scopes + {:ok, access_token} = Logflare.Auth.create_access_token(user, %{scopes: scopes}) + + Plug.Conn.put_req_header(conn, "authorization", "Bearer #{access_token.token}") + end + + def add_access_token(conn, %Logflare.Partners.Partner{} = partner, scopes) do + scopes = if is_list(scopes), do: Enum.join(scopes, " "), else: scopes + {:ok, access_token} = Logflare.Auth.create_access_token(partner, %{scopes: scopes}) + + Plug.Conn.put_req_header(conn, "authorization", "Bearer #{access_token.token}") + end end