-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
122 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule MarketWeb.VendorSessionController do | ||
use MarketWeb, :controller | ||
|
||
alias Market.Accounts | ||
alias MarketWeb.VendorAuth | ||
|
||
action_fallback MarketWeb.FallbackController | ||
|
||
def create(conn, %{"vendor" => vendor_params}) do | ||
%{"email" => email, "password" => password} = vendor_params | ||
|
||
if vendor = Accounts.get_vendor_by_email_and_password(email, password) do | ||
conn | ||
|> VendorAuth.log_in_vendor(vendor, vendor_params) | ||
|> put_status(:created) | ||
|> render(:show, vendor: vendor) | ||
else | ||
# In order to prevent user enumeration attacks, don't disclose whether the email is registered. | ||
render(conn, :error, error_message: "Invalid email or password") | ||
end | ||
end | ||
|
||
def delete(conn, _params) do | ||
conn | ||
|> VendorAuth.log_out_vendor() | ||
|> send_resp(:no_content, "") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule MarketWeb.VendorSessionJSON do | ||
def show(%{vendor: vendor}) do | ||
%{id: vendor.id} | ||
end | ||
|
||
def error(_) do | ||
%{errors: "🐗"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/market_web/controllers/vendor_session_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule MarketWeb.VendorSessionControllerTest do | ||
use MarketWeb.ConnCase, async: true | ||
|
||
import Market.AccountsFixtures | ||
|
||
setup do | ||
%{vendor: vendor_fixture()} | ||
end | ||
|
||
describe "POST /vendors/log_in" do | ||
test "logs the vendor in", %{conn: conn, vendor: vendor} do | ||
conn = | ||
conn | ||
|> init_test_session([]) | ||
|> post(~p"/vendors/log_in", %{ | ||
"vendor" => %{"email" => vendor.email, "password" => valid_vendor_password()} | ||
}) | ||
|
||
assert get_session(conn, :vendor_token) | ||
|
||
# Now do a logged in request and assert on the menu | ||
# conn = get(conn, ~p"/") | ||
# response = html_response(conn, 200) | ||
# assert response =~ vendor.email | ||
# assert response =~ ~p"/vendors/settings" | ||
# assert response =~ ~p"/vendors/log_out" | ||
end | ||
|
||
test "logs the vendor in with remember me", %{conn: conn, vendor: vendor} do | ||
conn = init_test_session(conn, []) | ||
|
||
conn = post(conn, ~p"/vendors/log_in", %{ | ||
"vendor" => %{ | ||
"email" => vendor.email, | ||
"password" => valid_vendor_password(), | ||
"remember_me" => "true" | ||
} | ||
}) | ||
|
||
assert conn.resp_cookies["_market_web_vendor_remember_me"] | ||
end | ||
|
||
test "emits error message with invalid credentials", %{conn: conn, vendor: vendor} do | ||
conn = | ||
conn | ||
|> init_test_session([]) | ||
|> post(~p"/vendors/log_in", %{ | ||
"vendor" => %{"email" => vendor.email, "password" => "invalid_password"} | ||
}) | ||
|
||
response = json_response(conn, 200) | ||
end | ||
end | ||
|
||
describe "DELETE /vendors/log_out" do | ||
test "logs the vendor out", %{conn: conn, vendor: vendor} do | ||
conn = | ||
conn |> init_test_session([]) |> log_in_vendor(vendor) |> delete(~p"/vendors/log_out") | ||
|
||
refute get_session(conn, :vendor_token) | ||
end | ||
|
||
test "succeeds even if the vendor is not logged in", %{conn: conn} do | ||
conn = conn |> init_test_session([]) |> delete(~p"/vendors/log_out") | ||
refute get_session(conn, :vendor_token) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters