Update specs for updated controller methods

This commit is contained in:
David Taylor 2017-07-18 18:23:20 +01:00
parent 2275b048f9
commit 0a9ef040a1
5 changed files with 137 additions and 60 deletions

View File

@ -62,6 +62,10 @@ class DiscourseChat::ChatController < ApplicationController
begin begin
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME} providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME}
if not defined? params[:channel] and defined? params[:channel][:provider]
raise Discourse::InvalidParameters, 'Provider is not valid'
end
requested_provider = params[:channel][:provider] requested_provider = params[:channel][:provider]
if not providers.include? requested_provider if not providers.include? requested_provider
@ -111,21 +115,6 @@ class DiscourseChat::ChatController < ApplicationController
render json: success_json render json: success_json
end end
def list_rules
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME}
requested_provider = params[:provider]
if not providers.include? requested_provider
raise Discourse::NotFound
end
rules = DiscourseChat::Rule.with_provider(requested_provider)
render_serialized rules, DiscourseChat::RuleSerializer, root: 'rules'
end
def create_rule def create_rule
begin begin
hash = params.require(:rule).permit(:channel_id, :filter, :category_id, tags:[]) hash = params.require(:rule).permit(:channel_id, :filter, :category_id, tags:[])

View File

@ -57,6 +57,15 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
end end
end end
# Don't want this to end up as anything other than an integer
def channel_id=(val)
if val.nil? or val.blank?
super(nil)
else
super(val.to_i)
end
end
# Mock foreign key # Mock foreign key
# Could raise RecordNotFound # Could raise RecordNotFound
def channel def channel

View File

@ -11,7 +11,6 @@ module DiscourseChat
put '/channels/:id' => "chat#update_channel" put '/channels/:id' => "chat#update_channel"
delete '/channels/:id' => "chat#destroy_channel" delete '/channels/:id' => "chat#destroy_channel"
get '/rules' => "chat#list_rules"
post '/rules' => "chat#create_rule" post '/rules' => "chat#create_rule"
put '/rules/:id' => "chat#update_rule" put '/rules/:id' => "chat#update_rule"
delete '/rules/:id' => "chat#destroy_rule" delete '/rules/:id' => "chat#destroy_rule"

View File

@ -6,7 +6,9 @@ describe 'Chat Controller', type: :request do
let(:topic) { Fabricate(:topic, posts: [first_post]) } let(:topic) { Fabricate(:topic, posts: [first_post]) }
let(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }
let(:category2) { Fabricate(:category) }
let(:tag) { Fabricate(:tag) } let(:tag) { Fabricate(:tag) }
let(:channel) { DiscourseChat::Channel.create(provider:'dummy') }
include_context "dummy provider" include_context "dummy provider"
@ -44,17 +46,17 @@ describe 'Chat Controller', type: :request do
json = JSON.parse(response.body) json = JSON.parse(response.body)
expect(json['providers'].size).to eq(1) expect(json['providers'].size).to eq(2)
expect(json['providers'][0]).to eq('name'=> 'dummy', expect(json['providers'][0]).to eq('name'=> 'dummy',
'id'=> 'dummy', 'id'=> 'dummy',
'channel_regex'=> nil 'channel_parameters'=> []
) )
end end
end end
end end
describe 'testing providers' do describe 'testing channels' do
include_examples 'admin constraints', 'get', '/admin/plugins/chat/test.json' include_examples 'admin constraints', 'get', '/admin/plugins/chat/test.json'
context 'when signed in as an admin' do context 'when signed in as an admin' do
@ -63,23 +65,23 @@ describe 'Chat Controller', type: :request do
end end
it 'should return the right response' do it 'should return the right response' do
post '/admin/plugins/chat/test.json', provider: 'dummy', channel: '#general', topic_id: topic.id post '/admin/plugins/chat/test.json', channel_id: channel.id, topic_id: topic.id
expect(response).to be_success expect(response).to be_success
json = JSON.parse(response.body) json = JSON.parse(response.body)
end end
it 'should fail for invalid provider' do it 'should fail for invalid channel' do
post '/admin/plugins/chat/test.json', provider: 'someprovider', channel: '#general', topic_id: topic.id post '/admin/plugins/chat/test.json', channel_id: 999, topic_id: topic.id
expect(response).not_to be_success expect(response).not_to be_success
end end
end end
end end
describe 'viewing rules' do describe 'viewing channels' do
include_examples 'admin constraints', 'get', '/admin/plugins/chat/rules.json' include_examples 'admin constraints', 'get', '/admin/plugins/chat/channels.json'
context 'when signed in as an admin' do context 'when signed in as an admin' do
before do before do
@ -87,29 +89,26 @@ describe 'Chat Controller', type: :request do
end end
it 'should return the right response' do it 'should return the right response' do
rule = DiscourseChat::Rule.create({provider: 'dummy', channel: '#general', filter:'follow', category_id:category.id, tags:[tag.name]}) rule = DiscourseChat::Rule.create(channel: channel, filter:'follow', category_id:category.id, tags:[tag.name])
get '/admin/plugins/chat/rules.json', provider:'dummy' get '/admin/plugins/chat/channels.json', provider:'dummy'
expect(response).to be_success expect(response).to be_success
rules = JSON.parse(response.body)['rules'] channels = JSON.parse(response.body)['channels']
expect(rules.count).to eq(1) expect(channels.count).to eq(1)
expect(rules.first).to eq( expect(channels.first).to eq(
"channel" => "#general", "id" => channel.id,
"category_id" => category.id, "provider" => 'dummy',
"tags" => [tag.name], "data" => {},
"filter" => "follow", "rules" => [{"id" => rule.id, "filter" => "follow", "channel_id" => channel.id, "category_id" => category.id, "tags" => [tag.name], "error_key" => nil}]
"error_key" => nil,
"id" => rule.id,
"provider" => 'dummy'
) )
end end
it 'should fail for invalid provider' do it 'should fail for invalid provider' do
get '/admin/plugins/chat/rules.json', provider:'someprovider' get '/admin/plugins/chat/channels.json', provider:'someprovider'
expect(response).not_to be_success expect(response).not_to be_success
end end
@ -117,6 +116,98 @@ describe 'Chat Controller', type: :request do
end end
end end
describe 'adding a channel' do
include_examples 'admin constraints', 'post', '/admin/plugins/chat/channels.json'
context 'as an admin' do
before do
sign_in(admin)
end
it 'should be able to add a new channel' do
post '/admin/plugins/chat/channels.json',
channel:{
provider: 'dummy',
data: {}
}
expect(response).to be_success
channel = DiscourseChat::Channel.all.first
expect(channel.provider).to eq('dummy')
end
it 'should fail for invalid params' do
post '/admin/plugins/chat/channels.json',
channel:{
provider: 'dummy2',
data: {val: 'something with whitespace'}
}
expect(response).not_to be_success
end
end
end
describe 'updating a channel' do
let(:channel){DiscourseChat::Channel.create(provider:'dummy2', data:{val:"something"})}
include_examples 'admin constraints', 'put', "/admin/plugins/chat/channels/1.json"
context 'as an admin' do
before do
sign_in(admin)
end
it 'should be able update a channel' do
put "/admin/plugins/chat/channels/#{channel.id}.json",
channel:{
data: {val: "something-else"}
}
expect(response).to be_success
channel = DiscourseChat::Channel.all.first
expect(channel.data).to eq({"val" => "something-else"})
end
it 'should fail for invalid params' do
put "/admin/plugins/chat/channels/#{channel.id}.json",
channel:{
data: {val: "something with whitespace"}
}
expect(response).not_to be_success
end
end
end
describe 'deleting a channel' do
let(:channel){DiscourseChat::Channel.create(provider:'dummy', data:{})}
include_examples 'admin constraints', 'delete', "/admin/plugins/chat/channels/1.json"
context 'as an admin' do
before do
sign_in(admin)
end
it 'should be able delete a channel' do
delete "/admin/plugins/chat/channels/#{channel.id}.json"
expect(response).to be_success
expect(DiscourseChat::Channel.all.size).to eq(0)
end
end
end
describe 'adding a rule' do describe 'adding a rule' do
include_examples 'admin constraints', 'put', '/admin/plugins/chat/rules.json' include_examples 'admin constraints', 'put', '/admin/plugins/chat/rules.json'
@ -127,10 +218,9 @@ describe 'Chat Controller', type: :request do
end end
it 'should be able to add a new rule' do it 'should be able to add a new rule' do
put '/admin/plugins/chat/rules.json', post '/admin/plugins/chat/rules.json',
rule:{ rule:{
provider: 'dummy', channel_id: channel.id,
channel: '#general',
category_id: category.id, category_id: category.id,
filter: 'watch', filter: 'watch',
tags: [tag.name] tags: [tag.name]
@ -140,8 +230,7 @@ describe 'Chat Controller', type: :request do
rule = DiscourseChat::Rule.all.first rule = DiscourseChat::Rule.all.first
expect(rule.provider).to eq('dummy') expect(rule.channel_id).to eq(channel.id)
expect(rule.channel).to eq('#general')
expect(rule.category_id).to eq(category.id) expect(rule.category_id).to eq(category.id)
expect(rule.filter).to eq('watch') expect(rule.filter).to eq('watch')
expect(rule.tags).to eq([tag.name]) expect(rule.tags).to eq([tag.name])
@ -149,10 +238,9 @@ describe 'Chat Controller', type: :request do
end end
it 'should fail for invalid params' do it 'should fail for invalid params' do
put '/admin/plugins/chat/rules.json', post '/admin/plugins/chat/rules.json',
rule:{ rule:{
provider: 'dummy', channel_id: channel.id,
channel: '#general',
category_id: category.id, category_id: category.id,
filter: 'watch', filter: 'watch',
tags: ['somenonexistanttag'] tags: ['somenonexistanttag']
@ -165,7 +253,7 @@ describe 'Chat Controller', type: :request do
end end
describe 'updating a rule' do describe 'updating a rule' do
let(:rule){DiscourseChat::Rule.create({provider: 'dummy', channel: '#general', filter:'follow', category_id:category.id, tags:[tag.name]})} let(:rule){DiscourseChat::Rule.create(channel: channel, filter:'follow', category_id:category.id, tags:[tag.name])}
include_examples 'admin constraints', 'put', "/admin/plugins/chat/rules/1.json" include_examples 'admin constraints', 'put', "/admin/plugins/chat/rules/1.json"
@ -178,9 +266,8 @@ describe 'Chat Controller', type: :request do
it 'should be able update a rule' do it 'should be able update a rule' do
put "/admin/plugins/chat/rules/#{rule.id}.json", put "/admin/plugins/chat/rules/#{rule.id}.json",
rule:{ rule:{
provider: rule.provider, channel_id: channel.id,
channel: '#random', category_id: category2.id,
category_id: rule.category_id,
filter: rule.filter, filter: rule.filter,
tags: rule.tags tags: rule.tags
} }
@ -188,20 +275,13 @@ describe 'Chat Controller', type: :request do
expect(response).to be_success expect(response).to be_success
rule = DiscourseChat::Rule.all.first rule = DiscourseChat::Rule.all.first
expect(rule.category_id).to eq(category2.id)
expect(rule.provider).to eq('dummy')
expect(rule.channel).to eq('#random')
expect(rule.category_id).to eq(category.id)
expect(rule.filter).to eq('follow')
expect(rule.tags).to eq([tag.name])
end end
it 'should fail for invalid params' do it 'should fail for invalid params' do
put "/admin/plugins/chat/rules/#{rule.id}.json", put "/admin/plugins/chat/rules/#{rule.id}.json",
rule:{ rule:{
provider: 'dummy', channel_id: channel.id,
channel: '#general',
category_id: category.id, category_id: category.id,
filter: 'watch', filter: 'watch',
tags: ['somenonexistanttag'] tags: ['somenonexistanttag']
@ -214,7 +294,7 @@ describe 'Chat Controller', type: :request do
end end
describe 'deleting a rule' do describe 'deleting a rule' do
let(:rule){DiscourseChat::Rule.create({provider: 'dummy', channel: '#general', 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" include_examples 'admin constraints', 'delete', "/admin/plugins/chat/rules/1.json"

View File

@ -48,7 +48,7 @@ RSpec.shared_context "validated dummy provider" do
PROVIDER_NAME = "dummy2".freeze PROVIDER_NAME = "dummy2".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting
CHANNEL_PARAMETERS = [ CHANNEL_PARAMETERS = [
{key: "val", regex: '\S+'} {key: "val", regex: '^\S+$'}
] ]
@@sent_messages = [] @@sent_messages = []