Fix tests to work with Rails 5.1. (#8)

This commit is contained in:
Guo Xiang Tan 2017-09-25 11:06:27 +08:00 committed by GitHub
parent 47a6a89e5a
commit 49f58ec190
6 changed files with 109 additions and 60 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)