Last active
October 5, 2023 08:46
-
-
Save mplatts/a404eb149887d958dcc10c755154410b to your computer and use it in GitHub Desktop.
Elixir Phoenix Cheatsheet
This file contains hidden or 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
mix help | |
iex -S mix phx.server # like rails c | |
h Enum => shows help for Enum | |
Dossy.Accounts.__info__(:functions) => will list functions in this module | |
mix phx.routes | |
# DATABASE | |
mix ecto.gen.migration add_weather_table | |
mix ecto.migrate | |
mix ecto.rollback |
This file contains hidden or 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
defmodule Dossy.Repo.Migrations.AddUserRole do | |
use Ecto.Migration | |
def change do | |
alter table(:accounts_users) do | |
add :role, :text | |
end | |
end | |
create table(:weather) do | |
add :city, :string, size: 40 | |
add :temp_lo, :integer | |
add :temp_hi, :integer | |
add :prcp, :float | |
timestamps() | |
end | |
create index(:posts, [:name]) | |
drop index(:posts, [:name]) | |
end |
This file contains hidden or 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
defmodule Dossy.Accounts.User do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias Dossy.Accounts.User | |
schema "accounts_users" do | |
field :email, :string | |
field :avatar, :string | |
field :first_name, :string | |
field :last_name, :string | |
field :auth_provider, :string | |
field :encrypted_password, :string | |
field :role, :string | |
field :password, :string, virtual: true | |
field :password_confirmation, :string, virtual: true | |
timestamps() | |
end | |
@doc "Changeset for creating new user with a password" | |
def changeset_for_create(%User{} = user, attrs) do | |
user | |
|> cast(attrs, [ | |
:first_name, | |
:last_name, | |
:auth_provider, | |
:avatar, | |
:email, | |
:password, | |
:password_confirmation, | |
:role | |
]) | |
|> validate_required([ | |
:first_name, | |
:last_name, | |
:auth_provider, | |
:email, | |
:password, | |
:password_confirmation, | |
:role | |
]) | |
|> validate_format(:email, ~r/@/) | |
|> validate_length(:password, min: 5) | |
|> validate_confirmation(:password, message: "Password does not match") | |
|> validate_inclusion(:role, ["dossy_creator", "dossy_viewer"]) | |
|> unique_constraint(:email, message: "Email already taken") | |
|> generate_encrypted_password | |
end | |
# Private ----------------------------- | |
defp generate_encrypted_password(current_changeset) do | |
case current_changeset do | |
%Ecto.Changeset{valid?: true, changes: %{password: password}} -> | |
put_change(current_changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password)) | |
_ -> | |
current_changeset | |
end | |
end | |
end |
This file contains hidden or 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
alias Dossy.Accounts | |
[ | |
%{ | |
first_name: "John", | |
last_name: "Doe", | |
email: "[email protected]", | |
password: "password", | |
password_confirmation: "password", | |
auth_provider: "password" | |
}, | |
%{ | |
first_name: "Matt", | |
last_name: "Platts", | |
email: "[email protected]", | |
password: "password", | |
password_confirmation: "password", | |
auth_provider: "password" | |
}, | |
] | |
|> Enum.map(&Accounts.create_user(&1)) |
This file contains hidden or 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
defmodule Dossy.Web.AuthControllerTest do | |
use Dossy.Web.ConnCase | |
describe "create user" do | |
test "registering the user works", %{conn: conn} do | |
user_details = %{ | |
first_name: "John", | |
last_name: "Smith", | |
email: "[email protected]", | |
password: "password", | |
password_confirmation: "password", | |
} | |
conn = post(conn, "/v1/auth/register", user: user_details) | |
assert %{ | |
"user"=> user_response, | |
"jwt" => token_response, | |
"exp" => _exp_response | |
} = json_response(conn, 201) | |
%{"id"=> id} = user_response | |
conn = conn | |
|> recycle() | |
|> put_req_header("authorization", "Bearer #{token_response}") | |
conn = get(conn, user_path(conn, :show, id)) | |
assert json_response(conn, 200) == %{ | |
"id" => id, | |
"first_name" => user_details.first_name, | |
"last_name" => user_details.last_name, | |
"email" => user_details.email, | |
"avatar" => nil | |
} | |
end | |
test "does not create user and renders errors when data is invalid", %{conn: conn} do | |
user_details = %{ | |
first_name: nil, | |
last_name: "Smith", | |
email: "[email protected]", | |
} | |
conn = post(conn, "v1/auth/register", user: user_details) | |
errors = json_response(conn, 422)["errors"] | |
assert errors != %{} | |
assert errors["first_name"], "can't be blank" | |
end | |
test "passwords must match", %{conn: conn} do | |
user_details = %{ | |
first_name: "John", | |
last_name: "Smith", | |
email: "[email protected]", | |
password: "password", | |
password_confirmation: "completely different", | |
} | |
conn = post(conn, "/v1/auth/register", user: user_details) | |
errors = json_response(conn, 422)["errors"] | |
assert errors != %{} | |
assert errors["password_confirmation"] == ["Password does not match"] | |
end | |
end | |
describe "login" do | |
test "login with password", %{conn: conn} do | |
user = insert(:user) | |
session_details = %{ | |
email: user.email, | |
password: "password" | |
} | |
conn = post(conn, "/v1/auth/identity/callback", session_details) | |
assert %{ | |
"user"=> user_response, | |
"jwt" => _token_response, | |
"exp" => _exp_response | |
} = json_response(conn, 200) | |
assert user_response["first_name"] == user.first_name | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment