FIX: Do not send staff welcome message if user already has role

FIX: Do not send staff welcome message if user already has role

diff --git a/app/models/concerns/roleable.rb b/app/models/concerns/roleable.rb
index 1361184..ba90048 100644
--- a/app/models/concerns/roleable.rb
+++ b/app/models/concerns/roleable.rb
@@ -19,6 +19,7 @@ module Roleable
   end
 
   def grant_moderation!
+    return if moderator
     set_permission('moderator', true)
     auto_approve_user
     enqueue_staff_welcome_message(:moderator)
@@ -29,6 +30,7 @@ module Roleable
   end
 
   def grant_admin!
+    return if admin
     set_permission('admin', true)
     auto_approve_user
     enqueue_staff_welcome_message(:admin)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 2a50cd8..1b69613 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -164,6 +164,30 @@ describe User do
     end
   end
 
+  context 'enqueue_staff_welcome_message' do
+    let!(:first_admin) { Fabricate(:admin) }
+    let(:user) { Fabricate(:user) }
+
+    it 'enqueues message for admin' do
+      expect {
+        user.grant_admin!
+      }.to change { Jobs::SendSystemMessage.jobs.count }.by 1
+    end
+
+    it 'enqueues message for moderator' do
+      expect {
+        user.grant_moderation!
+      }.to change { Jobs::SendSystemMessage.jobs.count }.by 1
+    end
+
+    it 'skips the message if already an admin' do
+      user.update(admin: true)
+      expect {
+        user.grant_admin!
+      }.to change { Jobs::SendSystemMessage.jobs.count }.by 0
+    end
+  end
+
   context '.set_default_tags_preferences' do
     let(:tag) { Fabricate(:tag) }
 

GitHub sha: 6caad5c0

1 Like