From 970c38c7fe90fb5966ed0315446c20e3c34ae57a Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 19 Jul 2017 21:26:16 +0100 Subject: [PATCH] Add telegram specs --- .../provider/telegram/telegram_initializer.rb | 1 - .../telegram_command_controller_spec.rb | 93 +++++++++++++++++++ .../telegram/telegram_provider_spec.rb | 30 ++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb create mode 100644 spec/lib/discourse_chat/provider/telegram/telegram_provider_spec.rb diff --git a/lib/discourse_chat/provider/telegram/telegram_initializer.rb b/lib/discourse_chat/provider/telegram/telegram_initializer.rb index 2f218e3..cff63b4 100644 --- a/lib/discourse_chat/provider/telegram/telegram_initializer.rb +++ b/lib/discourse_chat/provider/telegram/telegram_initializer.rb @@ -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() 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 new file mode 100644 index 0000000..d995a26 --- /dev/null +++ b/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/lib/discourse_chat/provider/telegram/telegram_provider_spec.rb b/spec/lib/discourse_chat/provider/telegram/telegram_provider_spec.rb new file mode 100644 index 0000000..38f4966 --- /dev/null +++ b/spec/lib/discourse_chat/provider/telegram/telegram_provider_spec.rb @@ -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