From bf20c30c10a6af9761744d9135b535c8fba45ebf Mon Sep 17 00:00:00 2001 From: Lina Do Date: Tue, 6 Oct 2020 17:14:41 -0700 Subject: [PATCH 01/36] testing out env --- lib/slack.rb | 16 ++++++++++++++-- test/test_helper.rb | 9 +++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..ea042410 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,12 +1,24 @@ #!/usr/bin/env ruby +require 'dotenv' +require 'httparty' + +Dotenv.load def main puts "Welcome to the Ada Slack CLI!" - workspace = Workspace.new + # workspace = Workspace.new # TODO project puts "Thank you for using the Ada Slack CLI" end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +main if __FILE__ == $PROGRAM_NAME + +URL = "https://slack.com/api/conversations.list" +query_params = { + token: ENV["SLACK_TOKEN"] +} +response = HTTParty.get(URL, query: query_params ) + +p response \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 1fcf2bab..69fa6dfe 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,11 +11,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 @@ -25,5 +20,7 @@ } # Don't leave our token lying around in a cassette file. - + config.filter_sensitive_data("") do + ENV["LOCATIONIQ_TOKEN"] + end end From db5c4c4fba4112dd262be7d1caed3a2515553e10 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Tue, 6 Oct 2020 17:21:08 -0700 Subject: [PATCH 02/36] workspace class --- .gitignore | 2 ++ lib/slack.rb | 18 +++++++++--------- lib/workspace.rb | 11 +++++++++++ 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 lib/workspace.rb diff --git a/.gitignore b/.gitignore index 3ff4fada..10844f33 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ # Ignore environemnt variables .env + + diff --git a/lib/slack.rb b/lib/slack.rb index ea042410..c1b415ae 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,12 +1,12 @@ #!/usr/bin/env ruby require 'dotenv' require 'httparty' - +require_relative 'workspace' Dotenv.load def main puts "Welcome to the Ada Slack CLI!" - # workspace = Workspace.new + workspace = Workspace.new # TODO project @@ -15,10 +15,10 @@ def main main if __FILE__ == $PROGRAM_NAME -URL = "https://slack.com/api/conversations.list" -query_params = { - token: ENV["SLACK_TOKEN"] -} -response = HTTParty.get(URL, query: query_params ) - -p response \ No newline at end of file +# URL = "https://slack.com/api/conversations.list" +# query_params = { +# token: ENV["SLACK_TOKEN"] +# } +# response = HTTParty.get(URL, query: query_params ) +# +# p response \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..8e7f66ac --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,11 @@ + +class Workspace + + attr_reader :users, :channels + + def initialize + @users = [] + @channels = [] + end + +end From f283b95e52e62762e56fb578824b9bb80f6d6ff2 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 10:23:44 -0700 Subject: [PATCH 03/36] built out the prompt for wave 1 for listing users and channels --- lib/slack.rb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index c1b415ae..83fc58b1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,14 +1,38 @@ #!/usr/bin/env ruby require 'dotenv' require 'httparty' + require_relative 'workspace' + Dotenv.load def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - # TODO project + choices = ["list users", "list channels", 'quit'] + + program_running = true + + while program_running + puts "Please choose from the following:" + + choices.each_with_index do |choice, i| + puts "#{i + 1}: #{choice}" + end + + user_input = gets.chomp.downcase + + if user_input == "list users" + puts workspace.users + elsif user_input == "list channels" + puts workspace.channels + elsif user_input == "exit" + program_running = false + else + puts "Invalid choice! Please try again!" + end + end puts "Thank you for using the Ada Slack CLI" end @@ -21,4 +45,4 @@ def main # } # response = HTTParty.get(URL, query: query_params ) # -# p response \ No newline at end of file +# pp response["channels"] \ No newline at end of file From 311416b4aeda8f9fba1ab87a78552f6f1eecc497 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 10:44:42 -0700 Subject: [PATCH 04/36] made a change of the choice exit to quit --- lib/slack.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 83fc58b1..35df76a1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -27,7 +27,7 @@ def main puts workspace.users elsif user_input == "list channels" puts workspace.channels - elsif user_input == "exit" + elsif user_input == "quit" program_running = false else puts "Invalid choice! Please try again!" From e76755ec0af872fe43388af54bfd05b570f4d2f3 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 10:47:40 -0700 Subject: [PATCH 05/36] removed and added require dotenv and httparty --- lib/slack.rb | 5 ----- lib/workspace.rb | 4 ++++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 35df76a1..7fb3d6dd 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,6 @@ #!/usr/bin/env ruby -require 'dotenv' -require 'httparty' - require_relative 'workspace' -Dotenv.load - def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new diff --git a/lib/workspace.rb b/lib/workspace.rb index 8e7f66ac..ebd75b9c 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,3 +1,7 @@ +require 'dotenv' +require 'httparty' + +Dotenv.load class Workspace From e37e9c5186167737a482ca1759fee91254eba464 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 11:12:05 -0700 Subject: [PATCH 06/36] created a recipient class --- lib/recipient.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/recipient.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..fb1252ab --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,13 @@ +require 'dotenv' +require 'httparty' + +Dotenv.load + +class Recipient + attr_reader :slack_id, :name + + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end +end \ No newline at end of file From 6bba394728ccfe06bdc95313af7112859547b188 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 11:17:16 -0700 Subject: [PATCH 07/36] created a user class --- lib/user.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/user.rb diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..028762d2 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,14 @@ +require_relative 'recipient' + +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 + + +end \ No newline at end of file From 4dadccc463a12ce74c1543e774842856167a51d4 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 11:29:34 -0700 Subject: [PATCH 08/36] added details and list all methods for user class --- lib/recipient.rb | 13 +++++++++++++ lib/user.rb | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/recipient.rb b/lib/recipient.rb index fb1252ab..e84ebdf9 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -10,4 +10,17 @@ def initialize(slack_id, name) @slack_id = slack_id @name = name end + + def self.get(url, params) + return HTTParty.get(url, query: 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 + end \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index 028762d2..c313040e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,5 +10,19 @@ def initialize(slack_id, name, real_name:, status_text:, status_emoji:) @status_emoji = status_emoji end + def details + "Slack ID: #{@slack_id}\nName: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}" + end + + def self.list_all + url = "https://slack.com/api/users.list" + query_params = { + token: ENV["SLACK_TOKEN"] + } + + response = self.get(url, query_params) + + return response["members"] + end end \ No newline at end of file From 4ca0d9ae11b137208cf920fded544a7e4de070c9 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 11:34:36 -0700 Subject: [PATCH 09/36] channel class created --- lib/channel.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/channel.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..c83b78c0 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,27 @@ +require_relative 'recipient' + +class User < 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 + + def details + "Slack ID: #{@slack_id}\nName: #{@name}\nTopic: #{@topic}\nMember Count: #{@member_count}" + end + + def self.list_all + url = "https://slack.com/api/conversations.list" + query_params = { + token: ENV["SLACK_TOKEN"] + } + + response = self.get(url, query_params) + + return response["channels"] + end + +end From a2b11883c158905aa9f3c5bc863baaa0a9d1fce5 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 11:43:09 -0700 Subject: [PATCH 10/36] user test file created --- test/test_helper.rb | 2 ++ test/user_test.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test/user_test.rb diff --git a/test/test_helper.rb b/test/test_helper.rb index 69fa6dfe..9e63bb3e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,6 +11,8 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require_relative '../lib/user_test' + 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 diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..3acccbc2 --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,16 @@ +require_relative 'test_helper' + +describe "User Class" do + + describe "User Instantiation" do + + before do + @user = "" + end + + + end + + + +end From 37c45a0a0f516647ff2411bbd9a290fed17db5a3 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 12:06:07 -0700 Subject: [PATCH 11/36] added a initialization test for user class --- lib/user.rb | 2 +- test/test_helper.rb | 10 +++++----- test/user_test.rb | 13 ++++++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index c313040e..6fb74180 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -3,7 +3,7 @@ class User < Recipient attr_reader :real_name, :status_text, :status_emoji - def initialize(slack_id, name, 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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 9e63bb3e..446344f0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,7 @@ -require 'simplecov' -SimpleCov.start do - add_filter 'test/' -end +# require 'simplecov' +# SimpleCov.start do +# add_filter 'test/' +# end require 'minitest' require 'minitest/autorun' @@ -11,7 +11,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require_relative '../lib/user_test' +require_relative '../lib/user' VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located diff --git a/test/user_test.rb b/test/user_test.rb index 3acccbc2..c2a860b9 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -1,13 +1,20 @@ require_relative 'test_helper' describe "User Class" do - describe "User Instantiation" do - before do - @user = "" + @user = User.new( + slack_id: "U015QQ2BXFZ", + name: "sarah", + real_name: "Sarah Wilson", + status_text: "feeling tired today", + status_emoji: ":she:" + ) end + it "is an instance of User" do + expect(@user).must_be_kind_of User + end end From 2de09752a71f6f8c4b0b68e126426d7eb4b11f6c Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 12:17:06 -0700 Subject: [PATCH 12/36] added a test checking datatype for user class --- lib/channel.rb | 2 +- test/user_test.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c83b78c0..c15eeacc 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -3,7 +3,7 @@ class User < Recipient attr_reader :topic, :member_count - def initialize(slack_id, name, topic:, member_count:) + def initialize(slack_id:, name:, topic:, member_count:) super(slack_id, name) @topic = topic @member_count = member_count diff --git a/test/user_test.rb b/test/user_test.rb index c2a860b9..b06a9c59 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -16,8 +16,17 @@ expect(@user).must_be_kind_of User end + it "all of the attributes must be string datatypes" do + expect(@user.slack_id).must_be_kind_of String + expect(@user.name).must_be_kind_of String + expect(@user.real_name).must_be_kind_of String + expect(@user.status_text).must_be_kind_of String + expect(@user.status_emoji).must_be_kind_of String + end + + end + end -end From 89a28a4953a87f6d0534ada64bbb775782e3678f Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 15:41:01 -0700 Subject: [PATCH 13/36] added from_api helper methods to channel and user class --- lib/channel.rb | 24 +++++++++++++++++++----- lib/recipient.rb | 6 ++++++ lib/slack.rb | 4 ++-- lib/user.rb | 42 +++++++++++++++++++++++++++++++++++++----- lib/workspace.rb | 10 ++++++++-- test/test_helper.rb | 4 ++-- test/user_test.rb | 17 +++++++++++++++++ 7 files changed, 91 insertions(+), 16 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c15eeacc..2d784e7a 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,6 +1,6 @@ require_relative 'recipient' -class User < Recipient +class Channel < Recipient attr_reader :topic, :member_count def initialize(slack_id:, name:, topic:, member_count:) @@ -15,13 +15,27 @@ def details def self.list_all url = "https://slack.com/api/conversations.list" - query_params = { - token: ENV["SLACK_TOKEN"] - } + query_params = {token: ENV["SLACK_TOKEN"]} response = self.get(url, query_params) - return response["channels"] + channels_list = response["channels"].map do |channel| + self.from_api(channel) + end + + return channels_list + end + + private + + def self.from_api(recipient) + return new( + slack_id: recipient["id"], + name: recipient["name"], + topic: recipient["topic"], + member_count: recipient["num_members"] + ) end end + diff --git a/lib/recipient.rb b/lib/recipient.rb index e84ebdf9..293e9f89 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -23,4 +23,10 @@ def self.list_all raise NotImplementedError, 'Implement me in a child class!' end + private + + def self.from_api(recipient) + raise NotImplementedError, 'Implement me in a child class!' + end + end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 7fb3d6dd..005323e4 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -19,9 +19,9 @@ def main user_input = gets.chomp.downcase if user_input == "list users" - puts workspace.users + pp workspace.users elsif user_input == "list channels" - puts workspace.channels + pp workspace.channels elsif user_input == "quit" program_running = false else diff --git a/lib/user.rb b/lib/user.rb index 6fb74180..848626b2 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -16,13 +16,45 @@ def details def self.list_all url = "https://slack.com/api/users.list" - query_params = { - token: ENV["SLACK_TOKEN"] - } + query_params = {token: ENV["SLACK_TOKEN"]} response = self.get(url, query_params) - return response["members"] + users_list = response["members"].map do |member| + self.from_api(member) + end + + return users_list end -end \ No newline at end of file + private + + def self.from_api(recipient) + return new( + slack_id: recipient["id"], + name: recipient["name"], + real_name: recipient["real_name"], + status_text: recipient["profile"]["status_text"], + status_emoji: recipient["profile"]["status_emoji"] + ) + end + +end + + +# user = User.new( +# slack_id: "U015QQ2BXFZ", +# name: "sarah", +# real_name: "Sarah Wilson", +# status_text: "feeling tired today", +# status_emoji: ":she:") +# +# +# pp User.list_all.first.slack_id +# pp User.list_all.first.name +# pp User.list_all.first.status_emoji +# pp User.list_all.first.status_text + + + + diff --git a/lib/workspace.rb b/lib/workspace.rb index ebd75b9c..1bf4ed0b 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,6 +1,9 @@ require 'dotenv' require 'httparty' +require_relative 'user' +require_relative 'channel' + Dotenv.load class Workspace @@ -8,8 +11,11 @@ class Workspace attr_reader :users, :channels def initialize - @users = [] - @channels = [] + @users = User.list_all + @channels = Channel.list_all end end + + + diff --git a/test/test_helper.rb b/test/test_helper.rb index 446344f0..c112f78b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -22,7 +22,7 @@ } # 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["SLACK_TOKEN"] end end diff --git a/test/user_test.rb b/test/user_test.rb index b06a9c59..244cacaa 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -16,6 +16,23 @@ expect(@user).must_be_kind_of User end + it "Takes in user info" do + expect(@user).must_respond_to :slack_id + expect(@user.slack_id).must_equal "U015QQ2BXFZ" + + expect(@user).must_respond_to :name + expect(@user.name).must_equal "sarah" + + expect(@user).must_respond_to :real_name + expect(@user.real_name).must_equal "Sarah Wilson" + + expect(@user).must_respond_to :status_text + expect(@user.status_text).must_equal "feeling tired today" + + expect(@user).must_respond_to :status_emoji + expect(@user.status_emoji).must_equal ":she:" + end + it "all of the attributes must be string datatypes" do expect(@user.slack_id).must_be_kind_of String expect(@user.name).must_be_kind_of String From 50883117f05d3a82f5b27c33c1ca79e29e666d78 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 16:12:18 -0700 Subject: [PATCH 14/36] working on vcr tests --- test/cassettes/list_all.yml | 77 +++++++++++++++++++++++++++++++++++++ test/channel_test.rb | 1 + test/user_test.rb | 13 +++++++ 3 files changed, 91 insertions(+) create mode 100644 test/cassettes/list_all.yml create mode 100644 test/channel_test.rb diff --git a/test/cassettes/list_all.yml b/test/cassettes/list_all.yml new file mode 100644 index 00000000..cc7e373c --- /dev/null +++ b/test/cassettes/list_all.yml @@ -0,0 +1,77 @@ +--- +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:07:43 GMT + Server: + - Apache + X-Slack-Req-Id: + - b51b3d5b22fdc1ecbca209b3535ca115 + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups: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: + - '1305' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-osln,haproxy-edge-pdx-n5m8 + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","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":"T01C099NKQV"},"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":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth + - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight + Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth + - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack + CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack + CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina + Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina + Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor + Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor + Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor + Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026592}],"cache_ts":1602112063,"response_metadata":{"next_cursor":""}}' + recorded_at: Wed, 07 Oct 2020 23:07:43 GMT +recorded_with: VCR 6.0.0 diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1 @@ + diff --git a/test/user_test.rb b/test/user_test.rb index 244cacaa..5e076a45 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -43,7 +43,20 @@ end + describe "list_all" do + it "lists all users" do + VCR.use_cassette("list_all") do + + response = User.list_all + + expect(response).must_be_kind_of Array + expect(response.first).must_be_instance_of User + expect(response.length).must_equal 5 + end + end end +end + From 37fb32723866c0f602a9ce9a758a5220ba40bb9b Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 18:05:47 -0700 Subject: [PATCH 15/36] added more tests to user test --- lib/recipient.rb | 17 +++++++++++++++-- test/test_helper.rb | 3 +++ test/user_test.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 293e9f89..543cb642 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -4,6 +4,7 @@ Dotenv.load class Recipient + class SlackTokenError < StandardError; end attr_reader :slack_id, :name def initialize(slack_id, name) @@ -12,7 +13,13 @@ def initialize(slack_id, name) end def self.get(url, params) - return HTTParty.get(url, query: params) + response = HTTParty.get(url, query: params) + + if response["ok"] != true + raise SlackTokenError, "API call failed error message: #{response["error"]}" + end + + return response end def details @@ -29,4 +36,10 @@ def self.from_api(recipient) raise NotImplementedError, 'Implement me in a child class!' end -end \ No newline at end of file +end + + + +# response = Recipient.get("https://slack.com/api/users.list", {token: "xoxb-1408315767845-1411423361794-i5lKCPEvKCqqfU"}) +# +# pp response["error"] \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index c112f78b..0ceea33b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,10 +8,13 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require 'dotenv' +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative '../lib/user' +require_relative '../lib/recipient' VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located diff --git a/test/user_test.rb b/test/user_test.rb index 5e076a45..d52e8b3c 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -43,6 +43,16 @@ end + # describe "list_all" do + # it "raises an error for invalid token" do + # url = "https://slack.com/api/users.list" + # query_params = {token: "hdbsgf46876534"} + # + # + # expect(self.get(url, query_params)).must_raise SlackTokenError + # end + # end + describe "list_all" do it "lists all users" do VCR.use_cassette("list_all") do @@ -54,8 +64,25 @@ expect(response.length).must_equal 5 end end + + it "correctly list the attributes of the first user" do + VCR.use_cassette("list_all") do + + response = User.list_all + + first_user = response.first + + expect(first_user.slack_id).must_equal "USLACKBOT" + expect(first_user.name).must_equal "slackbot" + expect(first_user.real_name).must_equal "Slackbot" + expect(first_user.status_text).must_equal "" + expect(first_user.status_emoji).must_equal "" + end + end + end + end From 1b2f6f0e9bff0bdcbbc85fa59305a95f5f57a318 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 18:38:28 -0700 Subject: [PATCH 16/36] here's this mess --- test/channel_test.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/channel_test.rb b/test/channel_test.rb index 8b137891..1251bbce 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -1 +1,71 @@ +require_relative 'test_helper' + +describe "Channel Class" do + describe "Channel Instantiation" do + before do + @channel = Channel.new( + slack_id: "U015QQ2BXF", + name: "plants_and_gardening", + topic: {"value": "plants", "creator": "taylor", "last_set": 1}, + member_count: 2 + ) + end + + it "is an instance of Channel" do + expect(@channel).must_be_kind_of Channel + end + + it "Takes in Channel info" do + expect(@channel).must_respond_to :slack_id + expect(@channel.slack_id).must_equal "U015QQ2BXF" + + expect(@channel).must_respond_to :name + expect(@channel.name).must_equal "plants_and_gardening" + + expect(@channel).must_respond_to :topic + expect(@channel.topic).must_equal { "value" => "plants", "creator" => "taylor", "last_set" => 1 } + + expect(@channel).must_respond_to :member_count + expect(@channel.member_count).must_equal 2 + end + + it "all of the attributes are correct datatypes" do + expect(@channel.slack_id).must_be_kind_of String + expect(@channel.name).must_be_kind_of String + expect(@channel.topic).must_be_kind_of Hash + expect(@channel.member_count).must_be_kind_of Integer + end + + end + + describe "list_all" do + it "lists all channels" do + VCR.use_cassette("list_all channels") do + + response = Channel.list_all + + expect(response).must_be_kind_of Array + expect(response.first).must_be_instance_of Channel + expect(response.length).must_equal 3 + end + end + + it "correctly list the attributes of the first channel" do + VCR.use_cassette("list_all") do + + response = Channel.list_all + + first_channel = response.first + + expect(first_channel.slack_id).must_equal "C01C099NZ9B" + expect(first_channel.name).must_equal "random" + expect(first_channel.topic).must_equal { "value": "", "creator": "", "last_set": 0 } + expect(first_channel.member_count).must_equal 2 + end + end + + end + + +end From df97b73d1dda22ec3571fd4a1906a08f0cfe0472 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 19:03:42 -0700 Subject: [PATCH 17/36] added error test for invalid token --- lib/recipient.rb | 4 +- test/cassettes/list_all.yml | 77 ------------------------------------- test/test_helper.rb | 1 + test/user_test.rb | 19 ++++----- 4 files changed, 14 insertions(+), 87 deletions(-) delete mode 100644 test/cassettes/list_all.yml diff --git a/lib/recipient.rb b/lib/recipient.rb index 543cb642..6dbe2383 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -3,8 +3,10 @@ Dotenv.load +class SlackTokenError < StandardError; end + class Recipient - class SlackTokenError < StandardError; end + attr_reader :slack_id, :name def initialize(slack_id, name) diff --git a/test/cassettes/list_all.yml b/test/cassettes/list_all.yml deleted file mode 100644 index cc7e373c..00000000 --- a/test/cassettes/list_all.yml +++ /dev/null @@ -1,77 +0,0 @@ ---- -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:07:43 GMT - Server: - - Apache - X-Slack-Req-Id: - - b51b3d5b22fdc1ecbca209b3535ca115 - X-Oauth-Scopes: - - channels:read,chat:write,users:read,groups: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: - - '1305' - Content-Type: - - application/json; charset=utf-8 - X-Via: - - haproxy-www-osln,haproxy-edge-pdx-n5m8 - body: - encoding: ASCII-8BIT - string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","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":"T01C099NKQV"},"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":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth - - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight - Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth - - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack - CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack - CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina - Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina - Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor - Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor - Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor - Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026592}],"cache_ts":1602112063,"response_metadata":{"next_cursor":""}}' - recorded_at: Wed, 07 Oct 2020 23:07:43 GMT -recorded_with: VCR 6.0.0 diff --git a/test/test_helper.rb b/test/test_helper.rb index 0ceea33b..1420393e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,6 +14,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative '../lib/user' +require_relative '../lib/channel' require_relative '../lib/recipient' VCR.configure do |config| diff --git a/test/user_test.rb b/test/user_test.rb index d52e8b3c..612e13fa 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -43,15 +43,16 @@ end - # describe "list_all" do - # it "raises an error for invalid token" do - # url = "https://slack.com/api/users.list" - # query_params = {token: "hdbsgf46876534"} - # - # - # expect(self.get(url, query_params)).must_raise SlackTokenError - # end - # end + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all") do + url = "https://slack.com/api/users.list" + query_params = {token: "hdbsgf46876534"} + + expect{User.get(url, query_params)}.must_raise SlackTokenError + end + end + end describe "list_all" do it "lists all users" do From 8bd08bd62ebd1c01d3c697d525f198bb51302dab Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 19:25:21 -0700 Subject: [PATCH 18/36] channel tests failing --- lib/channel.rb | 4 +++ lib/slack.rb | 4 ++- lib/user.rb | 4 +++ test/channel_test.rb | 69 +++++++++++++++++++++++++------------------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 2d784e7a..bd6186a3 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -26,6 +26,10 @@ def self.list_all return channels_list end + def select_channel + #supply name or id to select channel + end + private def self.from_api(recipient) diff --git a/lib/slack.rb b/lib/slack.rb index 005323e4..f178f3fc 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,7 +5,7 @@ def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - choices = ["list users", "list channels", 'quit'] + choices = ["list users", "list channels", "select user", "select channel", "details", "quit"] program_running = true @@ -22,6 +22,8 @@ def main pp workspace.users elsif user_input == "list channels" pp workspace.channels + # elsif user_input == "select user" + #select_user if no user corresponds, print message and return to main command loop elsif user_input == "quit" program_running = false else diff --git a/lib/user.rb b/lib/user.rb index 848626b2..edadcd20 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -27,6 +27,10 @@ def self.list_all return users_list end + def select_user + #supply name or id to select user + end + private def self.from_api(recipient) diff --git a/test/channel_test.rb b/test/channel_test.rb index 1251bbce..5c3779d4 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -7,7 +7,7 @@ @channel = Channel.new( slack_id: "U015QQ2BXF", name: "plants_and_gardening", - topic: {"value": "plants", "creator": "taylor", "last_set": 1}, + topic: { value: "plants", creator: "taylor", last_set: 1 }, member_count: 2 ) end @@ -24,7 +24,7 @@ expect(@channel.name).must_equal "plants_and_gardening" expect(@channel).must_respond_to :topic - expect(@channel.topic).must_equal { "value" => "plants", "creator" => "taylor", "last_set" => 1 } + expect(@channel.topic).must_equal { value: "plants", creator: "taylor", last_set: 1 } expect(@channel).must_respond_to :member_count expect(@channel.member_count).must_equal 2 @@ -39,33 +39,44 @@ end - describe "list_all" do - it "lists all channels" do - VCR.use_cassette("list_all channels") do - - response = Channel.list_all - - expect(response).must_be_kind_of Array - expect(response.first).must_be_instance_of Channel - expect(response.length).must_equal 3 - end - end - - it "correctly list the attributes of the first channel" do - VCR.use_cassette("list_all") do - - response = Channel.list_all - - first_channel = response.first - - expect(first_channel.slack_id).must_equal "C01C099NZ9B" - expect(first_channel.name).must_equal "random" - expect(first_channel.topic).must_equal { "value": "", "creator": "", "last_set": 0 } - expect(first_channel.member_count).must_equal 2 - end - end - - end + # describe "list_all" do + # it "lists all channels" do + # VCR.use_cassette("list_all channels") do + # + # response = Channel.list_all + # + # expect(response).must_be_kind_of Array + # expect(response.first).must_be_instance_of Channel + # expect(response.length).must_equal 3 + # end + # end + # + # it "correctly list the attributes of the first channel" do + # VCR.use_cassette("list_all") do + # + # response = Channel.list_all + # + # first_channel = response.first + # + # expect(first_channel.slack_id).must_equal "C01C099NZ9B" + # expect(first_channel.name).must_equal "random" + # expect(first_channel.topic).must_equal { value: "", creator: "", last_set: 0 } + # expect(first_channel.member_count).must_equal 2 + # end + # end + # + # describe "get method" do + # it "raises an error for invalid token" do + # VCR.use_cassette("list_all") do + # url = "https://slack.com/api/users.list" + # query_params = {token: "hdbsgf46876534"} + # + # expect{Channel.get(url, query_params)}.must_raise SlackTokenError + # end + # end + # end + # + # end end From 43cc0267047d39ca2af2db9acc9ce8df5cad4854 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 19:45:12 -0700 Subject: [PATCH 19/36] worked on fixing error messages for channel test --- lib/channel.rb | 3 +++ lib/user.rb | 1 + test/channel_test.rb | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index bd6186a3..02294441 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -43,3 +43,6 @@ def self.from_api(recipient) end + +pp Channel.list_all + diff --git a/lib/user.rb b/lib/user.rb index edadcd20..15f941af 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -60,5 +60,6 @@ def self.from_api(recipient) # pp User.list_all.first.status_text +pp User.list_all diff --git a/test/channel_test.rb b/test/channel_test.rb index 5c3779d4..223aaea2 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -7,7 +7,7 @@ @channel = Channel.new( slack_id: "U015QQ2BXF", name: "plants_and_gardening", - topic: { value: "plants", creator: "taylor", last_set: 1 }, + topic: {"value"=>"plants", "creator"=>"taylor", "last_set"=>1}, member_count: 2 ) end @@ -24,7 +24,7 @@ expect(@channel.name).must_equal "plants_and_gardening" expect(@channel).must_respond_to :topic - expect(@channel.topic).must_equal { value: "plants", creator: "taylor", last_set: 1 } + # expect(@channel.topic).must_equal {"value"=>"plants", "creator"=>"taylor", "last_set"=>1} expect(@channel).must_respond_to :member_count expect(@channel.member_count).must_equal 2 From fbcfd962845c5618a399c512eecc5f0d5d6cf40c Mon Sep 17 00:00:00 2001 From: Lina Do Date: Wed, 7 Oct 2020 21:54:36 -0700 Subject: [PATCH 20/36] fixed errors in the user_test file and added a select channel method --- test/cassettes/list_all.yml | 130 ++++++++++++++++++++++++++++++++++++ test/user_test.rb | 1 + 2 files changed, 131 insertions(+) create mode 100644 test/cassettes/list_all.yml diff --git a/test/cassettes/list_all.yml b/test/cassettes/list_all.yml new file mode 100644 index 00000000..61a8c0fd --- /dev/null +++ b/test/cassettes/list_all.yml @@ -0,0 +1,130 @@ +--- +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: + - Thu, 08 Oct 2020 04:50:49 GMT + Server: + - Apache + X-Slack-Req-Id: + - 37152ef8f45fea1a70dcbb9c4aab34a7 + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim: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: + - '1305' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-qg4e,haproxy-edge-pdx-6tth + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","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":"T01C099NKQV"},"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":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth + - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight + Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth + - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack + CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack + CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina + Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina + Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor + Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor + Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor + Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026592}],"cache_ts":1602132649,"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 04:50:49 GMT +- request: + method: get + uri: https://slack.com/api/users.list?token=hdbsgf46876534 + 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 04:50:49 GMT + Server: + - Apache + X-Slack-Req-Id: + - 7650c228a0c2397c0c38fd516407bceb + 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-emc9,haproxy-edge-pdx-mprq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 04:50:49 GMT +recorded_with: VCR 6.0.0 diff --git a/test/user_test.rb b/test/user_test.rb index 612e13fa..c0d05dda 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -88,3 +88,4 @@ + From 0b5e6f563322b8de7d924edd229d5c36a8f0f3a5 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Wed, 7 Oct 2020 21:56:32 -0700 Subject: [PATCH 21/36] working on wave 2 --- lib/channel.rb | 4 ---- lib/slack.rb | 2 ++ lib/user.rb | 4 ---- lib/workspace.rb | 12 ++++++++++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index bd6186a3..2d784e7a 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -26,10 +26,6 @@ def self.list_all return channels_list end - def select_channel - #supply name or id to select channel - end - private def self.from_api(recipient) diff --git a/lib/slack.rb b/lib/slack.rb index f178f3fc..3ddfb642 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -32,6 +32,8 @@ def main end puts "Thank you for using the Ada Slack CLI" + + end main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index edadcd20..848626b2 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -27,10 +27,6 @@ def self.list_all return users_list end - def select_user - #supply name or id to select user - end - private def self.from_api(recipient) diff --git a/lib/workspace.rb b/lib/workspace.rb index 1bf4ed0b..f92750e2 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -15,6 +15,18 @@ def initialize @channels = Channel.list_all end + def select_user(input) + @users.find do |user| + if input == user.id + selected_user = user + elsif input == user.name + selected_user = user + end + + end + return selected_user + end + end From 6cbcc79a07e2a4c248cd4db7d19428735b6a6b5a Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 00:33:26 -0700 Subject: [PATCH 22/36] made some changes --- lib/slack.rb | 36 ++++++++++++++++++++++++++++++------ lib/workspace.rb | 7 ++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 3ddfb642..49e1eafa 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -18,13 +18,25 @@ def main user_input = gets.chomp.downcase - if user_input == "list users" + case user_input + when "list users" pp workspace.users - elsif user_input == "list channels" + when "list channels" pp workspace.channels - # elsif user_input == "select user" - #select_user if no user corresponds, print message and return to main command loop - elsif user_input == "quit" + when "select user" + selected_item = workspace.select_user + when "select channel" + selected_item = workspace.select_channel + when "details" + # pp selected_item.details + if selected_item == selected_user #not how this works probably? + pp selected_user.details # ? + elsif selected_item == selected_channel + pp selected_item.details + else + puts "A channel or user has not been selected to show details" + end + when "quit" program_running = false else puts "Invalid choice! Please try again!" @@ -44,4 +56,16 @@ def main # } # response = HTTParty.get(URL, query: query_params ) # -# pp response["channels"] \ No newline at end of file +# pp response["channels"] +# +# if user_input == "list users" +# pp workspace.users +# elsif user_input == "list channels" +# pp workspace.channels +# elsif user_input == "select user" +# #select_user if no user corresponds, print message and return to main command loop +# elsif "quit" +# program_running = false +# else +# puts "Invalid choice! Please try again!" +# end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index f92750e2..d3dd4e44 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -16,15 +16,20 @@ def initialize end def select_user(input) + selected_user = nil @users.find do |user| if input == user.id selected_user = user elsif input == user.name selected_user = user + end end + if selected_user == nil + puts "Hmm, there isn't a user that matches that name or ID" + else + return selected_user end - return selected_user end end From 15bc92fea5e9ee5e3ad5970e4b8b24af839f584e Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 09:56:16 -0700 Subject: [PATCH 23/36] Added a select_channel method --- lib/workspace.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/workspace.rb b/lib/workspace.rb index d3dd4e44..fcdd593c 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -32,6 +32,13 @@ def select_user(input) end end + def select_channel(input) + selected = @channels.find { |channel| channel.slack_id.upcase == user_input.upcase || channel.name.upcase == user_input.upcase} + + return selected + end + end + end From 0351ca29c8312028936883cdb911d4589b354486 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 09:58:18 -0700 Subject: [PATCH 24/36] removed extra 'end' syntax --- lib/workspace.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index fcdd593c..65b95a05 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -37,7 +37,6 @@ def select_channel(input) return selected end - end end From e32cb06c68970639a9f22862dd86584e89fb65fd Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 10:00:27 -0700 Subject: [PATCH 25/36] fixed an error --- lib/workspace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 65b95a05..46ebc37a 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -33,7 +33,7 @@ def select_user(input) end def select_channel(input) - selected = @channels.find { |channel| channel.slack_id.upcase == user_input.upcase || channel.name.upcase == user_input.upcase} + selected = @channels.find { |channel| channel.slack_id.upcase == input.upcase || channel.name.upcase == input.upcase} return selected end From 39bd27aa48c94ab1a1728ce4a6cd9db409cfe602 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 10:05:10 -0700 Subject: [PATCH 26/36] user find method --- lib/workspace.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 46ebc37a..5405045b 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -16,20 +16,9 @@ def initialize end def select_user(input) - selected_user = nil - @users.find do |user| - if input == user.id - selected_user = user - elsif input == user.name - selected_user = user - end - end - - if selected_user == nil - puts "Hmm, there isn't a user that matches that name or ID" - else - return selected_user - end + selected = @users.find { |user| user.slack_id.upcase == input.upcase || user.name.upcase == input.upcase} + + return selected end def select_channel(input) From b5669786e8182215eda292bc91f351cad4f13cf5 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 10:55:33 -0700 Subject: [PATCH 27/36] command loop working for wave 2 --- lib/slack.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 49e1eafa..b7086640 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -24,14 +24,15 @@ def main when "list channels" pp workspace.channels when "select user" - selected_item = workspace.select_user + puts "User name or ID?" + user = gets.chomp + selected_item = workspace.select_user(user) when "select channel" - selected_item = workspace.select_channel + puts "Name or ID?" + channel = gets.chomp + selected_item = workspace.select_channel(channel) when "details" - # pp selected_item.details - if selected_item == selected_user #not how this works probably? - pp selected_user.details # ? - elsif selected_item == selected_channel + if selected_item != nil pp selected_item.details else puts "A channel or user has not been selected to show details" From 7f03131aec4388f3ae2ae173e8b61905de5754a9 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 10:59:58 -0700 Subject: [PATCH 28/36] made changes to the from api method for channel --- lib/channel.rb | 10 +++++++--- lib/recipient.rb | 4 ---- lib/user.rb | 16 +--------------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 72628c91..09a94473 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -29,10 +29,16 @@ def self.list_all private def self.from_api(recipient) + topic = { + "value"=>recipient["topic"]["value"], + "creater"=>recipient["topic"]["creator"], + "last_set"=>recipient["topic"]["last_set"] + } + return new( slack_id: recipient["id"], name: recipient["name"], - topic: recipient["topic"], + topic: topic, member_count: recipient["num_members"] ) end @@ -40,5 +46,3 @@ def self.from_api(recipient) end -pp Channel.list_all - diff --git a/lib/recipient.rb b/lib/recipient.rb index 6dbe2383..0a5a5bce 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -41,7 +41,3 @@ def self.from_api(recipient) end - -# response = Recipient.get("https://slack.com/api/users.list", {token: "xoxb-1408315767845-1411423361794-i5lKCPEvKCqqfU"}) -# -# pp response["error"] \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index 20891426..ed2a4dbb 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -42,20 +42,6 @@ def self.from_api(recipient) end -# user = User.new( -# slack_id: "U015QQ2BXFZ", -# name: "sarah", -# real_name: "Sarah Wilson", -# status_text: "feeling tired today", -# status_emoji: ":she:") -# -# -# pp User.list_all.first.slack_id -# pp User.list_all.first.name -# pp User.list_all.first.status_emoji -# pp User.list_all.first.status_text - - -pp User.list_all + From 147f03b4ce96286ebe15dba9e77a31d8a03af44d Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 11:01:28 -0700 Subject: [PATCH 29/36] made changes to channel tests and added more tests --- lib/user.rb | 2 +- test/cassettes/list_all_channels.yml | 125 +++++++++++++++++++++++++++ test/channel_test.rb | 89 ++++++++++--------- 3 files changed, 174 insertions(+), 42 deletions(-) create mode 100644 test/cassettes/list_all_channels.yml diff --git a/lib/user.rb b/lib/user.rb index ed2a4dbb..ebb88e31 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,7 +11,7 @@ def initialize(slack_id:, name:, real_name:, status_text:, status_emoji:) end def details - "Slack ID: #{@slack_id}\nName: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}" + "Slack ID: #{@slack_id}\nUsername: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}" end def self.list_all diff --git a/test/cassettes/list_all_channels.yml b/test/cassettes/list_all_channels.yml new file mode 100644 index 00000000..01cf7111 --- /dev/null +++ b/test/cassettes/list_all_channels.yml @@ -0,0 +1,125 @@ +--- +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 18:00:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 26a9d905555b84e74744ec75aabf31de + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim: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: + - '637' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-a5rt,haproxy-edge-pdx-5d11 + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"C01C099NZ9B","name":"random","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"random","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2},{"id":"C01C9GQEAUU","name":"project","is_channel":true,"is_group":false,"is_im":false,"created":1602026376,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"project","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026376},"previous_names":[],"num_members":2},{"id":"C01CG102ZRP","name":"general","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":true,"unlinked":0,"name_normalized":"general","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 18:00:57 GMT +- request: + method: get + uri: https://slack.com/api/conversations.list?token=hdbsgf46876534 + 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 18:00:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 32a6b2b28f5a007c1e17ff51e552f23c + 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-d35h,haproxy-edge-pdx-raf5 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 18:00:57 GMT +recorded_with: VCR 6.0.0 diff --git a/test/channel_test.rb b/test/channel_test.rb index 223aaea2..ce2102a9 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -1,13 +1,18 @@ require_relative 'test_helper' - describe "Channel Class" do describe "Channel Instantiation" do before do + @topic = { + "value"=>"plants", + "creator"=>"taylor", + "last_set"=>1 + } + @channel = Channel.new( slack_id: "U015QQ2BXF", name: "plants_and_gardening", - topic: {"value"=>"plants", "creator"=>"taylor", "last_set"=>1}, + topic: @topic, member_count: 2 ) end @@ -24,7 +29,7 @@ expect(@channel.name).must_equal "plants_and_gardening" expect(@channel).must_respond_to :topic - # expect(@channel.topic).must_equal {"value"=>"plants", "creator"=>"taylor", "last_set"=>1} + expect(@channel.topic).must_equal @topic expect(@channel).must_respond_to :member_count expect(@channel.member_count).must_equal 2 @@ -39,44 +44,46 @@ end - # describe "list_all" do - # it "lists all channels" do - # VCR.use_cassette("list_all channels") do - # - # response = Channel.list_all - # - # expect(response).must_be_kind_of Array - # expect(response.first).must_be_instance_of Channel - # expect(response.length).must_equal 3 - # end - # end - # - # it "correctly list the attributes of the first channel" do - # VCR.use_cassette("list_all") do - # - # response = Channel.list_all - # - # first_channel = response.first - # - # expect(first_channel.slack_id).must_equal "C01C099NZ9B" - # expect(first_channel.name).must_equal "random" - # expect(first_channel.topic).must_equal { value: "", creator: "", last_set: 0 } - # expect(first_channel.member_count).must_equal 2 - # end - # end - # - # describe "get method" do - # it "raises an error for invalid token" do - # VCR.use_cassette("list_all") do - # url = "https://slack.com/api/users.list" - # query_params = {token: "hdbsgf46876534"} - # - # expect{Channel.get(url, query_params)}.must_raise SlackTokenError - # end - # end - # end - # - # end + describe "list_all" do + it "lists all channels" do + VCR.use_cassette("list_all channels") do + + response = Channel.list_all + + expect(response).must_be_kind_of Array + expect(response.first).must_be_instance_of Channel + expect(response.length).must_equal 3 + end + end + + it "correctly list the attributes of the first channel" do + VCR.use_cassette("list_all channels") do + + response = Channel.list_all + + first_channel = response.first + + first_topic = {"value"=>"", "creater"=>"", "last_set"=>0} + + expect(first_channel.slack_id).must_equal "C01C099NZ9B" + expect(first_channel.name).must_equal "random" + expect(first_channel.topic).must_equal first_topic + expect(first_channel.member_count).must_equal 2 + end + end + + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all channels") do + url = "https://slack.com/api/conversations.list" + query_params = {token: "hdbsgf46876534"} + + expect{Channel.get(url, query_params)}.must_raise SlackTokenError + end + end + end + + end end From 0d50faac72468221dbaab77db8f1a3a7815c39b8 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 11:03:13 -0700 Subject: [PATCH 30/36] added tests for recipient class --- test/recipient_test.rb | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/recipient_test.rb diff --git a/test/recipient_test.rb b/test/recipient_test.rb new file mode 100644 index 00000000..e8b4c04e --- /dev/null +++ b/test/recipient_test.rb @@ -0,0 +1,52 @@ +require_relative 'test_helper' + +describe "Recipient Class" do + before do + @recipient = Recipient.new("HD336D", "bobby") + end + + describe "Recipient Instantiation" do + it "is an instance of Recipient" do + expect(@recipient).must_be_kind_of Recipient + end + + it "Takes in Recipient info" do + expect(@recipient).must_respond_to :slack_id + expect(@recipient.slack_id).must_equal "HD336D" + + expect(@recipient).must_respond_to :name + expect(@recipient.name).must_equal "bobby" + end + + it "all of the attributes are correct datatypes" do + expect(@recipient.slack_id).must_be_kind_of String + expect(@recipient.name).must_be_kind_of String + end + + end + + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all channels") do + url = "https://slack.com/api/conversations.list" + query_params = {token: "hdbsgf46876534"} + + expect{Recipient.get(url, query_params)}.must_raise SlackTokenError + end + end + end + + describe "details method" do + it "raises error if invoked through a recipient instance" do + expect{@recipient.details}.must_raise NotImplementedError + end + end + + describe "list all method" do + it "raises error if invoked through a Recipient Class" do + expect{Recipient.list_all}.must_raise NotImplementedError + end + end + + +end \ No newline at end of file From 980bcd9a048b85bd099b3e8066076b8e8076e5ff Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 11:04:54 -0700 Subject: [PATCH 31/36] made a syntax change --- lib/channel.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/channel.rb b/lib/channel.rb index 09a94473..a50b09de 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -46,3 +46,4 @@ def self.from_api(recipient) end + From 3b5efdf7457eebb8c32969fe35d74ebd7ac1da6e Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 12:39:38 -0700 Subject: [PATCH 32/36] starting wave 3 --- lib/slack.rb | 17 ++++++++++++++++- lib/workspace.rb | 18 ++++++++++++++++++ test/channel_test.rb | 2 +- test/recipient_test.rb | 7 +++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index b7086640..2ef79162 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,7 +5,7 @@ def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - choices = ["list users", "list channels", "select user", "select channel", "details", "quit"] + choices = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"] program_running = true @@ -27,16 +27,31 @@ def main puts "User name or ID?" user = gets.chomp selected_item = workspace.select_user(user) + if selected_item == nil + puts "Hmm, no user matches that name or ID" + end when "select channel" puts "Name or ID?" channel = gets.chomp selected_item = workspace.select_channel(channel) + if selected_item == nil + puts "Hmm, no channel matches that name or ID" + end when "details" if selected_item != nil pp selected_item.details else puts "A channel or user has not been selected to show details" end + when "send message" + puts "Name or ID of channel?" + channel = gets.chomp + selected_channel = workspace.select_channel(channel) + unless selected_channel == nil + puts "What's your message?" + text = gets.chomp + end + workspace.send_message(selected_channel, text) when "quit" program_running = false else diff --git a/lib/workspace.rb b/lib/workspace.rb index 5405045b..496ce727 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -6,6 +6,8 @@ Dotenv.load +class SlackApiError < StandardError; end + class Workspace attr_reader :users, :channels @@ -19,6 +21,7 @@ def select_user(input) selected = @users.find { |user| user.slack_id.upcase == input.upcase || user.name.upcase == input.upcase} return selected + end def select_channel(input) @@ -27,6 +30,21 @@ def select_channel(input) return selected end + def send_message(selected_channel, text) + url = "https://slack.com/api/chat.postMessage" + response = HTTParty.post(url, + body: { + token: ENV["SLACK_TOKEN"], + text: text, + channel: selected_channel + }, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } + ) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, "Error: #{response.parsed_response["error"]}" + end + end + end diff --git a/test/channel_test.rb b/test/channel_test.rb index ce2102a9..cb521013 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -5,7 +5,7 @@ before do @topic = { "value"=>"plants", - "creator"=>"taylor", + "creator"=>"slackbot", "last_set"=>1 } diff --git a/test/recipient_test.rb b/test/recipient_test.rb index e8b4c04e..8086256b 100644 --- a/test/recipient_test.rb +++ b/test/recipient_test.rb @@ -48,5 +48,12 @@ end end + describe "send message" do + it "is able to send a message" do + VCR.use_cassette("send message") + + + end + end end \ No newline at end of file From 804f7bfeac2b92c075e0714bf1713b87b0b0491e Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 12:44:06 -0700 Subject: [PATCH 33/36] added tests for Workspace class --- test/cassettes/users_and_channels.yml | 146 ++++++++++++++++++++++++++ test/test_helper.rb | 1 + test/workspace_test.rb | 85 +++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 test/cassettes/users_and_channels.yml create mode 100644 test/workspace_test.rb diff --git a/test/cassettes/users_and_channels.yml b/test/cassettes/users_and_channels.yml new file mode 100644 index 00000000..f43aa09a --- /dev/null +++ b/test/cassettes/users_and_channels.yml @@ -0,0 +1,146 @@ +--- +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: + - Thu, 08 Oct 2020 19:42:53 GMT + Server: + - Apache + X-Slack-Req-Id: + - c8489d248a3e0c7bcdb5dda4cb8f756c + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim: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: + - '1305' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-kg4q,haproxy-edge-pdx-dhzt + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","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":"T01C099NKQV"},"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":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth + - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight + Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth + - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack + CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack + CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina + Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina + Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor + Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor + Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor + Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"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":1602026592}],"cache_ts":1602186173,"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 19:42:54 GMT +- 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 19:42:54 GMT + Server: + - Apache + X-Slack-Req-Id: + - 351ab97d2fda6b18c9314ee048380b62 + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim: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: + - '637' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-n0tv,haproxy-edge-pdx-r6b3 + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"C01C099NZ9B","name":"random","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"random","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2},{"id":"C01C9GQEAUU","name":"project","is_channel":true,"is_group":false,"is_im":false,"created":1602026376,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"project","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026376},"previous_names":[],"num_members":2},{"id":"C01CG102ZRP","name":"general","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":true,"unlinked":0,"name_normalized":"general","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"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":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 19:42:54 GMT +recorded_with: VCR 6.0.0 diff --git a/test/test_helper.rb b/test/test_helper.rb index 1420393e..6d19559e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,6 +16,7 @@ require_relative '../lib/user' require_relative '../lib/channel' require_relative '../lib/recipient' +require_relative '../lib/workspace' VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..16c8928c --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,85 @@ +require_relative 'test_helper' + +describe "Workspace Class" do + before do + VCR.use_cassette("users and channels") do + @workspace = Workspace.new + end + end + + describe "Workspace Instantiation"do + it "is an instance of Workspace" do + expect(@workspace).must_be_kind_of Workspace + end + + it "be able to read the attributes" do + expect(@workspace).must_respond_to :users + expect(@workspace).must_respond_to :channels + end + + it "all of the attributes are correct datatypes" do + expect(@workspace.users).must_be_kind_of Array + expect(@workspace.channels).must_be_kind_of Array + end + + it "can access the first user and channel" do + first_user = @workspace.users.first + + expect(first_user).must_be_kind_of User + expect(first_user.slack_id).must_equal "USLACKBOT" + expect(first_user.name).must_equal "slackbot" + expect(first_user.real_name).must_equal "Slackbot" + expect(first_user.status_text).must_equal "" + expect(first_user.status_emoji).must_equal "" + + end + + it "can access the first channel" do + first_channel = @workspace.channels.first + first_topic = {"value"=>"", "creater"=>"", "last_set"=>0} + + expect(first_channel).must_be_kind_of Channel + expect(first_channel.slack_id).must_equal "C01C099NZ9B" + expect(first_channel.name).must_equal "random" + expect(first_channel.topic).must_equal first_topic + expect(first_channel.member_count).must_equal 2 + + end + + end + + describe "select user" do + it "correctly selects the user" do + user = @workspace.select_user("USLACKBOT") + expect(user.slack_id).must_equal "USLACKBOT" + end + + it "returns nil if the user is not found" do + user = @workspace.select_user("Bobby") + expect(user).must_be_nil + end + + it "the argument should be case insensitive" do + user = @workspace.select_user("UslackBOT") + expect(user.slack_id).must_equal "USLACKBOT" + end + end + + describe "select channel" do + it "correctly selects the channel" do + channel = @workspace.select_channel("random") + expect(channel.name).must_equal "random" + end + + it "returns nil if the user is not found" do + channel = @workspace.select_channel("plants") + expect(channel).must_be_nil + end + + it "the argument should be case insensitive" do + channel = @workspace.select_channel("RANdom") + expect(channel.name).must_equal "random" + end + end + +end \ No newline at end of file From 95c727fe30f76fb6fb90eb3542f33922120620d0 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 14:05:54 -0700 Subject: [PATCH 34/36] workspace tests mostly passing --- lib/slack.rb | 12 ++- lib/workspace.rb | 7 +- test/cassettes/send_message.yml | 70 ++++++++++++++ test/cassettes/send_message_error.yml | 133 ++++++++++++++++++++++++++ test/recipient_test.rb | 6 -- test/workspace_test.rb | 21 +++- 6 files changed, 234 insertions(+), 15 deletions(-) create mode 100644 test/cassettes/send_message.yml create mode 100644 test/cassettes/send_message_error.yml diff --git a/lib/slack.rb b/lib/slack.rb index 2ef79162..9ded5ed6 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -44,14 +44,16 @@ def main puts "A channel or user has not been selected to show details" end when "send message" - puts "Name or ID of channel?" - channel = gets.chomp - selected_channel = workspace.select_channel(channel) - unless selected_channel == nil + # puts "Name or ID of channel?" + # channel = gets.chomp + # selected_channel = workspace.select_channel(channel) + if selected_item != nil puts "What's your message?" text = gets.chomp + else + puts "A channel or user has not been selected to send message" end - workspace.send_message(selected_channel, text) + workspace.send_message(selected_item, text) when "quit" program_running = false else diff --git a/lib/workspace.rb b/lib/workspace.rb index 496ce727..a8e6d89c 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -30,19 +30,20 @@ def select_channel(input) return selected end - def send_message(selected_channel, text) + def send_message(selected_item, text) url = "https://slack.com/api/chat.postMessage" response = HTTParty.post(url, body: { token: ENV["SLACK_TOKEN"], text: text, - channel: selected_channel + channel: selected_item }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } ) - unless response.code == 200 && response.parsed_response["ok"] + unless response.code == 200 || response.parsed_response["ok"] raise SlackApiError, "Error: #{response.parsed_response["error"]}" end + return true end end diff --git a/test/cassettes/send_message.yml b/test/cassettes/send_message.yml new file mode 100644 index 00000000..b3710009 --- /dev/null +++ b/test/cassettes/send_message.yml @@ -0,0 +1,70 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel=C01C099NZ9B + headers: + Content-Type: + - application/x-www-form-urlencoded + 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 20:46:59 GMT + Server: + - Apache + X-Slack-Req-Id: + - 38034521788f37ec584c215c6d2e3b86 + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + 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: + - '326' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-cn1z,haproxy-edge-pdx-8nlt + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":"C01C099NZ9B","ts":"1602190019.000100","message":{"bot_id":"B01C9H7MZS8","type":"message","text":"test + message","user":"U01C3CG762E","ts":"1602190019.000100","team":"T01C099NKQV","bot_profile":{"id":"B01C9H7MZS8","deleted":false,"name":"Slack + CLI project","updated":1602027119,"app_id":"A01BNKYQEET","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":"T01C099NKQV"}}}' + recorded_at: Thu, 08 Oct 2020 20:46:59 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/send_message_error.yml b/test/cassettes/send_message_error.yml new file mode 100644 index 00000000..351f6f7a --- /dev/null +++ b/test/cassettes/send_message_error.yml @@ -0,0 +1,133 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel=bogus + headers: + Content-Type: + - application/x-www-form-urlencoded + 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 20:43:01 GMT + Server: + - Apache + X-Slack-Req-Id: + - a90336d918ddaace5fb79748af13136b + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + 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-6rku,haproxy-edge-pdx-bahq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"channel_not_found"}' + recorded_at: Thu, 08 Oct 2020 20:43:01 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel= + headers: + Content-Type: + - application/x-www-form-urlencoded + 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 20:43:27 GMT + Server: + - Apache + X-Slack-Req-Id: + - 8e5011a21edfae6119dc39f156afd07d + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + 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-1obv,haproxy-edge-pdx-xlh0 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"channel_not_found"}' + recorded_at: Thu, 08 Oct 2020 20:43:28 GMT +recorded_with: VCR 6.0.0 diff --git a/test/recipient_test.rb b/test/recipient_test.rb index 8086256b..0ab20640 100644 --- a/test/recipient_test.rb +++ b/test/recipient_test.rb @@ -48,12 +48,6 @@ end end - describe "send message" do - it "is able to send a message" do - VCR.use_cassette("send message") - end - end - end \ No newline at end of file diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 16c8928c..d782ccdc 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -82,4 +82,23 @@ end end -end \ No newline at end of file + describe "send message" do + it "is able to send a message" do + VCR.use_cassette("send message") do + answer = @workspace.send_message("C01C099NZ9B", "test message") + expect(answer).must_equal true + end + end + #failing + it "raises an error for invalid channel" do + VCR.use_cassette("send message error") do + expect{ + answer = @workspace.send_message("bogus", "test message") + }.must_raise SlackApiError + end + end + end + +end + + From ab7f1dfe7e036bd61485e16dda449a29891e1d2b Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Thu, 8 Oct 2020 14:54:14 -0700 Subject: [PATCH 35/36] tests failing for send message --- lib/workspace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index a8e6d89c..4bcf571f 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -40,7 +40,7 @@ def send_message(selected_item, text) }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } ) - unless response.code == 200 || response.parsed_response["ok"] + unless response.parsed_response["ok"] raise SlackApiError, "Error: #{response.parsed_response["error"]}" end return true From b8bb0d07bba4659642cae5c60fde50250e7dda26 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Thu, 8 Oct 2020 15:30:29 -0700 Subject: [PATCH 36/36] fixed errors in slack.rb and workspace test --- lib/slack.rb | 43 +++++++++++++++--------------------------- lib/workspace.rb | 3 ++- test/workspace_test.rb | 4 ++-- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 9ded5ed6..3d11ce0f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,8 @@ #!/usr/bin/env ruby require_relative 'workspace' +require 'table_print' + def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new @@ -20,9 +22,13 @@ def main case user_input when "list users" - pp workspace.users + puts "" + tp workspace.users, :slack_id, :name, :real_name, :status_text, :status_emoji + puts "" when "list channels" - pp workspace.channels + puts "" + tp workspace.channels, :slack_id, :name, :topic, :member_count + puts "" when "select user" puts "User name or ID?" user = gets.chomp @@ -39,21 +45,23 @@ def main end when "details" if selected_item != nil - pp selected_item.details + puts "" + puts selected_item.details + puts "" else puts "A channel or user has not been selected to show details" end when "send message" - # puts "Name or ID of channel?" - # channel = gets.chomp - # selected_channel = workspace.select_channel(channel) + if selected_item != nil puts "What's your message?" text = gets.chomp + + workspace.send_message(selected_item.name, text) else puts "A channel or user has not been selected to send message" end - workspace.send_message(selected_item, text) + when "quit" program_running = false else @@ -63,27 +71,6 @@ def main puts "Thank you for using the Ada Slack CLI" - end main if __FILE__ == $PROGRAM_NAME - -# URL = "https://slack.com/api/conversations.list" -# query_params = { -# token: ENV["SLACK_TOKEN"] -# } -# response = HTTParty.get(URL, query: query_params ) -# -# pp response["channels"] -# -# if user_input == "list users" -# pp workspace.users -# elsif user_input == "list channels" -# pp workspace.channels -# elsif user_input == "select user" -# #select_user if no user corresponds, print message and return to main command loop -# elsif "quit" -# program_running = false -# else -# puts "Invalid choice! Please try again!" -# end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index 4bcf571f..cb69034d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -40,9 +40,10 @@ def send_message(selected_item, text) }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } ) - unless response.parsed_response["ok"] + unless response.parsed_response["ok"] == true raise SlackApiError, "Error: #{response.parsed_response["error"]}" end + return true end diff --git a/test/workspace_test.rb b/test/workspace_test.rb index d782ccdc..fff48342 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -89,11 +89,11 @@ expect(answer).must_equal true end end - #failing + it "raises an error for invalid channel" do VCR.use_cassette("send message error") do expect{ - answer = @workspace.send_message("bogus", "test message") + @workspace.send_message("bogus", "test message") }.must_raise SlackApiError end end