-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Membership: Revoke memberships from an organization
- Loading branch information
Showing
7 changed files
with
226 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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Memberships | ||
class Revoke < BaseMutation | ||
include AuthenticableApiUser | ||
|
||
graphql_name 'RevokeMembership' | ||
description 'Revoke a membership' | ||
|
||
argument :id, ID, required: true | ||
|
||
type Types::MembershipType | ||
|
||
def resolve(id:) | ||
result = ::Memberships::RevokeService.new(context[:current_user]).revoke(id) | ||
|
||
result.success? ? result.membership : result_error(result) | ||
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
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module Memberships | ||
class RevokeService < BaseService | ||
def revoke(id) | ||
membership = Membership.find_by(id: id) | ||
return result.fail!(code: 'not_found') unless membership | ||
|
||
if result.user.id == membership.user.id | ||
return result.fail!( | ||
code: 'unprocessable_entity', | ||
message: 'revoke_own_membership', | ||
) | ||
end | ||
|
||
membership.mark_as_revoked! | ||
|
||
result.membership = membership | ||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.fail_with_validations!(e.record) | ||
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
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Mutations::Memberships::Revoke, type: :graphql do | ||
let(:membership) { create(:membership) } | ||
let(:organization) { membership.organization } | ||
|
||
let(:mutation) do | ||
<<-GQL | ||
mutation($input: RevokeMembershipInput!) { | ||
revokeMembership(input: $input) { | ||
id | ||
revokedAt | ||
} | ||
} | ||
GQL | ||
end | ||
|
||
it 'Revokes a membership' do | ||
user = create(:user) | ||
|
||
result = execute_graphql( | ||
current_user: user, | ||
query: mutation, | ||
variables: { | ||
input: { id: membership.id }, | ||
}, | ||
) | ||
|
||
data = result['data']['revokeMembership'] | ||
|
||
expect(data['id']).to eq(membership.id) | ||
expect(data['revokedAt']).to be_present | ||
end | ||
|
||
it 'Cannot Revoke my own membership' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
query: mutation, | ||
variables: { | ||
input: { id: membership.id }, | ||
}, | ||
) | ||
|
||
expect(result['errors'].first['message']).to eq('revoke_own_membership') | ||
expect(result['errors'].first['extensions']['status']).to eq(422) | ||
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Memberships::RevokeService, type: :service do | ||
subject(:revoke_service) { described_class.new(membership.user) } | ||
|
||
let(:membership) { create(:membership) } | ||
|
||
describe '.revoke' do | ||
context 'when revoking my own membership' do | ||
it 'returns an error' do | ||
result = revoke_service.revoke(membership.id) | ||
|
||
expect(result).not_to be_success | ||
expect(result.error).to eq('revoke_own_membership') | ||
end | ||
end | ||
|
||
context 'when membership is not found' do | ||
it 'returns an error' do | ||
result = revoke_service.revoke(nil) | ||
|
||
expect(result).not_to be_success | ||
expect(result.error).to eq('not_found') | ||
end | ||
end | ||
|
||
context 'when revoking another membership' do | ||
let(:another_membership) { create(:membership) } | ||
|
||
it 'revokes the memembership' do | ||
result = revoke_service.revoke(another_membership.id) | ||
|
||
expect(result).to be_success | ||
expect(result.membership.id).to eq(another_membership.id) | ||
end | ||
end | ||
end | ||
end |