From 49f58ec190559975f0dd10f384e8fa4bcfdc4e06 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 25 Sep 2017 11:06:27 +0800 Subject: [PATCH] Fix tests to work with Rails 5.1. (#8) --- app/controllers/chat_controller.rb | 12 ++-- app/models/plugin_model.rb | 5 +- spec/controllers/chat_controller_spec.rb | 55 +++++++++++-------- .../mattermost_command_controller_spec.rb | 22 +++++--- .../slack/slack_command_controller_spec.rb | 40 +++++++++----- .../telegram_command_controller_spec.rb | 35 +++++++++--- 6 files changed, 109 insertions(+), 60 deletions(-) diff --git a/app/controllers/chat_controller.rb b/app/controllers/chat_controller.rb index 3b74f49..2b2863e 100644 --- a/app/controllers/chat_controller.rb +++ b/app/controllers/chat_controller.rb @@ -6,11 +6,13 @@ class DiscourseChat::ChatController < ApplicationController end def list_providers - providers = ::DiscourseChat::Provider.enabled_providers.map { |x| { - name: x::PROVIDER_NAME, - id: x::PROVIDER_NAME, - channel_parameters: (defined? x::CHANNEL_PARAMETERS) ? x::CHANNEL_PARAMETERS : [] - }} + providers = ::DiscourseChat::Provider.enabled_providers.map do |x| + { + name: x::PROVIDER_NAME, + id: x::PROVIDER_NAME, + channel_parameters: (defined? x::CHANNEL_PARAMETERS) ? x::CHANNEL_PARAMETERS : [] + } + end render json: providers, root: 'providers' end diff --git a/app/models/plugin_model.rb b/app/models/plugin_model.rb index cc552bb..b353e2a 100644 --- a/app/models/plugin_model.rb +++ b/app/models/plugin_model.rb @@ -3,6 +3,7 @@ class DiscourseChat::PluginModel < PluginStoreRow KEY_PREFIX = 'unimplemented' after_initialize :init_plugin_model + default_scope { self.default_scope } def init_plugin_model self.type_name ||= 'JSON' @@ -13,11 +14,13 @@ class DiscourseChat::PluginModel < PluginStoreRow def self.default_scope where(type_name: 'JSON') .where(plugin_name: self::PLUGIN_NAME) - .where("key like?", "#{self::KEY_PREFIX}%") + .where("key LIKE ?", "#{self::KEY_PREFIX}%") end before_save :set_key + private + def set_key self.key ||= self.class.alloc_key end diff --git a/spec/controllers/chat_controller_spec.rb b/spec/controllers/chat_controller_spec.rb index 08443c6..1157144 100644 --- a/spec/controllers/chat_controller_spec.rb +++ b/spec/controllers/chat_controller_spec.rb @@ -20,14 +20,14 @@ describe 'Chat Controller', type: :request do shared_examples 'admin constraints' do |action, route| context 'when user is not signed in' do it 'should raise the right error' do - expect { send(action, route) }.to raise_error(ActionController::RoutingError) + expect { public_send(action, route) }.to raise_error(ActionController::RoutingError) end end context 'when user is not an admin' do it 'should raise the right error' do sign_in(Fabricate(:user)) - expect { send(action, route) }.to raise_error(ActionController::RoutingError) + expect { public_send(action, route) }.to raise_error(ActionController::RoutingError) end end end @@ -66,7 +66,9 @@ describe 'Chat Controller', type: :request do end it 'should return the right response' do - post '/admin/plugins/chat/test.json', channel_id: channel.id, topic_id: topic.id + post '/admin/plugins/chat/test.json', params: { + channel_id: channel.id, topic_id: topic.id + } expect(response).to be_success @@ -74,7 +76,9 @@ describe 'Chat Controller', type: :request do end it 'should fail for invalid channel' do - post '/admin/plugins/chat/test.json', channel_id: 999, topic_id: topic.id + post '/admin/plugins/chat/test.json', params: { + channel_id: 999, topic_id: topic.id + } expect(response).not_to be_success end @@ -92,7 +96,7 @@ describe 'Chat Controller', type: :request do it 'should return the right response' do rule = DiscourseChat::Rule.create(channel: channel, filter: 'follow', category_id: category.id, tags: [tag.name]) - get '/admin/plugins/chat/channels.json', provider: 'dummy' + get '/admin/plugins/chat/channels.json', params: { provider: 'dummy' } expect(response).to be_success @@ -110,7 +114,7 @@ describe 'Chat Controller', type: :request do end it 'should fail for invalid provider' do - get '/admin/plugins/chat/channels.json', provider: 'someprovider' + get '/admin/plugins/chat/channels.json', params: { provider: 'someprovider' } expect(response).not_to be_success end @@ -128,25 +132,27 @@ describe 'Chat Controller', type: :request do end it 'should be able to add a new channel' do - post '/admin/plugins/chat/channels.json', + post '/admin/plugins/chat/channels.json', params: { channel: { provider: 'dummy', data: {} } + } expect(response).to be_success - channel = DiscourseChat::Channel.all.first + channel = DiscourseChat::Channel.all.last expect(channel.provider).to eq('dummy') end it 'should fail for invalid params' do - post '/admin/plugins/chat/channels.json', + post '/admin/plugins/chat/channels.json', params: { channel: { provider: 'dummy2', data: { val: 'something with whitespace' } } + } expect(response).not_to be_success @@ -166,25 +172,26 @@ describe 'Chat Controller', type: :request do end it 'should be able update a channel' do - put "/admin/plugins/chat/channels/#{channel.id}.json", + put "/admin/plugins/chat/channels/#{channel.id}.json", params: { channel: { data: { val: "something-else" } } + } expect(response).to be_success - channel = DiscourseChat::Channel.all.first + channel = DiscourseChat::Channel.all.last expect(channel.data).to eq("val" => "something-else") end it 'should fail for invalid params' do - put "/admin/plugins/chat/channels/#{channel.id}.json", + put "/admin/plugins/chat/channels/#{channel.id}.json", params: { channel: { data: { val: "something with whitespace" } } + } expect(response).not_to be_success - end end end @@ -204,7 +211,6 @@ describe 'Chat Controller', type: :request do delete "/admin/plugins/chat/channels/#{channel.id}.json" expect(response).to be_success - expect(DiscourseChat::Channel.all.size).to eq(0) end end @@ -220,17 +226,18 @@ describe 'Chat Controller', type: :request do end it 'should be able to add a new rule' do - post '/admin/plugins/chat/rules.json', + post '/admin/plugins/chat/rules.json', params: { rule: { channel_id: channel.id, category_id: category.id, filter: 'watch', tags: [tag.name] } + } expect(response).to be_success - rule = DiscourseChat::Rule.all.first + rule = DiscourseChat::Rule.all.last expect(rule.channel_id).to eq(channel.id) expect(rule.category_id).to eq(category.id) @@ -240,16 +247,16 @@ describe 'Chat Controller', type: :request do end it 'should fail for invalid params' do - post '/admin/plugins/chat/rules.json', + post '/admin/plugins/chat/rules.json', params: { rule: { channel_id: channel.id, category_id: category.id, filter: 'watch', tags: ['somenonexistanttag'] } + } expect(response).not_to be_success - end end end @@ -266,37 +273,38 @@ describe 'Chat Controller', type: :request do end it 'should be able update a rule' do - put "/admin/plugins/chat/rules/#{rule.id}.json", + put "/admin/plugins/chat/rules/#{rule.id}.json", params: { rule: { channel_id: channel.id, category_id: category2.id, filter: rule.filter, tags: rule.tags } + } expect(response).to be_success - rule = DiscourseChat::Rule.all.first + rule = DiscourseChat::Rule.all.last expect(rule.category_id).to eq(category2.id) end it 'should fail for invalid params' do - put "/admin/plugins/chat/rules/#{rule.id}.json", + put "/admin/plugins/chat/rules/#{rule.id}.json", params: { rule: { channel_id: channel.id, category_id: category.id, filter: 'watch', tags: ['somenonexistanttag'] } + } expect(response).not_to be_success - end end end describe 'deleting a rule' do - let(:rule) { DiscourseChat::Rule.create(channel_id: channel.id, filter: 'follow', category_id: category.id, tags: [tag.name]) } + let(:rule) { DiscourseChat::Rule.create!(channel_id: channel.id, filter: 'follow', category_id: category.id, tags: [tag.name]) } include_examples 'admin constraints', 'delete', "/admin/plugins/chat/rules/1.json" @@ -310,7 +318,6 @@ describe 'Chat Controller', type: :request do delete "/admin/plugins/chat/rules/#{rule.id}.json" expect(response).to be_success - expect(DiscourseChat::Rule.all.size).to eq(0) end end diff --git a/spec/lib/discourse_chat/provider/mattermost/mattermost_command_controller_spec.rb b/spec/lib/discourse_chat/provider/mattermost/mattermost_command_controller_spec.rb index 387f3a4..2e8cb60 100644 --- a/spec/lib/discourse_chat/provider/mattermost/mattermost_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/mattermost/mattermost_command_controller_spec.rb @@ -38,7 +38,9 @@ describe 'Mattermost Command Controller', type: :request do token = 'sometoken' SiteSetting.chat_integration_mattermost_incoming_webhook_token = token - post '/chat-integration/mattermost/command.json', text: 'help', token: token + post '/chat-integration/mattermost/command.json', params: { + text: 'help', token: token + } expect(response.status).to eq(200) end @@ -46,14 +48,16 @@ describe 'Mattermost Command Controller', type: :request do describe 'when the token is invalid' do it 'should raise the right error' do - expect { post '/chat-integration/mattermost/command.json', text: 'help' } + expect { post '/chat-integration/mattermost/command.json', params: { text: 'help' } } .to raise_error(ActionController::ParameterMissing) end end describe 'when incoming webhook token has not been set' do it 'should raise the right error' do - post '/chat-integration/mattermost/command.json', text: 'help', token: 'some token' + post '/chat-integration/mattermost/command.json', params: { + text: 'help', token: 'some token' + } expect(response.status).to eq(403) end @@ -72,10 +76,11 @@ describe 'Mattermost Command Controller', type: :request do describe 'add new rule' do it 'should add a new rule correctly' do - post "/chat-integration/mattermost/command.json", + post "/chat-integration/mattermost/command.json", params: { text: "watch #{category.slug}", channel_name: 'welcome', token: token + } json = JSON.parse(response.body) @@ -90,10 +95,11 @@ describe 'Mattermost Command Controller', type: :request do context 'from an unknown channel' do it 'creates the channel' do - post "/chat-integration/mattermost/command.json", - text: "watch #{category.slug}", - channel_name: 'general', - token: token + post "/chat-integration/mattermost/command.json", params: { + text: "watch #{category.slug}", + channel_name: 'general', + token: token + } json = JSON.parse(response.body) diff --git a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb index 35f2727..ec52aac 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb @@ -38,7 +38,9 @@ describe 'Slack Command Controller', type: :request do token = 'sometoken' SiteSetting.chat_integration_slack_incoming_webhook_token = token - post '/chat-integration/slack/command.json', text: 'help', token: token + post '/chat-integration/slack/command.json', params: { + text: 'help', token: token + } expect(response.status).to eq(200) end @@ -46,14 +48,16 @@ describe 'Slack Command Controller', type: :request do describe 'when the token is invalid' do it 'should raise the right error' do - expect { post '/chat-integration/slack/command.json', text: 'help' } + expect { post '/chat-integration/slack/command.json', params: { text: 'help' } } .to raise_error(ActionController::ParameterMissing) end end describe 'when incoming webhook token has not been set' do it 'should raise the right error' do - post '/chat-integration/slack/command.json', text: 'help', token: 'some token' + post '/chat-integration/slack/command.json', params: { + text: 'help', token: 'some token' + } expect(response.status).to eq(403) end @@ -72,10 +76,11 @@ describe 'Slack Command Controller', type: :request do describe 'add new rule' do it 'should add a new rule correctly' do - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "watch #{category.slug}", channel_name: 'welcome', token: token + } json = JSON.parse(response.body) @@ -90,10 +95,11 @@ describe 'Slack Command Controller', type: :request do context 'from an unknown channel' do it 'creates the channel' do - post "/chat-integration/slack/command.json", - text: "watch #{category.slug}", - channel_name: 'general', - token: token + post "/chat-integration/slack/command.json", params: { + text: "watch #{category.slug}", + channel_name: 'general', + token: token + } json = JSON.parse(response.body) @@ -184,12 +190,13 @@ describe 'Slack Command Controller', type: :request do .with(body: /attachments/) .to_return(body: { ok: true }.to_json) - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } expect(command_stub).to have_been_requested end @@ -199,12 +206,13 @@ describe 'Slack Command Controller', type: :request do .with(body: /1501801629\.052212/) .to_return(body: { ok: true }.to_json) - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post https://sometestslack.slack.com/archives/C6029G78F/p1501801629052212", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } expect(command_stub).to have_been_requested end @@ -214,12 +222,13 @@ describe 'Slack Command Controller', type: :request do .with(body: /1501801629\.052212/) .to_return(body: { ok: true }.to_json) - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post 4", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } expect(command_stub).to have_been_requested end @@ -229,12 +238,13 @@ describe 'Slack Command Controller', type: :request do .with(body: /1501615820\.949638/) .to_return(body: { ok: true }.to_json) - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } expect(command_stub).to have_been_requested end @@ -243,12 +253,13 @@ describe 'Slack Command Controller', type: :request do it 'deals with failed API calls correctly' do stub1 = stub_request(:post, "https://slack.com/api/users.list").to_return(status: 403) - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post 2", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } json = JSON.parse(response.body) @@ -258,12 +269,13 @@ describe 'Slack Command Controller', type: :request do it 'errors correctly if there is no api key' do SiteSetting.chat_integration_slack_access_token = '' - post "/chat-integration/slack/command.json", + post "/chat-integration/slack/command.json", params: { text: "post 2", response_url: 'https://hooks.slack.com/commands/1234', channel_name: 'general', channel_id: 'C6029G78F', token: token + } json = JSON.parse(response.body) diff --git a/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb b/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb index e6f9b05..6a1f6d7 100644 --- a/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb @@ -36,7 +36,10 @@ describe 'Telegram Command Controller', type: :request do describe 'when forum is private' do it 'should not redirect to login page' do SiteSetting.login_required = true - post '/chat-integration/telegram/command/shhh.json', message: { chat: { id: 123 }, text: '/help' } + + post '/chat-integration/telegram/command/shhh.json', params: { + message: { chat: { id: 123 }, text: '/help' } + } expect(response.status).to eq(200) end @@ -44,7 +47,10 @@ describe 'Telegram Command Controller', type: :request do describe 'when the token is invalid' do it 'should raise the right error' do - post '/chat-integration/telegram/command/blah.json', message: { chat: { id: 123 }, text: '/help' } + post '/chat-integration/telegram/command/blah.json', params: { + message: { chat: { id: 123 }, text: '/help' } + } + expect(response.status).to eq(403) end end @@ -52,7 +58,9 @@ describe 'Telegram Command Controller', type: :request do describe 'when token has not been set' do it 'should raise the right error' do SiteSetting.chat_integration_telegram_access_token = "" - post '/chat-integration/telegram/command/blah.json', message: { chat: { id: 123 }, text: '/help' } + post '/chat-integration/telegram/command/blah.json', params: { + message: { chat: { id: 123 }, text: '/help' } + } expect(response.status).to eq(403) end @@ -68,7 +76,9 @@ describe 'Telegram Command Controller', type: :request do describe 'add new rule' do it 'should add a new rule correctly' do - post '/chat-integration/telegram/command/shhh.json', message: { chat: { id: 123 }, text: "/watch #{category.slug}" } + post '/chat-integration/telegram/command/shhh.json', params: { + message: { chat: { id: 123 }, text: "/watch #{category.slug}" } + } expect(response.status).to eq(200) expect(stub).to have_been_requested.once @@ -81,7 +91,9 @@ describe 'Telegram Command Controller', type: :request do end it 'should add a new rule correctly using group chat syntax' do - post '/chat-integration/telegram/command/shhh.json', message: { chat: { id: 123 }, text: "/watch@my-awesome-bot #{category.slug}" } + post '/chat-integration/telegram/command/shhh.json', params: { + message: { chat: { id: 123 }, text: "/watch@my-awesome-bot #{category.slug}" } + } expect(response.status).to eq(200) expect(stub).to have_been_requested.once @@ -95,7 +107,10 @@ describe 'Telegram Command Controller', type: :request do context 'from an unknown channel' do it 'does nothing' do - post '/chat-integration/telegram/command/shhh.json', message: { chat: { id: 456 }, text: "/watch #{category.slug}" } + post '/chat-integration/telegram/command/shhh.json', params: { + message: { chat: { id: 456 }, text: "/watch #{category.slug}" } + } + expect(DiscourseChat::Rule.all.size).to eq(0) expect(DiscourseChat::Channel.all.size).to eq(1) end @@ -103,12 +118,16 @@ describe 'Telegram Command Controller', type: :request do end it "should respond only to a specific command in a broadcast channel" do - post '/chat-integration/telegram/command/shhh.json', channel_post: { chat: { id: 123 }, text: "something" } + post '/chat-integration/telegram/command/shhh.json', params: { + channel_post: { chat: { id: 123 }, text: "something" } + } expect(response.status).to eq(200) expect(stub).to have_been_requested.times(0) - post '/chat-integration/telegram/command/shhh.json', channel_post: { chat: { id: 123 }, text: "/getchatid" } + post '/chat-integration/telegram/command/shhh.json', params: { + channel_post: { chat: { id: 123 }, text: "/getchatid" } + } expect(response.status).to eq(200) expect(stub).to have_been_requested.times(1)