-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
140 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class GroupMembershipsController < AuthenticationController | ||
def create | ||
result = CreateGroupMembership.call(user_id: current_user.id, | ||
group_id: params[:group_id]) | ||
if result.success? | ||
head :no_content | ||
else | ||
render json: result.errors, status: :internal_server_error | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class CreateGroupMembership < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless valid_input? | ||
end | ||
|
||
def execute | ||
context.membership = UserGroupMembership.create(user_id: context.user_id, | ||
group_id: context.group_id) | ||
end | ||
|
||
def validate_output | ||
context.fail!(errors: context.membership.errors) unless membership_saved? | ||
end | ||
|
||
private | ||
|
||
def valid_input? | ||
context.user_id && context.group_id | ||
end | ||
|
||
def membership_saved? | ||
context.membership.persisted? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
RSpec.describe GroupMembershipsController do | ||
describe "POST #create" do | ||
let(:user) { create(:confirmed_user) } | ||
let(:group) { create(:group) } | ||
let(:params) { { id: group.id } } | ||
let(:arguments) { { user_id: user.id, group_id: params[:group_id] } } | ||
let(:context) { double(:context, success?: true) } | ||
|
||
before(:example) do | ||
authenticate | ||
|
||
allow(CreateGroupMembership).to receive(:call).with(arguments). | ||
and_return(context) | ||
|
||
allow(context).to receive(:errors).and_return(["join failed"]) | ||
end | ||
|
||
it "calls CreatePasswordResetOrganizer" do | ||
expect(CreateGroupMembership).to receive(:call) | ||
post :create, params | ||
end | ||
|
||
context "when successful" do | ||
it "returns HTTP status 204" do | ||
post :create, params | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
|
||
context "when unsuccessful" do | ||
let(:context) { double(:context, success?: false) } | ||
|
||
it "returns HTTP status 500" do | ||
post :create, params | ||
expect(response).to have_http_status(500) | ||
end | ||
|
||
it "renders an error" do | ||
post :create, params | ||
expect(json).to eq(["join failed"]) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
RSpec.describe CreateGroupMembership do | ||
describe ".call" do | ||
let(:user) { create(:confirmed_user) } | ||
let(:group) { create(:group) } | ||
|
||
subject do | ||
described_class.call(user_id: user.id, group_id: group.id) | ||
end | ||
|
||
context "when successful" do | ||
it "returns a successful context" do | ||
is_expected.to be_a_success | ||
end | ||
|
||
it "creates a new UserGroupMembership" do | ||
subject | ||
expect(user.groups.first).to eq(group) | ||
end | ||
end | ||
|
||
context "when UserGroupMembership is not persisted" do | ||
before do | ||
allow_any_instance_of(UserGroupMembership).to receive(:persisted?). | ||
and_return(false) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
end | ||
|
||
context "when user_id is not provided" do | ||
subject do | ||
described_class.call(group_id: group.id) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
|
||
it "returns an error" do | ||
expect(subject.errors).to eq("invalid input") | ||
end | ||
end | ||
|
||
context "when group_id is not provided" do | ||
subject do | ||
described_class.call(user_id: user.id) | ||
end | ||
|
||
it "fails" do | ||
is_expected.to be_a_failure | ||
end | ||
|
||
it "returns an error" do | ||
expect(subject.errors).to eq("invalid input") | ||
end | ||
end | ||
end | ||
end |