diff --git a/.gitignore b/.gitignore index 8d6a243f..db0e8058 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ build-iPhoneSimulator/ # Ignore cassette files /specs/cassettes/ + +# Ignore .DS_Store +.DS_Store diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..3f0054fa --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,51 @@ +require_relative 'recipient' + +module SlackCLI + class Channel < Recipient + attr_reader :topic, :member_count + + def initialize(slack_id:, name:, topic:, member_count:) + super(slack_id: slack_id, name: name) + @topic = topic + @member_count = member_count + end + + def self.list + response = self.get("https://slack.com/api/channels.list") + + channels = response["channels"].map do |channel| + slack_id = channel["id"] + name = channel["name"] + topic = channel["topic"]["value"] + member_count = channel["members"].length + + SlackCLI::Channel.new( + slack_id: slack_id, + name: name, + topic: topic, + member_count: member_count + ) + end + + return channels + end + + def details + key = ENV["API_TOKEN"] + response = HTTParty.get("https://slack.com/api/channels.info", query: {token: key , channel: @slack_id}) + if response.keys.include? "error" || response["ok"] == false + raise SlackCLI::SlackApiError + end + + purpose = response["channel"]["purpose"]["value"] + is_private = response["channel"]["is_private"] ? "true" : "false" + + "Slack ID: #{slack_id}\n" + + "Name: #{name}\n" + + "Topic: #{topic}\n" + + "Member Count: #{member_count}\n" + + "Purpose: #{purpose}\n" + + "Is Private? #{is_private}" + end + end +end diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..26dc294e --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,55 @@ +require 'httparty' +require 'dotenv' + +Dotenv.load + +module SlackCLI + class SlackApiError < StandardError; end + class Recipient + attr_reader :slack_id, :name + + def initialize(slack_id:, name:) + @slack_id = slack_id + @name = name + end + + def self.get(url) + key = ENV["API_TOKEN"] + response = HTTParty.get(url, query: {token: key }) + if response.keys.include? "error" || response["ok"] == false + raise SlackCLI::SlackApiError + end + + return response + end + + def send_message(message) + # pull out key to be @key? + key = ENV["API_TOKEN"] + response = HTTParty.post( + "https://slack.com/api/chat.postMessage", + headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, + body: { + "token": key, + "channel": @slack_id, + "text": message + } + ) + + if response.keys.include? "error" || response["ok"] == false + raise SlackCLI::SlackApiError.new("Message not sent due to error: #{response["error"]}") + else + return true + end + end + + def details + raise NotImplementedError.new("Details should be implemented in child class") + end + + def self.list + raise NotImplementedError.new("Self.list should be implemented in child class") + end + + end +end diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..3e5045cb 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,110 @@ #!/usr/bin/env ruby +require_relative 'workspace' +require 'table_print' +require 'terminal-table' def main - puts "Welcome to the Ada Slack CLI!" + puts "\nWelcome to the Ada Slack CLI!" + workspace = SlackCLI::Workspace.new + channels_count = workspace.channels.length + users_count = workspace.users.length + puts "\n#{channels_count} channels and #{users_count} users loaded" + + choice = "" + until choice == "7" || choice == "quit" + puts "\nWhat would you like to do?" + menu_options + print "Action: " + choice = gets.chomp.downcase + + case choice + when "1","list users" + puts "\n" + list_users(workspace) - # TODO project + when "2","list channels" + puts "\n" + list_channels(workspace) + when "3","select user" + puts "Select User" + print "Please enter a Username or Slack ID: " + query = gets.chomp.downcase + query_result = workspace.select_user(query) + if query_result == nil + puts "\nUser with Username or Slack ID '#{query}' does not exist." + else + puts "\nUser '#{query_result.name}' selected" + end + + when "4","select channel" + puts "Select Channel" + print "Please enter a Channel Name or Slack ID: " + query = gets.chomp.downcase + query_result = workspace.select_channel(query) + if query_result == nil + puts "\nChannel with Name or Slack ID '#{query}' does not exist." + else + puts "\nChannel '#{query_result.name}' selected" + end + + when "5","details" + if workspace.show_details == nil + puts "\nNo user or channel selected." + else + puts "\nDetails:" + puts "#{workspace.show_details}" + end + + when "6","send message" + if workspace.selected == nil + puts "\nNo recipient selected to send message to." + else + puts "Send Message to '#{workspace.selected.name}'" + print "Please enter message text: " + message_text = gets.chomp + + until message_text != "" + puts "No message entered." + print "Please enter message text: " + message_text = gets.chomp + end + if workspace.send_message(message_text) + puts "Message successfully sent" + end + end + + when "7","quit" + break + else + puts "That option does not exist" + end + + end + puts "Thank you for using the Ada Slack CLI" end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +def menu_options + numbered_options = [ + ["1", "list users"], + ["2", "list channels"], + ["3", "select user"], + ["4", "select channel"], + ["5", "details"], + ["6", "send message"], + ["7","quit"] + ] + menu_table = Terminal::Table.new :headings => ["#", "Action"], :rows => numbered_options + puts menu_table +end + +def list_users(workspace) + tp workspace.users, :slack_id, :name, :real_name +end + +def list_channels(workspace) + tp workspace.channels, :slack_id, :name, :topic, :member_count +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..f1af5855 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,48 @@ +require_relative 'recipient' +require 'httparty' +require 'dotenv' + +Dotenv.load + + +module SlackCLI + class User < Recipient + attr_reader :real_name + + def initialize(slack_id:, name:, real_name:) + super(slack_id: slack_id, name: name) + @real_name = real_name + end + + def self.list + response = self.get("https://slack.com/api/users.list") + + users = response["members"].map do |member| + slack_id = member["id"] + name = member["name"] + real_name = member["real_name"] + + SlackCLI::User.new(slack_id: slack_id, name: name, real_name: real_name) + end + + return users + end + + def details + key = ENV["API_TOKEN"] + response = HTTParty.get("https://slack.com/api/users.info", query: {token: key , user: @slack_id}) + if response.keys.include? "error" || response["ok"] == false + raise SlackCLI::SlackApiError + end + + status = response["user"]["profile"]["status_text"] + is_bot = response["user"]["is_bot"] ? "true" : "false" + + "Slack ID: #{slack_id}\n" + + "Name: #{name}\n" + + "Real Name: #{real_name}\n" + + "Status: #{status}\n" + + "Is Bot?: #{is_bot}" + end + end +end diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..bb3bb184 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,48 @@ +require_relative 'user' +require_relative 'channel' + +module SlackCLI + class Workspace + attr_reader :users, :channels + attr_accessor :selected + + def initialize + @users = SlackCLI::User.list + @channels = SlackCLI::Channel.list + @selected = nil + end + + def find_instance(recipient_list, name_or_id) + instance = recipient_list.find do |recipient| + recipient.name.downcase == name_or_id.downcase || + recipient.slack_id.downcase == name_or_id.downcase + end + + return instance + end + + def select_user(name_or_id) + user = find_instance(users, name_or_id) + @selected = user if user != nil + + return @selected + end + + def select_channel(name_or_id) + channel = find_instance(channels, name_or_id) + @selected = channel if channel != nil + + return @selected + end + + def show_details + selected ? selected.details : nil + end + + def send_message(message_text) + @selected.send_message(message_text) + return true + end + end + +end diff --git a/test/cassettes/get_channel_info.yml b/test/cassettes/get_channel_info.yml new file mode 100644 index 00000000..45422849 --- /dev/null +++ b/test/cassettes/get_channel_info.yml @@ -0,0 +1,77 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/channels.info?channel=CN9NG0YUE&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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '408' + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 22:28:29 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 404ec31c-a6a6-4b05-a0f4-fc44588bc115 + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-qdiq + X-Cache: + - Miss from cloudfront + Via: + - 1.1 a2a7227d0a99f50bffb8ba79de64ab0f.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - LUJ76hop7c0sYDZMAqjmBFynR6rJGBBFjHa-HEyLUSoWn-OSXP5C7g== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":{"id":"CN9NG0YUE","name":"random","is_channel":true,"created":1568143383,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"last_read":"1568413677.000100","latest":{"user":"UN9NG04TG","type":"message","subtype":"channel_purpose","ts":"1568413677.000100","text":"<@UN9NG04TG> + set the channel purpose: RANDOMNESS!!! YES!","purpose":"RANDOMNESS!!! YES!"},"unread_count":0,"unread_count_display":0,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"RANDOMNESS!!! + YES!","creator":"UN9NG04TG","last_set":1568413677},"previous_names":[]}}' + http_version: + recorded_at: Fri, 13 Sep 2019 22:28:29 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/get_user_info.yml b/test/cassettes/get_user_info.yml new file mode 100644 index 00000000..70b190bb --- /dev/null +++ b/test/cassettes/get_user_info.yml @@ -0,0 +1,151 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.info?token=&user=USLACKBOT + 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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '484' + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 22:07:12 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - c2897c03-e838-48b9-8c62-7da44a72faea + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read,read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-xtep + X-Cache: + - Miss from cloudfront + Via: + - 1.1 73bd23077f64204bc8f5efea09d16ebd.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - bby2dVN_c7fJqTG6sTp1ex7MoD5p0B--z0NB-3Lfk1X2Lhr_AIJJ3A== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"user":{"id":"USLACKBOT","team_id":"TMW2GFQAF","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"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":"TMW2GFQAF"},"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,"has_2fa":false}}' + http_version: + recorded_at: Fri, 13 Sep 2019 22:07:12 GMT +- request: + method: get + uri: https://slack.com/api/channels.info?channel=CN9NG0YUE&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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '492' + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 22:27:37 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - d6886f22-154b-46de-9bc9-3d65c878f05b + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-rg8i + X-Cache: + - Miss from cloudfront + Via: + - 1.1 9f2fa0c5a611130a0b26673bb62c8388.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - gWLJ31KhQgRpf7mzeZmmQ_AMxpB0iDJ4NUUhKhRbplT9ea0sOji2hg== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":{"id":"CN9NG0YUE","name":"random","is_channel":true,"created":1568143383,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"last_read":"1568399587.000200","latest":{"type":"message","subtype":"bot_message","text":"trying + to send from recipient_test","ts":"1568399587.000200","username":"Slack API + Tester","bot_id":"BMXAJ972P"},"unread_count":0,"unread_count_display":0,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[]}}' + http_version: + recorded_at: Fri, 13 Sep 2019 22:27:37 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/list_channels.yml b/test/cassettes/list_channels.yml new file mode 100644 index 00000000..ad222de9 --- /dev/null +++ b/test/cassettes/list_channels.yml @@ -0,0 +1,81 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/channels.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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '585' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 22:21:18 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 59b42be1-cf11-4c6d-a5d6-ea505f07aee8 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-1ir4 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 e785d36fcfe1e6758d8c1a9a71445bbe.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - fF0VOr1CNSyQkCXj_W2ndTic1nGZx9AAIULbLZLatkeuAfD1GQ02xg== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CMW2GGWN7","name":"slack-cli","is_channel":true,"created":1568143384,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"slack-cli","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"Learning + how to play with APIs","creator":"UN9NG04TG","last_set":1568240342},"previous_names":[],"num_members":2},{"id":"CN7G55J00","name":"general","is_channel":true,"created":1568143383,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"general","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2},{"id":"CN9NG0YUE","name":"random","is_channel":true,"created":1568143383,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 22:21:18 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/list_users.yml b/test/cassettes/list_users.yml new file mode 100644 index 00000000..cddf38e6 --- /dev/null +++ b/test/cassettes/list_users.yml @@ -0,0 +1,78 @@ +--- +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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '937' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 22:21:17 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - '03495af8-4728-4eb2-9050-04e21309ac5a' + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-abbx + X-Cache: + - Miss from cloudfront + Via: + - 1.1 72f0ac9702110cafbb646d71a297e2c7.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - wuimShfsPNucXt4Z_rsoGlGBowHLdUaOtCPMIvq_FUfrLtTia0Zt3w== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TMW2GFQAF","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"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":"TMW2GFQAF"},"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":"UN9C6R7EK","team_id":"TMW2GFQAF","name":"davenport.paige","deleted":false,"color":"4bbe2e","real_name":"Paige","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Paige","real_name_normalized":"Paige","display_name":"Paige","display_name_normalized":"Paige","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g68b577a7f23","image_24":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568143450,"has_2fa":false},{"id":"UN9NG04TG","team_id":"TMW2GFQAF","name":"angele.zam","deleted":false,"color":"9f69e7","real_name":"Angele + Zamarron","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Angele + Zamarron","real_name_normalized":"Angele Zamarron","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gee46bbc96e3","first_name":"Angele","last_name":"Zamarron","image_24":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568221139}],"cache_ts":1568240477,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 22:21:17 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/new_workspace.yml b/test/cassettes/new_workspace.yml new file mode 100644 index 00000000..ea5b1da2 --- /dev/null +++ b/test/cassettes/new_workspace.yml @@ -0,0 +1,156 @@ +--- +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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '937' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 22:21:18 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 56caa364-bbdb-4253-82df-802f24025077 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-zrg4 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 2dc84924ce70e874a873764fe1415858.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - 8PTFc2n9hlaUQ58SSl7yaoH1xonhhP217wsYQ25Cd92rbfFmt_itIw== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TMW2GFQAF","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"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":"TMW2GFQAF"},"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":"UN9C6R7EK","team_id":"TMW2GFQAF","name":"davenport.paige","deleted":false,"color":"4bbe2e","real_name":"Paige","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Paige","real_name_normalized":"Paige","display_name":"Paige","display_name_normalized":"Paige","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g68b577a7f23","image_24":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568143450,"has_2fa":false},{"id":"UN9NG04TG","team_id":"TMW2GFQAF","name":"angele.zam","deleted":false,"color":"9f69e7","real_name":"Angele + Zamarron","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Angele + Zamarron","real_name_normalized":"Angele Zamarron","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gee46bbc96e3","first_name":"Angele","last_name":"Zamarron","image_24":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568221139}],"cache_ts":1568240478,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 22:21:18 GMT +- request: + method: get + uri: https://slack.com/api/channels.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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '585' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 22:21:18 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - c3e0d2b4-f1d4-49c9-83f8-7ada7ce9fc12 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-5t66 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 d9a3481018b2f1931201627713f68e77.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19 + X-Amz-Cf-Id: + - YPPodD1kozMF7uPYjQYFOku7eC_Oo1oWBY3NZHLdR3ooB4sceDVHhA== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CMW2GGWN7","name":"slack-cli","is_channel":true,"created":1568143384,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"slack-cli","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"Learning + how to play with APIs","creator":"UN9NG04TG","last_set":1568240342},"previous_names":[],"num_members":2},{"id":"CN7G55J00","name":"general","is_channel":true,"created":1568143383,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"general","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2},{"id":"CN9NG0YUE","name":"random","is_channel":true,"created":1568143383,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 22:21:18 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/recipient_gets_error.yml b/test/cassettes/recipient_gets_error.yml new file mode 100644 index 00000000..36c31c96 --- /dev/null +++ b/test/cassettes/recipient_gets_error.yml @@ -0,0 +1,64 @@ +--- +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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '55' + Connection: + - keep-alive + Date: + - Thu, 12 Sep 2019 22:16:25 GMT + Server: + - Apache + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - cd2823c7-2ea0-424b-9c5a-d963cc8d77ee + X-Xss-Protection: + - '0' + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-xqej + X-Cache: + - Miss from cloudfront + Via: + - 1.1 6172bb1a5d00a3b06ae3700570ebe117.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C2 + X-Amz-Cf-Id: + - Rmav1bUO6rAla9n3GnYKPMZBRBKn_wTtkm5fZenExaeNTOkCz4FiXw== + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + http_version: + recorded_at: Thu, 12 Sep 2019 22:16:25 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/recipient_send_message.yml b/test/cassettes/recipient_send_message.yml new file mode 100644 index 00000000..2b9c210f --- /dev/null +++ b/test/cassettes/recipient_send_message.yml @@ -0,0 +1,431 @@ +--- +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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '939' + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:33:07 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - '08a7c5a0-ca97-440f-a3c1-db5dc3931a25' + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read,read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-fc14 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 bf3ec4bcb6f4f29d898c3c4e0f95a185.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - Q3B-P3oxrRB-uyAe1r2ecGkJ6AUseQP5vJqQQ23eK0lwdMNoY57s8w== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"UN9C6R7EK","team_id":"TMW2GFQAF","name":"davenport.paige","deleted":false,"color":"4bbe2e","real_name":"Paige","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Paige","real_name_normalized":"Paige","display_name":"Paige","display_name_normalized":"Paige","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g68b577a7f23","email":"davenport.paige@outlook.com","image_24":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/68b577a7f23086761e086b6d295e3c23.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0002-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568143450,"has_2fa":false},{"id":"UN9NG04TG","team_id":"TMW2GFQAF","name":"angele.zam","deleted":false,"color":"9f69e7","real_name":"Angele + Zamarron","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Angele + Zamarron","real_name_normalized":"Angele Zamarron","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gee46bbc96e3","email":"angele.zam@gmail.com","first_name":"Angele","last_name":"Zamarron","image_24":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/ee46bbc96e30be4133113950b023753e.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0020-512.png","status_text_canonical":"","team":"TMW2GFQAF"},"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":1568221139,"has_2fa":false},{"id":"USLACKBOT","team_id":"TMW2GFQAF","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"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":"TMW2GFQAF"},"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}],"cache_ts":1568399587}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:33:07 GMT +- request: + method: get + uri: https://slack.com/api/channels.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: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '556' + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:33:07 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - bb76e184-8d80-42b1-88ff-ba93f789a489 + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-lqa3 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 48b1d9f5c5a47a0b424a9637eb513cee.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - 1sK0EV59sv54FFJ4wxFaWFEIOCf8BYspwJfgan-BMduXYFOQOlBRMQ== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CN7G55J00","name":"general","is_channel":true,"created":1568143383,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"general","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2},{"id":"CN9NG0YUE","name":"random","is_channel":true,"created":1568143383,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UN9NG04TG","last_set":1568143383},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UN9NG04TG","last_set":1568143383},"previous_names":[],"num_members":2},{"id":"CMW2GGWN7","name":"slack-cli","is_channel":true,"created":1568143384,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN9NG04TG","name_normalized":"slack-cli","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UN9C6R7EK","UN9NG04TG"],"topic":{"value":"A + topic","creator":"UN9NG04TG","last_set":1568240532},"purpose":{"value":"Learning + how to play with APIs","creator":"UN9NG04TG","last_set":1568240342},"previous_names":[],"num_members":2}]}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:33:07 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=CN9NG0YUE&text=trying%20to%20send%20from%20recipient_test + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:33:07 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 82442a5f-5f6c-4709-85b6-f6634688d9a2 + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot,post + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-qj5i + X-Cache: + - Miss from cloudfront + Via: + - 1.1 d2bb0dc1233d3ab1747a4a160c14c25b.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - RVB1p-_GZR8P70j_-SloT2GZiD03w2BNnXJEKWRvkaZgYpKVOnuHrA== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"CN9NG0YUE","ts":"1568399587.000200","message":{"type":"message","subtype":"bot_message","text":"trying + to send from recipient_test","ts":"1568399587.000200","username":"Slack API + Tester","bot_id":"BMXAJ972P"}}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:33:07 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=USLACKBOT&text=trying%20to%20send%20from%20recipient_test%20to%20slackbot + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:34:26 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 3fce6fb4-fbd4-4a6b-a08a-c6698f517deb + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot,post + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-wuu0 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 7e81687e34febf53ccb2929005d5ef12.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - 6Nr1PmhBwzgVlmpPv7RRIVtI4YKdR093LZdQbDeKjO5Al8GXEFGeJQ== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DN4B8GNRF","ts":"1568399666.000100","message":{"type":"message","subtype":"bot_message","text":"trying + to send from recipient_test to slackbot","ts":"1568399666.000100","username":"Slack + API Tester","bot_id":"BMXAJ972P"}}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:34:26 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=UN9C6R7EK&text=trying%20to%20send%20from%20recipient_test%20to%20paige + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:35:29 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - b6ecd8c4-3e6f-4a17-835f-146d9f808428 + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot,post + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-lcub + X-Cache: + - Miss from cloudfront + Via: + - 1.1 098a28c90e7d4eca5d6cebe57828e74d.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - NOYN1POknjZGNn_QiLDj2ckimDGLhIQfk9qYO5mx5tN-O2jiw4xe2Q== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DN7GCE0BF","ts":"1568399729.000100","message":{"type":"message","subtype":"bot_message","text":"trying + to send from recipient_test to paige","ts":"1568399729.000100","username":"Slack + API Tester","bot_id":"BMXAJ972P"}}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:35:29 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=55555&text=trying%20to%20send%20from%20recipient_test%20to%20nonexistent%20User + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 18:42:50 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 629faf13-a809-4944-824b-888449f591b7 + X-Oauth-Scopes: + - identify,read,post,client,apps,admin + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot,post + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-7kp7 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 15e808532464d90b13614947e41d0d22.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - HIO51-C1 + X-Amz-Cf-Id: + - dLupf76HTDqjnSxGV0tSw350Ca2wwfc5g4X7Q5jV1S0yXwVbhOuWuw== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"channel_not_found"}' + http_version: + recorded_at: Fri, 13 Sep 2019 18:42:50 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/workspace_send_message.yml b/test/cassettes/workspace_send_message.yml new file mode 100644 index 00000000..8b515bf2 --- /dev/null +++ b/test/cassettes/workspace_send_message.yml @@ -0,0 +1,72 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=CN9NG0YUE&text=trying%20to%20send%20from%20workspace_test + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 13 Sep 2019 23:28:14 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 33e5121b-5c27-46fd-ac6c-7bad9a66d591 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-1a8v + X-Cache: + - Miss from cloudfront + Via: + - 1.1 34f8ef0e4c880df0650a814412a26ea6.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - KRPhJSqM-hB_FD2tI6CyLYQ-HTKH0je_bNLcqBh75i4WcCatTXwV2g== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"CN9NG0YUE","ts":"1568417294.000200","message":{"type":"message","subtype":"bot_message","text":"trying + to send from workspace_test","ts":"1568417294.000200","username":"Branches + - A & P - API Project","bot_id":"BN7GH7PEU"}}' + http_version: + recorded_at: Fri, 13 Sep 2019 23:28:14 GMT +recorded_with: VCR 5.0.0 diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..7fd19049 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,62 @@ +require_relative 'test_helper' + +describe "Channel" do + describe "initialize" do + it "can be initialized as child class of Recipient" do + slack_id = "TD83838H" + name = "#random" + topic = "random thoughts" + member_count = "4" + + + new_channel = SlackCLI::Channel.new( + slack_id: slack_id, + name: name, + topic: topic, + member_count: member_count + ) + + expect(new_channel.class < SlackCLI::Recipient).must_equal true + end + end + + describe "self.list" do + it "creates a list of all channels" do + VCR.use_cassette("list_channels") do + channel_list = SlackCLI::Channel.list + + expect(channel_list).must_be_instance_of Array + expect(channel_list.first).must_be_instance_of SlackCLI::Channel + end + end + end + + describe "details" do + before do + VCR.use_cassette("list_channels") do + @channel_list = SlackCLI::Channel.list + end + end + + it "returns a string" do + VCR.use_cassette("get_channel_info") do + channel_details = @channel_list[2].details + + expect(channel_details).must_be_instance_of String + end + end + + it "returns accurate details about the channel" do + VCR.use_cassette("get_channel_info") do + channel_details = @channel_list[2].details + + expect(channel_details).must_include "CN9NG0YUE" + expect(channel_details).must_include "random" + expect(channel_details).must_include "Non-work banter and water cooler conversation" + expect(channel_details).must_include "2" + expect(channel_details).must_include "RANDOMNESS!!! YES!" + expect(channel_details).must_include "false" + end + end + end +end diff --git a/test/recipient_test.rb b/test/recipient_test.rb new file mode 100644 index 00000000..b9cd5f8f --- /dev/null +++ b/test/recipient_test.rb @@ -0,0 +1,54 @@ +require_relative 'test_helper' + +describe "Recipient" do + describe "initialize" do + it "can be initialized with an ID and a name" do + slack_id = "TD83838H" + name = "SlackBot" + + new_recipient = SlackCLI::Recipient.new(slack_id: slack_id, name: name) + + expect(new_recipient).must_be_instance_of SlackCLI::Recipient + end + end + + describe "self.get" do + it "raises an error if the API returns an error response" do + # "recipient_gets" cassette was created with invalid token + VCR.use_cassette("recipient_gets_error") do + assert_raises(SlackCLI::SlackApiError) { + SlackCLI::Recipient.get("https://slack.com/api/users.list") + } + end + end + end + + describe "send_message" do + it "sends a message and returns true if it was successful" do + VCR.use_cassette("recipient_send_message") do + workspace = SlackCLI::Workspace.new + channel_random = workspace.find_instance(workspace.channels, "random") + # slack_id = "CN9NG0YUE" # "random" channel + response = channel_random.send_message("trying to send from recipient_test") + expect(response).must_equal true + + user_slackbot = workspace.find_instance(workspace.users, "slackbot") + response_2 = user_slackbot.send_message("trying to send from recipient_test to slackbot") + expect(response_2).must_equal true + end + end + it "raises an error if message isn't sent" do + VCR.use_cassette("recipient_send_message") do + assert_raises(SlackCLI::SlackApiError) { + fake_user = SlackCLI::User.new( + slack_id: 55555, + name: "fake name", + real_name: "fake real name" + ) + fake_user.send_message("trying to send from recipient_test to nonexistent User") + } + end + end + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 90aeb408..a6c21919 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,7 +11,20 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require_relative '../lib/recipient.rb' +require_relative '../lib/user.rb' +require_relative '../lib/channel.rb' +require_relative '../lib/workspace.rb' + VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" - config.hook_into :webmock + config.cassette_library_dir = "test/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } + # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["API_TOKEN"] + end end diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..61d4bd5f --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,57 @@ +require_relative 'test_helper' + +describe "User" do + describe "initialize" do + it "can be initialized as child class of Recipient" do + slack_id = "TD83838H" + name = "SlackBot" + real_name = "SlackBot" + + new_recipient = SlackCLI::User.new( + slack_id: slack_id, + name: name, + real_name: real_name + ) + + expect(new_recipient.class < SlackCLI::Recipient).must_equal true + end + end + + describe "self.list" do + it "creates a list of at least 1 user" do + VCR.use_cassette("list_users") do + users_list = SlackCLI::User.list + + expect(users_list).must_be_instance_of Array + expect(users_list.first).must_be_instance_of SlackCLI::User + end + end + end + + describe "details" do + before do + VCR.use_cassette("list_users") do + @users_list = SlackCLI::User.list + end + end + + it "returns a string" do + VCR.use_cassette("get_user_info") do + user_details = @users_list[0].details + + expect(user_details).must_be_instance_of String + end + end + + it "returns accurate details about the user" do + VCR.use_cassette("get_user_info") do + user_details = @users_list[0].details + + expect(user_details).must_include "USLACKBOT" + expect(user_details).must_include "slackbot" + expect(user_details).must_include "Slackbot" + expect(user_details).must_include "false" + end + end + end +end diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..b57c6eaf --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,149 @@ +require_relative 'test_helper' + +describe "Workspace" do + before do + VCR.use_cassette("new_workspace") do + @new_workspace = SlackCLI::Workspace.new + end + end + + describe "initialize" do + it "can initialize" do + expect(@new_workspace).must_be_instance_of SlackCLI::Workspace + end + + it "stores instances of User in @users" do + expect(@new_workspace.users.first).must_be_instance_of SlackCLI::User + end + end + + describe "find_instance" do + it "finds a User given their Slack Id or Name" do + id = "USLACKBOT" + user_1 = @new_workspace.find_instance(@new_workspace.users, id) + + name = "Slackbot" + user_2 = @new_workspace.find_instance(@new_workspace.users, name) + + expect(user_1).must_be_instance_of SlackCLI::User + expect(user_1.slack_id.downcase).must_equal id.downcase + + expect(user_2).must_be_instance_of SlackCLI::User + expect(user_2.name.downcase).must_equal name.downcase + end + + it "finds a User even if the case doesn't match" do + id = "uslackBOT" + user_1 = @new_workspace.find_instance(@new_workspace.users, id) + + name = "sLacKbOt" + user_2 = @new_workspace.find_instance(@new_workspace.users, name) + + expect(user_1).must_be_instance_of SlackCLI::User + expect(user_1.slack_id.downcase).must_equal id.downcase + + expect(user_2).must_be_instance_of SlackCLI::User + expect(user_2.name.downcase).must_equal name.downcase + end + + it "returns nil if user or channel is not found" do + name = "mr. slackbot" + channel_name = "not a channel" + user = @new_workspace.find_instance(@new_workspace.users, name) + channel = @new_workspace.find_instance(@new_workspace.channels, channel_name) + + expect(user).must_be_nil + expect(channel).must_be_nil + end + + it "finds a channel given the channel name or id" do + id = "CN9NG0YUE" + channel_1 = @new_workspace.find_instance(@new_workspace.channels, id) + + name = "random" + channel_2 = @new_workspace.find_instance(@new_workspace.channels, name) + + expect(channel_1).must_be_instance_of SlackCLI::Channel + expect(channel_1.slack_id.downcase).must_equal id.downcase + + expect(channel_2).must_be_instance_of SlackCLI::Channel + expect(channel_2.name.downcase).must_equal name.downcase + end + end + + describe "select_user" do + before do + @new_workspace.selected == nil + end + + it "assigns a User to selected" do + before_selected = @new_workspace.selected + user_1_name = "Slackbot" + + selection = @new_workspace.select_user(user_1_name) + + expect(before_selected).must_be_nil + expect(@new_workspace.selected).must_equal selection + end + end + + describe "select_channel" do + before do + @new_workspace.selected == nil + end + + it "assigns a Channel to selected" do + before_selected = @new_workspace.selected + channel_1_name = "random" + + selection = @new_workspace.select_channel(channel_1_name) + + expect(before_selected).must_be_nil + expect(@new_workspace.selected).must_equal selection + end + + it "reassigns a Channel to selected" do + old_selection = @new_workspace.select_channel("general") + new_selection = @new_workspace.select_channel("random") + + expect(@new_workspace.selected).wont_equal old_selection + expect(@new_workspace.selected.name).must_equal "random" + end + end + + describe "show_details" do + it "returns details for a selected recipient" do + VCR.use_cassette("get_user_info") do + channel_id = "CN9NG0YUE" + user_id = "USLACKBOT" + + @new_workspace.select_channel(channel_id) + details_1 = @new_workspace.show_details + @new_workspace.select_user(user_id) + details_2 = @new_workspace.show_details + + expect(details_1).must_include "random" + expect(details_2).must_include "slackbot" + end + end + + it "returns nil if no user or channel is selected" do + @new_workspace.selected == nil + + expect(@new_workspace.show_details).must_be_nil + end + end + + describe "send_message" do + it "returns true if a message was sent" do + channel_random = @new_workspace.find_instance(@new_workspace.channels, "random") + @new_workspace.selected = channel_random + + VCR.use_cassette("workspace_send_message") do + response = @new_workspace.send_message("trying to send from workspace_test") + expect(response).must_equal true + end + end + + end +end