diff options
Diffstat (limited to 'spec/services/achievements/revoke_service_spec.rb')
-rw-r--r-- | spec/services/achievements/revoke_service_spec.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/spec/services/achievements/revoke_service_spec.rb b/spec/services/achievements/revoke_service_spec.rb new file mode 100644 index 00000000000..c9925f1e3df --- /dev/null +++ b/spec/services/achievements/revoke_service_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Achievements::RevokeService, feature_category: :user_profile do + describe '#execute' do + let_it_be(:developer) { create(:user) } + let_it_be(:maintainer) { create(:user) } + let_it_be(:group) { create(:group) } + let_it_be(:achievement) { create(:achievement, namespace: group) } + let_it_be(:user_achievement) { create(:user_achievement, achievement: achievement) } + + let(:user_achievement_param) { user_achievement } + + subject(:response) { described_class.new(current_user, user_achievement_param).execute } + + before_all do + group.add_developer(developer) + group.add_maintainer(maintainer) + end + + context 'when user does not have permission' do + let(:current_user) { developer } + + it 'returns an error' do + expect(response).to be_error + expect(response.message).to match_array( + ['You have insufficient permissions to revoke this achievement']) + end + end + + context 'when user has permission' do + let(:current_user) { maintainer } + + it 'revokes an achievement' do + expect(response).to be_success + end + + context 'when the achievement has already been revoked' do + let_it_be(:revoked_achievement) { create(:user_achievement, :revoked, achievement: achievement) } + let(:user_achievement_param) { revoked_achievement } + + it 'returns the correct error' do + expect(response).to be_error + expect(response.message) + .to contain_exactly('This achievement has already been revoked') + end + end + + context 'when the user achievement fails to save' do + let(:user_achievement_param) { instance_double('Achievements::UserAchievement') } + + it 'returns the correct error' do + allow(user_achievement_param).to receive(:save).and_return(false) + allow(user_achievement_param).to receive(:achievement).and_return(achievement) + allow(user_achievement_param).to receive(:revoked?).and_return(false) + allow(user_achievement_param).to receive(:errors).and_return(nil) + expect(user_achievement_param).to receive(:assign_attributes) + + expect(response).to be_error + expect(response.message).to match_array(["Failed to revoke achievement"]) + end + end + end + end +end |