Skip to content

Commit 7e5311e

Browse files
committed
Ensure Project.create_changeset casts a proper ProjectUser association for owner
1 parent 96bc1c5 commit 7e5311e

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

Diff for: test/models/project_test.exs

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
defmodule CodeCorps.ProjectTest do
22
use CodeCorps.ModelCase
33

4-
alias CodeCorps.Project
4+
import CodeCorps.Project
5+
6+
alias CodeCorps.{Project, ProjectUser, Repo}
57

68
describe "changeset" do
79
@valid_attrs %{title: "A title"}
@@ -43,42 +45,46 @@ defmodule CodeCorps.ProjectTest do
4345
end
4446
end
4547

46-
describe "create_changeset" do
47-
@valid_attrs %{title: "A title", organization_id: 1, owner_id: 1}
48-
@invalid_attrs %{}
49-
48+
describe "create_changeset/3" do
5049
test "with valid attributes" do
51-
changeset = Project.create_changeset(%Project{}, @valid_attrs)
50+
organization = insert(:organization)
51+
attrs = %{title: "A title", organization_id: organization.id}
52+
changeset = create_changeset(%Project{}, attrs)
5253
assert changeset.valid?
5354
end
5455

5556
test "with invalid attributes" do
56-
changeset = Project.create_changeset(%Project{}, @invalid_attrs)
57+
changeset = create_changeset(%Project{}, %{})
5758
refute changeset.valid?
5859
end
5960

60-
test "accepts setting of organization_id" do
61-
changeset = Project.create_changeset(%Project{}, %{organization_id: 1})
62-
assert {:ok, 1} == changeset |> fetch_change(:organization_id)
61+
test "casts :organization_id and ensures organization exists" do
62+
attrs = %{title: "A title", organization_id: -1}
63+
changeset = create_changeset(%Project{}, attrs)
64+
65+
assert {:error, failed_insert_changeset} = changeset |> Repo.insert()
66+
67+
refute failed_insert_changeset.valid?
68+
assert error_message(failed_insert_changeset, :organization) == "does not exist"
6369
end
6470

65-
test "associates the ordered default task lists to the project" do
71+
test "casts and inserts proper associated records" do
6672
organization = insert(:organization)
67-
user = insert(:user)
68-
changeset = Project.create_changeset(
69-
%Project{},
70-
%{organization_id: organization.id, title: "Title", owner_id: user.id}
71-
)
73+
attrs = %{title: "A title", organization_id: organization.id}
74+
changeset = Project.create_changeset(%Project{}, attrs)
7275

7376
{_, project} = Repo.insert(changeset)
7477

7578
task_list_orders = for task_list <- project.task_lists, do: task_list.order
7679

7780
assert Enum.all?(task_list_orders), "some of the orders are not set (nil)"
7881
assert task_list_orders == Enum.sort(task_list_orders), "task lists order does not correspond to their position"
79-
end
8082

81-
test "also inserts an owner role project_user record"
83+
project_user = Repo.one(ProjectUser)
84+
assert project_user.project_id == project.id
85+
assert project_user.user_id == organization.owner_id
86+
assert project_user.role == "owner"
87+
end
8288
end
8389

8490
describe "update_changeset" do

Diff for: web/controllers/project_controller.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule CodeCorps.ProjectController do
2626
|> approved_filter(true)
2727
end
2828

29-
def handle_create(_conn, attributes) do
29+
def handle_create(conn, attributes) do
3030
%Project{} |> Project.create_changeset(attributes)
3131
end
3232

Diff for: web/models/project.ex

+17
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ defmodule CodeCorps.Project do
6464
|> changeset(params)
6565
|> cast(params, [:organization_id])
6666
|> put_assoc(:task_lists, TaskList.default_task_lists())
67+
|> put_member_assoc()
6768
|> generate_icon_color(:default_color)
69+
|> assoc_constraint(:organization)
6870
end
6971

7072
@doc """
@@ -82,4 +84,19 @@ defmodule CodeCorps.Project do
8284
struct
8385
|> cast(params, [:total_monthly_donated])
8486
end
87+
88+
@spec put_member_assoc(Changeset.t) :: Changeset.t
89+
defp put_member_assoc(changeset) do
90+
case changeset |> get_change(:organization_id) |> get_organization do
91+
nil ->
92+
changeset
93+
organization ->
94+
changeset
95+
|> put_assoc(:project_users, [%{user_id: organization.owner_id, role: "owner"}])
96+
end
97+
end
98+
99+
@spec get_organization(integer | nil) :: CodeCorps.Organization.t :: nil
100+
defp get_organization(nil), do: nil
101+
defp get_organization(id), do: CodeCorps.Repo.get(CodeCorps.Organization, id)
85102
end

0 commit comments

Comments
 (0)