Add telegram specs

This commit is contained in:
David Taylor 2017-07-19 21:26:16 +01:00
parent 4422f14c6d
commit 970c38c7fe
3 changed files with 123 additions and 1 deletions

View File

@ -4,7 +4,6 @@ DiscourseEvent.on(:site_setting_saved) do |sitesetting|
if (isEnabledSetting or isAccessToken)
enabled = isEnabledSetting ? sitesetting.value == 't' : SiteSetting.chat_integration_telegram_enabled
# Rails.logger.error("JOB ENQUEUED"+sitesetting.value+SiteSetting.chat_integration_telegram_enabled.to_s)
if enabled
Scheduler::Defer.later("Setup Telegram Webhook") do
DiscourseChat::Provider::TelegramProvider.setup_webhook()

View File

@ -0,0 +1,93 @@
require 'rails_helper'
describe 'Telegram Command Controller', type: :request do
let(:category) { Fabricate(:category) }
let!(:chan1){DiscourseChat::Channel.create!(provider:'telegram', data:{name: 'Amazing Channel', chat_id:'123'})}
describe 'with plugin disabled' do
it 'should return a 404' do
post '/chat-integration/telegram/command/abcd.json'
expect(response.status).to eq(404)
end
end
describe 'with plugin enabled and provider disabled' do
before do
SiteSetting.chat_integration_enabled = true
SiteSetting.chat_integration_telegram_enabled = false
end
it 'should return a 404' do
post '/chat-integration/telegram/command/abcd.json'
expect(response.status).to eq(404)
end
end
describe 'slash commands endpoint' do
before do
SiteSetting.chat_integration_enabled = true
SiteSetting.chat_integration_telegram_secret = "shhh"
SiteSetting.chat_integration_telegram_access_token = "TOKEN"
SiteSetting.chat_integration_telegram_enabled = true
end
let!(:stub){stub_request(:post, 'https://api.telegram.org/botTOKEN/sendMessage').to_return(body: "{\"ok\":true}")}
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' }
expect(response.status).to eq(200)
end
end
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' }
expect(response.status).to eq(403)
end
end
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' }
expect(response.status).to eq(403)
end
end
describe 'when token is valid' do
let(:token) { "TOKEN" }
before do
SiteSetting.chat_integration_telegram_enable_slash_commands = true
end
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}" }
expect(response.status).to eq(200)
expect(stub).to have_been_requested.once
rule = DiscourseChat::Rule.all.first
expect(rule.channel).to eq(chan1)
expect(rule.filter).to eq('watch')
expect(rule.category_id).to eq(category.id)
expect(rule.tags).to eq(nil)
end
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}" }
expect(DiscourseChat::Rule.all.size).to eq(0)
expect(DiscourseChat::Channel.all.size).to eq(1)
end
end
end
end
end
end

View File

@ -0,0 +1,30 @@
require 'rails_helper'
RSpec.describe DiscourseChat::Provider::TelegramProvider do
let(:post) { Fabricate(:post) }
describe '.trigger_notifications' do
before do
SiteSetting.chat_integration_telegram_access_token = "TOKEN"
SiteSetting.chat_integration_telegram_secret = 'shhh'
SiteSetting.chat_integration_telegram_enabled = true
end
let(:chan1){DiscourseChat::Channel.create!(provider:'telegram', data:{name: "Awesome Channel", chat_id: '123'})}
it 'sends a webhook request' do
stub1 = stub_request(:post, 'https://api.telegram.org/botTOKEN/sendMessage').to_return(body: "{\"ok\":true}")
described_class.trigger_notification(post, chan1)
expect(stub1).to have_been_requested.once
end
it 'handles errors correctly' do
stub1 = stub_request(:post, 'https://api.telegram.org/botTOKEN/sendMessage').to_return(body: "{\"ok\":false, \"description\":\"chat not found\"}")
expect(stub1).to have_been_requested.times(0)
expect{described_class.trigger_notification(post, chan1)}.to raise_exception(::DiscourseChat::ProviderError)
expect(stub1).to have_been_requested.once
end
end
end