2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-10 16:04:37 -04:00
|
|
|
require 'rails_helper'
|
2017-07-13 09:01:30 -04:00
|
|
|
require_relative '../dummy_provider'
|
2017-07-10 16:04:37 -04:00
|
|
|
|
|
|
|
describe 'Chat Controller', type: :request do
|
2018-06-07 20:57:43 -04:00
|
|
|
|
|
|
|
let(:topic) { Fabricate(:post).topic }
|
2017-07-10 16:04:37 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:category) { Fabricate(:category) }
|
2017-07-18 13:23:20 -04:00
|
|
|
let(:category2) { Fabricate(:category) }
|
2017-07-10 16:04:37 -04:00
|
|
|
let(:tag) { Fabricate(:tag) }
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:channel) { DiscourseChat::Channel.create(provider: 'dummy') }
|
2017-07-10 16:04:37 -04:00
|
|
|
|
|
|
|
include_context "dummy provider"
|
2017-07-31 08:12:09 -04:00
|
|
|
include_context "validated dummy provider"
|
2017-07-10 16:04:37 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.chat_integration_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'admin constraints' do |action, route|
|
|
|
|
context 'when user is not signed in' do
|
|
|
|
it 'should raise the right error' do
|
2018-01-11 22:48:00 -05:00
|
|
|
public_send(action, route)
|
|
|
|
expect(response.status).to eq(404)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not an admin' do
|
|
|
|
it 'should raise the right error' do
|
|
|
|
sign_in(Fabricate(:user))
|
2018-01-11 22:48:00 -05:00
|
|
|
public_send(action, route)
|
|
|
|
expect(response.status).to eq(404)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'listing providers' do
|
|
|
|
include_examples 'admin constraints', 'get', '/admin/plugins/chat/providers.json'
|
|
|
|
|
|
|
|
context 'when signed in as an admin' do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
|
|
|
get '/admin/plugins/chat/providers.json'
|
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2020-09-25 14:47:30 -04:00
|
|
|
json = response.parsed_body
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(json['providers'].size).to eq(2)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-10-10 01:09:25 -04:00
|
|
|
expect(json['providers'].find { |h| h['name'] == 'dummy' }).to eq(
|
|
|
|
'name' => 'dummy',
|
|
|
|
'id' => 'dummy',
|
|
|
|
'channel_parameters' => []
|
|
|
|
)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
describe 'testing channels' do
|
2017-07-10 16:04:37 -04:00
|
|
|
include_examples 'admin constraints', 'get', '/admin/plugins/chat/test.json'
|
|
|
|
|
|
|
|
context 'when signed in as an admin' do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/test.json', params: {
|
|
|
|
channel_id: channel.id, topic_id: topic.id
|
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
it 'should fail for invalid channel' do
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/test.json', params: {
|
|
|
|
channel_id: 999, topic_id: topic.id
|
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(422)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
describe 'viewing channels' do
|
|
|
|
include_examples 'admin constraints', 'get', '/admin/plugins/chat/channels.json'
|
2017-07-10 16:04:37 -04:00
|
|
|
|
|
|
|
context 'when signed in as an admin' do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
2017-10-10 01:09:25 -04:00
|
|
|
rule = DiscourseChat::Rule.create(
|
|
|
|
channel: channel,
|
|
|
|
filter: 'follow',
|
|
|
|
category_id: category.id,
|
|
|
|
tags: [tag.name]
|
|
|
|
)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-09-24 23:06:27 -04:00
|
|
|
get '/admin/plugins/chat/channels.json', params: { provider: 'dummy' }
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2020-09-25 14:47:30 -04:00
|
|
|
channels = response.parsed_body['channels']
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(channels.count).to eq(1)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(channels.first).to eq(
|
|
|
|
"id" => channel.id,
|
|
|
|
"provider" => 'dummy',
|
|
|
|
"data" => {},
|
2017-07-18 18:08:06 -04:00
|
|
|
"error_key" => nil,
|
2018-08-20 08:17:54 -04:00
|
|
|
"error_info" => nil,
|
2017-08-01 15:53:39 -04:00
|
|
|
"rules" => [{ "id" => rule.id, "type" => 'normal', "group_name" => nil, "group_id" => nil, "filter" => "follow", "channel_id" => channel.id, "category_id" => category.id, "tags" => [tag.name] }]
|
2017-07-10 16:04:37 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail for invalid provider' do
|
2018-01-11 22:48:00 -05:00
|
|
|
get '/admin/plugins/chat/channels.json', params: { provider: 'someprovider' }
|
|
|
|
expect(response.status).to eq(400)
|
2017-07-18 13:23:20 -04:00
|
|
|
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
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/channels.json', params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
channel: {
|
2017-07-18 13:23:20 -04:00
|
|
|
provider: 'dummy',
|
|
|
|
data: {}
|
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-18 13:23:20 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-18 13:23:20 -04:00
|
|
|
|
2017-09-24 23:06:27 -04:00
|
|
|
channel = DiscourseChat::Channel.all.last
|
2017-07-18 13:23:20 -04:00
|
|
|
|
|
|
|
expect(channel.provider).to eq('dummy')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail for invalid params' do
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/channels.json', params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
channel: {
|
2017-07-18 13:23:20 -04:00
|
|
|
provider: 'dummy2',
|
2017-08-01 15:53:39 -04:00
|
|
|
data: { val: 'something with whitespace' }
|
2017-07-18 13:23:20 -04:00
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-18 13:23:20 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(422)
|
2017-07-18 13:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'updating a channel' do
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:channel) { DiscourseChat::Channel.create(provider: 'dummy2', data: { val: "something" }) }
|
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
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
|
2017-09-24 23:06:27 -04:00
|
|
|
put "/admin/plugins/chat/channels/#{channel.id}.json", params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
channel: {
|
|
|
|
data: { val: "something-else" }
|
2017-07-18 13:23:20 -04:00
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-18 13:23:20 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-18 13:23:20 -04:00
|
|
|
|
2017-09-24 23:06:27 -04:00
|
|
|
channel = DiscourseChat::Channel.all.last
|
2017-08-01 15:53:39 -04:00
|
|
|
expect(channel.data).to eq("val" => "something-else")
|
2017-07-18 13:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail for invalid params' do
|
2017-09-24 23:06:27 -04:00
|
|
|
put "/admin/plugins/chat/channels/#{channel.id}.json", params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
channel: {
|
|
|
|
data: { val: "something with whitespace" }
|
2017-07-18 13:23:20 -04:00
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(422)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
2017-07-18 13:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'deleting a channel' do
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:channel) { DiscourseChat::Channel.create(provider: 'dummy', data: {}) }
|
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
include_examples 'admin constraints', 'delete', "/admin/plugins/chat/channels/1.json"
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
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"
|
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(DiscourseChat::Channel.all.size).to eq(0)
|
|
|
|
end
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'adding a rule' do
|
|
|
|
include_examples 'admin constraints', 'put', '/admin/plugins/chat/rules.json'
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to add a new rule' do
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/rules.json', params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
rule: {
|
2017-07-18 13:23:20 -04:00
|
|
|
channel_id: channel.id,
|
2017-07-10 16:04:37 -04:00
|
|
|
category_id: category.id,
|
|
|
|
filter: 'watch',
|
|
|
|
tags: [tag.name]
|
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-09-24 23:06:27 -04:00
|
|
|
rule = DiscourseChat::Rule.all.last
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(rule.channel_id).to eq(channel.id)
|
2017-07-10 16:04:37 -04:00
|
|
|
expect(rule.category_id).to eq(category.id)
|
|
|
|
expect(rule.filter).to eq('watch')
|
|
|
|
expect(rule.tags).to eq([tag.name])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail for invalid params' do
|
2017-09-24 23:06:27 -04:00
|
|
|
post '/admin/plugins/chat/rules.json', params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
rule: {
|
2017-07-18 13:23:20 -04:00
|
|
|
channel_id: channel.id,
|
2017-07-10 16:04:37 -04:00
|
|
|
category_id: category.id,
|
|
|
|
filter: 'watch',
|
|
|
|
tags: ['somenonexistanttag']
|
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(422)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'updating a rule' do
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:rule) { DiscourseChat::Rule.create(channel: channel, filter: 'follow', category_id: category.id, tags: [tag.name]) }
|
|
|
|
|
2017-07-10 16:04:37 -04:00
|
|
|
include_examples 'admin constraints', 'put', "/admin/plugins/chat/rules/1.json"
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able update a rule' do
|
2017-09-24 23:06:27 -04:00
|
|
|
put "/admin/plugins/chat/rules/#{rule.id}.json", params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
rule: {
|
2017-07-18 13:23:20 -04:00
|
|
|
channel_id: channel.id,
|
|
|
|
category_id: category2.id,
|
2017-07-10 16:04:37 -04:00
|
|
|
filter: rule.filter,
|
|
|
|
tags: rule.tags
|
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2017-09-24 23:06:27 -04:00
|
|
|
rule = DiscourseChat::Rule.all.last
|
2017-07-18 13:23:20 -04:00
|
|
|
expect(rule.category_id).to eq(category2.id)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail for invalid params' do
|
2017-09-24 23:06:27 -04:00
|
|
|
put "/admin/plugins/chat/rules/#{rule.id}.json", params: {
|
2017-08-01 15:53:39 -04:00
|
|
|
rule: {
|
2017-07-18 13:23:20 -04:00
|
|
|
channel_id: channel.id,
|
2017-07-10 16:04:37 -04:00
|
|
|
category_id: category.id,
|
|
|
|
filter: 'watch',
|
|
|
|
tags: ['somenonexistanttag']
|
|
|
|
}
|
2017-09-24 23:06:27 -04:00
|
|
|
}
|
2017-07-10 16:04:37 -04:00
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(422)
|
2017-07-10 16:04:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'deleting a rule' do
|
2017-10-10 01:09:25 -04:00
|
|
|
let(:rule) do
|
|
|
|
DiscourseChat::Rule.create!(
|
|
|
|
channel_id: channel.id,
|
|
|
|
filter: 'follow',
|
|
|
|
category_id: category.id,
|
|
|
|
tags: [tag.name]
|
|
|
|
)
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-07-10 16:04:37 -04:00
|
|
|
include_examples 'admin constraints', 'delete', "/admin/plugins/chat/rules/1.json"
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able delete a rule' do
|
|
|
|
delete "/admin/plugins/chat/rules/#{rule.id}.json"
|
|
|
|
|
2018-06-07 20:57:43 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-07-10 16:04:37 -04:00
|
|
|
expect(DiscourseChat::Rule.all.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|