FIX: add translations and check for message and if assignment is successful in spec (#22)

FIX: add translations and check for message and if assignment is successful in spec (#22)
From b20b4c088d1f733dd98cd99be68e3c22e5144daa Mon Sep 17 00:00:00 2001
From: Saurabh Patel <saurabh.finch@gmail.com>
Date: Thu, 6 Dec 2018 14:04:31 +0700
Subject: [PATCH] FIX: add translations and check for message and if assignment
 is successful in spec (#22)


diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb
index 32250bd..7e95f60 100644
--- a/app/controllers/discourse_assign/assign_controller.rb
+++ b/app/controllers/discourse_assign/assign_controller.rb
@@ -63,7 +63,7 @@ module DiscourseAssign
       raise Discourse::NotFound unless assign_to
 
       if topic.custom_fields && topic.custom_fields['assigned_to_id'] == assign_to.id.to_s
-        return render json: { failed: 'Already assigned to the user' }, status: 400
+        return render json: { failed: I18n.t('discourse_assign.already_assigned', username: username) }, status: 400
       end
 
       assigner = TopicAssigner.new(topic, current_user)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 1af4e65..d9a0f48 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -16,6 +16,7 @@ en:
     assigned_to: "Topic assigned to @%{username}"
     unassigned: "Topic was unassigned"
     already_claimed: "That topic has already been claimed."
+    already_assigned: 'Topic is already assigned to @%{username}'
     flag_assigned: "Sorry, that flag's topic is assigned to another user"
     flag_unclaimed: "You must claim that topic before acting on the flag"
     topic_assigned_excerpt: "assigned you the topic '%{title}'"
diff --git a/spec/requests/assign_controller_spec.rb b/spec/requests/assign_controller_spec.rb
index 1b63310..f4eecce 100644
--- a/spec/requests/assign_controller_spec.rb
+++ b/spec/requests/assign_controller_spec.rb
@@ -6,16 +6,17 @@ RSpec.describe DiscourseAssign::AssignController do
   let(:post) { Fabricate(:post) }
   let(:user2) { Fabricate(:active_user) }
 
-  context 'assign' do
+  context '#assign' do
 
     it 'assigns topic to a user' do
       sign_in(user)
 
-      put '/assign/assign', params: {
+      put '/assign/assign.json', params: {
         topic_id: post.topic_id, username: user2.username
       }
 
       expect(response.status).to eq(200)
+      expect(post.topic.reload.custom_fields['assigned_to_id']).to eq(user2.id.to_s)
     end
 
     it 'fails to assign topic to the user if its already assigned to the same user' do
@@ -26,12 +27,14 @@ RSpec.describe DiscourseAssign::AssignController do
       }
 
       expect(response.status).to eq(200)
+      expect(post.topic.reload.custom_fields['assigned_to_id']).to eq(user2.id.to_s)
 
       put '/assign/assign.json', params: {
         topic_id: post.topic_id, username: user2.username
       }
 
       expect(response.status).to eq(400)
+      expect(JSON.parse(response.body)['failed']).to eq(I18n.t('discourse_assign.already_assigned', username: user2.username))
     end
 
   end

GitHub

1 Like