Skip to content

Commit 959a6a5

Browse files
authored
Merge pull request #1267 from code-corps/add-organization-invite-approval
Add organization approval
2 parents e7a1abb + 64cab29 commit 959a6a5

File tree

9 files changed

+55
-10
lines changed

9 files changed

+55
-10
lines changed

lib/code_corps/model/organization.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ defmodule CodeCorps.Organization do
6161
|> put_change(:approved, false)
6262
end
6363

64+
@spec update_changeset(struct, map) :: Changeset.t
65+
def update_changeset(struct, params \\ %{}) do
66+
struct
67+
|> changeset(params)
68+
|> cast(params, [:approved])
69+
end
70+
6471
defp maybe_generate_slug(%Changeset{changes: %{slug: _}} = changeset) do
6572
changeset
6673
end

lib/code_corps/policy/organization.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ defmodule CodeCorps.Policy.Organization do
2626
Returns a boolean indicating if the specified user is allowed to update the
2727
specified organization.
2828
"""
29-
@spec update?(User.t, Organization.t) :: boolean
30-
def update?(%User{admin: true}, %Organization{}), do: true
31-
def update?(%User{} = user, %Organization{} = organization), do: organization |> owned_by?(user)
29+
@spec update?(User.t, Organization.t, map) :: boolean
30+
def update?(%User{admin: true}, %Organization{}, %{}), do: true
31+
def update?(%User{}, %Organization{}, %{"approved" => true}), do: false
32+
def update?(%User{} = user, %Organization{} = organization, %{}), do: organization |> owned_by?(user)
3233

3334
@spec get_invite(String.t) :: OrganizationInvite.t | nil
3435
defp get_invite(code) do

lib/code_corps/policy/policy.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defmodule CodeCorps.Policy do
5050

5151
# Organization
5252
defp can?(%User{} = current_user, :create, %Organization{}, %{} = params), do: Policy.Organization.create?(current_user, params)
53-
defp can?(%User{} = current_user, :update, %Organization{} = organization, %{}), do: Policy.Organization.update?(current_user, organization)
53+
defp can?(%User{} = current_user, :update, %Organization{} = organization, %{} = params), do: Policy.Organization.update?(current_user, organization, params)
5454

5555
# OrganizationGithubAppInstallation
5656
defp can?(%User{} = current_user, :create, %OrganizationGithubAppInstallation{}, %{} = params), do: Policy.OrganizationGithubAppInstallation.create?(current_user, params)

lib/code_corps_web/controllers/organization_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule CodeCorpsWeb.OrganizationController do
4242
with %Organization{} = organization <- Organization |> Repo.get(id),
4343
%User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource,
4444
{:ok, :authorized} <- current_user |> Policy.authorize(:update, organization),
45-
{:ok, %Organization{} = organization} <- organization |> Organization.changeset(params) |> Repo.update,
45+
{:ok, %Organization{} = organization} <- organization |> Organization.update_changeset(params) |> Repo.update,
4646
organization <- preload(organization)
4747
do
4848
conn |> render("show.json-api", data: organization)

lib/code_corps_web/router.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ defmodule CodeCorpsWeb.Router do
4848
get "/", PageController, :index
4949
end
5050

51+
if Mix.env == :dev do
52+
forward "/sent_emails", Bamboo.EmailPreviewPlug
53+
end
54+
5155
scope "/", CodeCorpsWeb, host: "api." do
5256
pipe_through [:stripe_webhooks]
5357

lib/code_corps_web/views/organization_view.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule CodeCorpsWeb.OrganizationView do
55
use JaSerializer.PhoenixView
66

77
attributes [
8-
:cloudinary_public_id, :description, :icon_thumb_url,
8+
:approved, :cloudinary_public_id, :description, :icon_thumb_url,
99
:icon_large_url, :name, :slug, :inserted_at, :updated_at
1010
]
1111

test/lib/code_corps/model/organization_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,24 @@ defmodule CodeCorps.OrganizationTest do
6363
assert changeset |> get_field(:slug) == "custom-slug"
6464
end
6565
end
66+
67+
describe "update_changeset" do
68+
@valid_struct %Organization{
69+
description: "Building a better future.",
70+
name: "Code Corps"
71+
}
72+
@valid_attrs %{
73+
approved: true
74+
}
75+
76+
test "with valid struct and attributes" do
77+
changeset = Organization.update_changeset(@valid_struct, @valid_attrs)
78+
assert changeset.valid?
79+
end
80+
81+
test "with invalid struct" do
82+
changeset = Organization.update_changeset(%Organization{}, %{})
83+
refute changeset.valid?
84+
end
85+
end
6686
end

test/lib/code_corps/policy/organization_test.exs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule CodeCorps.Policy.OrganizationTest do
22
use CodeCorps.PolicyCase
33

4-
import CodeCorps.Policy.Organization, only: [create?: 2, update?: 2]
4+
import CodeCorps.Policy.Organization, only: [create?: 2, update?: 3]
55

66
describe "create" do
77
test "returns true when user is an admin" do
@@ -36,19 +36,31 @@ defmodule CodeCorps.Policy.OrganizationTest do
3636
test "returns true when user is an admin" do
3737
user = insert(:user, admin: true)
3838
organization = insert(:organization)
39-
assert update?(user, organization)
39+
assert update?(user, organization, %{})
40+
end
41+
42+
test "returns false when user is approving as the admin" do
43+
user = insert(:user, admin: true)
44+
organization = build(:organization, owner_id: user.id)
45+
assert update?(user, organization, %{"approved" => "true"})
46+
end
47+
48+
test "returns false when user is approving as the organization owner" do
49+
user = insert(:user)
50+
organization = build(:organization, owner_id: user.id)
51+
assert update?(user, organization, %{"approved" => "true"})
4052
end
4153

4254
test "returns true when user is the organization owner" do
4355
user = insert(:user)
4456
organization = build(:organization, owner_id: user.id)
45-
assert update?(user, organization)
57+
assert update?(user, organization, %{})
4658
end
4759

4860
test "returns false when user is not the organization owner" do
4961
user = insert(:user)
5062
organization = build(:organization)
51-
refute update?(user, organization)
63+
refute update?(user, organization, %{})
5264
end
5365
end
5466
end

test/lib/code_corps_web/views/organization_view_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule CodeCorpsWeb.OrganizationViewTest do
1818
expected_json = %{
1919
"data" => %{
2020
"attributes" => %{
21+
"approved" => organization.approved,
2122
"cloudinary-public-id" => nil,
2223
"description" => organization.description,
2324
"icon-large-url" => "#{host}/icons/organization_default_large_blue.png",

0 commit comments

Comments
 (0)