Skip to content

Commit

Permalink
Use config.token and config.aliases if set (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek committed Apr 9, 2020
1 parent 2155bf3 commit f096855
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.14.1 (Next)

* Your contribution here.
* [#254](https://github.com/slack-ruby/slack-ruby-bot/pull/254): Allow setting of `config.token` and `config.aliases` in initializer - [@wasabigeek](https://github.com/wasabigeek).
* [#253](https://github.com/slack-ruby/slack-ruby-bot/pull/253): Remove reference to unsupported Giphy content rating - [@wasabigeek](https://github.com/wasabigeek).

### 0.14.0 (2020/4/2)
Expand Down
28 changes: 16 additions & 12 deletions lib/slack-ruby-bot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

module SlackRubyBot
class App < Server
def initialize(options = {})
SlackRubyBot.configure do |config|
config.token = ENV['SLACK_API_TOKEN'] || raise("Missing ENV['SLACK_API_TOKEN'].")
config.aliases = ENV['SLACK_RUBY_BOT_ALIASES'].split(' ') if ENV['SLACK_RUBY_BOT_ALIASES']
end
Slack.configure do |config|
config.token = SlackRubyBot.config.token
end
super
end

def config
SlackRubyBot.config
end

def self.instance
@instance ||= new
@instance ||= begin
configure!
new(token: SlackRubyBot.config.token)
end
end

def self.configure!
SlackRubyBot.configure do |config|
config.token = ENV['SLACK_API_TOKEN'] if ENV.key?('SLACK_API_TOKEN')
raise('Missing Slack API Token.') unless config.token.present?

config.aliases = ENV['SLACK_RUBY_BOT_ALIASES'].split(' ') if ENV.key?('SLACK_RUBY_BOT_ALIASES')
end
Slack.configure do |config|
config.token = SlackRubyBot.config.token
end
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
context 'not configured' do
before do
@slack_api_token = ENV.delete('SLACK_API_TOKEN')
SlackRubyBot.configure { |config| config.token = nil }
end
after do
ENV['SLACK_API_TOKEN'] = @slack_api_token
end
it 'requires SLACK_API_TOKEN' do
expect { subject }.to raise_error RuntimeError, "Missing ENV['SLACK_API_TOKEN']."
expect { described_class.instance }.to raise_error RuntimeError, 'Missing Slack API Token.'
end
end
end
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/rspec/support/slack_api_key.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.before :all do
config.before :each do
ENV['SLACK_API_TOKEN'] ||= 'test'
end
end
63 changes: 62 additions & 1 deletion spec/slack-ruby-bot/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,71 @@ def app
it_behaves_like 'a slack ruby bot'

describe '.instance' do
let(:token) { 'slack-api-token' }
let(:klass) { Class.new(SlackRubyBot::App) }

it 'creates an instance of the App subclass' do
klass = Class.new(SlackRubyBot::App)
expect(klass.instance.class).to be klass
end

it 'sets config.token from ENV' do
expect(klass.instance.config.token).to eq('test')
end

context 'when token is not defined' do
before do
ENV.delete('SLACK_API_TOKEN')
SlackRubyBot::Config.token = nil
end

it 'raises error' do
expect { klass.instance }.to raise_error RuntimeError, 'Missing Slack API Token.'
end
end

context 'when token is defined in ENV but not config' do
before do
SlackRubyBot::Config.token = nil
end

it 'sets config.token from ENV' do
expect(klass.instance.config.token).to eq('test')
end
end

context 'when token is not defined in ENV but is defined in config' do
before do
ENV.delete('SLACK_API_TOKEN')
end

it 'sets config.token from config' do
expect(klass.instance.config.token).to eq('testtoken')
end
end

context 'when aliases are defined in config only' do
let(:aliases) { %w[alias alias2] }
before do
SlackRubyBot::Config.aliases = aliases
end

it 'sets config.aliases' do
expect(klass.instance.config.aliases).to eq(aliases)
end
end

context 'when aliases are defined in ENV only' do
before do
ENV['SLACK_RUBY_BOT_ALIASES'] = 'alias3 alias4'
end
after do
ENV.delete('SLACK_RUBY_BOT_ALIASES')
end

it 'sets config.aliases from env' do
expect(klass.instance.config.aliases).to eq(%w[alias3 alias4])
end
end
end

describe 'executable' do
Expand Down

0 comments on commit f096855

Please sign in to comment.