Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config.token and config.aliases if set #254

Merged
merged 20 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 in initializer - [@wasabigeek](https://github.com/wasabigeek).
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
* [#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
25 changes: 17 additions & 8 deletions lib/slack-ruby-bot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
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

Expand All @@ -18,11 +11,27 @@ def config
end

def self.instance
@instance ||= new
@instance ||= begin
configure!
new
end
end

private

def self.configure!
SlackRubyBot.configure do |config|
config.token ||= ENV['SLACK_API_TOKEN']
raise("Missing ENV['SLACK_API_TOKEN'].") if config.token.nil?

config.aliases = ENV['SLACK_RUBY_BOT_ALIASES'].split(' ') if ENV['SLACK_RUBY_BOT_ALIASES']
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
end
Slack.configure do |config|
config.token = SlackRubyBot.config.token
end
end
private_class_method :configure!
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved

def hello(client, _data)
if client.team && client.self
SlackRubyBot.configure do |config|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
# frozen_string_literal: true

shared_examples 'a slack ruby bot' do
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
let(:token) { 'slack-api-token' }

context 'not configured' do
before do
@slack_api_token = ENV.delete('SLACK_API_TOKEN')
SlackRubyBot.configure { |config| config.token = nil }
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
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 ENV['SLACK_API_TOKEN']."
end
end

context 'when SLACK_API_TOKEN is defined in ENV but not config' do
it 'sets config.token from ENV' do
allow(ENV).to receive(:[]).and_call_original.at_least(:once)
allow(ENV).to receive(:[]).with('SLACK_API_TOKEN').and_return(token)
SlackRubyBot.configure { |config| config.token = nil }
Copy link
Contributor Author

@wasabigeek wasabigeek Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern deviates from the existing, but I do feel it makes the tests more isolated from the setup in spec_helper. What do you think @dblock?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.


expect(described_class.instance.config.token).to eq(token)
end
end

context 'when SLACK_API_TOKEN is not defined in ENV but is defined in config' do
it 'sets config.token from config' do
allow(ENV).to receive(:[]).at_least(:once)
allow(ENV).to receive(:[]).with('SLACK_API_TOKEN').and_return(nil)
SlackRubyBot.configure { |config| config.token = token }

expect(described_class.instance.config.token).to eq(token)
end
end

context 'when SLACK_API_TOKEN is defined in ENV and config' do
it 'sets config.token from config' do
token2 = 'another-api-token'
allow(ENV).to receive(:[]).at_least(:once)
allow(ENV).to receive(:[]).with('SLACK_API_TOKEN').and_return(token2)
SlackRubyBot.configure { |config| config.token = token }

expect(described_class.instance.config.token).to eq(token)
end
end
end