From 3e73a21cbeb5df9b229239a0de93f3a005020446 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Tue, 6 Oct 2020 14:08:43 -0700 Subject: [PATCH 01/44] Added verification.rb --- lib/verification.rb | 24 ++++++++++++++++++++++++ test/test_helper.rb | 3 +++ 2 files changed, 27 insertions(+) create mode 100644 lib/verification.rb diff --git a/lib/verification.rb b/lib/verification.rb new file mode 100644 index 00000000..00da7d3b --- /dev/null +++ b/lib/verification.rb @@ -0,0 +1,24 @@ +require 'dotenv' +require 'httparty' +require 'awesome_print' + +Dotenv.load # allows access to ENV['VARIABLE_NAME'] + +CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" + +def verification + query = { + token: ENV["SLACK_TOKEN"] + } + + request = HTTParty.get(CONVERSATIONS_LIST_URL, query: query) + + if request.code != 200 || request["ok"] != true + return "API request failed with error code #{request.code} and #{request["error"]}." + else + channel_names = request["channels"].map { |channel| channel["name"] } + return channel_names + end +end + +ap verification diff --git a/test/test_helper.rb b/test/test_helper.rb index 1fcf2bab..b52881be 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,5 +25,8 @@ } # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["LOCATIONIQ_TOKEN"] + end end From 3c7929534f90e86722781b808b8f9051777fc793 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Tue, 6 Oct 2020 14:25:16 -0700 Subject: [PATCH 02/44] added workspace class, reconfigured test-helper, added workspace class to slack.rb --- .gitignore | 3 +++ lib/slack.rb | 2 +- lib/workspace.rb | 9 +++++++++ test/test_helper.rb | 15 ++++++++------- 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 lib/workspace.rb diff --git a/.gitignore b/.gitignore index 3ff4fada..a8783b85 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ # Ignore environemnt variables .env +.floo +.flooignore +.idea/ diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..4a488c9c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,5 +1,5 @@ #!/usr/bin/env ruby - +require_relative 'workspace' def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..d7c48cca --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,9 @@ + +class Workspace + attr_reader :users, :channels + def initialize + @users = [] + @channels = [] + end + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b52881be..393e3cf4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,19 +2,20 @@ SimpleCov.start do add_filter 'test/' end - +require 'dotenv' require 'minitest' require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" - config.hook_into :webmock -end +# VCR.configure do |config| +# config.cassette_library_dir = "test/cassettes" +# config.hook_into :webmock +# end VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located @@ -25,8 +26,8 @@ } # Don't leave our token lying around in a cassette file. - config.filter_sensitive_data("") do - ENV["LOCATIONIQ_TOKEN"] + config.filter_sensitive_data("") do + ENV[""] end end From aeed83c094734968b869e1458ded3c961b153abd Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Tue, 6 Oct 2020 15:01:06 -0700 Subject: [PATCH 03/44] added workspace_test.rb, added requires to all files. Unable to run tests --- lib/channels.rb | 19 +++++++++++++++++++ lib/slack.rb | 4 +++- lib/users.rb | 0 lib/workspace.rb | 28 ++++++++++++++++++++++++++-- test/test_helper.rb | 3 +++ test/workspace_test.rb | 17 +++++++++++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 lib/channels.rb create mode 100644 lib/users.rb create mode 100644 test/workspace_test.rb diff --git a/lib/channels.rb b/lib/channels.rb new file mode 100644 index 00000000..05355ded --- /dev/null +++ b/lib/channels.rb @@ -0,0 +1,19 @@ +class Channel + + + + def channel_name_list + query = { + token: ENV["SLACK_TOKEN"] + } + + request = HTTParty.get(CONVERSATIONS_LIST_URL, query: query) + + if request.code != 200 || request["ok"] != true + return "API request failed with error code #{request.code} and #{request["error"]}." + else + channel_names = request["channels"].map { |channel| channel["name"] } + return channel_names + end + end +end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 4a488c9c..9f8506e8 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -4,7 +4,9 @@ def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - # TODO project + puts "1. list users/n 2. list channels/n 3. quit" + + puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/users.rb b/lib/users.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/workspace.rb b/lib/workspace.rb index d7c48cca..71abb905 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,4 +1,5 @@ - +require_relative '../test/test_helper' +CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" class Workspace attr_reader :users, :channels def initialize @@ -6,4 +7,27 @@ def initialize @channels = [] end -end \ No newline at end of file + def load_channels + query = { + token: ENV["SLACK_TOKEN"] + } + + request = error_message(HTTParty.get(CONVERSATIONS_LIST_URL, query: query)) + + if request.class == String + raise ArgumentError, request + end + @channels = request["channels"].map { |channel| channel["name"] } + end + + def error_message(response) + + if response.code != 200 || response["ok"] != true + return "API request failed with error code #{response.code} and #{response["error"]}." + else + return response + end + end +end + + diff --git a/test/test_helper.rb b/test/test_helper.rb index 393e3cf4..b28e8a88 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,9 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require_relative '../lib/workspace' +require_relative '../lib/channels' +require_relative '../lib/users' Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..c1f3ef9d --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,17 @@ +require_relative 'test_helper' + + +describe "workspace class" do + describe "workspace instantiation" do + it "is an instance of workspace" do + new_workspace = Workspace.new + expect(new_workspace).must_be_instance_of Workspace + end + end + describe "load channels" do + it "loads channels" do + + end + + end +end \ No newline at end of file From 7989599cca4d5c73dde5d11d64181cc5aa36b239 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Tue, 6 Oct 2020 15:28:44 -0700 Subject: [PATCH 04/44] Added positive nominal #load_channels test --- lib/workspace.rb | 12 +++++++----- test/workspace_test.rb | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 71abb905..9b1ab196 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,5 +1,7 @@ require_relative '../test/test_helper' + CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" + class Workspace attr_reader :users, :channels def initialize @@ -8,26 +10,26 @@ def initialize end def load_channels + query = { - token: ENV["SLACK_TOKEN"] + token: ENV['SLACK_TOKEN'] } request = error_message(HTTParty.get(CONVERSATIONS_LIST_URL, query: query)) - if request.class == String - raise ArgumentError, request - end + raise ArgumentError, request if request.class == String + @channels = request["channels"].map { |channel| channel["name"] } end def error_message(response) - if response.code != 200 || response["ok"] != true return "API request failed with error code #{response.code} and #{response["error"]}." else return response end end + end diff --git a/test/workspace_test.rb b/test/workspace_test.rb index c1f3ef9d..72cebd40 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -2,15 +2,35 @@ describe "workspace class" do + describe "workspace instantiation" do + before do + @new_workspace = Workspace.new + end + it "is an instance of workspace" do - new_workspace = Workspace.new - expect(new_workspace).must_be_instance_of Workspace + expect(@new_workspace).must_be_instance_of Workspace + end + + it "establishes the base data structures when instantiated" do + [:users, :channels].each do |keyword| + expect(@new_workspace).must_respond_to keyword + end + + expect(@new_workspace.users).must_be_kind_of Array + expect(@new_workspace.channels).must_be_kind_of Array end + end + describe "load channels" do - it "loads channels" do + before do + @new_workspace = Workspace.new + end + it "loads channels" do + @new_workspace.load_channels + expect(@new_workspace.channels).wont_be_empty end end From 187109944550b7d1bbbb45aa49b84f4cc21d3ae6 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Tue, 6 Oct 2020 15:29:25 -0700 Subject: [PATCH 05/44] Added `require httparty` and commented out VCR config... for now. --- test/test_helper.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index b28e8a88..c6876083 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,7 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require 'httparty' require_relative '../lib/workspace' require_relative '../lib/channels' require_relative '../lib/users' @@ -20,17 +21,17 @@ # config.hook_into :webmock # end -VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" # folder where casettes will be located - config.hook_into :webmock # tie into this other tool called webmock - config.default_cassette_options = { - :record => :new_episodes, # record new data when we don't have it yet - :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match - } - - # Don't leave our token lying around in a cassette file. - config.filter_sensitive_data("") do - ENV[""] - end - -end +# VCR.configure do |config| +# config.cassette_library_dir = "test/cassettes" # folder where casettes will be located +# config.hook_into :webmock # tie into this other tool called webmock +# config.default_cassette_options = { +# :record => :new_episodes, # record new data when we don't have it yet +# :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match +# } +# +# # Don't leave our token lying around in a cassette file. +# config.filter_sensitive_data("") do +# ENV[""] +# end +# +# end From 48afa3fa7d4fb5f6cacd34781ba1c2b4f6e1dfa4 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Tue, 6 Oct 2020 15:57:08 -0700 Subject: [PATCH 06/44] Added additional tests for #load_channels --- lib/workspace.rb | 10 ++++++++-- test/workspace_test.rb | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 9b1ab196..b630ab22 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -10,7 +10,6 @@ def initialize end def load_channels - query = { token: ENV['SLACK_TOKEN'] } @@ -19,9 +18,16 @@ def load_channels raise ArgumentError, request if request.class == String - @channels = request["channels"].map { |channel| channel["name"] } + #README specifies "name, topic, member count, and Slack ID"; topic interpreted as 'purpose' + @channels = request["channels"].map do |channel| + { name: channel["name"], + topic: channel["purpose"]["value"], + member_count: channel["num_members"], + id: channel["id"] } + end end + def error_message(response) if response.code != 200 || response["ok"] != true return "API request failed with error code #{response.code} and #{response["error"]}." diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 72cebd40..b6441ba5 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -1,5 +1,6 @@ require_relative 'test_helper' +# Is it most appropriate to create a whole set of false test data? Even for APIs??? describe "workspace class" do @@ -26,12 +27,25 @@ describe "load channels" do before do @new_workspace = Workspace.new + @new_workspace.load_channels + @channel_list = @new_workspace.channels end it "loads channels" do - @new_workspace.load_channels - expect(@new_workspace.channels).wont_be_empty + expect(@channel_list).wont_be_empty + end + + it "@channels is an array of hashes" do + expect(@channel_list).must_be_kind_of Array + expect(@channel_list.all? { |channel| channel.class == Hash }).must_equal true + end + + it "correctly loads list of channels" do + current_channels = ["general", "random", "slackcli"] + expect(@channel_list.each { |channel| current_channels.include?(channel[:name]) }) + expect(@channel_list.length).must_equal 3 end end + end \ No newline at end of file From 9d4a62c076cfbd0639050153a7ba5b323d1a5b4b Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Tue, 6 Oct 2020 16:22:02 -0700 Subject: [PATCH 07/44] added load_user method and tests --- lib/workspace.rb | 21 ++++++++++++++++++++- test/test_helper.rb | 1 + test/workspace_test.rb | 23 ++++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index b630ab22..2784a9f8 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,7 +1,7 @@ require_relative '../test/test_helper' CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" - +USERS_LIST_URL = "https://slack.com/api/users.list" class Workspace attr_reader :users, :channels def initialize @@ -27,6 +27,25 @@ def load_channels end end + def load_users + query = { + token: ENV['SLACK_TOKEN'] + } + + request = error_message(HTTParty.get(USERS_LIST_URL, query: query)) + + + raise ArgumentError, request if request.class == String + + + @users = request["members"].map do |user| + ap user + { name: user["name"], + real_name: user["real_name"], + id: user["id"] } + end + end + def error_message(response) if response.code != 200 || response["ok"] != true diff --git a/test/test_helper.rb b/test/test_helper.rb index c6876083..d35e0986 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,6 +9,7 @@ require 'minitest/skip_dsl' require 'vcr' require 'httparty' +require "awesome_print" require_relative '../lib/workspace' require_relative '../lib/channels' require_relative '../lib/users' diff --git a/test/workspace_test.rb b/test/workspace_test.rb index b6441ba5..7e130627 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -31,7 +31,7 @@ @channel_list = @new_workspace.channels end - it "loads channels" do + it "populates the array" do expect(@channel_list).wont_be_empty end @@ -47,5 +47,26 @@ end end + describe "load users" do + before do + @new_workspace = Workspace.new + @new_workspace.load_users + @user_list = @new_workspace.users + end + it "populates the array" do + expect(@user_list).wont_be_empty + end + + it "@users is an array of hashes" do + expect(@user_list).must_be_kind_of Array + expect(@user_list.all? { |user| user.class == Hash }).must_equal true + end + it "correctly loads list of channels" do + current_users = ["slackbot", "gomezrc1220", "water_christabel_slac", "christabel.escarez", "waterrachaelapi_proje", ] + expect(@user_list.each { |user| current_users.include?(user[:name]) }) + expect(@user_list.length).must_equal 5 + end + + end end \ No newline at end of file From cb7416408a0b6a2f6fc054e08ebf21dc5790eb8f Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Tue, 6 Oct 2020 16:23:12 -0700 Subject: [PATCH 08/44] Fixed indentation --- lib/workspace.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 2784a9f8..574acf51 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -2,6 +2,7 @@ CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" USERS_LIST_URL = "https://slack.com/api/users.list" + class Workspace attr_reader :users, :channels def initialize @@ -44,7 +45,7 @@ def load_users real_name: user["real_name"], id: user["id"] } end - end + end def error_message(response) From eef19de970b28080828f5ae5b6b820589ac69a33 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Tue, 6 Oct 2020 16:42:46 -0700 Subject: [PATCH 09/44] added driver code for CLI program in lib/slack.rb and input validation --- lib/slack.rb | 40 +++++++++++++++++++++++++++++++++++++++- lib/workspace.rb | 3 --- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 9f8506e8..21a21684 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -4,11 +4,49 @@ def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - puts "1. list users/n 2. list channels/n 3. quit" + puts "1. list users\n2. list channels\n3. quit" + selection = input_validation(gets.chomp, 3) + while selection == 1 || selection == 2 + if selection == 1 + workspace.load_users + ap workspace.users + else + workspace.load_channels + ap workspace.channels + end + + puts "Make another selection:" + puts "1. list users\n2. list channels\n3. quit" + selection = input_validation(gets.chomp, 3) + end puts "Thank you for using the Ada Slack CLI" end +def input_validation(input, max_num) + input = translate_input(input) + + while !(1..max_num).include? input + puts "Invalid input. Please re-enter a selection." + input = translate_input(gets.chomp) + end + + return input +end + +def translate_input(string_input) + case string_input.downcase + when "list users", "1" + return 1 + when "list channels", "2" + return 2 + when "quit", "3" + return 3 + else + return string_input + end +end + main if __FILE__ == $PROGRAM_NAME \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index 574acf51..e66ec46a 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -35,12 +35,9 @@ def load_users request = error_message(HTTParty.get(USERS_LIST_URL, query: query)) - raise ArgumentError, request if request.class == String - @users = request["members"].map do |user| - ap user { name: user["name"], real_name: user["real_name"], id: user["id"] } From 37a7b14faa92c177c5f930682f8ea02db90a81e1 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 11:47:00 -0700 Subject: [PATCH 10/44] created user and channel classes --- lib/channels.rb | 35 ++++++++++++++++++++--------------- lib/recipient.rb | 31 +++++++++++++++++++++++++++++++ lib/users.rb | 26 ++++++++++++++++++++++++++ lib/workspace.rb | 17 +++++------------ 4 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 lib/recipient.rb diff --git a/lib/channels.rb b/lib/channels.rb index 05355ded..c747c3a5 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -1,19 +1,24 @@ -class Channel - - - - def channel_name_list - query = { - token: ENV["SLACK_TOKEN"] - } +require 'test/test_helper' +CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" +class Channel < Recipient + attr_reader :topic, :member_count, + def initialize(slack_id, name, topic, member_count) + super(slack_id, name) + @topic = topic + @member_count = member_count + end - request = HTTParty.get(CONVERSATIONS_LIST_URL, query: query) + def details + return "Channel id: #{@slack_id}\nChannel name: #{@name}\nTopic: #{@topic}\nNumber of members: #{@member_count}" + end - if request.code != 200 || request["ok"] != true - return "API request failed with error code #{request.code} and #{request["error"]}." - else - channel_names = request["channels"].map { |channel| channel["name"] } - return channel_names + def self.list_all + query = {token: ENV['SLACK_TOKEN']} + + request = self.get(CONVERSATIONS_LIST_URL, query ) + + return request["channels"].map do |channel| + self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"]) end end -end \ No newline at end of file +end diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..69bd74ad --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,31 @@ +class Recipient + attr_reader :slack_id, :name + + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end + # def send_meassage(message) + # + # end + def self.get(url, params) + return error_message(HTTParty.get(url,params)) + end + + def details + raise NotImplementedError, 'Implement me in a child class!' + end + def self.list_all + raise NotImplementedError, 'Implement me in a child class!' + end + + private + + def error_message(response) + if response.code != 200 || response["ok"] != true + raise ArgumentError "API request failed with error code #{response.code} and #{response["error"]}." + else + return response + end + end +end \ No newline at end of file diff --git a/lib/users.rb b/lib/users.rb index e69de29b..a54681bd 100644 --- a/lib/users.rb +++ b/lib/users.rb @@ -0,0 +1,26 @@ +require_relative "test/test_helper" +USERS_LIST_URL = "https://slack.com/api/users.list" +class User < Recipient + attr_reader :real_name, :status_text, :status_emoji + def initialize(slack_id, name, real_name, status_text, status_emoji) + super(slack_id, name) + @real_name = real_name + @status_text = status_text + @status_emoji = status_emoji + end + + def details + return "User id: #{@slack_id}\nUsername: #{@name}\nReal name: #{@real_name}\nStatus text: #{@status_text}\nStatus emoji: #{@status_emoji}" + end + + def self.list_all + query = {token: ENV['SLACK_TOKEN']} + + request = self.get(USERS_LIST_URL, query ) + + @users = request["members"].map do |user| + self.new(user["id"],user["name"],user["real_name"],user["profile"]["status_text"],user["profile"]["status_emoji"]) + + end + end +end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index e66ec46a..edab04c7 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -11,21 +11,14 @@ def initialize end def load_channels - query = { - token: ENV['SLACK_TOKEN'] - } - request = error_message(HTTParty.get(CONVERSATIONS_LIST_URL, query: query)) - raise ArgumentError, request if request.class == String - #README specifies "name, topic, member count, and Slack ID"; topic interpreted as 'purpose' - @channels = request["channels"].map do |channel| - { name: channel["name"], - topic: channel["purpose"]["value"], - member_count: channel["num_members"], - id: channel["id"] } - end + + + + + @channels = Channel.list_all end def load_users From 76923affb72c3f4f62bf15ab3b974ffc57a6bafd Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:45:34 -0700 Subject: [PATCH 11/44] Refactored `require` headers and moved "load channels" test to channel_test.rb as "self.list_all" --- lib/workspace.rb | 26 ++++---------------------- test/workspace_test.rb | 23 +---------------------- 2 files changed, 5 insertions(+), 44 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index edab04c7..acb1d56b 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,43 +1,25 @@ require_relative '../test/test_helper' +require_relative 'user' +require_relative 'channel' -CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" USERS_LIST_URL = "https://slack.com/api/users.list" class Workspace attr_reader :users, :channels + def initialize @users = [] @channels = [] end def load_channels - - - - - - - @channels = Channel.list_all end def load_users - query = { - token: ENV['SLACK_TOKEN'] - } - - request = error_message(HTTParty.get(USERS_LIST_URL, query: query)) - - raise ArgumentError, request if request.class == String - - @users = request["members"].map do |user| - { name: user["name"], - real_name: user["real_name"], - id: user["id"] } - end + @users = User.list_all end - def error_message(response) if response.code != 200 || response["ok"] != true return "API request failed with error code #{response.code} and #{response["error"]}." diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 7e130627..305199a0 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -1,4 +1,5 @@ require_relative 'test_helper' +require_relative '../lib/workspace' # Is it most appropriate to create a whole set of false test data? Even for APIs??? @@ -24,29 +25,7 @@ end - describe "load channels" do - before do - @new_workspace = Workspace.new - @new_workspace.load_channels - @channel_list = @new_workspace.channels - end - - it "populates the array" do - expect(@channel_list).wont_be_empty - end - it "@channels is an array of hashes" do - expect(@channel_list).must_be_kind_of Array - expect(@channel_list.all? { |channel| channel.class == Hash }).must_equal true - end - - it "correctly loads list of channels" do - current_channels = ["general", "random", "slackcli"] - expect(@channel_list.each { |channel| current_channels.include?(channel[:name]) }) - expect(@channel_list.length).must_equal 3 - end - - end describe "load users" do before do @new_workspace = Workspace.new From 8d853c0666c8da0372d50c85c2428026a6072b2e Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:45:58 -0700 Subject: [PATCH 12/44] Refactored `require` headers --- lib/{users.rb => user.rb} | 5 ++++- test/user_test.rb | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) rename lib/{users.rb => user.rb} (91%) create mode 100644 test/user_test.rb diff --git a/lib/users.rb b/lib/user.rb similarity index 91% rename from lib/users.rb rename to lib/user.rb index a54681bd..09447b10 100644 --- a/lib/users.rb +++ b/lib/user.rb @@ -1,5 +1,8 @@ -require_relative "test/test_helper" +require_relative '../test/test_helper' +require_relative 'recipient' + USERS_LIST_URL = "https://slack.com/api/users.list" + class User < Recipient attr_reader :real_name, :status_text, :status_emoji def initialize(slack_id, name, real_name, status_text, status_emoji) diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..a7ea2d1e --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,2 @@ +require_relative 'test_helper' +require_relative '../lib/user' From 0ba025ba2e91b0e878aa00ab7cb1f45febc65808 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:46:03 -0700 Subject: [PATCH 13/44] Refactored `require` headers --- test/test_helper.rb | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index d35e0986..e1d0b69c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,29 +10,29 @@ require 'vcr' require 'httparty' require "awesome_print" -require_relative '../lib/workspace' -require_relative '../lib/channels' -require_relative '../lib/users' +# require_relative '../lib/workspace' +# require_relative '../lib/channel' +# require_relative '../lib/user' Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# VCR.configure do |config| -# config.cassette_library_dir = "test/cassettes" -# config.hook_into :webmock -# end +VCR.configure do |config| + config.cassette_library_dir = "test/cassettes" + config.hook_into :webmock +end + +VCR.configure do |config| + config.cassette_library_dir = "test/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } -# VCR.configure do |config| -# config.cassette_library_dir = "test/cassettes" # folder where casettes will be located -# config.hook_into :webmock # tie into this other tool called webmock -# config.default_cassette_options = { -# :record => :new_episodes, # record new data when we don't have it yet -# :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match -# } -# -# # Don't leave our token lying around in a cassette file. -# config.filter_sensitive_data("") do -# ENV[""] -# end -# -# end + # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_TOKEN"] + end + +end From 8611f02ddf7293c5b88ac5d0999d31d37a569d45 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:46:50 -0700 Subject: [PATCH 14/44] Changed #error_message to .error_message --- lib/recipient.rb | 2 +- test/recipient_test.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 test/recipient_test.rb diff --git a/lib/recipient.rb b/lib/recipient.rb index 69bd74ad..34e7b129 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -21,7 +21,7 @@ def self.list_all private - def error_message(response) + def self.error_message(response) if response.code != 200 || response["ok"] != true raise ArgumentError "API request failed with error code #{response.code} and #{response["error"]}." else diff --git a/test/recipient_test.rb b/test/recipient_test.rb new file mode 100644 index 00000000..1926b9ed --- /dev/null +++ b/test/recipient_test.rb @@ -0,0 +1,2 @@ +require_relative 'test_helper' +require_relative '../lib/recipient' From 1a73d914b87babfb8fd7ccadff44cfa9f325273b Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:47:11 -0700 Subject: [PATCH 15/44] Refactored `require` headers --- lib/{channels.rb => channel.rb} | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) rename lib/{channels.rb => channel.rb} (80%) diff --git a/lib/channels.rb b/lib/channel.rb similarity index 80% rename from lib/channels.rb rename to lib/channel.rb index c747c3a5..e4392a07 100644 --- a/lib/channels.rb +++ b/lib/channel.rb @@ -1,7 +1,11 @@ -require 'test/test_helper' +require_relative '../test/test_helper' +require_relative 'recipient' + CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" + class Channel < Recipient - attr_reader :topic, :member_count, + attr_reader :topic, :member_count + def initialize(slack_id, name, topic, member_count) super(slack_id, name) @topic = topic @@ -18,7 +22,7 @@ def self.list_all request = self.get(CONVERSATIONS_LIST_URL, query ) return request["channels"].map do |channel| - self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"]) + self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"].to_i) end end end From 73e2c7d54f6fbedc00f50e8f81e60f5ded3ef2d0 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:47:59 -0700 Subject: [PATCH 16/44] Wrote tests for constructor and test for .list_all --- test/channel_test.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 test/channel_test.rb diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..cc64e5d2 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,82 @@ +require_relative 'test_helper' +require_relative '../lib/channel' + +describe "Channel class" do + + describe "instantiation" do + + before do + @new_channel = Channel.new("some_id", "some_name", "some topic", 300) + end + + it "is an instance of Channel" do + expect(@new_channel).must_be_instance_of Channel + end + + it "establishes the base data structures when instantiated" do + [:slack_id, :name, :topic, :member_count].each do |keyword| + expect(@new_channel).must_respond_to keyword + end + + expect(@new_channel.slack_id).must_be_kind_of String + expect(@new_channel.name).must_be_kind_of String + expect(@new_channel.topic).must_be_kind_of String + expect(@new_channel.member_count).must_be_kind_of Integer + end + + end + + describe "self.get" do + + before do + @query = {token: ENV["SLACK_TOKEN"]} + end + + it "calls Slack API conversations.list" do + VCR.use_cassette("get conversations.list") do + conversations = Channel.get(CONVERSATIONS_LIST_URL, query: @query) + + channel_names = conversations["channels"].map { |channel| channel["name"] } + + expect(conversations.body).wont_be_nil + expect(conversations).must_be_instance_of HTTParty::Response + + ["random", "slackcli", "general"].each do |keyword| + expect(channel_names.include?(keyword)).must_equal true + end + end + end + + it "will raise an exception if the search fails" do + + end + end + + describe "details" do + + end + + describe "self.list_all" do + before do + @new_channel = Channel.new("some_id", "some_name", "some topic", 300) + @new_channel.load_channels + @channel_list = @new_workspace.channels + end + + it "populates the array" do + expect(@channel_list).wont_be_empty + end + + it "@channels is an array of hashes" do + expect(@channel_list).must_be_kind_of Array + expect(@channel_list.all? { |channel| channel.class == Hash }).must_equal true + end + + it "correctly loads list of channels" do + current_channels = ["general", "random", "slackcli"] + expect(@channel_list.each { |channel| current_channels.include?(channel[:name]) }) + expect(@channel_list.length).must_equal 3 + end + end + +end \ No newline at end of file From 6a1f624a2651c064e509ca2d6384ab5233671919 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 15:58:57 -0700 Subject: [PATCH 17/44] Wrote exception test for .list_all --- lib/recipient.rb | 2 +- test/channel_test.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 34e7b129..335d62da 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -23,7 +23,7 @@ def self.list_all def self.error_message(response) if response.code != 200 || response["ok"] != true - raise ArgumentError "API request failed with error code #{response.code} and #{response["error"]}." + raise ArgumentError, "API request failed with error code #{response.code} and #{response["error"]}." else return response end diff --git a/test/channel_test.rb b/test/channel_test.rb index cc64e5d2..8819e25a 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -33,7 +33,7 @@ end it "calls Slack API conversations.list" do - VCR.use_cassette("get conversations.list") do + VCR.use_cassette("get conversations list") do conversations = Channel.get(CONVERSATIONS_LIST_URL, query: @query) channel_names = conversations["channels"].map { |channel| channel["name"] } @@ -48,7 +48,11 @@ end it "will raise an exception if the search fails" do - + VCR.use_cassette("get conversations list") do + expect { + Channel.get(CONVERSATIONS_LIST_URL, query: {token: "unauthed test token"}) + }.must_raise ArgumentError + end end end From fcc0b2fa886bf4b4b4b26a0777903190d5c3912f Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 16:24:19 -0700 Subject: [PATCH 18/44] Modified self.get call in .list_all and added `:query` keyword --- lib/channel.rb | 4 ++-- lib/recipient.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index e4392a07..e7e3e803 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,9 +17,9 @@ def details end def self.list_all - query = {token: ENV['SLACK_TOKEN']} + query = {token: ENV["SLACK_TOKEN"]} - request = self.get(CONVERSATIONS_LIST_URL, query ) + request = self.get(CONVERSATIONS_LIST_URL, query: query) return request["channels"].map do |channel| self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"].to_i) diff --git a/lib/recipient.rb b/lib/recipient.rb index 335d62da..0acbc7bf 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -9,6 +9,7 @@ def initialize(slack_id, name) # # end def self.get(url, params) + sleep(1) return error_message(HTTParty.get(url,params)) end From 89aacbfa47fabb24112dec6809de5ef7e22ced42 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 16:25:59 -0700 Subject: [PATCH 19/44] Added (and commented out) VCR config line to allow HTTP requests outside of VCR control blocks --- test/test_helper.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index e1d0b69c..da6c1e6d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,11 +17,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" - config.hook_into :webmock -end - VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located config.hook_into :webmock # tie into this other tool called webmock @@ -30,6 +25,8 @@ :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match } + # config.allow_http_connections_when_no_cassette = true #added by Rachael and Christabel to allow to HTTP Requests without cassettes + # Don't leave our token lying around in a cassette file. config.filter_sensitive_data("") do ENV["SLACK_TOKEN"] From 4286d7370222113d9ebb73291bff4408aefd8e0b Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 16:26:17 -0700 Subject: [PATCH 20/44] Modified self.get call in .list_all and added `:query` keyword --- lib/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/user.rb b/lib/user.rb index 09447b10..2ac0ed32 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -19,7 +19,7 @@ def details def self.list_all query = {token: ENV['SLACK_TOKEN']} - request = self.get(USERS_LIST_URL, query ) + request = self.get(USERS_LIST_URL, query: query) @users = request["members"].map do |user| self.new(user["id"],user["name"],user["real_name"],user["profile"]["status_text"],user["profile"]["status_emoji"]) From 909558b8b32a13f5d68d9e51702e2d065cbf4c9e Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 16:26:55 -0700 Subject: [PATCH 21/44] Added sleep(1) to .get to prevent token lockout in cases of infinite loops during testing. --- lib/recipient.rb | 1 + test/channel_test.rb | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 0acbc7bf..02165ef7 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -29,4 +29,5 @@ def self.error_message(response) return response end end + end \ No newline at end of file diff --git a/test/channel_test.rb b/test/channel_test.rb index 8819e25a..4803e2ac 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -54,33 +54,38 @@ }.must_raise ArgumentError end end + end describe "details" do + before do + @new_channel = Channel.new("some_id", "some_name", "some topic", 300) + @details = @new_channel.details + end + + it "returns String" do + expect(@details).must_be_kind_of String + end end describe "self.list_all" do + before do - @new_channel = Channel.new("some_id", "some_name", "some topic", 300) - @new_channel.load_channels - @channel_list = @new_workspace.channels + VCR.use_cassette("get conversations list") do + @channel_list = Channel.list_all + end end it "populates the array" do expect(@channel_list).wont_be_empty end - it "@channels is an array of hashes" do + it "@channels is an array of Channel objects" do expect(@channel_list).must_be_kind_of Array - expect(@channel_list.all? { |channel| channel.class == Hash }).must_equal true + expect(@channel_list.all? { |channel| channel.class == Channel }).must_equal true end - it "correctly loads list of channels" do - current_channels = ["general", "random", "slackcli"] - expect(@channel_list.each { |channel| current_channels.include?(channel[:name]) }) - expect(@channel_list.length).must_equal 3 - end end end \ No newline at end of file From 413bc43eff5a2170ae635c623fa8caa7c2120487 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 16:54:44 -0700 Subject: [PATCH 22/44] added user tests and added extra details test to channel_test.rb --- test/channel_test.rb | 3 ++ test/user_test.rb | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/test/channel_test.rb b/test/channel_test.rb index 4803e2ac..ac4fa6d4 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -66,6 +66,9 @@ it "returns String" do expect(@details).must_be_kind_of String end + it "returns the correct string" do + expect(@details).must_equal "Channel id: some_id\nChannel name: some_name\nTopic: some topic\nNumber of members: 300" + end end diff --git a/test/user_test.rb b/test/user_test.rb index a7ea2d1e..a6dafa25 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -1,2 +1,96 @@ require_relative 'test_helper' require_relative '../lib/user' + +describe "User class" do + + describe "instantiation" do + + before do + @new_user = User.new("some_id", "some_name", "some_name", "blah", ":)") + end + + it "is an instance of User" do + expect(@new_user).must_be_instance_of User + end + + it "establishes the base data structures when instantiated" do + [:slack_id, :name, :real_name, :status_text, :status_emoji].each do |keyword| + expect(@new_user).must_respond_to keyword + end + + expect(@new_user.slack_id).must_be_kind_of String + expect(@new_user.name).must_be_kind_of String + expect(@new_user.real_name).must_be_kind_of String + expect(@new_user.status_text).must_be_kind_of String + expect(@new_user.status_emoji).must_be_kind_of String + end + + end + + describe "self.get" do + + before do + @query = {token: ENV["SLACK_TOKEN"]} + end + + it "calls Slack API users.list" do + VCR.use_cassette("get users list") do + users = User.get(USERS_LIST_URL, query: @query) + + user_names = users["members"].map { |user| user["name"] } + + expect(users.body).wont_be_nil + expect(users).must_be_instance_of HTTParty::Response + + ["slackbot", "gomezrc1220", "christabot", "christabel.escarez", "waterrachaelapi_proje"].each do |keyword| + expect(user_names.include?(keyword)).must_equal true + end + end + end + + it "will raise an exception if the search fails" do + VCR.use_cassette("get users list") do + expect { + User.get(USERS_LIST_URL, query: {token: "unauthed test token"}) + }.must_raise ArgumentError + end + end + + end + + describe "details" do + before do + @new_user = User.new("some_id", "some_name", "some_name", "blah", ":)") + @details = @new_user.details + end + + it "returns String" do + expect(@details).must_be_kind_of String + end + + it "returns the correct string" do + expect(@details).must_equal "User id: some_id\nUsername: some_name\nReal name: some_name\nStatus text: blah\nStatus emoji: :)" + end + + end + + describe "self.list_all" do + + before do + VCR.use_cassette("get users list") do + @user_list = User.list_all + end + end + + it "populates the array" do + expect(@user_list).wont_be_empty + end + + it "@channels is an array of User objects" do + expect(@user_list).must_be_kind_of Array + expect(@user_list.all? { |user| user.class == User }).must_equal true + end + + end + +end From e0292e57dbf9eb731bde4771262ebdfedf7c992f Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 17:36:23 -0700 Subject: [PATCH 23/44] added select_channel, select_user, and show_details and wrote tests for select_channel and select_user. --- lib/workspace.rb | 17 +++++++--- test/workspace_test.rb | 77 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 19 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index acb1d56b..cf356837 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -2,7 +2,6 @@ require_relative 'user' require_relative 'channel' -USERS_LIST_URL = "https://slack.com/api/users.list" class Workspace attr_reader :users, :channels @@ -20,11 +19,19 @@ def load_users @users = User.list_all end - def error_message(response) - if response.code != 200 || response["ok"] != true - return "API request failed with error code #{response.code} and #{response["error"]}." + def select_channel(search_term) + return @channels.find{|channel| channel.name == search_term.downcase || channel.slack_id == search_term.upcase} + end + + def select_user(search_term) + return @users.find{|user| user.name == search_term.downcase || user.slack_id == search_term.upcase} + end + + def show_details(recipient) + if recipient + return recipient.details else - return response + return "No recipient available to display details" end end diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 305199a0..a5837fae 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -24,28 +24,77 @@ end end - + describe "load channels" do + before do + VCR.use_cassette("get conversations list") do + @new_workspace = Workspace.new + @new_workspace.load_channels + end + end + it "populates the @channels instance variable" do + expect(@new_workspace.channels).wont_be_empty + end + end describe "load users" do before do - @new_workspace = Workspace.new - @new_workspace.load_users - @user_list = @new_workspace.users + VCR.use_cassette("get users list") do + @new_workspace = Workspace.new + @new_workspace.load_users + end end - it "populates the array" do - expect(@user_list).wont_be_empty + it "populates the @users instance variable" do + expect(@new_workspace.users).wont_be_empty end - - it "@users is an array of hashes" do - expect(@user_list).must_be_kind_of Array - expect(@user_list.all? { |user| user.class == Hash }).must_equal true + end + + describe "select channel" do + before do + VCR.use_cassette("get conversations list") do + @new_workspace = Workspace.new + @new_workspace.load_channels + end + end + it "returns a channel object" do + found_channel = @new_workspace.select_channel("random") + expect(found_channel).must_be_kind_of Channel end + it "returns nil if object not found" do + not_found_channel = @new_workspace.select_channel("bloop") + expect(not_found_channel).must_be_nil + end + end - it "correctly loads list of channels" do - current_users = ["slackbot", "gomezrc1220", "water_christabel_slac", "christabel.escarez", "waterrachaelapi_proje", ] - expect(@user_list.each { |user| current_users.include?(user[:name]) }) - expect(@user_list.length).must_equal 5 + describe "select user" do + before do + VCR.use_cassette("get users list") do + @new_workspace = Workspace.new + @new_workspace.load_users + end end + it "returns a user object" do + found_user = @new_workspace.select_user("slackbot") + expect(found_user).must_be_kind_of User + end + it "returns nil if object not found" do + not_found_user = @new_workspace.select_user("bloop") + expect(not_found_user).must_be_nil + end + end + describe "show details" do + before do + @new_workspace = Workspace.new + VCR.use_cassette("get users list") do + @new_workspace.load_users + end + VCR.use_cassette("get conversations list") do + @new_workspace.load_channels + end + end + it "returns String" do + found_user = @new_workspace.select_user("slackbot") + expect(show_details(found_user)).must_be_kind_of String + end end end \ No newline at end of file From 44b98c913eaddf3280bc53a1bc0e7ec00813750a Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 17:37:04 -0700 Subject: [PATCH 24/44] added cassette files --- test/cassettes/get_conversations_list.yml | 72 ++++++++++++ test/cassettes/get_users_list.yml | 129 ++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 test/cassettes/get_conversations_list.yml create mode 100644 test/cassettes/get_users_list.yml diff --git a/test/cassettes/get_conversations_list.yml b/test/cassettes/get_conversations_list.yml new file mode 100644 index 00000000..35eb0ca3 --- /dev/null +++ b/test/cassettes/get_conversations_list.yml @@ -0,0 +1,72 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/conversations.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 00:26:15 GMT + Server: + - Apache + X-Slack-Req-Id: + - ddc4d7fbcbf0cc01d5e506cb586067cd + X-Oauth-Scopes: + - chat:write,channels:read,users:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,groups:read,mpim:read,im:read,read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '647' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-zxri,haproxy-edge-pdx-74gg + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"C01BKRLQ4UF","name":"random","is_channel":true,"is_group":false,"is_im":false,"created":1601943301,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"random","is_shared":false,"parent_conversation":null,"creator":"U01BXFBQ18D","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01BKRLPT7Z"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + channel is for... well, everything else. It\u2019s a place for team jokes, + spur-of-the-moment ideas, and funny GIFs. Go wild!","creator":"U01BXFBQ18D","last_set":1601943301},"previous_names":[],"num_members":3},{"id":"C01BXFENUR3","name":"slackcli","is_channel":true,"is_group":false,"is_im":false,"created":1601943445,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"slackcli","is_shared":false,"parent_conversation":null,"creator":"U01BXFBQ18D","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01BKRLPT7Z"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + *channel* is for working on a project. Hold meetings, share docs, and make + decisions together with your team.","creator":"U01BXFBQ18D","last_set":1601943445},"previous_names":[],"num_members":2},{"id":"C01CD723J0H","name":"general","is_channel":true,"is_group":false,"is_im":false,"created":1601943301,"is_archived":false,"is_general":true,"unlinked":0,"name_normalized":"general","is_shared":false,"parent_conversation":null,"creator":"U01BXFBQ18D","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01BKRLPT7Z"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + is the one channel that will always include everyone. It\u2019s a great spot + for announcements and team-wide conversations.","creator":"U01BXFBQ18D","last_set":1601943301},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 00:26:15 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/get_users_list.yml b/test/cassettes/get_users_list.yml new file mode 100644 index 00000000..2a196c01 --- /dev/null +++ b/test/cassettes/get_users_list.yml @@ -0,0 +1,129 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 07 Oct 2020 23:43:52 GMT + Server: + - Apache + X-Slack-Req-Id: + - 857645eedae3f8610bd5f65a346c635d + X-Oauth-Scopes: + - chat:write,channels:read,users:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '1319' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-z9gt,haproxy-edge-pdx-tfuw + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01BKRLPT7Z","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/80588\/marketing\/img\/avatars\/slackbot\/avatar-slackbot.png","image_512":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_512.png","status_text_canonical":"","team":"T01BKRLPT7Z"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"U01BXFBQ18D","team_id":"T01BKRLPT7Z","name":"gomezrc1220","deleted":false,"color":"9f69e7","real_name":"Rachael + Gomez","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Rachael + Gomez","real_name_normalized":"Rachael Gomez","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"4b6270619864","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-05\/1393870876631_4b6270619864d83852e3_1024.png","status_text_canonical":"","team":"T01BKRLPT7Z"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1601943304},{"id":"U01C0JB1FB4","team_id":"T01BKRLPT7Z","name":"christabot","deleted":false,"color":"e7392d","real_name":"Christabot","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Christabot","real_name_normalized":"Christabot","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g6bf8b9adec4","api_app_id":"A01C0J7RNGJ","always_active":false,"bot_id":"B01CQDPVC48","first_name":"Christabot","last_name":"","image_24":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/6bf8b9adec4556f2274abbcadb932de0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0022-512.png","status_text_canonical":"","team":"T01BKRLPT7Z"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1602099127},{"id":"U01C6PPQXJQ","team_id":"T01BKRLPT7Z","name":"christabel.escarez","deleted":false,"color":"4bbe2e","real_name":"Christabel + Sebastian","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Christabel + Sebastian","real_name_normalized":"Christabel Sebastian","display_name":"Christabel + Sebastian","display_name_normalized":"Christabel Sebastian","fields":null,"status_text":"vibin + ~~~","status_emoji":":palm_tree:","status_expiration":0,"avatar_hash":"ged110857fb2","image_24":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/ed110857fb2730f2e5016f61e8846f90.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png","status_text_canonical":"","team":"T01BKRLPT7Z"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1602113813},{"id":"U01C6PZLEBW","team_id":"T01BKRLPT7Z","name":"waterrachaelapi_proje","deleted":false,"color":"3c989f","real_name":"Water-Rachael-API + Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Water-Rachael-API + Project","real_name_normalized":"Water-Rachael-API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g500d535fa7d","api_app_id":"A01BTS37RQE","always_active":false,"bot_id":"B01BXFQKZ4M","image_24":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/500d535fa7d1f82f70a6864ed14df800.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0018-512.png","status_text_canonical":"","team":"T01BKRLPT7Z"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1601943944}],"cache_ts":1602114232,"response_metadata":{"next_cursor":""}}' + recorded_at: Wed, 07 Oct 2020 23:43:52 GMT +- request: + method: get + uri: https://slack.com/api/users.list?token=unauthed%20test%20token + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 07 Oct 2020 23:44:56 GMT + Server: + - Apache + X-Slack-Req-Id: + - 5df70fa650ed849a1d6b53274ad8c901 + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-28oy,haproxy-edge-pdx-bm9l + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Wed, 07 Oct 2020 23:44:56 GMT +recorded_with: VCR 6.0.0 From f197b062be1f88e65de65ebe1e985ec8320d29d2 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 18:26:41 -0700 Subject: [PATCH 25/44] added final tests to show_details checking positive and negative nominals --- test/workspace_test.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/workspace_test.rb b/test/workspace_test.rb index a5837fae..2d60afc0 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -92,9 +92,23 @@ @new_workspace.load_channels end end - it "returns String" do + it "returns String for user object" do found_user = @new_workspace.select_user("slackbot") - expect(show_details(found_user)).must_be_kind_of String + + expect(@new_workspace.show_details(found_user)).must_be_kind_of String + end + it "returns String for channel object" do + found_channel = @new_workspace.select_channel("random") + + expect(@new_workspace.show_details(found_channel)).must_be_kind_of String + end + it "returns message for no details available" do + found_channel = @new_workspace.select_channel("nope") + expect(@new_workspace.show_details(found_channel)).must_equal "No recipient available to display details" + end + it "returns message for no details available" do + found_user = @new_workspace.select_user("nope2.0") + expect(@new_workspace.show_details(found_user)).must_equal "No recipient available to display details" end end end \ No newline at end of file From 5bcbeb7002c5434cf6a1cc469526843d731ace2c Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 19:02:07 -0700 Subject: [PATCH 26/44] added selections for select user, select channel and details for these selections. As well as validation. --- lib/slack.rb | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 21a21684..eb8fc3f9 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,27 +5,47 @@ def main workspace = Workspace.new - puts "1. list users\n2. list channels\n3. quit" - selection = input_validation(gets.chomp, 3) + puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit" + selection = number_validation(gets.chomp, 5) - while selection == 1 || selection == 2 - if selection == 1 + while (1..4).include? selection + case selection + when 1 workspace.load_users ap workspace.users - else + when 2 workspace.load_channels ap workspace.channels + when 3 + workspace.load_users + puts "Please input a username or slack id" + selected_user = workspace.select_user(gets.chomp) + puts "Would you like details?" + details_selection = input_validation(gets.chomp.downcase) + if details_selection == "yes" + puts workspace.show_details(selected_user) + end + when 4 + workspace.load_channels + puts "Please input a channel name or slack id" + selected_channel = workspace.select_channel(gets.chomp) + puts "Would you like details?" + details_selection = input_validation(gets.chomp.downcase) + if details_selection == "yes" + puts workspace.show_details(selected_channel) + end + end puts "Make another selection:" - puts "1. list users\n2. list channels\n3. quit" - selection = input_validation(gets.chomp, 3) + puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit" + selection = number_validation(gets.chomp, 5) end puts "Thank you for using the Ada Slack CLI" end -def input_validation(input, max_num) +def number_validation(input, max_num) input = translate_input(input) while !(1..max_num).include? input @@ -36,14 +56,26 @@ def input_validation(input, max_num) return input end +def input_validation(input) + until ["yes", "no"].include? input + puts "Invalid input. Please re-enter either a yes or no." + input = gets.chomp.downcase + end + return input +end + def translate_input(string_input) case string_input.downcase when "list users", "1" return 1 when "list channels", "2" return 2 - when "quit", "3" + when "select user", "3" return 3 + when "select channel", "4" + return 4 + when "quit", "5" + return 5 else return string_input end From 7e22e63aead790df42d3fe1ccf2bd8f5ba7ebb32 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 19:02:45 -0700 Subject: [PATCH 27/44] uncommented vcr config that allows http request outside of VCR control blocks --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index da6c1e6d..01aeb095 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,7 +25,7 @@ :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match } - # config.allow_http_connections_when_no_cassette = true #added by Rachael and Christabel to allow to HTTP Requests without cassettes + config.allow_http_connections_when_no_cassette = true #added by Rachael and Christabel to allow to HTTP Requests without cassettes # Don't leave our token lying around in a cassette file. config.filter_sensitive_data("") do From 66a0947d765785c010857e790115f41b3c3553c2 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 7 Oct 2020 19:03:10 -0700 Subject: [PATCH 28/44] changed nil output sentence to be more clear to the user what went wrong. --- lib/workspace.rb | 2 +- test/workspace_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index cf356837..059ac3e7 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -31,7 +31,7 @@ def show_details(recipient) if recipient return recipient.details else - return "No recipient available to display details" + return "Invalid recipient. Unable to display details" end end diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 2d60afc0..1502f8e6 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -104,11 +104,11 @@ end it "returns message for no details available" do found_channel = @new_workspace.select_channel("nope") - expect(@new_workspace.show_details(found_channel)).must_equal "No recipient available to display details" + expect(@new_workspace.show_details(found_channel)).must_equal "Invalid recipient. Unable to display details" end it "returns message for no details available" do found_user = @new_workspace.select_user("nope2.0") - expect(@new_workspace.show_details(found_user)).must_equal "No recipient available to display details" + expect(@new_workspace.show_details(found_user)).must_equal "Invalid recipient. Unable to display details" end end end \ No newline at end of file From 52c5fa921c4bd70bf4126e17cc3a007dcf80b716 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Wed, 7 Oct 2020 19:44:19 -0700 Subject: [PATCH 29/44] Removed test_helper require --- lib/channel.rb | 1 - lib/slack.rb | 4 ++ lib/user.rb | 1 - lib/workspace.rb | 2 - test/cassettes/get_conversations_list.yml | 53 +++++++++++++++++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index e7e3e803..1dbcdb8b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,4 +1,3 @@ -require_relative '../test/test_helper' require_relative 'recipient' CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" diff --git a/lib/slack.rb b/lib/slack.rb index eb8fc3f9..a7dcf35e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,10 @@ #!/usr/bin/env ruby +require 'dotenv' require_relative 'workspace' + def main + Dotenv.load + puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new diff --git a/lib/user.rb b/lib/user.rb index 2ac0ed32..683c3763 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,4 +1,3 @@ -require_relative '../test/test_helper' require_relative 'recipient' USERS_LIST_URL = "https://slack.com/api/users.list" diff --git a/lib/workspace.rb b/lib/workspace.rb index 059ac3e7..affbd1df 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,8 +1,6 @@ -require_relative '../test/test_helper' require_relative 'user' require_relative 'channel' - class Workspace attr_reader :users, :channels diff --git a/test/cassettes/get_conversations_list.yml b/test/cassettes/get_conversations_list.yml index 35eb0ca3..9b8410b4 100644 --- a/test/cassettes/get_conversations_list.yml +++ b/test/cassettes/get_conversations_list.yml @@ -69,4 +69,57 @@ http_interactions: is the one channel that will always include everyone. It\u2019s a great spot for announcements and team-wide conversations.","creator":"U01BXFBQ18D","last_set":1601943301},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' recorded_at: Thu, 08 Oct 2020 00:26:15 GMT +- request: + method: get + uri: https://slack.com/api/conversations.list?token=unauthed%20test%20token + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 02:05:37 GMT + Server: + - Apache + X-Slack-Req-Id: + - 199aad22c97ec70e85dba73240486a9d + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-u1v1,haproxy-edge-pdx-mprq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 02:05:37 GMT recorded_with: VCR 6.0.0 From 8d47a90b2b4ce4e641fb7822f42a8af7fb3b2a5f Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 11:41:35 -0700 Subject: [PATCH 30/44] created send_message method and tests for recipient_test.rb send message method is not passing its test --- lib/recipient.rb | 13 +++-- test/cassettes/post_message_to_channel.yml | 58 ++++++++++++++++++++++ test/recipient_test.rb | 56 +++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 test/cassettes/post_message_to_channel.yml diff --git a/lib/recipient.rb b/lib/recipient.rb index 02165ef7..be312374 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,3 +1,4 @@ +POST_URL = "https://slack.com/api/chat.postMessage" class Recipient attr_reader :slack_id, :name @@ -5,9 +6,15 @@ def initialize(slack_id, name) @slack_id = slack_id @name = name end - # def send_meassage(message) - # - # end + + def send_message(message) + params = {token: ENV['SLACK_TOKEN'], channel: @slack_id, text: message} + sleep(1) + puts params + # return self.class.error_message(HTTParty.post(POST_URL,params)) + puts HTTParty.post(POST_URL,params) + end + def self.get(url, params) sleep(1) return error_message(HTTParty.get(url,params)) diff --git a/test/cassettes/post_message_to_channel.yml b/test/cassettes/post_message_to_channel.yml new file mode 100644 index 00000000..d06c2c45 --- /dev/null +++ b/test/cassettes/post_message_to_channel.yml @@ -0,0 +1,58 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 18:15:00 GMT + Server: + - Apache + X-Xss-Protection: + - '0' + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Accepted-Oauth-Scopes: + - chat:write:bot + X-Slack-Req-Id: + - 97b1b915c8e4b9b6cf05a363259893c4 + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + Content-Length: + - '53' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-lo72,haproxy-edge-pdx-locq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"not_authed"}' + recorded_at: Thu, 08 Oct 2020 18:15:00 GMT +recorded_with: VCR 6.0.0 diff --git a/test/recipient_test.rb b/test/recipient_test.rb index 1926b9ed..ca82bc12 100644 --- a/test/recipient_test.rb +++ b/test/recipient_test.rb @@ -1,2 +1,58 @@ require_relative 'test_helper' require_relative '../lib/recipient' + +describe "Recipient class" do + + describe "instantiation" do + + before do + @new_recipient = Recipient.new("some_id", "some_name") + end + + it "is an instance of Recipient" do + expect(@new_recipient).must_be_instance_of Recipient + end + + it "establishes the base data structures when instantiated" do + [:slack_id, :name].each do |keyword| + expect(@new_recipient).must_respond_to keyword + end + + expect(@new_recipient.slack_id).must_be_kind_of String + expect(@new_recipient.name).must_be_kind_of String + + end + + end + + + describe "details" do + before do + @new_recipient = Recipient.new("some_id", "some_name") + end + + it "raises an error" do + expect{@new_recipient.details}.must_raise NotImplementedError + end + end + + describe "self.list_all" do + it "raises an error" do + expect{Recipient.list_all}.must_raise NotImplementedError + end + end + + describe "send message" do + before do + @channel_recipient = Recipient.new("C01BKRLQ4UF", "random") + @user_recipient = Recipient.new("U01C0JB1FB4", "christabot") + @message = "Testing" + end + it "sends a message to a channel" do + VCR.use_cassette("post message to channel") do + @response = @channel_recipient.send_message(@message) + end + expect(@response).must_equal true + end + end +end \ No newline at end of file From 9a92c48a3708909a3f74b5895b5bc1c0171e414d Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 11:51:07 -0700 Subject: [PATCH 31/44] changed cassette name for expected Errors in .get test --- test/channel_test.rb | 2 +- test/user_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/channel_test.rb b/test/channel_test.rb index ac4fa6d4..8b0a4b01 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -48,7 +48,7 @@ end it "will raise an exception if the search fails" do - VCR.use_cassette("get conversations list") do + VCR.use_cassette("get conversations list -- bad token") do expect { Channel.get(CONVERSATIONS_LIST_URL, query: {token: "unauthed test token"}) }.must_raise ArgumentError diff --git a/test/user_test.rb b/test/user_test.rb index a6dafa25..2f839196 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -49,7 +49,7 @@ end it "will raise an exception if the search fails" do - VCR.use_cassette("get users list") do + VCR.use_cassette("get users list -- bad token") do expect { User.get(USERS_LIST_URL, query: {token: "unauthed test token"}) }.must_raise ArgumentError From cb11811a22c22d353b179e1952a3e2807b96198e Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 11:59:18 -0700 Subject: [PATCH 32/44] Add cassettes --- test/cassettes/post_message_to_channel.yml | 64 ++++++++++++++++++ ...post_message_to_channel_--_nil_message.yml | 66 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 test/cassettes/post_message_to_channel_--_nil_message.yml diff --git a/test/cassettes/post_message_to_channel.yml b/test/cassettes/post_message_to_channel.yml index d06c2c45..0c8ca7d2 100644 --- a/test/cassettes/post_message_to_channel.yml +++ b/test/cassettes/post_message_to_channel.yml @@ -55,4 +55,68 @@ http_interactions: encoding: ASCII-8BIT string: '{"ok":false,"error":"not_authed"}' recorded_at: Thu, 08 Oct 2020 18:15:00 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=C01BKRLQ4UF&text=Testing + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 18:46:24 GMT + Server: + - Apache + X-Slack-Req-Id: + - 370b27daada22e83a85bb43d44ec99e9 + X-Oauth-Scopes: + - users:read,chat:write,channels:read,channels:history,im:history,im:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '335' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-75m7,haproxy-edge-pdx-tfuw + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":"C01BKRLQ4UF","ts":"1602182784.001300","message":{"bot_id":"B01CQDPVC48","type":"message","text":"Testing","user":"U01C0JB1FB4","ts":"1602182784.001300","team":"T01BKRLPT7Z","bot_profile":{"id":"B01CQDPVC48","deleted":false,"name":"Water + - Christabel - Slack CLI","updated":1601943921,"app_id":"A01C0J7RNGJ","icons":{"image_36":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_36.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/service_72.png"},"team_id":"T01BKRLPT7Z"}}}' + recorded_at: Thu, 08 Oct 2020 18:46:24 GMT recorded_with: VCR 6.0.0 diff --git a/test/cassettes/post_message_to_channel_--_nil_message.yml b/test/cassettes/post_message_to_channel_--_nil_message.yml new file mode 100644 index 00000000..a600a669 --- /dev/null +++ b/test/cassettes/post_message_to_channel_--_nil_message.yml @@ -0,0 +1,66 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=C01BKRLQ4UF&text= + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 18:57:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 215b08c382410717dd96a4fc75f8984c + X-Oauth-Scopes: + - users:read,chat:write,channels:read,channels:history,im:history,im:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '50' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-9kiv,haproxy-edge-pdx-k9dj + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"no_text"}' + recorded_at: Thu, 08 Oct 2020 18:57:57 GMT +recorded_with: VCR 6.0.0 From 3ae4924f35a038536d3c5dff87c46f2298ecdf7b Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 11:59:38 -0700 Subject: [PATCH 33/44] Added addtional tests for send_message (in progress) --- lib/recipient.rb | 4 +--- test/recipient_test.rb | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index be312374..b4af1042 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -10,9 +10,7 @@ def initialize(slack_id, name) def send_message(message) params = {token: ENV['SLACK_TOKEN'], channel: @slack_id, text: message} sleep(1) - puts params - # return self.class.error_message(HTTParty.post(POST_URL,params)) - puts HTTParty.post(POST_URL,params) + return self.class.error_message(HTTParty.post(POST_URL, body: params)) end def self.get(url, params) diff --git a/test/recipient_test.rb b/test/recipient_test.rb index ca82bc12..730d34c2 100644 --- a/test/recipient_test.rb +++ b/test/recipient_test.rb @@ -48,11 +48,42 @@ @user_recipient = Recipient.new("U01C0JB1FB4", "christabot") @message = "Testing" end + it "sends a message to a channel" do VCR.use_cassette("post message to channel") do - @response = @channel_recipient.send_message(@message) + response = @channel_recipient.send_message(@message) + expect(response["ok"]).must_equal true end - expect(@response).must_equal true end + + it "sends a message to a user" do + + end + + it "expect error_message for bad `channel`" do + VCR.use_cassette("post message to channel -- bad recipient") do + bad_recipient = Recipient.new("failure", "still a failure") + expect { + bad_recipient.send_message(@message) + }.must_raise ArgumentError + end + end + + it "expect error_message for bad `user`" do + + end + + it "expect error_message for nil `text` to a channel" do + VCR.use_cassette("post message to channel -- nil message") do + expect { + @channel_recipient.send_message(nil) + }.must_raise ArgumentError + end + end + + it "expect error_message for nil `text` to a user" do + + end + end end \ No newline at end of file From edbced795daef34bea48b49de120091666de4e4a Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 14:12:04 -0700 Subject: [PATCH 34/44] Added cassettes --- .../post_message_--_bad_recipient.yml | 66 ++++++++++++++++++ test/cassettes/post_message_to_user.yml | 67 +++++++++++++++++++ .../post_message_to_user_--_nil_message.yml | 66 ++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 test/cassettes/post_message_--_bad_recipient.yml create mode 100644 test/cassettes/post_message_to_user.yml create mode 100644 test/cassettes/post_message_to_user_--_nil_message.yml diff --git a/test/cassettes/post_message_--_bad_recipient.yml b/test/cassettes/post_message_--_bad_recipient.yml new file mode 100644 index 00000000..a5995850 --- /dev/null +++ b/test/cassettes/post_message_--_bad_recipient.yml @@ -0,0 +1,66 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=failure&text=Testing + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 21:10:51 GMT + Server: + - Apache + X-Slack-Req-Id: + - 7a44c2d729cbb20a2438d49080194867 + X-Oauth-Scopes: + - users:read,chat:write,channels:read,channels:history,im:history,im:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '60' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-k2o1,haproxy-edge-pdx-333x + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"channel_not_found"}' + recorded_at: Thu, 08 Oct 2020 21:10:51 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/post_message_to_user.yml b/test/cassettes/post_message_to_user.yml new file mode 100644 index 00000000..a5bcc91e --- /dev/null +++ b/test/cassettes/post_message_to_user.yml @@ -0,0 +1,67 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=U01C6PZLEBW&text=Testing + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 21:08:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 7ca785a5ffc3e5badcd49944d0fa2f9d + X-Oauth-Scopes: + - users:read,chat:write,channels:read,channels:history,im:history,im:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '337' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-ot84,haproxy-edge-pdx-bivi + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":"D01CF3F5WDS","ts":"1602191337.000300","message":{"bot_id":"B01CQDPVC48","type":"message","text":"Testing","user":"U01C0JB1FB4","ts":"1602191337.000300","team":"T01BKRLPT7Z","bot_profile":{"id":"B01CQDPVC48","deleted":false,"name":"Water + - Christabel - Slack CLI","updated":1601943921,"app_id":"A01C0J7RNGJ","icons":{"image_36":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_36.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/service_72.png"},"team_id":"T01BKRLPT7Z"}}}' + recorded_at: Thu, 08 Oct 2020 21:08:57 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/post_message_to_user_--_nil_message.yml b/test/cassettes/post_message_to_user_--_nil_message.yml new file mode 100644 index 00000000..55277d98 --- /dev/null +++ b/test/cassettes/post_message_to_user_--_nil_message.yml @@ -0,0 +1,66 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=U01C6PZLEBW&text= + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 21:11:34 GMT + Server: + - Apache + X-Slack-Req-Id: + - c1b0951e50a72d66ec5e22394069349e + X-Oauth-Scopes: + - users:read,chat:write,channels:read,channels:history,im:history,im:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '50' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-vtee,haproxy-edge-pdx-1350 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"no_text"}' + recorded_at: Thu, 08 Oct 2020 21:11:34 GMT +recorded_with: VCR 6.0.0 From 3e58c1b3e989a18255175ca8c2574dd280cc374d Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 14:12:19 -0700 Subject: [PATCH 35/44] Added all tests for send_message --- test/recipient_test.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test/recipient_test.rb b/test/recipient_test.rb index 730d34c2..7492058a 100644 --- a/test/recipient_test.rb +++ b/test/recipient_test.rb @@ -45,7 +45,7 @@ describe "send message" do before do @channel_recipient = Recipient.new("C01BKRLQ4UF", "random") - @user_recipient = Recipient.new("U01C0JB1FB4", "christabot") + @user_recipient = Recipient.new("U01C6PZLEBW", "Rachael's bot") @message = "Testing" end @@ -57,11 +57,15 @@ end it "sends a message to a user" do + VCR.use_cassette("post message to user") do + response = @user_recipient.send_message(@message) + expect(response["ok"]).must_equal true + end end - it "expect error_message for bad `channel`" do - VCR.use_cassette("post message to channel -- bad recipient") do + it "expect error_message for bad recipient" do + VCR.use_cassette("post message -- bad recipient") do bad_recipient = Recipient.new("failure", "still a failure") expect { bad_recipient.send_message(@message) @@ -69,10 +73,6 @@ end end - it "expect error_message for bad `user`" do - - end - it "expect error_message for nil `text` to a channel" do VCR.use_cassette("post message to channel -- nil message") do expect { @@ -82,7 +82,11 @@ end it "expect error_message for nil `text` to a user" do - + VCR.use_cassette("post message to user -- nil message") do + expect { + @user_recipient.send_message(nil) + }.must_raise ArgumentError + end end end From f28a63bd6addfbc9d9ea0bbdb169ed3ac1bf7c07 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 14:38:47 -0700 Subject: [PATCH 36/44] added require httparty in recipient.rb --- lib/recipient.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/recipient.rb b/lib/recipient.rb index b4af1042..1587fec3 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,4 +1,5 @@ POST_URL = "https://slack.com/api/chat.postMessage" +require 'httparty' class Recipient attr_reader :slack_id, :name From ccda1c65b57b1b476a30fd789e3fd9a7796ca95d Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 14:39:10 -0700 Subject: [PATCH 37/44] added selections for send message to slack.rb. --- lib/slack.rb | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index a7dcf35e..723feec8 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,7 +1,9 @@ #!/usr/bin/env ruby require 'dotenv' +require "table_print" require_relative 'workspace' + def main Dotenv.load @@ -16,29 +18,51 @@ def main case selection when 1 workspace.load_users - ap workspace.users + tp workspace.users when 2 workspace.load_channels - ap workspace.channels + tp workspace.channels when 3 workspace.load_users puts "Please input a username or slack id" selected_user = workspace.select_user(gets.chomp) - puts "Would you like details?" - details_selection = input_validation(gets.chomp.downcase) - if details_selection == "yes" - puts workspace.show_details(selected_user) + if selected_user + puts "Would you like details?" + details_selection = input_validation(gets.chomp.downcase) + if details_selection == "yes" + puts workspace.show_details(selected_user) + end + puts "Would you like to send a message to #{selected_user.real_name}?" + message_selection = input_validation(gets.chomp.downcase) + if message_selection == "yes" + puts "Please write your message." + message = gets.chomp + selected_user.send_message(message) + end + else + puts "No user found." end + when 4 workspace.load_channels puts "Please input a channel name or slack id" selected_channel = workspace.select_channel(gets.chomp) - puts "Would you like details?" - details_selection = input_validation(gets.chomp.downcase) - if details_selection == "yes" - puts workspace.show_details(selected_channel) + if selected_channel + puts "Would you like details?" + details_selection = input_validation(gets.chomp.downcase) + if details_selection == "yes" + puts workspace.show_details(selected_channel) + end + puts "Would you like to make a post on #{selected_channel.name}?" + message_selection = input_validation(gets.chomp.downcase) + if message_selection == "yes" + puts "Please write your post." + message = gets.chomp + selected_channel.send_message(message) + end + else + puts "No channel found." end - end puts "Make another selection:" From f65cf013409522e589c0a431bca36f9c0af4525f Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 14:40:58 -0700 Subject: [PATCH 38/44] added cassettes --- .../get_conversations_list_--_bad_token.yml | 56 +++++++++++++++++++ .../cassettes/get_users_list_--_bad_token.yml | 56 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/cassettes/get_conversations_list_--_bad_token.yml create mode 100644 test/cassettes/get_users_list_--_bad_token.yml diff --git a/test/cassettes/get_conversations_list_--_bad_token.yml b/test/cassettes/get_conversations_list_--_bad_token.yml new file mode 100644 index 00000000..12c60d63 --- /dev/null +++ b/test/cassettes/get_conversations_list_--_bad_token.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/conversations.list?token=unauthed%20test%20token + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 21:39:45 GMT + Server: + - Apache + X-Slack-Req-Id: + - 6b79dee0178048fc8eb716792ca19601 + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-sd3m,haproxy-edge-pdx-rqgu + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 21:39:45 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/get_users_list_--_bad_token.yml b/test/cassettes/get_users_list_--_bad_token.yml new file mode 100644 index 00000000..db937462 --- /dev/null +++ b/test/cassettes/get_users_list_--_bad_token.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token=unauthed%20test%20token + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 21:39:44 GMT + Server: + - Apache + X-Slack-Req-Id: + - dbeaf7aa1cc28caae5059e989a9ad883 + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-m88x,haproxy-edge-pdx-1g64 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 21:39:44 GMT +recorded_with: VCR 6.0.0 From dab22bb950d63167b084160685a0fcd2c5d7277b Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 14:41:07 -0700 Subject: [PATCH 39/44] removed sleeps --- lib/recipient.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 1587fec3..861918c2 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -10,12 +10,10 @@ def initialize(slack_id, name) def send_message(message) params = {token: ENV['SLACK_TOKEN'], channel: @slack_id, text: message} - sleep(1) return self.class.error_message(HTTParty.post(POST_URL, body: params)) end def self.get(url, params) - sleep(1) return error_message(HTTParty.get(url,params)) end From b049650650fed61f5888b801ff052bc3e7a00af7 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 8 Oct 2020 14:58:49 -0700 Subject: [PATCH 40/44] reformmated how list of users and channels each print using table print. added a count of members and channels at the beginning of the interface --- lib/slack.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 723feec8..f2fbec62 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -9,6 +9,10 @@ def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new + workspace.load_users + workspace.load_channels + puts "Amount of users in workspace: #{workspace.users.length}" + puts "Amount of channels in workspace: #{workspace.channels.length}" puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit" @@ -17,13 +21,10 @@ def main while (1..4).include? selection case selection when 1 - workspace.load_users - tp workspace.users + tp workspace.users, "slack_id", "name", "real_name", "status_text", "status_emoji" when 2 - workspace.load_channels - tp workspace.channels + tp workspace.channels, "slack_id", "name", "topic", "member_count" when 3 - workspace.load_users puts "Please input a username or slack id" selected_user = workspace.select_user(gets.chomp) if selected_user @@ -44,7 +45,6 @@ def main end when 4 - workspace.load_channels puts "Please input a channel name or slack id" selected_channel = workspace.select_channel(gets.chomp) if selected_channel From aa96783bfa892f2ed1d0db110c574abeabfd5ee3 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 15:19:44 -0700 Subject: [PATCH 41/44] Add cassette --- test/cassettes/get_users_list.yml | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/test/cassettes/get_users_list.yml b/test/cassettes/get_users_list.yml index 2a196c01..29d40ba8 100644 --- a/test/cassettes/get_users_list.yml +++ b/test/cassettes/get_users_list.yml @@ -126,4 +126,59 @@ http_interactions: encoding: ASCII-8BIT string: '{"ok":false,"error":"invalid_auth"}' recorded_at: Wed, 07 Oct 2020 23:44:56 GMT +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 22:15:16 GMT + Server: + - Apache + X-Xss-Protection: + - '0' + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Accepted-Oauth-Scopes: + - users:read + X-Slack-Req-Id: + - d9cd6ecdf095c383a71e7ebc644f46cf + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + Content-Length: + - '53' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-g9kt,haproxy-edge-pdx-herk + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"not_authed"}' + recorded_at: Thu, 08 Oct 2020 22:15:16 GMT recorded_with: VCR 6.0.0 From dcd3e2f769e6d9cd6c561b92181bd1be3e133ea1 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 15:20:15 -0700 Subject: [PATCH 42/44] Combined selections 3/4 in `main` case statement --- lib/slack.rb | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index f2fbec62..e1d81cc8 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -24,45 +24,31 @@ def main tp workspace.users, "slack_id", "name", "real_name", "status_text", "status_emoji" when 2 tp workspace.channels, "slack_id", "name", "topic", "member_count" - when 3 - puts "Please input a username or slack id" - selected_user = workspace.select_user(gets.chomp) - if selected_user - puts "Would you like details?" - details_selection = input_validation(gets.chomp.downcase) - if details_selection == "yes" - puts workspace.show_details(selected_user) - end - puts "Would you like to send a message to #{selected_user.real_name}?" - message_selection = input_validation(gets.chomp.downcase) - if message_selection == "yes" - puts "Please write your message." - message = gets.chomp - selected_user.send_message(message) - end + when 3, 4 + puts "Please input a name or slack id" + if selection == 3 + selected_recipient = workspace.select_user(gets.chomp) else - puts "No user found." + selected_recipient = workspace.select_channel(gets.chomp) end - when 4 - puts "Please input a channel name or slack id" - selected_channel = workspace.select_channel(gets.chomp) - if selected_channel - puts "Would you like details?" + if selected_recipient + puts "Would you like details? (yes/no)" details_selection = input_validation(gets.chomp.downcase) if details_selection == "yes" - puts workspace.show_details(selected_channel) + puts workspace.show_details(selected_recipient) end - puts "Would you like to make a post on #{selected_channel.name}?" + puts "Would you like to #{selected_recipient.class == User ? "send a message" : "post"} to #{selected_recipient.name}? (yes/no)" message_selection = input_validation(gets.chomp.downcase) if message_selection == "yes" - puts "Please write your post." + puts "Please write your message." message = gets.chomp - selected_channel.send_message(message) + selected_recipient.send_message(message) end else - puts "No channel found." + puts "No #{(selection == 3) ? "user" : "channel"} found." end + end puts "Make another selection:" @@ -76,7 +62,7 @@ def main def number_validation(input, max_num) input = translate_input(input) - while !(1..max_num).include? input + until (1..max_num).include? input puts "Invalid input. Please re-enter a selection." input = translate_input(gets.chomp) end From 96a1faf819b670f7cf2dfa101c5208be738cdaee Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 15:24:20 -0700 Subject: [PATCH 43/44] Changed .error_message condition to only evaluate response["ok"] and output response["error"] --- lib/recipient.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 861918c2..1b386a34 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,5 +1,7 @@ -POST_URL = "https://slack.com/api/chat.postMessage" require 'httparty' + +POST_URL = "https://slack.com/api/chat.postMessage" + class Recipient attr_reader :slack_id, :name @@ -27,8 +29,8 @@ def self.list_all private def self.error_message(response) - if response.code != 200 || response["ok"] != true - raise ArgumentError, "API request failed with error code #{response.code} and #{response["error"]}." + if response["ok"] != true + raise ArgumentError, "API request failed with error: #{response["error"]}." else return response end From 00d37c44f59a1b26662d8c1bfb76093e5fde3236 Mon Sep 17 00:00:00 2001 From: Christabel Sebastian Date: Thu, 8 Oct 2020 15:26:02 -0700 Subject: [PATCH 44/44] added .to_i to member_count constructor --- lib/channel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/channel.rb b/lib/channel.rb index 1dbcdb8b..2899f89b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -8,7 +8,7 @@ class Channel < Recipient def initialize(slack_id, name, topic, member_count) super(slack_id, name) @topic = topic - @member_count = member_count + @member_count = member_count.to_i end def details