8000 fix(huddl): tie organizer-only huddl details to the current role by mwoods79 · Pull Request #726 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ defmodule Huddlz.Communities.Huddl do

field_policy :cancellation_reason do
authorize_if expr(
creator_id == ^actor(:id) or group.owner_id == ^actor(:id) or
group.owner_id == ^actor(:id) or
exists(group.group_members, user_id == ^actor(:id) and role == :organizer) or
exists(attendees, user_id == ^actor(:id))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Huddlz.Communities.Huddl.Preparations.FilterByVisibility do
Filters huddlz based on visibility and lifecycle rules:
- Published and cancelled public huddlz in public groups are visible to everyone, before and after they end
- Published private huddlz are only visible to group members
- Drafts are visible only to their organizers
- Private cancelled huddlz remain visible to organizers and people with RSVP history
- Drafts are visible only to the group's current owner and organizers
- Private cancelled huddlz remain visible to the group's current owner and
organizers, and to people with RSVP history
- Having created a huddl grants nothing: access follows the current role

This preparation leverages Ash calculations and relationships for a more
declarative approach to visibility filtering.
Expand Down Expand Up @@ -33,7 +35,7 @@ defmodule Huddlz.Communities.Huddl.Preparations.FilterByVisibility do
(published and not cancelled and
(is_publicly_visible == true or exists(group.members, id == ^actor.id))) or
((cancelled or not published) and
(creator_id == ^actor.id or group.owner_id == ^actor.id or
(group.owner_id == ^actor.id or
exists(
group.group_members,
user_id == ^actor.id and role == :organizer
Expand Down
79 changes: 79 additions & 0 deletions test/features/former_organizer_access.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@async @database @conn @former_organizer_access
Feature: Organizer-only huddl details follow the current role
As a group owner
I want drafts and cancellation details to follow a person's current role
So that someone I demote or remove stops seeing what only organizers see

Scenario Outline: A former organizer cannot re 10BC8 ad a draft they created
Given I created a draft huddl as an organizer of a private group
And the owner has since <change>
When I read the huddl I created through "<api>"
Then the API does not reveal the huddl I created

Examples:
| change | api |
| made me a member | JSON:API |
| made me a member | GraphQL |
| removed me from the group | JSON:API |
| removed me from the group | GraphQL |

Scenario Outline: A former organizer without an RSVP cannot read a cancelled private huddl they created
Given I created a huddl as an organizer of a private group
And I withdrew my RSVP to the huddl I created
And the owner cancelled the huddl I created because "The venue flooded"
And the owner has since <change>
When I read the huddl I created through "<api>"
Then the API does not reveal the huddl I created

Examples:
| change | api |
| made me a member | JSON:API |
| made me a member | GraphQL |
| removed me from the group | JSON:API |
| removed me from the group | GraphQL |

Scenario Outline: A former organizer without an RSVP cannot read why a public huddl they created was cancelled
Given I created a huddl as an organizer of a public group
And I withdrew my RSVP to the huddl I created
And the owner cancelled the huddl I created because "The venue flooded"
And the owner has since made me a member
When I read the huddl I created through "<api>"
Then the API shows the huddl I created as cancelled without a reason

Examples:
| api |
| JSON:API |
| GraphQL |

Scenario Outline: A former organizer who kept their RSVP still reads why the huddl was cancelled
Given I created a huddl as an organizer of a private group
And the owner cancelled the huddl I created because "The venue flooded"
And the owner has since removed me from the group
When I read the huddl I created through "<api>"
Then the API shows the huddl I created as cancelled because "The venue flooded"

Examples:
| api |
| JSON:API |
| GraphQL |

Scenario Outline: An organizer still reads a draft they created
Given I created a draft huddl as an organizer of a private group
When I read the huddl I created through "<api>"
Then the API shows the huddl I created as a draft

Examples:
| api |
| JSON:API |
| GraphQL |

Scenario Outline: A former owner who stays an organizer still reads a draft they created
Given I created a draft huddl as the owner of a private group
And I have since transferred the group to another organizer
When I read the huddl I created through "<api>"
Then the API shows the huddl I created as a draft

Examples:
| api |
| JSON:API |
| GraphQL |
163 changes: 163 additions & 0 deletions test/features/step_definitions/former_organizer_access_steps.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
defmodule FormerOrganizerAccessSteps do
use Cucumber.StepDefinition

import ExUnit.Assertions
import Huddlz.Generator
import HuddlzWeb.ApiCase
import Phoenix.ConnTest

alias Huddlz.Communities

step "I created a draft huddl as an organizer of a {word} group",
%{args: [visibility]} = context do
organizer_with_huddl(context, visibility, draft: true)
end

step "I created a huddl as an organizer of a {word} group", %{args: [visibility]} = context do
organizer_with_huddl(context, visibility, draft: false)
end

step "I created a draft huddl as the owner of a private group", context do
me = generate(user())
successor = generate(user())

{group, _memberships} =
generate_group_with_members(
owner: me,
group: [is_public: false],
members: [%{user: successor, role: :organizer}]
)

huddl = generate(huddl(group_id: group.id, actor: me, draft: true))

Map.merge(context, %{
created_huddl: huddl,
created_group: group,
created_owner: me,
created_successor: successor,
me: me,
conn: authenticated_conn(context.conn, me)
})
end

step "I have since transferred the group to another organizer", context do
Communities.transfer_group_ownership!(context.created_group, context.created_successor.id,
actor: context.me
)

context
end

step "I withdrew my RSVP to the huddl I created", context do
Communities.cancel_rsvp_huddl!(context.created_huddl, actor: context.me)
context
end

step "the owner cancelled the huddl I created because {string}", %{args: [reason]} = context do
cancelled =
Communities.cancel_huddl!(context.created_huddl, reason, %{}, actor: context.created_owner)

Map.put(context, :created_huddl, cancelled)
end

step "the owner has since made me a member", context do
Communities.change_member_role!(context.my_membership, :member, actor: context.created_owner)
context
end

step "the owner has since removed me from the group", context do
Communities.remove_member!(
context.my_membership,
context.created_group.id,
context.me.id,
actor: context.created_owner
)

context
end

step "I read the huddl I created through {string}", %{args: [api]} = context do
Map.merge(context, %{
created_response: read_huddl(context.conn, context.created_huddl.id, api),
created_api: api
})
end

step "the API does not reveal the huddl I created", context do
assert read_result(context) == :not_found
refute context.created_response.resp_body =~ context.created_huddl.title
context
end

step "the API shows the huddl I created as a draft", context do
assert %{status: "draft"} = read_result(context)
context
end

step "the API shows the huddl I created as cancelled without a reason", context do
assert %{status: "cancelled", cancellation_reason: nil} = read_result(context)
refute context.created_response.resp_body =~ context.created_huddl.cancellation_reason
context
end

step "the API shows the huddl I created as cancelled because {string}",
%{args: [reason]} = context do
assert %{status: "cancelled", cancellation_reason: ^reason} = read_result(context)
context
end

defp organizer_with_huddl(context, visibility, huddl_opts) do
owner = generate(user())
me = generate(user())

{group, [membership]} =
generate_group_with_members(
owner: owner,
group: [is_public: visibility == "public"],
members: [%{user: me, role: :organizer}]
)

huddl = generate(huddl([group_id: group.id, actor: me] ++ huddl_opts))

Map.merge(context, %{
created_huddl: huddl,
created_group: group,
created_owner: owner,
my_membership: membership,
me: me,
conn: authenticated_conn(context.conn, me)
})
end

defp read_huddl(conn, id, "JSON:API") do
dispatch(conn, HuddlzWeb.Endpoint, :get, "/api/json/huddlz/#{id}", %{})
end

defp read_huddl(conn, id, "GraphQL") do
gql_post(conn, ~s|{ getHuddl(id: "#{id}") { id title status cancellationReason } }|)
end

defp read_result(%{created_api: "JSON:API", created_response: %{status: 404} = conn}) do
assert [%{"code" => "not_found"}] = json_response(conn, 404)["errors"]
:not_found
end

defp read_result(%{created_api: "JSON:API", created_response: conn}) do
attributes = json_response(conn, 200)["data"]["attributes"]

%{status: attributes["status"], cancellation_reason: attributes["cancellation_reason"]}
end

defp read_result(%{created_api: "GraphQL", created_response: conn}) do
case json_response(conn, 200)["data"]["getHuddl"] do
nil ->
:not_found

huddl ->
%{
status: String.downcase(huddl["status"]),
cancellation_reason: huddl["cancellationReason"]
}
end
end
end
0