Skip to content

Commit

Permalink
Merge pull request #1717 from hauleth/test/cleanup-tests
Browse files Browse the repository at this point in the history
test: cleanup tests
  • Loading branch information
Ziinc authored Oct 4, 2023
2 parents 4991fc9 + 7446aa7 commit 118bd8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"}],
Expand Down
50 changes: 26 additions & 24 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

0 comments on commit 118bd8c

Please sign in to comment.