From 8f8b84e532ca16f32cfb32be59efd9e0e86dc923 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Tue, 6 Oct 2020 11:32:20 -0700 Subject: [PATCH 1/7] Add coverage directory to .gitignore file --- .gitignore | 1 + lib/channel.rb | 1 + lib/recipient.rb | 1 + lib/user.rb | 1 + lib/workspace.rb | 1 + test/slack_test.rb | 1 + 6 files changed, 6 insertions(+) create mode 100644 lib/channel.rb create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 lib/workspace.rb create mode 100644 test/slack_test.rb diff --git a/.gitignore b/.gitignore index 3ff4fada..ba5c552f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ # Ignore environemnt variables .env +coverage diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1 @@ + diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1 @@ + diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1 @@ + diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1 @@ + diff --git a/test/slack_test.rb b/test/slack_test.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/slack_test.rb @@ -0,0 +1 @@ + From fbb425c2bee2e3e69ae7af0d911f79efdd07eb9c Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Tue, 6 Oct 2020 17:37:57 -0700 Subject: [PATCH 2/7] setup complete --- lib/channel.rb | 15 +++++++++++++++ lib/recipient.rb | 10 ++++++++++ lib/slack.rb | 21 ++++++++++++++++++++- lib/user.rb | 25 +++++++++++++++++++++++++ lib/workspace.rb | 25 +++++++++++++++++++++++++ test/fake_test.rb | 27 +++++++++++++++++++++++++++ test/slack_test.rb | 1 - test/test_helper.rb | 10 ++++------ 8 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 test/fake_test.rb delete mode 100644 test/slack_test.rb diff --git a/lib/channel.rb b/lib/channel.rb index 8b137891..cb3fdce2 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1 +1,16 @@ +class Channel < Recipient + def self.list_all + response = HTTParty.get('https://slack.com/api/channels.list', query: { + token: ENV['SLACK_API_TOKEN'] + }) + + pp response + + users = response["members"] + + users.each do |user| + p user["name"] + end + end +end diff --git a/lib/recipient.rb b/lib/recipient.rb index 8b137891..593eb4d2 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1 +1,11 @@ +class Recipient + + attr_reader :name, slack_id + + def initialize(name, slack_id) + @name = name + @slack_id = slack_id + end + +end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..ba57ef27 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,10 +1,29 @@ #!/usr/bin/env ruby +# Use the dotenv gem to load environment variables +# Use HTTParty to send a GET request to the channels.list endpoint +# Check that the request completed successfully, and print relevant information to the console if it didn't +# Loop through the results and print out the name of each channel + +require 'prettyprint' +require 'awesome_print' +require 'httparty' +require 'dotenv' +Dotenv.load + +require_relative 'workspace' + def main puts "Welcome to the Ada Slack CLI!" + # TODO project + workspace = Workspace.new - # TODO project + puts ENV['SLACK_API_TOKEN'] + + response = HTTParty.get('https://slack.com/api/users.list', query: { + token: ENV['SLACK_API_TOKEN'] + }) puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index 8b137891..45292e24 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1 +1,26 @@ +# user.rb +require 'httparty' +require 'prettyprint' +require 'dotenv' +Dotenv.load + +class User < Recipient + + def initialize(real_name,status_text, status_emoji) + super(slack_id,name) + end + + + def self.list_all + response = HTTParty.get('https://slack.com/api/users.list', query: { + token: ENV['SLACK_API_TOKEN'] + }) + + users = response["members"] + + users.each do |user| + p user["name"] + end + end +end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index 8b137891..725b8270 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1 +1,26 @@ +class Workspace + attr_reader :users,:channels + + def initialize + @users = [] + @channels = [] + end + + def select_channel + + end + + def select_user + + end + + def show_details + + end + + def send_message + + end + +end diff --git a/test/fake_test.rb b/test/fake_test.rb new file mode 100644 index 00000000..41e20fce --- /dev/null +++ b/test/fake_test.rb @@ -0,0 +1,27 @@ +require_relative 'test_helper' +require_relative '../lib/slack' + + +# Example tests from Location IQ +describe "get_location" do + it "can find a location" do + VCR.use_cassette("location_find") do + location = "Seattle" + response = get_location(location) + + expect(response["Seattle"]).wont_be_nil + expect(response["Seattle"][:lon]).must_equal "-122.3300624" + expect(response["Seattle"][:lat]).must_equal "47.6038321" + end + end + + it "will raise an exception if the search fails" do + VCR.use_cassette("location_find") do + location = "" + expect { + response = get_location(location) + }.must_raise SearchError + end + end +end + diff --git a/test/slack_test.rb b/test/slack_test.rb deleted file mode 100644 index 8b137891..00000000 --- a/test/slack_test.rb +++ /dev/null @@ -1 +0,0 @@ - diff --git a/test/test_helper.rb b/test/test_helper.rb index 1fcf2bab..3a2647cd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,12 +9,8 @@ require 'minitest/skip_dsl' require 'vcr' -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" - config.hook_into :webmock -end +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located @@ -25,5 +21,7 @@ } # Don't leave our token lying around in a cassette file. - + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end end From 8559d461a1c2a6ec4bf954bbffb856657efbaeed Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Wed, 7 Oct 2020 18:43:14 -0700 Subject: [PATCH 3/7] wave 1 complete --- lib/channel.rb | 38 ++++++++++++++++++++++++++++++-------- lib/recipient.rb | 14 +++++++++++--- lib/slack.rb | 27 +++++++++++++++++++-------- lib/user.rb | 27 +++++++++++++++++++-------- lib/workspace.rb | 36 +++++++++++++++++++++++++++++++++--- 5 files changed, 112 insertions(+), 30 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index cb3fdce2..cfdb7864 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,16 +1,38 @@ +require 'httparty' +require 'prettyprint' +require 'dotenv' + +require_relative 'recipient' + +Dotenv.load + + class Channel < Recipient - def self.list_all - response = HTTParty.get('https://slack.com/api/channels.list', query: { - token: ENV['SLACK_API_TOKEN'] - }) + attr_reader :topic,:member_count + + def initialize(slack_id, name, topic, member_count) + super(slack_id, name) + @topic = topic + @member_count = member_count + end - pp response + def self.list_all + channel_url = 'https://slack.com/api/conversations.list' + response = self.get(channel_url, query: { token: ENV['SLACK_API_TOKEN']}) - users = response["members"] + channels = response["channels"] - users.each do |user| - p user["name"] + list = [] + channels.each do |channel| + list << Channel.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"]) end + return list + end + + def information + return "\n channel name: #{self.name} \n topic: #{self.topic} \n user count: #{self.member_count} \n slack_id: #{self.slack_id}" end + end + diff --git a/lib/recipient.rb b/lib/recipient.rb index 593eb4d2..ad8e5fa5 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,11 +1,19 @@ +require 'httparty' +require 'prettyprint' +require 'dotenv' + +Dotenv.load class Recipient - attr_reader :name, slack_id + attr_reader :slack_id, :name - def initialize(name, slack_id) - @name = name + def initialize(slack_id, name) @slack_id = slack_id + @name = name end + def self.get(url, params) + return HTTParty.get(url, params) + end end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index ba57ef27..ed1ce662 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,17 +15,28 @@ def main puts "Welcome to the Ada Slack CLI!" - # TODO project workspace = Workspace.new - puts ENV['SLACK_API_TOKEN'] + until false + puts "\n" + puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." + puts "Please enter one of the following: list users, list channels, quit" + input = gets.chomp + + case input + when "list users" + puts workspace.list_users + when "list channels" + puts workspace.list_channels + when "quit" + puts "Thank you for using the Ada Slack CLI" + break + else + puts "please enter a valid input: list users, list channels, quit" + end - response = HTTParty.get('https://slack.com/api/users.list', query: { - token: ENV['SLACK_API_TOKEN'] - }) - - puts "Thank you for using the Ada Slack CLI" +end end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index 45292e24..fb60602d 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,26 +1,37 @@ -# user.rb require 'httparty' require 'prettyprint' require 'dotenv' +require_relative 'recipient' + Dotenv.load class User < Recipient - def initialize(real_name,status_text, status_emoji) - super(slack_id,name) - end + attr_reader :real_name, :status_text, :status_emoji + def initialize(slack_id, name, real_name, status_text, status_emoji) + super(slack_id, name) + @real_name = real_name + @status_text = status_text + @status_emoji = status_emoji + end def self.list_all - response = HTTParty.get('https://slack.com/api/users.list', query: { - token: ENV['SLACK_API_TOKEN'] - }) + user_url = 'https://slack.com/api/users.list' + response = self.get(user_url, query: { token: ENV['SLACK_API_TOKEN']}) users = response["members"] + list = [] users.each do |user| - p user["name"] + list << User.new(user["id"], user["name"], user["profile"]["real_name"], user["profile"]["status_text"], user["profile"]["status_emoji"]) end + return list end + + def information + return "#{self.name}, #{self.real_name}, #{self.slack_id}" + end + end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index 725b8270..d274f7bd 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,14 +1,40 @@ +require 'httparty' +require 'dotenv' + + +require_relative 'user' +require_relative 'channel' + +Dotenv.load + class Workspace attr_reader :users,:channels def initialize - @users = [] - @channels = [] + @users = User.list_all + @channels = Channel.list_all end - def select_channel + def list_users + user_data = [] + @users.each do |user| + user_data << user.information + end + return user_data + end + def list_channels + channel_data = [] + @channels.each do |channel| + channel_data << channel.information + end + return channel_data + end + + def select_channel + @channels.find { |channel| channel["name"] = name } + end end def select_user @@ -23,4 +49,8 @@ def send_message end + def list_formatted + + end + end From c2347fad5a2982acb4d2b3539f476007dc11ad79 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Wed, 7 Oct 2020 20:09:24 -0700 Subject: [PATCH 4/7] wave 2 complete --- lib/channel.rb | 2 -- lib/slack.rb | 12 +++++++++++- lib/workspace.rb | 23 ++++++++++++----------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index cfdb7864..d86efbd3 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -6,7 +6,6 @@ Dotenv.load - class Channel < Recipient attr_reader :topic,:member_count @@ -16,7 +15,6 @@ def initialize(slack_id, name, topic, member_count) @topic = topic @member_count = member_count end - def self.list_all channel_url = 'https://slack.com/api/conversations.list' response = self.get(channel_url, query: { token: ENV['SLACK_API_TOKEN']}) diff --git a/lib/slack.rb b/lib/slack.rb index ed1ce662..67d0dd5d 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -21,7 +21,7 @@ def main until false puts "\n" puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." - puts "Please enter one of the following: list users, list channels, quit" + puts "Please enter one of the following: list users, list channels, select user, select channel, quit. Enter show details for more info when a user or channel is selected." input = gets.chomp case input @@ -29,6 +29,16 @@ def main puts workspace.list_users when "list channels" puts workspace.list_channels + when "select channel" + input = gets.chomp + workspace.select_channel(input) + puts "selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + when "select user" + input = gets.chomp + workspace.select_user(input) + puts "selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + when "show details" + puts workspace.show_details when "quit" puts "Thank you for using the Ada Slack CLI" break diff --git a/lib/workspace.rb b/lib/workspace.rb index d274f7bd..f3cebb4d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,11 +9,12 @@ class Workspace - attr_reader :users,:channels + attr_reader :users,:channels,:selected def initialize @users = User.list_all @channels = Channel.list_all + @selected = nil end def list_users @@ -32,25 +33,25 @@ def list_channels return channel_data end - def select_channel - @channels.find { |channel| channel["name"] = name } - end + def select_channel(input) + @selected = @channels.find { |channel| input == channel.name || input == channel.slack_id} + raise ArgumentError.new("#{input} not found") if @selected == nil + return @selected end - def select_user - + def select_user(input) + @selected = @users.find { |user| input == user.name || input == user.slack_id} + raise ArgumentError.new("#{input} not found") if @selected == nil + return @selected end def show_details - + raise ArgumentError.new("no user or channel selected") if @selected == nil + return @selected.information end def send_message end - def list_formatted - - end - end From 610d568b8edc6c348124407ae5731ac5e769301c Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 8 Oct 2020 19:57:17 -0700 Subject: [PATCH 5/7] wave 3 complete --- lib/channel.rb | 3 +- lib/slack.rb | 13 ++++- lib/user.rb | 10 +++- lib/workspace.rb | 30 +++++++--- test/channel_test.rb | 25 +++++++++ test/fake_test.rb | 27 --------- test/test_helper.rb | 13 +++-- test/user_test.rb | 25 +++++++++ test/workspace_test.rb | 124 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 226 insertions(+), 44 deletions(-) create mode 100644 test/channel_test.rb delete mode 100644 test/fake_test.rb create mode 100644 test/user_test.rb create mode 100644 test/workspace_test.rb diff --git a/lib/channel.rb b/lib/channel.rb index d86efbd3..48a4be79 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -28,9 +28,10 @@ def self.list_all return list end - def information + def details return "\n channel name: #{self.name} \n topic: #{self.topic} \n user count: #{self.member_count} \n slack_id: #{self.slack_id}" end + end diff --git a/lib/slack.rb b/lib/slack.rb index 67d0dd5d..5c4adf8a 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -21,24 +21,31 @@ def main until false puts "\n" puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." - puts "Please enter one of the following: list users, list channels, select user, select channel, quit. Enter show details for more info when a user or channel is selected." + puts "Please enter one of the following: list users, list channels, select user, select channel, send a message, quit. Enter show details for more info when a user or channel is selected." input = gets.chomp + case input when "list users" puts workspace.list_users when "list channels" puts workspace.list_channels when "select channel" + puts "please enter a channel name or ID:" input = gets.chomp workspace.select_channel(input) - puts "selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts "\n selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" when "select user" + puts "please enter a username or ID:" input = gets.chomp workspace.select_user(input) - puts "selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts "\n selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" when "show details" puts workspace.show_details + when "send a message" + puts "What message would you like to send?" + message = gets.chomp.to_s + puts workspace.send_message(message) when "quit" puts "Thank you for using the Ada Slack CLI" break diff --git a/lib/user.rb b/lib/user.rb index fb60602d..3aa84ddf 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -21,6 +21,8 @@ def self.list_all user_url = 'https://slack.com/api/users.list' response = self.get(user_url, query: { token: ENV['SLACK_API_TOKEN']}) + raise ArgumentError.new("invalid request") if response["ok"] == false + users = response["members"] list = [] @@ -30,8 +32,12 @@ def self.list_all return list end - def information - return "#{self.name}, #{self.real_name}, #{self.slack_id}" + # def information + # return "#{self.name}, #{self.real_name}, #{self.slack_id}" + # end + + def details + return "\n username: #{self.name} \n real name: #{self.real_name} \n slack ID: #{self.slack_id} \n status: #{self.status_text} \n emoji: #{self.status_emoji}" end end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb index f3cebb4d..011c456a 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -7,6 +7,8 @@ Dotenv.load +class SlackApiError < StandardError; end + class Workspace attr_reader :users,:channels,:selected @@ -20,7 +22,7 @@ def initialize def list_users user_data = [] @users.each do |user| - user_data << user.information + user_data << user.details end return user_data end @@ -28,30 +30,44 @@ def list_users def list_channels channel_data = [] @channels.each do |channel| - channel_data << channel.information + channel_data << channel.details end return channel_data end def select_channel(input) @selected = @channels.find { |channel| input == channel.name || input == channel.slack_id} - raise ArgumentError.new("#{input} not found") if @selected == nil + # raise ArgumentError.new("#{input} not found") if @selected == nil return @selected end def select_user(input) @selected = @users.find { |user| input == user.name || input == user.slack_id} - raise ArgumentError.new("#{input} not found") if @selected == nil + # raise ArgumentError.new("#{input} not found") if @selected == nil return @selected end def show_details raise ArgumentError.new("no user or channel selected") if @selected == nil - return @selected.information + return @selected.details end - def send_message + def send_message(message) + + response = HTTParty.post( + "https://slack.com/api/chat.postMessage", + body: { + token: ENV['SLACK_API_TOKEN'], + text: message, + channel: @selected.slack_id + }) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, "Error when posting #{message} to #{@selected.name}, error: #{response.parsed_response["error"]}" + end + end end -end + +# response.code == 200 && + diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..84f41b90 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,25 @@ +require_relative 'test_helper' +require_relative '../lib/slack' + +describe "Channel class" do + describe "channel list_all" do + it "can call the API and receive a response" do + VCR.use_cassette("channel_instance") do + all_channels = Channel.list_all + + expect(all_channels).must_be_instance_of Array + + all_channels.each do |channel| + expect(channel).must_be_instance_of Channel + end + end + end + end +end + +# return nil for invalid input +# workspacehas list ofsers and channels +# canfind user by ID +# can find user by name +# canfind channel by ID +# can find channel name diff --git a/test/fake_test.rb b/test/fake_test.rb deleted file mode 100644 index 41e20fce..00000000 --- a/test/fake_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require_relative 'test_helper' -require_relative '../lib/slack' - - -# Example tests from Location IQ -describe "get_location" do - it "can find a location" do - VCR.use_cassette("location_find") do - location = "Seattle" - response = get_location(location) - - expect(response["Seattle"]).wont_be_nil - expect(response["Seattle"][:lon]).must_equal "-122.3300624" - expect(response["Seattle"][:lat]).must_equal "47.6038321" - end - end - - it "will raise an exception if the search fails" do - VCR.use_cassette("location_find") do - location = "" - expect { - response = get_location(location) - }.must_raise SearchError - end - end -end - diff --git a/test/test_helper.rb b/test/test_helper.rb index 3a2647cd..19d1ea12 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,14 +1,19 @@ -require 'simplecov' -SimpleCov.start do - add_filter 'test/' -end +# require 'simplecov' +# SimpleCov.start do +# add_filter 'test/' +# end +require 'dotenv' require 'minitest' require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require_relative "../lib/channel" +require_relative "../lib/user" +require_relative "../lib/workspace" + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..8cea0d93 --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,25 @@ +require_relative 'test_helper' +require_relative '../lib/slack' + +describe "User class" do + describe "user list_all" do + it "can call the API and receive a response" do + VCR.use_cassette("user_instance") do + all_users = User.list_all + + expect(all_users).must_be_instance_of Array + + all_users.each do |user| + expect(user).must_be_instance_of User + end + end + end + end + end + +# return nil for invalid input +# workspacehas list ofsers and channels +# canfind user by ID +# can find user by name +# canfind channel by ID +# can find channel name diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..f8d70f30 --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,124 @@ +require_relative 'test_helper' +require_relative '../lib/slack' + + +describe "Workspace" do + describe "can initialize a workspace" do + before do + VCR.use_cassette("workspace") do + @workspace = Workspace.new + end + end + + it "can initialize a workspace" do + + expect(@workspace).must_be_kind_of Workspace + expect(@workspace.users).must_be_kind_of Array + expect(@workspace.channels).must_be_kind_of Array + expect(@workspace.selected).must_be_nil + end + + it "initializes with user and channel objects" do + + expect(@workspace.users.first).must_be_kind_of User + expect(@workspace.channels.first).must_be_kind_of Channel + end +end + + describe "can list users and channels" do + before do + VCR.use_cassette("workspace_list_all") do + @workspace = Workspace.new + end + end + + it "can list all users" do + + expect(@workspace.list_users).must_be_kind_of Array + expect(@workspace.list_users.length).must_equal 6 + expect(@workspace.list_users.first).must_be_kind_of String + + end + + it "can list all channels" do + + expect(@workspace.list_channels).must_be_kind_of Array + expect(@workspace.list_channels.length).must_equal 4 + expect(@workspace.list_channels.first).must_be_kind_of String + + end + end + + describe "can select users or channels" do + before do + VCR.use_cassette("select_channels") do + @workspace = Workspace.new + end + end + + it "can select a user by name" do + user = @workspace.select_user("slackbot") + expect(user).must_be_instance_of User + expect(user.name).must_equal "slackbot" + + user = @workspace.select_user("mahaelmais") + expect(user).must_be_instance_of User + expect(user.name).must_equal "mahaelmais" + expect(user.slack_id).must_equal "U01CQGU5LKS" + end + + it "can select a user by ID" do + user = @workspace.select_user("U01BXJZPYAH") + expect(user).must_be_instance_of User + expect(user.name).must_equal "sophie.e.messing" + expect(user.slack_id).must_equal "U01BXJZPYAH" + + user = @workspace.select_user("U01CQHU8WBS") + expect(user).must_be_instance_of User + expect(user.name).must_equal "earth_mahaelmais_api_" + expect(user.slack_id).must_equal "U01CQHU8WBS" + end + + it "can select a channel by name" do + channel = @workspace.select_channel("random") + expect(channel).must_be_instance_of Channel + expect(channel.name).must_equal "random" + expect(channel.slack_id).must_equal "C01C0TJK6G3" + + end + + it "can select a channel by ID" do + channel = @workspace.select_channel("C01BXNGFDTP") + expect(channel).must_be_instance_of Channel + expect(channel.name).must_equal "test-channel" + expect(channel.slack_id).must_equal "C01BXNGFDTP" + + end + + it "will return nil for invalid user input" do + user = @workspace.select_user("xxx") + expect(user).must_be_nil + end + + it "will return nil for invalid channel input" do + user = @workspace.select_user("00001111") + expect(user).must_be_nil + end + + it "can show details for selected user" do + user = @workspace.select_user("mahaelmais") + user_details = @workspace.show_details + + expect(user_details).must_be_kind_of String + expect(user_details).must_include "U01CQGU5LKS" + end + + it "can show details for selected channel" do + channel = @workspace.select_channel("general") + channel_details = @workspace.show_details + + expect(channel_details).must_be_kind_of String + expect(channel_details).must_include "C01BTVCEXCN" + end + end + end From cc2cc7207f2fa91cb4c9462ffb81bcb9fb07a869 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Fri, 9 Oct 2020 11:58:34 -0700 Subject: [PATCH 6/7] better error handling for invalid entry in slack.rb --- lib/slack.rb | 112 ++++++++++++++++++++++++++++------------- lib/user.rb | 4 -- lib/workspace.rb | 35 +++++++------ test/channel_test.rb | 21 +++----- test/test_helper.rb | 4 +- test/user_test.rb | 11 +--- test/workspace_test.rb | 101 ++++++++++++++++++------------------- 7 files changed, 158 insertions(+), 130 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 5c4adf8a..dace0d6d 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -9,51 +9,95 @@ require 'awesome_print' require 'httparty' require 'dotenv' + Dotenv.load require_relative 'workspace' def main - puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new + puts "\nWelcome to the Ada Slack CLI!" + puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." + + statement = "\nPlease enter one of the following commands:\n• list users\n• list channels\n• select user\n• select channel\n• show details\n• send a message\n• quit\n" + until false + + puts statement puts "\n" - puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." - puts "Please enter one of the following: list users, list channels, select user, select channel, send a message, quit. Enter show details for more info when a user or channel is selected." - input = gets.chomp - - - case input - when "list users" - puts workspace.list_users - when "list channels" - puts workspace.list_channels - when "select channel" - puts "please enter a channel name or ID:" - input = gets.chomp - workspace.select_channel(input) - puts "\n selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" - when "select user" - puts "please enter a username or ID:" - input = gets.chomp - workspace.select_user(input) - puts "\n selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" - when "show details" - puts workspace.show_details - when "send a message" - puts "What message would you like to send?" - message = gets.chomp.to_s - puts workspace.send_message(message) - when "quit" - puts "Thank you for using the Ada Slack CLI" - break - else - puts "please enter a valid input: list users, list channels, quit" - end + input = gets.chomp.downcase + case input + when "list users" + puts workspace.list_users + when "list channels" + puts workspace.list_channels + + when "select channel" + puts "Please enter a channel name or ID:" + input = gets.chomp + workspace.select_channel(input) + until workspace.select_channel(input) != nil + puts "Invalid channel #{input}. Please try again." + input = gets.chomp + workspace.select_channel(input) + end + puts "\n Selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts " Enter #{"show details"} for more details on #{workspace.selected.name}" + + + when "select user" + puts "Please enter a username or ID:" + input = gets.chomp + workspace.select_user(input) + until workspace.select_user(input) != nil + puts "Invalid user #{input}. Please try again." + input = gets.chomp + workspace.select_user(input) + end + puts "\n Selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts " Enter #{"show details"} for more details on #{workspace.selected.name}" + + when "show details" + if workspace.selected == nil + puts "Details for whom? Please enter a user or channel" + input = gets.chomp + workspace.ask_to_select(input) + if workspace.ask_to_select(input) == nil + puts "Invalid input #{input}. Please try again." + input = gets.chomp + workspace.ask_to_select(input) + end + end + + puts workspace.show_details + + when "send a message" + if workspace.selected == nil + puts "To whom?" + input = gets.chomp + workspace.ask_to_select(input) + if workspace.ask_to_select(input) == nil + puts "Invalid input #{input}. Please try again." + input = gets.chomp + workspace.ask_to_select(input) + end + end + + puts "What message would you like to send?" + message = gets.chomp.to_s + puts workspace.send_message(message) + + when "quit" + puts "Thank you for using the Ada Slack CLI" + break + else + puts "Invalid input #{input}" + end + end end -end + main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index 3aa84ddf..de246ab6 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -32,10 +32,6 @@ def self.list_all return list end - # def information - # return "#{self.name}, #{self.real_name}, #{self.slack_id}" - # end - def details return "\n username: #{self.name} \n real name: #{self.real_name} \n slack ID: #{self.slack_id} \n status: #{self.status_text} \n emoji: #{self.status_emoji}" end diff --git a/lib/workspace.rb b/lib/workspace.rb index 011c456a..2c782d97 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,7 +1,6 @@ require 'httparty' require 'dotenv' - require_relative 'user' require_relative 'channel' @@ -37,37 +36,41 @@ def list_channels def select_channel(input) @selected = @channels.find { |channel| input == channel.name || input == channel.slack_id} - # raise ArgumentError.new("#{input} not found") if @selected == nil return @selected end def select_user(input) @selected = @users.find { |user| input == user.name || input == user.slack_id} - # raise ArgumentError.new("#{input} not found") if @selected == nil return @selected end + def ask_to_select(input) + select_channel(input) + if select_channel(input) == nil + select_user(input) + end + end + def show_details raise ArgumentError.new("no user or channel selected") if @selected == nil return @selected.details end - def send_message(message) + def send_message(message) - response = HTTParty.post( - "https://slack.com/api/chat.postMessage", - body: { - token: ENV['SLACK_API_TOKEN'], - text: message, - channel: @selected.slack_id - }) - unless response.code == 200 && response.parsed_response["ok"] - raise SlackApiError, "Error when posting #{message} to #{@selected.name}, error: #{response.parsed_response["error"]}" - end + response = HTTParty.post( + "https://slack.com/api/chat.postMessage", + body: { + token: ENV['SLACK_API_TOKEN'], + text: message, + channel: @selected.slack_id + }) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, "Error when posting #{message} to #{@selected.name}, error: #{response.parsed_response["error"]}" end - end + end +end -# response.code == 200 && diff --git a/test/channel_test.rb b/test/channel_test.rb index 84f41b90..8a187a80 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -4,22 +4,15 @@ describe "Channel class" do describe "channel list_all" do it "can call the API and receive a response" do - VCR.use_cassette("channel_instance") do - all_channels = Channel.list_all + VCR.use_cassette("channel_instance") do + all_channels = Channel.list_all - expect(all_channels).must_be_instance_of Array + expect(all_channels).must_be_instance_of Array - all_channels.each do |channel| - expect(channel).must_be_instance_of Channel + all_channels.each do |channel| + expect(channel).must_be_instance_of Channel + end end end - end end -end - -# return nil for invalid input -# workspacehas list ofsers and channels -# canfind user by ID -# can find user by name -# canfind channel by ID -# can find channel name +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 19d1ea12..28f8ef6c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -21,8 +21,8 @@ config.cassette_library_dir = "test/cassettes" # folder where casettes will be located config.hook_into :webmock # tie into this other tool called webmock config.default_cassette_options = { - :record => :new_episodes, # record new data when we don't have it yet - :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match } # Don't leave our token lying around in a cassette file. diff --git a/test/user_test.rb b/test/user_test.rb index 8cea0d93..3cd00594 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -13,13 +13,6 @@ expect(user).must_be_instance_of User end end + end end - end - end - -# return nil for invalid input -# workspacehas list ofsers and channels -# canfind user by ID -# can find user by name -# canfind channel by ID -# can find channel name +end \ No newline at end of file diff --git a/test/workspace_test.rb b/test/workspace_test.rb index f8d70f30..6a985e98 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -1,7 +1,6 @@ require_relative 'test_helper' require_relative '../lib/slack' - describe "Workspace" do describe "can initialize a workspace" do before do @@ -10,20 +9,20 @@ end end - it "can initialize a workspace" do + it "can initialize a workspace" do expect(@workspace).must_be_kind_of Workspace expect(@workspace.users).must_be_kind_of Array expect(@workspace.channels).must_be_kind_of Array expect(@workspace.selected).must_be_nil - end + end - it "initializes with user and channel objects" do + it "initializes with user and channel objects" do expect(@workspace.users.first).must_be_kind_of User expect(@workspace.channels.first).must_be_kind_of Channel + end end -end describe "can list users and channels" do before do @@ -56,62 +55,62 @@ end end - it "can select a user by name" do - user = @workspace.select_user("slackbot") - expect(user).must_be_instance_of User - expect(user.name).must_equal "slackbot" + it "can select a user by name" do + user = @workspace.select_user("slackbot") + expect(user).must_be_instance_of User + expect(user.name).must_equal "slackbot" - user = @workspace.select_user("mahaelmais") - expect(user).must_be_instance_of User - expect(user.name).must_equal "mahaelmais" - expect(user.slack_id).must_equal "U01CQGU5LKS" - end + user = @workspace.select_user("mahaelmais") + expect(user).must_be_instance_of User + expect(user.name).must_equal "mahaelmais" + expect(user.slack_id).must_equal "U01CQGU5LKS" + end - it "can select a user by ID" do - user = @workspace.select_user("U01BXJZPYAH") - expect(user).must_be_instance_of User - expect(user.name).must_equal "sophie.e.messing" - expect(user.slack_id).must_equal "U01BXJZPYAH" + it "can select a user by ID" do + user = @workspace.select_user("U01BXJZPYAH") + expect(user).must_be_instance_of User + expect(user.name).must_equal "sophie.e.messing" + expect(user.slack_id).must_equal "U01BXJZPYAH" - user = @workspace.select_user("U01CQHU8WBS") - expect(user).must_be_instance_of User - expect(user.name).must_equal "earth_mahaelmais_api_" - expect(user.slack_id).must_equal "U01CQHU8WBS" - end + user = @workspace.select_user("U01CQHU8WBS") + expect(user).must_be_instance_of User + expect(user.name).must_equal "earth_mahaelmais_api_" + expect(user.slack_id).must_equal "U01CQHU8WBS" + end - it "can select a channel by name" do - channel = @workspace.select_channel("random") - expect(channel).must_be_instance_of Channel - expect(channel.name).must_equal "random" - expect(channel.slack_id).must_equal "C01C0TJK6G3" + it "can select a channel by name" do + channel = @workspace.select_channel("random") + expect(channel).must_be_instance_of Channel + expect(channel.name).must_equal "random" + expect(channel.slack_id).must_equal "C01C0TJK6G3" - end + end - it "can select a channel by ID" do - channel = @workspace.select_channel("C01BXNGFDTP") - expect(channel).must_be_instance_of Channel - expect(channel.name).must_equal "test-channel" - expect(channel.slack_id).must_equal "C01BXNGFDTP" + it "can select a channel by ID" do + channel = @workspace.select_channel("C01BXNGFDTP") + expect(channel).must_be_instance_of Channel + expect(channel.name).must_equal "test-channel" + expect(channel.slack_id).must_equal "C01BXNGFDTP" - end + end - it "will return nil for invalid user input" do - user = @workspace.select_user("xxx") - expect(user).must_be_nil - end + it "will return nil for invalid user input" do + user = @workspace.select_user("xxx") + expect(user).must_be_nil + end - it "will return nil for invalid channel input" do - user = @workspace.select_user("00001111") - expect(user).must_be_nil - end + it "will return nil for invalid channel input" do + user = @workspace.select_user("00001111") + expect(user).must_be_nil + end - it "can show details for selected user" do - user = @workspace.select_user("mahaelmais") - user_details = @workspace.show_details + it "can show details for selected user" do + user = @workspace.select_user("mahaelmais") + user_details = @workspace.show_details - expect(user_details).must_be_kind_of String - expect(user_details).must_include "U01CQGU5LKS" - end + expect(user_details).must_be_kind_of String + expect(user_details).must_include "U01CQGU5LKS" + end it "can show details for selected channel" do channel = @workspace.select_channel("general") @@ -121,4 +120,4 @@ expect(channel_details).must_include "C01BTVCEXCN" end end - end +end From 6c3709e2b2c38c4e52ffc24637fb2db8c09ed218 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Fri, 9 Oct 2020 16:59:42 -0700 Subject: [PATCH 7/7] wave 3 complete. test coverage 100% --- lib/recipient.rb | 15 +++ lib/slack.rb | 117 +++++++++++---------- lib/workspace.rb | 22 +--- test/cassettes/channel_send_not_authed.yml | 66 ++++++++++++ test/channel_test.rb | 25 +++++ test/test_helper.rb | 8 +- test/user_test.rb | 26 +++++ test/workspace_test.rb | 47 +++++++++ 8 files changed, 250 insertions(+), 76 deletions(-) create mode 100644 test/cassettes/channel_send_not_authed.yml diff --git a/lib/recipient.rb b/lib/recipient.rb index ad8e5fa5..3d2e505a 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -4,6 +4,8 @@ Dotenv.load +class SlackApiError < StandardError; end + class Recipient attr_reader :slack_id, :name @@ -16,4 +18,17 @@ def initialize(slack_id, name) def self.get(url, params) return HTTParty.get(url, params) end + + def send_message(message) + response = HTTParty.post( + "https://slack.com/api/chat.postMessage", + body: { + token: ENV['SLACK_API_TOKEN'], + text: message, + channel: @slack_id + }) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, "Error when posting #{message} to #{@name}, error: #{response.parsed_response["error"]}" + end + end end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index dace0d6d..99ece09e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,9 +1,3 @@ -#!/usr/bin/env ruby - -# Use the dotenv gem to load environment variables -# Use HTTParty to send a GET request to the channels.list endpoint -# Check that the request completed successfully, and print relevant information to the console if it didn't -# Loop through the results and print out the name of each channel require 'prettyprint' require 'awesome_print' @@ -16,7 +10,12 @@ def main + begin workspace = Workspace.new + rescue SlackApiError => error + puts "error occurred: #{error.message}" + return + end puts "\nWelcome to the Ada Slack CLI!" puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels." @@ -29,72 +28,80 @@ def main puts "\n" input = gets.chomp.downcase - case input - when "list users" - puts workspace.list_users - when "list channels" - puts workspace.list_channels - - when "select channel" - puts "Please enter a channel name or ID:" - input = gets.chomp - workspace.select_channel(input) - until workspace.select_channel(input) != nil - puts "Invalid channel #{input}. Please try again." + begin + case input + when "list users" + puts workspace.list_users + when "list channels" + puts workspace.list_channels + + when "select channel" + puts "Please enter a channel name or ID:" input = gets.chomp workspace.select_channel(input) - end - puts "\n Selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" - puts " Enter #{"show details"} for more details on #{workspace.selected.name}" + until workspace.select_channel(input) != nil + puts "Invalid channel #{input}. Please try again." + input = gets.chomp + workspace.select_channel(input) + end + + puts "\n Selected channel: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts " Enter #{"show details"} for more details on #{workspace.selected.name}" - when "select user" - puts "Please enter a username or ID:" - input = gets.chomp - workspace.select_user(input) - until workspace.select_user(input) != nil - puts "Invalid user #{input}. Please try again." + when "select user" + puts "Please enter a username or ID:" input = gets.chomp workspace.select_user(input) - end - puts "\n Selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" - puts " Enter #{"show details"} for more details on #{workspace.selected.name}" - when "show details" - if workspace.selected == nil - puts "Details for whom? Please enter a user or channel" - input = gets.chomp - workspace.ask_to_select(input) - if workspace.ask_to_select(input) == nil - puts "Invalid input #{input}. Please try again." + until workspace.select_user(input) != nil + puts "Invalid user #{input}. Please try again." + input = gets.chomp + workspace.select_user(input) + end + + puts "\n Selected user: #{workspace.selected.name} \n ID: #{workspace.selected.slack_id}" + puts " Enter #{"show details"} for more details on #{workspace.selected.name}" + + when "show details" + if workspace.selected == nil + puts "Details for whom? Please enter a user or channel" input = gets.chomp workspace.ask_to_select(input) + until workspace.ask_to_select(input) != nil + puts "Invalid input #{input}. Please try again." + input = gets.chomp + workspace.ask_to_select(input) + end end - end - puts workspace.show_details + puts workspace.show_details - when "send a message" - if workspace.selected == nil - puts "To whom?" - input = gets.chomp - workspace.ask_to_select(input) - if workspace.ask_to_select(input) == nil - puts "Invalid input #{input}. Please try again." + when "send a message" + if workspace.selected == nil + puts "To whom?" input = gets.chomp workspace.ask_to_select(input) + until workspace.ask_to_select(input) != nil + puts "Invalid input #{input}. Please try again." + input = gets.chomp + workspace.ask_to_select(input) + end end - end - puts "What message would you like to send?" - message = gets.chomp.to_s - puts workspace.send_message(message) + puts "What message would you like to send?" + message = gets.chomp.to_s + puts workspace.send_message(message) + puts "message #{message} sent successfully!" - when "quit" - puts "Thank you for using the Ada Slack CLI" - break - else - puts "Invalid input #{input}" + when "quit" + puts "Thank you for using the Ada Slack CLI" + break + else + puts "Invalid input #{input}" + end + rescue SlackApiError => error + puts "error occurred: #{error.message}" end end end diff --git a/lib/workspace.rb b/lib/workspace.rb index 2c782d97..ffa3b28e 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -6,8 +6,6 @@ Dotenv.load -class SlackApiError < StandardError; end - class Workspace attr_reader :users,:channels,:selected @@ -46,30 +44,20 @@ def select_user(input) def ask_to_select(input) select_channel(input) - if select_channel(input) == nil + if @selected == nil select_user(input) end + return @selected end def show_details - raise ArgumentError.new("no user or channel selected") if @selected == nil + raise SlackApiError.new("no user or channel selected") if @selected == nil return @selected.details end - def send_message(message) - - response = HTTParty.post( - "https://slack.com/api/chat.postMessage", - body: { - token: ENV['SLACK_API_TOKEN'], - text: message, - channel: @selected.slack_id - }) - unless response.code == 200 && response.parsed_response["ok"] - raise SlackApiError, "Error when posting #{message} to #{@selected.name}, error: #{response.parsed_response["error"]}" - end - + raise SlackApiError.new("no user or channel selected") if @selected == nil + @selected.send_message(message) end end diff --git a/test/cassettes/channel_send_not_authed.yml b/test/cassettes/channel_send_not_authed.yml new file mode 100644 index 00000000..a602e196 --- /dev/null +++ b/test/cassettes/channel_send_not_authed.yml @@ -0,0 +1,66 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=This%20post%20should%20not%20work&channel=C01BTVCEXCN + 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: + - Fri, 09 Oct 2020 23:42:38 GMT + Server: + - Apache + X-Slack-Req-Id: + - f7906d9bd24d64df60e2678a99967159 + X-Oauth-Scopes: + - identify,channels:read,groups:read,im:read,users:read,chat:write + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '337' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-wtop,haproxy-edge-iad-gqnl + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"not_authed"}' + recorded_at: Fri, 09 Oct 2020 23:42:38 GMT +recorded_with: VCR 6.0.0 diff --git a/test/channel_test.rb b/test/channel_test.rb index 8a187a80..7d8c17aa 100644 --- a/test/channel_test.rb +++ b/test/channel_test.rb @@ -15,4 +15,29 @@ end end end + + describe "send message" do + + it "can send a message to a channel" do + VCR.use_cassette("channel_send_message") do + channel = Channel.new("C01BTVCEXCN", "general", "topic", 3) + channel.send_message("This post should work") + end + end + + it "will not send a message for invalid channel ID" do + VCR.use_cassette("channel_send_message") do + channel = Channel.new(99999, "test_channel", "this is a fake channel", 3) + expect { channel.send_message("This post should not work") }.must_raise SlackApiError + end + end + + it "returns an error for an invalid token" do + VCR.use_cassette("channel_send_not_authed") do + channel = Channel.new("C01BTVCEXCN", "general", "topic", 3) + + expect { channel.send_message("This post should not work") }.must_raise SlackApiError + end + end + end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 28f8ef6c..c2c5e1d8 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 'dotenv' require 'minitest' diff --git a/test/user_test.rb b/test/user_test.rb index 3cd00594..35ae9a56 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -15,4 +15,30 @@ end end end + + describe "send message" do + + it "can send a message to a user" do + VCR.use_cassette("user_send_message") do + user = User.new("U01CQGU5LKS", "mahaelmais", "mahaelmais", "", "") + user.send_message("This post should work") + end + end + + it "will not send a message for invalid user ID" do + VCR.use_cassette("user_send_message") do + user = User.new(99999, "test_user", "fake_user", "", "") + expect { user.send_message("This post should not work") }.must_raise SlackApiError + end + end + + it "returns an error for an invalid token" do + VCR.use_cassette("user_send_not_authed") do + user = User.new("U01CQGU5LKS", "mahaelmais", "mahaelmais", "", "") + + expect { user.send_message("This post should not work") }.must_raise SlackApiError + end + end + end + end \ No newline at end of file diff --git a/test/workspace_test.rb b/test/workspace_test.rb index 6a985e98..9f11c7d8 100644 --- a/test/workspace_test.rb +++ b/test/workspace_test.rb @@ -120,4 +120,51 @@ expect(channel_details).must_include "C01BTVCEXCN" end end + + describe "send_message" do + before do + VCR.use_cassette("send_message") do + @workspace = Workspace.new + end + end + + it "can send a message" do + VCR.use_cassette("send_message") do + @workspace.select_user("sophie.e.messing") + @workspace.send_message("This post should work") + end + end + + it "will raise an error if we send a message with no channel or user" do + expect { @workspace.send_message("This post should not work") }.must_raise SlackApiError + end + end + + describe "ask_to_select" do + it"can select a user" do + VCR.use_cassette("ask_to_select") do + @workspace = Workspace.new + end + expect(@workspace.ask_to_select("sophie.e.messing")).wont_be_nil + end + + it"can select a channel" do + VCR.use_cassette("ask_to_select") do + @workspace = Workspace.new + end + expect(@workspace.ask_to_select("general")).wont_be_nil + end + + it"can't select invalid input" do + VCR.use_cassette("ask_to_select") do + @workspace = Workspace.new + end + expect(@workspace.ask_to_select("xxxxxxxxxx")).must_be_nil + end + + end end + + + +