diff --git a/.rubocop.yml b/.rubocop.yml index 279cefbc..1b7f2aed 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -22,3 +22,8 @@ Metrics/LineLength: Max: 100 Style/Documentation: Enabled: false +Style/ModuleFunction: + Enabled: false +Naming/FileName: + Exclude: + - lib/slack-ruby-client.rb # Required to match gemspec name diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 56f72a9c..989f072a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,24 +1,17 @@ # This configuration was generated by -# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100` -# on 2019-08-01 15:06:19 -0700 using RuboCop version 0.73.0. +# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 1000` +# on 2019-08-20 22:22:29 -0700 using RuboCop version 0.73.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 2 -# Configuration parameters: AllowSafeAssignment. -Lint/AssignmentInCondition: - Exclude: - - 'lib/slack/real_time/concurrency/async.rb' - - 'lib/slack/real_time/socket.rb' - # Offense count: 5 # Configuration parameters: AllowComments. Lint/HandleExceptions: Exclude: - - 'lib/slack-ruby-client.rb' - 'lib/slack/real_time/concurrency/async.rb' + - 'lib/slack_ruby_client.rb' - 'spec/slack/real_time/concurrency/celluloid_spec.rb' - 'spec/slack/real_time/concurrency/eventmachine_spec.rb' @@ -63,20 +56,6 @@ Metrics/PerceivedComplexity: Exclude: - 'lib/slack/web/faraday/connection.rb' -# Offense count: 1 -# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. -# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS -Naming/FileName: - Exclude: - - 'lib/slack-ruby-client.rb' - -# Offense count: 1 -# Configuration parameters: EnforcedStyleForLeadingUnderscores. -# SupportedStylesForLeadingUnderscores: disallowed, required, optional -Naming/MemoizedInstanceVariableName: - Exclude: - - 'lib/slack/logger.rb' - # Offense count: 1 RSpec/AnyInstance: Exclude: @@ -113,29 +92,6 @@ RSpec/ContextWording: - 'spec/slack/web/client_spec.rb' - 'spec/support/real_time/connected_client.rb' -# Offense count: 1 -RSpec/DescribeClass: - Exclude: - - 'spec/integration/integration_spec.rb' - -# Offense count: 5 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterExampleGroup: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/auth_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/chat_spec.rb' - -# Offense count: 8 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterFinalLet: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/auth_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/channels_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/chat_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/groups_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/users_spec.rb' - # Offense count: 63 # Configuration parameters: Max. RSpec/ExampleLength: @@ -161,11 +117,6 @@ RSpec/ExampleLength: - 'spec/slack/web/client_spec.rb' - 'spec/slack/web/faraday/response/raise_error_spec.rb' -# Offense count: 2 -RSpec/ExpectInHook: - Exclude: - - 'spec/slack/real_time/client_spec.rb' - # Offense count: 16 # Configuration parameters: CustomTransform, IgnoreMethods. RSpec/FilePath: @@ -187,14 +138,6 @@ RSpec/FilePath: - 'spec/slack/web/api/endpoints/custom_specs/users_spec.rb' - 'spec/slack/web/api/errors/service_unavailable_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: implicit, each, example -RSpec/HookArgument: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/chat_spec.rb' - # Offense count: 13 # Configuration parameters: AssignmentOnly. RSpec/InstanceVariable: @@ -203,7 +146,7 @@ RSpec/InstanceVariable: - 'spec/slack/real_time/client_spec.rb' - 'spec/slack/slack_spec.rb' -# Offense count: 68 +# Offense count: 66 # Configuration parameters: . # SupportedStyles: have_received, receive RSpec/MessageSpies: @@ -238,17 +181,6 @@ RSpec/MultipleExpectations: - 'spec/slack/web/api/pagination/cursor_spec.rb' - 'spec/slack/web/client_spec.rb' -# Offense count: 53 -# Configuration parameters: IgnoreSharedExamples. -RSpec/NamedSubject: - Exclude: - - 'spec/slack/events/request_spec.rb' - - 'spec/slack/messages/formatting_spec.rb' - - 'spec/slack/web/api/mixins/channels_spec.rb' - - 'spec/slack/web/api/mixins/groups_spec.rb' - - 'spec/slack/web/api/mixins/users_spec.rb' - - 'spec/slack/web/faraday/response/raise_error_spec.rb' - # Offense count: 32 # Configuration parameters: Max. RSpec/NestedGroups: @@ -257,31 +189,6 @@ RSpec/NestedGroups: - 'spec/slack/web/api/pagination/cursor_spec.rb' - 'spec/slack/web/client_spec.rb' -# Offense count: 15 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: not_to, to_not -RSpec/NotToNot: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/auth_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/chat_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb' - -# Offense count: 2 -RSpec/RepeatedDescription: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/users_spec.rb' - -# Offense count: 2 -RSpec/ScatteredLet: - Exclude: - - 'spec/integration/integration_spec.rb' - -# Offense count: 4 -RSpec/ScatteredSetup: - Exclude: - - 'spec/integration/integration_spec.rb' - # Offense count: 3 RSpec/SubjectStub: Exclude: @@ -299,60 +206,3 @@ RSpec/VerifiedDoubles: - 'spec/slack/real_time/concurrency/eventmachine_spec.rb' - 'spec/slack/web/faraday/response/raise_error_spec.rb' - 'spec/support/real_time/connected_client.rb' - -# Offense count: 1 -Security/Open: - Exclude: - - 'lib/slack/real_time/client.rb' - -# Offense count: 2 -# Configuration parameters: EnforcedStyle. -# SupportedStyles: inline, group -Style/AccessModifierDeclarations: - Exclude: - - 'lib/slack/real_time/concurrency/eventmachine.rb' - - 'lib/slack/real_time/socket.rb' - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, never -Style/FrozenStringLiteralComment: - Exclude: - - 'spec/slack/web/api/endpoints/custom_specs/auth_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/channels_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/chat_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/groups_spec.rb' - - 'spec/slack/web/api/endpoints/custom_specs/users_spec.rb' - -# Offense count: 2 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/slack/real_time/socket.rb' - - 'lib/slack/web/faraday/response/raise_error.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, Autocorrect. -# SupportedStyles: module_function, extend_self -Style/ModuleFunction: - Exclude: - - 'lib/slack/config.rb' - - 'lib/slack/events/config.rb' - - 'lib/slack/web/config.rb' - -# Offense count: 1 -Style/MultilineTernaryOperator: - Exclude: - - 'spec/support/real_time/connected_client.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods. -# SupportedStyles: predicate, comparison -Style/NumericPredicate: - Exclude: - - 'spec/**/*' - - 'lib/slack/real_time/client.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index 610edd54..65a909f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ### 0.14.5 (Next) * [#293](https://github.com/slack-ruby/slack-ruby-client/pull/293): Rubocop auto-correct and comprehensive todo - [@jcraigk](https://github.com/jcraigk). +* [#297](https://github.com/slack-ruby/slack-ruby-client/pull/297): Various Rubocop fixes - [@jcraigk](https://github.com/jcraigk). + * Your contribution here. ### 0.14.4 (2019/7/28) diff --git a/bin/slack b/bin/slack index 9f11d524..a873f9fb 100755 --- a/bin/slack +++ b/bin/slack @@ -1,6 +1,6 @@ #!/usr/bin/env ruby require 'gli' -require 'slack-ruby-client' +require 'slack_ruby_client' include GLI::App diff --git a/lib/slack-ruby-client.rb b/lib/slack-ruby-client.rb index e81140ad..cca77626 100644 --- a/lib/slack-ruby-client.rb +++ b/lib/slack-ruby-client.rb @@ -15,12 +15,12 @@ require 'logger' begin require 'picky' -rescue LoadError +rescue LoadError # rubocop:disable Lint/HandleExceptions # ignore, only used in users_search end begin require 'openssl' -rescue LoadError +rescue LoadError # rubocop:disable Lint/HandleExceptions # Used in slack/web/config end require_relative 'slack/web/config' diff --git a/lib/slack/logger.rb b/lib/slack/logger.rb index 6330dd02..f52355f6 100644 --- a/lib/slack/logger.rb +++ b/lib/slack/logger.rb @@ -4,11 +4,11 @@ module Slack class Logger < ::Logger def self.default - @logger ||= begin - logger = new STDOUT - logger.level = Logger::WARN - logger - end + return @default if @default + + logger = new STDOUT + logger.level = Logger::WARN + @default = logger end end end diff --git a/lib/slack/real_time/client.rb b/lib/slack/real_time/client.rb index 8d69e798..cd5e6517 100644 --- a/lib/slack/real_time/client.rb +++ b/lib/slack/real_time/client.rb @@ -84,7 +84,7 @@ def run_loop @socket.connect! do |driver| driver.on :open do |event| logger.debug("#{self}##{__method__}") { event.class.name } - open(event) + open_event(event) callback(event, :open) end @@ -137,7 +137,7 @@ def run_ping! end def run_ping? - !websocket_ping.nil? && websocket_ping > 0 + !websocket_ping.nil? && websocket_ping.positive? end def websocket_ping_timer @@ -205,7 +205,7 @@ def send_json(data) @socket.send_data(data.to_json) end - def open(_event); end + def open_event(_event); end def close(_event) [@socket, socket_class].each do |s| diff --git a/lib/slack/real_time/concurrency/async.rb b/lib/slack/real_time/concurrency/async.rb index b0aaa787..b206bf30 100644 --- a/lib/slack/real_time/concurrency/async.rb +++ b/lib/slack/real_time/concurrency/async.rb @@ -82,7 +82,7 @@ def connect! def disconnect! super ensure - if restart = @restart + if (restart = @restart) @restart = nil restart.signal end diff --git a/lib/slack/real_time/concurrency/eventmachine.rb b/lib/slack/real_time/concurrency/eventmachine.rb index 25a8f898..9820d721 100644 --- a/lib/slack/real_time/concurrency/eventmachine.rb +++ b/lib/slack/real_time/concurrency/eventmachine.rb @@ -7,9 +7,6 @@ module RealTime module Concurrency module Eventmachine class Client < Faye::WebSocket::Client - attr_reader :logger - protected :logger - def initialize(url, protocols = nil, options = {}) @logger = options.fetch(:logger) || Slack::RealTime::Config.logger || Slack::Config.logger @@ -25,6 +22,10 @@ def write(data) logger.debug("#{self.class}##{__method__}") { data } super data end + + protected + + attr_reader :logger end class Socket < Slack::RealTime::Socket diff --git a/lib/slack/real_time/socket.rb b/lib/slack/real_time/socket.rb index 459c175b..6870e87f 100644 --- a/lib/slack/real_time/socket.rb +++ b/lib/slack/real_time/socket.rb @@ -5,8 +5,6 @@ class Socket attr_accessor :url attr_accessor :options attr_reader :driver - attr_reader :logger - protected :logger def initialize(url, options = {}) @url = url @@ -80,14 +78,16 @@ def close # When you call `driver.emit(:close)`, it will typically end up calling `client.close` # which will call `@socket.close` and end up back here. In order to break this infinite # recursion, we check and set `@driver = nil` before invoking `client.close`. - if driver = @driver - @driver = nil - driver.emit(:close) - end + return unless (driver = @driver) + + @driver = nil + driver.emit(:close) end protected + attr_reader :logger + def addr URI(url).host end diff --git a/lib/slack/web/faraday/response/raise_error.rb b/lib/slack/web/faraday/response/raise_error.rb index 9fd0f9ab..60b0a770 100644 --- a/lib/slack/web/faraday/response/raise_error.rb +++ b/lib/slack/web/faraday/response/raise_error.rb @@ -5,13 +5,12 @@ module Faraday module Response class RaiseError < ::Faraday::Response::Middleware def on_complete(env) - if env.status == 429 - raise Slack::Web::Api::Errors::TooManyRequestsError, env.response - elsif (body = env.body) && !body['ok'] - error_message = - body['error'] || body['errors'].map { |message| message['error'] }.join(',') - raise Slack::Web::Api::Errors::SlackError.new(error_message, env.response) - end + raise Slack::Web::Api::Errors::TooManyRequestsError, env.response if env.status == 429 + return unless (body = env.body) && !body['ok'] + + error_message = + body['error'] || body['errors'].map { |message| message['error'] }.join(',') + raise Slack::Web::Api::Errors::SlackError.new(error_message, env.response) end end end diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 03ff76c9..8f576418 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'spec_helper' -RSpec.describe 'integration test', skip: ( +RSpec.describe 'integration test', skip: ( # rubocop:disable RSpec/DescribeClass (!ENV['SLACK_API_TOKEN'] || !ENV['CONCURRENCY']) && 'missing SLACK_API_TOKEN and/or CONCURRENCY' ) do around do |ex| @@ -10,11 +10,13 @@ WebMock.disable_net_connect! end + let(:client) { Slack::RealTime::Client.new(token: ENV['SLACK_API_TOKEN']) } let(:logger) do logger = Logger.new(STDOUT) logger.level = Logger::INFO logger end + let!(:queue) { @queue = QueueWithTimeout.new } before do Thread.abort_on_exception = true @@ -22,17 +24,7 @@ Slack.configure do |slack| slack.logger = logger end - end - - after do - Slack.config.reset - end - - let(:client) { Slack::RealTime::Client.new(token: ENV['SLACK_API_TOKEN']) } - let!(:queue) { @queue = QueueWithTimeout.new } - - before do client.on :hello do logger.info( "Successfully connected, welcome '#{client.self.name}' to " \ @@ -51,6 +43,12 @@ end end + after do + Slack.config.reset + wait_for_server # wait for :closed to be pushed on queue + @server.join if @server.is_a?(::Thread) + end + def start_server dt = rand(10..20) logger.debug "#start_server, waiting #{dt} second(s)" @@ -79,11 +77,6 @@ def wait_for_server @queue = nil end - after do - wait_for_server # wait for :closed to be pushed on queue - @server.join if @server.is_a?(::Thread) - end - context 'client connected' do let(:channel) { "@#{client.self.id}" } let(:message) { SecureRandom.hex } diff --git a/spec/slack/events/request_spec.rb b/spec/slack/events/request_spec.rb index c6cc432f..e739688d 100644 --- a/spec/slack/events/request_spec.rb +++ b/spec/slack/events/request_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' RSpec.describe Slack::Events::Request do - subject do + subject(:request) do described_class.new(http_request) end @@ -37,10 +37,10 @@ end it 'reads http request' do - expect(subject.signature).to eq signature - expect(subject.body).to eq body - expect(subject.timestamp).to eq timestamp - expect(subject.version).to eq 'v0' + expect(request.signature).to eq signature + expect(request.body).to eq body + expect(request.timestamp).to eq timestamp + expect(request.version).to eq 'v0' end context 'time' do after do @@ -55,8 +55,8 @@ end it 'is invalid but not expired' do - expect(subject).not_to be_valid - expect(subject).not_to be_expired + expect(request).not_to be_valid + expect(request).not_to be_expired end end @@ -68,8 +68,8 @@ end it 'is invalid but not expired' do - expect(subject).not_to be_valid - expect(subject).not_to be_expired + expect(request).not_to be_valid + expect(request).not_to be_expired end end @@ -82,8 +82,8 @@ end it 'is invalid but not expired' do - expect(subject).not_to be_valid - expect(subject).not_to be_expired + expect(request).not_to be_valid + expect(request).not_to be_expired end end @@ -93,11 +93,11 @@ end it 'is valid' do - expect(subject).to be_valid - expect(subject).not_to be_expired + expect(request).to be_valid + expect(request).not_to be_expired end it 'does not raise an error and returns true' do - expect(subject.verify!).to be true + expect(request.verify!).to be true end end @@ -107,11 +107,11 @@ end it 'is valid but expired' do - expect(subject).to be_valid - expect(subject).to be_expired + expect(request).to be_valid + expect(request).to be_expired end it 'raises an error on verify!' do - expect { subject.verify! }.to raise_error Slack::Events::Request::TimestampExpired + expect { request.verify! }.to raise_error Slack::Events::Request::TimestampExpired end end @@ -121,11 +121,11 @@ end it 'is valid and not expired' do - expect(subject).to be_valid - expect(subject).not_to be_expired + expect(request).to be_valid + expect(request).not_to be_expired end it 'does not raise an error on verify!' do - expect(subject.verify!).to be true + expect(request.verify!).to be true end end @@ -135,11 +135,11 @@ end it 'is valid but expired' do - expect(subject).to be_valid - expect(subject).to be_expired + expect(request).to be_valid + expect(request).to be_expired end it 'raises an error on verify!' do - expect { subject.verify! }.to raise_error Slack::Events::Request::TimestampExpired + expect { request.verify! }.to raise_error Slack::Events::Request::TimestampExpired end end end @@ -150,17 +150,17 @@ end context 'without a signing secret parameter' do - subject do + subject(:request) do described_class.new(http_request) end it 'raises MissingSigningSecret' do - expect { subject.valid? }.to raise_error Slack::Events::Request::MissingSigningSecret + expect { request.valid? }.to raise_error Slack::Events::Request::MissingSigningSecret end end context 'with a signing secret parameter' do - subject do + subject(:request) do described_class.new(http_request, signing_secret: signing_secret, signature_expires_in: 30) @@ -171,8 +171,8 @@ end it 'is valid and not expired' do - expect(subject).to be_valid - expect(subject).not_to be_expired + expect(request).to be_valid + expect(request).not_to be_expired end end end diff --git a/spec/slack/messages/formatting_spec.rb b/spec/slack/messages/formatting_spec.rb index 30c660da..e3ba5ff1 100644 --- a/spec/slack/messages/formatting_spec.rb +++ b/spec/slack/messages/formatting_spec.rb @@ -2,51 +2,53 @@ require 'spec_helper' describe Slack::Messages::Formatting do - subject do + subject(:formatting) do described_class end context '#unescape' do it 'plain text' do - expect(subject.unescape('plain text')).to eq 'plain text' + expect(formatting.unescape('plain text')).to eq 'plain text' end it 'decodes an HTML-encoded message' do - expect(subject.unescape('Hello & <world>')).to eq 'Hello & ' + expect(formatting.unescape('Hello & <world>')).to eq 'Hello & ' end it 'unescapes a user reference' do - expect(subject.unescape('Hey <@U024BE7LH|bob>, did you see my file?')).to( + expect(formatting.unescape('Hey <@U024BE7LH|bob>, did you see my file?')).to( eq('Hey @bob, did you see my file?') ) end it 'unescapes a user reference without a name' do - expect(subject.unescape('<@U02BEFY4U> ^^^')).to eq '@U02BEFY4U ^^^' + expect(formatting.unescape('<@U02BEFY4U> ^^^')).to eq '@U02BEFY4U ^^^' end it 'unescapes a URL without text' do - expect(subject.unescape('This message contains a URL ')).to( + expect(formatting.unescape('This message contains a URL ')).to( eq('This message contains a URL http://foo.com/') ) end it 'unescapes a URL with text' do - expect(subject.unescape('So does this one: ')).to( + expect(formatting.unescape('So does this one: ')).to( eq('So does this one: www.foo.com') ) end it 'removes mailto' do - expect(subject.unescape('')).to eq 'Bob' + expect(formatting.unescape('')).to eq 'Bob' end it 'unlinkifies references' do - expect(subject.unescape('Hello <@U123|bob>, say hi to in <#C1234|general>')).to( + expect( + formatting.unescape('Hello <@U123|bob>, say hi to in <#C1234|general>') + ).to( eq('Hello @bob, say hi to @everyone in #general') ) end it 'can handle a lone >' do - expect(subject.unescape('Hello <@U123|bob> > file.txt')).to eq 'Hello @bob > file.txt' + expect(formatting.unescape('Hello <@U123|bob> > file.txt')).to eq 'Hello @bob > file.txt' end it 'unescapes a double smart quote' do - expect(subject.unescape('“hello”')).to eq '"hello"' + expect(formatting.unescape('“hello”')).to eq '"hello"' end it 'unescapes a single smart quote' do - expect(subject.unescape('‘hello’')).to eq "'hello'" + expect(formatting.unescape('‘hello’')).to eq "'hello'" end end end diff --git a/spec/slack/real_time/client_spec.rb b/spec/slack/real_time/client_spec.rb index 0b83ec06..460d91d2 100644 --- a/spec/slack/real_time/client_spec.rb +++ b/spec/slack/real_time/client_spec.rb @@ -137,7 +137,7 @@ end describe '#stop!' do before do - expect(socket).to receive(:disconnect!) + allow(socket).to receive(:disconnect!) client.stop! end @@ -276,7 +276,7 @@ end describe '#stop!' do before do - expect(socket).to receive(:disconnect!) + allow(socket).to receive(:disconnect!) client.stop! end diff --git a/spec/slack/web/api/endpoints/custom_specs/auth_spec.rb b/spec/slack/web/api/endpoints/custom_specs/auth_spec.rb index 1e2db0b6..e4330dab 100644 --- a/spec/slack/web/api/endpoints/custom_specs/auth_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/auth_spec.rb @@ -1,17 +1,21 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Auth do let(:client) { Slack::Web::Client.new } + context 'without auth', vcr: { cassette_name: 'web/auth_test_error' } do it 'fails with an exception' do expect { client.auth_test }.to raise_error Slack::Web::Api::Errors::SlackError, 'not_authed' end end + context 'with auth', vcr: { cassette_name: 'web/auth_test_success' } do it 'succeeds' do - expect { client.auth_test }.to_not raise_error + expect { client.auth_test }.not_to raise_error end end + context '429 error', vcr: { cassette_name: 'web/429_error' } do it 'fails with an specific exception' do begin diff --git a/spec/slack/web/api/endpoints/custom_specs/channels_spec.rb b/spec/slack/web/api/endpoints/custom_specs/channels_spec.rb index b270e07f..11a27351 100644 --- a/spec/slack/web/api/endpoints/custom_specs/channels_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/channels_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Channels do let(:client) { Slack::Web::Client.new } + context 'channels' do it 'info', vcr: { cassette_name: 'web/channels_info' } do json = client.channels_info(channel: '#general') diff --git a/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb b/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb index 2dec54e7..4ccc9e65 100644 --- a/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb @@ -1,10 +1,13 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Chat do let(:client) { Slack::Web::Client.new } + context 'chat_postEphemeral' do let(:user) { OpenStruct.new(user: { id: '123' }) } - before(:each) do + + before do allow(described_class).to receive(:users_id).and_return(user) end @@ -42,9 +45,10 @@ ) expect do client.chat_postEphemeral(channel: 'channel', text: 'text', user: '123') - end.to_not raise_error + end.not_to raise_error end end + context 'attachments argument' do it 'optional attachments' do expect(client).to( @@ -52,7 +56,7 @@ ) expect do client.chat_postEphemeral(channel: 'channel', text: 'text', user: '123', attachments: []) - end.to_not raise_error + end.not_to raise_error end it 'attachments without text' do expect(client).to( @@ -60,21 +64,22 @@ ) expect do client.chat_postEphemeral(channel: 'channel', attachments: [], user: '123') - end.to_not raise_error + end.not_to raise_error end end + context 'blocks argument' do it 'optional blocks' do expect(client).to receive(:post).with('chat.postEphemeral', hash_including(blocks: '[]')) expect do client.chat_postEphemeral(channel: 'channel', text: 'text', user: '123', blocks: []) - end.to_not raise_error + end.not_to raise_error end it 'blocks without text' do expect(client).to receive(:post).with('chat.postEphemeral', hash_including(blocks: '[]')) expect do client.chat_postEphemeral(channel: 'channel', blocks: [], user: '123') - end.to_not raise_error + end.not_to raise_error end end end @@ -98,15 +103,15 @@ end it 'only text' do expect(client).to receive(:post).with('chat.postMessage', hash_including(text: 'text')) - expect { client.chat_postMessage(channel: 'channel', text: 'text') }.to_not raise_error + expect { client.chat_postMessage(channel: 'channel', text: 'text') }.not_to raise_error end it 'only attachments' do expect(client).to receive(:post).with('chat.postMessage', hash_including(attachments: '[]')) - expect { client.chat_postMessage(channel: 'channel', attachments: []) }.to_not raise_error + expect { client.chat_postMessage(channel: 'channel', attachments: []) }.not_to raise_error end it 'only blocks' do expect(client).to receive(:post).with('chat.postMessage', hash_including(blocks: '[]')) - expect { client.chat_postMessage(channel: 'channel', blocks: []) }.to_not raise_error + expect { client.chat_postMessage(channel: 'channel', blocks: []) }.not_to raise_error end it 'all text, attachments and blocks' do expect(client).to( @@ -115,13 +120,14 @@ ) expect do client.chat_postMessage(channel: 'channel', text: 'text', attachments: [], blocks: []) - end.to_not raise_error + end.not_to raise_error end end end context 'chat_update' do let(:ts) { '1405894322.002768' } + it 'automatically converts attachments and blocks into JSON' do expect(client).to receive(:post).with( 'chat.update', @@ -140,6 +146,7 @@ end.to raise_error(ArgumentError, /Required arguments :ts missing>/) end end + context 'text, attachment and blocks arguments' do it 'requires text, attachments or blocks' do expect { client.chat_update(channel: 'channel', ts: ts) }.to( @@ -150,19 +157,19 @@ expect(client).to receive(:post).with('chat.update', hash_including(text: 'text')) expect do client.chat_update(channel: 'channel', text: 'text', ts: ts) - end.to_not raise_error + end.not_to raise_error end it 'only attachments' do expect(client).to receive(:post).with('chat.update', hash_including(attachments: '[]')) expect do client.chat_update(channel: 'channel', ts: ts, attachments: []) - end.to_not raise_error + end.not_to raise_error end it 'only blocks' do expect(client).to receive(:post).with('chat.update', hash_including(blocks: '[]')) expect do client.chat_update(channel: 'channel', ts: ts, blocks: []) - end.to_not raise_error + end.not_to raise_error end it 'all text, attachments and blocks' do expect(client).to( @@ -171,7 +178,7 @@ ) expect do client.chat_update(channel: 'channel', text: 'text', ts: ts, attachments: [], blocks: []) - end.to_not raise_error + end.not_to raise_error end end end diff --git a/spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb b/spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb index 46938944..9688eb96 100644 --- a/spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Dialog do let(:client) { Slack::Web::Client.new } + context 'dialog_open' do it 'automatically converts dialog into JSON' do expect(client).to receive(:post).with( @@ -27,7 +29,7 @@ expect(client).to( receive(:post).with('dialog.open', hash_including(trigger_id: '123', dialog: '[]')) ) - expect { client.dialog_open(dialog: [], trigger_id: '123') }.to_not raise_error + expect { client.dialog_open(dialog: [], trigger_id: '123') }.not_to raise_error end end end diff --git a/spec/slack/web/api/endpoints/custom_specs/groups_spec.rb b/spec/slack/web/api/endpoints/custom_specs/groups_spec.rb index b03c4d78..57d0a027 100644 --- a/spec/slack/web/api/endpoints/custom_specs/groups_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/groups_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Groups do let(:client) { Slack::Web::Client.new } + context 'groups' do it 'info', vcr: { cassette_name: 'web/groups_info' } do json = client.groups_info(channel: '#mpdm-dblock--rubybot--player1-1') diff --git a/spec/slack/web/api/endpoints/custom_specs/users_spec.rb b/spec/slack/web/api/endpoints/custom_specs/users_spec.rb index a1f7d412..6e20f9ac 100644 --- a/spec/slack/web/api/endpoints/custom_specs/users_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/users_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true require 'spec_helper' RSpec.describe Slack::Web::Api::Endpoints::Users do let(:client) { Slack::Web::Client.new } + context 'users' do it 'list', vcr: { cassette_name: 'web/users_list' } do json = client.users_list(presence: true) @@ -9,7 +11,8 @@ expect(json.members.size).to eq 9 expect(json.members.first.presence).to eq 'away' end - it 'list', vcr: { cassette_name: 'web/paginated_users_list' } do + + it 'paginated list', vcr: { cassette_name: 'web/paginated_users_list' } do members = [] client.users_list(presence: true, limit: 5) do |json| expect(json.ok).to be true @@ -17,10 +20,12 @@ end expect(members.size).to eq 23 end + it 'info', vcr: { cassette_name: 'web/users_info' } do json = client.users_info(user: '@aws') expect(json.user.name).to eq 'aws' end + if defined?(Picky) it 'search', vcr: { cassette_name: 'web/users_info' } do json = client.users_search(user: 'aws') diff --git a/spec/slack/web/api/mixins/channels_spec.rb b/spec/slack/web/api/mixins/channels_spec.rb index 57153405..d3fad67e 100644 --- a/spec/slack/web/api/mixins/channels_spec.rb +++ b/spec/slack/web/api/mixins/channels_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' RSpec.describe Slack::Web::Api::Mixins::Channels do - subject do + subject(:channels) do klass.new end @@ -13,7 +13,7 @@ end before do - allow(subject).to receive(:channels_list).and_return( + allow(channels).to receive(:channels_list).and_return( Slack::Messages::Message.new( 'channels' => [{ 'id' => 'CDEADBEEF', @@ -25,17 +25,17 @@ context '#channels_id' do it 'leaves channels specified by ID alone' do - expect(subject.channels_id(channel: 'C123456')).to( + expect(channels.channels_id(channel: 'C123456')).to( eq('ok' => true, 'channel' => { 'id' => 'C123456' }) ) end it 'translates a channel that starts with a #' do - expect(subject.channels_id(channel: '#general')).to( + expect(channels.channels_id(channel: '#general')).to( eq('ok' => true, 'channel' => { 'id' => 'CDEADBEEF' }) ) end it 'fails with an exception' do - expect { subject.channels_id(channel: '#invalid') }.to( + expect { channels.channels_id(channel: '#invalid') }.to( raise_error(Slack::Web::Api::Errors::SlackError, 'channel_not_found') ) end diff --git a/spec/slack/web/api/mixins/groups_spec.rb b/spec/slack/web/api/mixins/groups_spec.rb index 311755be..206bbc3a 100644 --- a/spec/slack/web/api/mixins/groups_spec.rb +++ b/spec/slack/web/api/mixins/groups_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' RSpec.describe Slack::Web::Api::Mixins::Groups do - subject do + subject(:groups) do klass.new end @@ -13,7 +13,7 @@ end before do - allow(subject).to receive(:groups_list).and_return( + allow(groups).to receive(:groups_list).and_return( Slack::Messages::Message.new( 'groups' => [{ 'id' => 'CDEADBEEF', @@ -25,17 +25,17 @@ context '#groups_id' do it 'leaves groups specified by ID alone' do - expect(subject.groups_id(channel: 'C123456')).to( + expect(groups.groups_id(channel: 'C123456')).to( eq('ok' => true, 'group' => { 'id' => 'C123456' }) ) end it 'translates a channel that starts with a #' do - expect(subject.groups_id(channel: '#general')).to( + expect(groups.groups_id(channel: '#general')).to( eq('ok' => true, 'group' => { 'id' => 'CDEADBEEF' }) ) end it 'fails with an exception' do - expect { subject.groups_id(channel: '#invalid') }.to( + expect { groups.groups_id(channel: '#invalid') }.to( raise_error(Slack::Web::Api::Errors::SlackError, 'channel_not_found') ) end diff --git a/spec/slack/web/api/mixins/users_spec.rb b/spec/slack/web/api/mixins/users_spec.rb index 1b04169e..ad29d2bd 100644 --- a/spec/slack/web/api/mixins/users_spec.rb +++ b/spec/slack/web/api/mixins/users_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' RSpec.describe Slack::Web::Api::Mixins::Users do - subject do + subject(:users) do klass.new end @@ -13,7 +13,7 @@ end before do - allow(subject).to receive(:users_list).and_return( + allow(users).to receive(:users_list).and_return( Slack::Messages::Message.new( 'members' => [{ 'id' => 'UDEADBEEF', @@ -26,13 +26,13 @@ context '#users_id' do it 'leaves users specified by ID alone' do - expect(subject.users_id(user: 'U123456')).to eq('ok' => true, 'user' => { 'id' => 'U123456' }) + expect(users.users_id(user: 'U123456')).to eq('ok' => true, 'user' => { 'id' => 'U123456' }) end it 'translates a user that starts with a #' do - expect(subject.users_id(user: '@aws')).to eq('ok' => true, 'user' => { 'id' => 'UDEADBEEF' }) + expect(users.users_id(user: '@aws')).to eq('ok' => true, 'user' => { 'id' => 'UDEADBEEF' }) end it 'fails with an exception' do - expect { subject.users_id(user: '@foo') }.to( + expect { users.users_id(user: '@foo') }.to( raise_error(Slack::Web::Api::Errors::SlackError, 'user_not_found') ) end @@ -41,7 +41,7 @@ if defined?(Picky) context '#users_search' do it 'finds a user' do - expect(subject.users_search(user: 'aws')).to( + expect(users.users_search(user: 'aws')).to( eq('ok' => true, 'members' => [{ 'id' => 'UDEADBEEF', 'name' => 'aws', 'profile' => {} }]) ) end diff --git a/spec/slack/web/faraday/response/raise_error_spec.rb b/spec/slack/web/faraday/response/raise_error_spec.rb index d39b6567..8106a561 100644 --- a/spec/slack/web/faraday/response/raise_error_spec.rb +++ b/spec/slack/web/faraday/response/raise_error_spec.rb @@ -2,8 +2,9 @@ require 'spec_helper' RSpec.describe Slack::Web::Faraday::Response::RaiseError do + subject(:raise_error_obj) { described_class.new } + describe '#on_complete' do - let(:subject) { described_class.new } let(:status) { 200 } let(:response) { nil } let(:body) { {} } @@ -13,7 +14,7 @@ let(:status) { 429 } it 'raises a TooManyRequestsError' do - expect { subject.on_complete(env) }.to( + expect { raise_error_obj.on_complete(env) }.to( raise_error(Slack::Web::Api::Errors::TooManyRequestsError) ) end @@ -23,7 +24,7 @@ let(:body) { { 'ok' => 'true' } } it 'is nil' do - expect(subject.on_complete(env)).to eq nil + expect(raise_error_obj.on_complete(env)).to eq nil end end @@ -31,7 +32,7 @@ let(:body) { { 'error' => 'already_in_channel' } } it 'raises a SlackError with the error message' do - expect { subject.on_complete(env) }.to( + expect { raise_error_obj.on_complete(env) }.to( raise_error(Slack::Web::Api::Errors::SlackError, 'already_in_channel') ) end @@ -48,7 +49,7 @@ end it 'raises a SlackError with the concatenated error messages' do - expect { subject.on_complete(env) }.to( + expect { raise_error_obj.on_complete(env) }.to( raise_error( Slack::Web::Api::Errors::SlackError, 'already_in_channel,something_else_terrible' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 12d6e730..48d5cd44 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ require 'rspec' require 'timecop' -require 'slack-ruby-client' +require 'slack_ruby_client' Dir[File.join(File.dirname(__FILE__), 'support', '**/*.rb')].each do |file| require file diff --git a/spec/support/real_time/connected_client.rb b/spec/support/real_time/connected_client.rb index 70b977cc..84ac8ac9 100644 --- a/spec/support/real_time/connected_client.rb +++ b/spec/support/real_time/connected_client.rb @@ -3,11 +3,14 @@ let(:client) { Slack::RealTime::Client.new(opts || {}) } let(:ws) { double(Slack::RealTime::Concurrency::Mock::WebSocket) } let(:url) do - Slack::RealTime.config.store_class == Slack::RealTime::Stores::Store ? - 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' : + if Slack::RealTime.config.store_class == Slack::RealTime::Stores::Store + 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' + else 'wss://mpmulti-w5tz.slack-msgs.com/websocket/uid' + end end let(:socket) { double(Slack::RealTime::Socket, connected?: true) } + before do Slack::RealTime.configure do |config| config.concurrency = Slack::RealTime::Concurrency::Mock