Skip to content

Commit

Permalink
chore: rename user controller to match resource name. Fix error logs …
Browse files Browse the repository at this point in the history
…emitted during tests
  • Loading branch information
Ziinc committed Dec 7, 2023
1 parent 90aa848 commit d1a4317
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LogflareWeb.Api.Partner.AccountController do
defmodule LogflareWeb.Api.Partner.UserController do
use LogflareWeb, :controller

alias Logflare.Partners
Expand Down
10 changes: 5 additions & 5 deletions lib/logflare_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ defmodule LogflareWeb.Router do
scope "/api/partner", LogflareWeb do
pipe_through([:api, :partner_api])

get("/accounts", Api.Partner.AccountController, :index)
post("/accounts", Api.Partner.AccountController, :create)
get("/users", Api.Partner.UserController, :index)
post("/users", Api.Partner.UserController, :create)

get("/accounts/:user_token", Api.Partner.AccountController, :get_user)
get("/accounts/:user_token/usage", Api.Partner.AccountController, :get_user_usage)
get("/users/:user_token", Api.Partner.UserController, :get_user)
get("/users/:user_token/usage", Api.Partner.UserController, :get_user_usage)

delete("/accounts/:user_token", Api.Partner.AccountController, :delete_user)
delete("/users/:user_token", Api.Partner.UserController, :delete_user)
end

scope "/api" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule LogflareWeb.Api.Partner.AccountControllerTest do
defmodule LogflareWeb.Api.Partner.UserControllerTest do
use LogflareWeb.ConnCase
alias Logflare.Partners
alias Logflare.Auth
alias Logflare.Users

setup do
{:ok, %{partner: insert(:partner)}}
stub(Goth, :fetch, fn _mod -> {:ok, %Goth.Token{token: "auth-token"}} end)
{:ok, %{partner: insert(:partner)}}
end

@allowed_fields MapSet.new(~w(api_quota company email name phone token))
Expand All @@ -20,7 +21,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do
assert [user_response] =
conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts")
|> get("/api/partner/users")
|> json_response(200)

assert user_response["token"] == user.token
Expand All @@ -36,7 +37,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> put_req_header("authorization", "Bearer potato")
|> get("/api/partner/accounts", %{email: email})
|> get("/api/partner/users", %{email: email})
|> json_response(401) == %{"error" => "Unauthorized"}
end
end
Expand All @@ -52,7 +53,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do
assert response =
conn
|> add_access_token(partner, ~w(partner))
|> post("/api/partner/accounts", %{email: email})
|> post("/api/partner/users", %{email: email})
|> json_response(201)

assert response["user"]["email"] == String.downcase(email)
Expand All @@ -69,7 +70,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do
test "returns 400 when no email is given", %{conn: conn, partner: partner} do
assert conn
|> add_access_token(partner, ~w(partner))
|> post("/api/partner/accounts")
|> post("/api/partner/users")
|> json_response(422)
end

Expand All @@ -78,7 +79,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> put_req_header("authorization", "Bearer potato")
|> post("/api/partner/accounts", %{email: email})
|> post("/api/partner/users", %{email: email})
|> json_response(401) == %{"error" => "Unauthorized"}
end
end
Expand All @@ -90,7 +91,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do
assert response =
conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts/#{user.token}")
|> get("/api/partner/users/#{user.token}")
|> json_response(200)

assert response["token"] == user.token
Expand All @@ -106,7 +107,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> put_req_header("authorization", "Bearer potato")
|> get("/api/partner/accounts/#{user.token}")
|> get("/api/partner/users/#{user.token}")
|> json_response(401) == %{"error" => "Unauthorized"}
end

Expand All @@ -121,7 +122,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts/#{user.token}")
|> get("/api/partner/users/#{user.token}")
|> response(404)
end
end
Expand All @@ -137,7 +138,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts/#{user.token}/usage")
|> get("/api/partner/users/#{user.token}/usage")
|> json_response(200) == %{"usage" => count}
end

Expand All @@ -147,7 +148,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> put_req_header("authorization", "Bearer potato")
|> get("/api/partner/accounts/#{user.token}/usage")
|> get("/api/partner/users/#{user.token}/usage")
|> json_response(401) == %{"error" => "Unauthorized"}
end

Expand All @@ -160,21 +161,20 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts/#{user.token}/usage")
|> get("/api/partner/users/#{user.token}/usage")
|> response(404)
end
end

describe "delete_user/2" do
test "returns 204 and deletes the user", %{conn: conn, partner: partner} do
stub(Goth, :fetch, fn _ -> {:error, ""} end)

{:ok, user} = Partners.create_user(partner, %{"email" => TestUtils.gen_email()})

assert response =
conn
|> add_access_token(partner, ~w(partner))
|> delete("/api/partner/accounts/#{user.token}")
|> delete("/api/partner/users/#{user.token}")
|> json_response(204)

assert response["token"] == user.token
Expand All @@ -196,7 +196,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> put_req_header("authorization", "Bearer potato")
|> delete("/api/partner/accounts/#{user.token}")
|> delete("/api/partner/users/#{user.token}")
|> json_response(401) == %{"error" => "Unauthorized"}
end

Expand All @@ -209,7 +209,7 @@ defmodule LogflareWeb.Api.Partner.AccountControllerTest do

assert conn
|> add_access_token(partner, ~w(partner))
|> get("/api/partner/accounts/#{user.token}")
|> get("/api/partner/users/#{user.token}")
|> response(404)
end
end
Expand Down

0 comments on commit d1a4317

Please sign in to comment.