diff --git a/.gitignore b/.gitignore index 3ff4fada..10844f33 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ # Ignore environemnt variables .env + + diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..a50b09de --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,49 @@ +require_relative 'recipient' + +class Channel < Recipient + attr_reader :topic, :member_count + + def initialize(slack_id:, name:, topic:, member_count:) + super(slack_id, name) + @topic = topic + @member_count = member_count + end + + def details + "Slack ID: #{@slack_id}\nName: #{@name}\nTopic: #{@topic}\nMember Count: #{@member_count}" + end + + def self.list_all + url = "https://slack.com/api/conversations.list" + query_params = {token: ENV["SLACK_TOKEN"]} + + response = self.get(url, query_params) + + channels_list = response["channels"].map do |channel| + self.from_api(channel) + end + + return channels_list + end + + private + + def self.from_api(recipient) + topic = { + "value"=>recipient["topic"]["value"], + "creater"=>recipient["topic"]["creator"], + "last_set"=>recipient["topic"]["last_set"] + } + + return new( + slack_id: recipient["id"], + name: recipient["name"], + topic: topic, + member_count: recipient["num_members"] + ) + end + +end + + + diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..0a5a5bce --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,43 @@ +require 'dotenv' +require 'httparty' + +Dotenv.load + +class SlackTokenError < 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, params) + response = HTTParty.get(url, query: params) + + if response["ok"] != true + raise SlackTokenError, "API call failed error message: #{response["error"]}" + end + + return response + end + + def details + raise NotImplementedError, 'Implement me in a child class!' + end + + def self.list_all + raise NotImplementedError, 'Implement me in a child class!' + end + + private + + def self.from_api(recipient) + raise NotImplementedError, 'Implement me in a child class!' + end + +end + + diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..3d11ce0f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,12 +1,76 @@ #!/usr/bin/env ruby +require_relative 'workspace' + +require 'table_print' def main puts "Welcome to the Ada Slack CLI!" workspace = Workspace.new - # TODO project + choices = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"] + + program_running = true + + while program_running + puts "Please choose from the following:" + + choices.each_with_index do |choice, i| + puts "#{i + 1}: #{choice}" + end + + user_input = gets.chomp.downcase + + case user_input + when "list users" + puts "" + tp workspace.users, :slack_id, :name, :real_name, :status_text, :status_emoji + puts "" + when "list channels" + puts "" + tp workspace.channels, :slack_id, :name, :topic, :member_count + puts "" + when "select user" + puts "User name or ID?" + user = gets.chomp + selected_item = workspace.select_user(user) + if selected_item == nil + puts "Hmm, no user matches that name or ID" + end + when "select channel" + puts "Name or ID?" + channel = gets.chomp + selected_item = workspace.select_channel(channel) + if selected_item == nil + puts "Hmm, no channel matches that name or ID" + end + when "details" + if selected_item != nil + puts "" + puts selected_item.details + puts "" + else + puts "A channel or user has not been selected to show details" + end + when "send message" + + if selected_item != nil + puts "What's your message?" + text = gets.chomp + + workspace.send_message(selected_item.name, text) + else + puts "A channel or user has not been selected to send message" + end + + when "quit" + program_running = false + else + puts "Invalid choice! Please try again!" + end + end puts "Thank you for using the Ada Slack CLI" + end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..ebb88e31 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,47 @@ +require_relative 'recipient' + +class User < Recipient + attr_reader :real_name, :status_text, :status_emoji + + def initialize(slack_id:, name:, real_name:, status_text:, status_emoji:) + super(slack_id, name) + @real_name = real_name + @status_text = status_text + @status_emoji = status_emoji + end + + def details + "Slack ID: #{@slack_id}\nUsername: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}" + end + + def self.list_all + url = "https://slack.com/api/users.list" + query_params = {token: ENV["SLACK_TOKEN"]} + + response = self.get(url, query_params) + + users_list = response["members"].map do |member| + self.from_api(member) + end + + return users_list + end + + private + + def self.from_api(recipient) + return new( + slack_id: recipient["id"], + name: recipient["name"], + real_name: recipient["real_name"], + status_text: recipient["profile"]["status_text"], + status_emoji: recipient["profile"]["status_emoji"] + ) + end + +end + + + + + diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..cb69034d --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,53 @@ +require 'dotenv' +require 'httparty' + +require_relative 'user' +require_relative 'channel' + +Dotenv.load + +class SlackApiError < StandardError; end + +class Workspace + + attr_reader :users, :channels + + def initialize + @users = User.list_all + @channels = Channel.list_all + end + + def select_user(input) + selected = @users.find { |user| user.slack_id.upcase == input.upcase || user.name.upcase == input.upcase} + + return selected + + end + + def select_channel(input) + selected = @channels.find { |channel| channel.slack_id.upcase == input.upcase || channel.name.upcase == input.upcase} + + return selected + end + + def send_message(selected_item, text) + url = "https://slack.com/api/chat.postMessage" + response = HTTParty.post(url, + body: { + token: ENV["SLACK_TOKEN"], + text: text, + channel: selected_item + }, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } + ) + unless response.parsed_response["ok"] == true + raise SlackApiError, "Error: #{response.parsed_response["error"]}" + end + + return true + end + +end + + + diff --git a/test/cassettes/list_all.yml b/test/cassettes/list_all.yml new file mode 100644 index 00000000..61a8c0fd --- /dev/null +++ b/test/cassettes/list_all.yml @@ -0,0 +1,130 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 04:50:49 GMT + Server: + - Apache + X-Slack-Req-Id: + - 37152ef8f45fea1a70dcbb9c4aab34a7 + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '1305' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-qg4e,haproxy-edge-pdx-6tth + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/80588\/marketing\/img\/avatars\/slackbot\/avatar-slackbot.png","image_512":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth + - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight + Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth + - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack + CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack + CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina + Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina + Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor + Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor + Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor + Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1602026592}],"cache_ts":1602132649,"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 04:50:49 GMT +- request: + method: get + uri: https://slack.com/api/users.list?token=hdbsgf46876534 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 04:50:49 GMT + Server: + - Apache + X-Slack-Req-Id: + - 7650c228a0c2397c0c38fd516407bceb + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-emc9,haproxy-edge-pdx-mprq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 04:50:49 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/list_all_channels.yml b/test/cassettes/list_all_channels.yml new file mode 100644 index 00000000..01cf7111 --- /dev/null +++ b/test/cassettes/list_all_channels.yml @@ -0,0 +1,125 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/conversations.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 18:00:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 26a9d905555b84e74744ec75aabf31de + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,groups:read,mpim:read,im:read,read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '637' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-a5rt,haproxy-edge-pdx-5d11 + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"C01C099NZ9B","name":"random","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"random","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + channel is for... well, everything else. It\u2019s a place for team jokes, + spur-of-the-moment ideas, and funny GIFs. Go wild!","creator":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2},{"id":"C01C9GQEAUU","name":"project","is_channel":true,"is_group":false,"is_im":false,"created":1602026376,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"project","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + *channel* is for working on a project. Hold meetings, share docs, and make + decisions together with your team.","creator":"U01C3J008KV","last_set":1602026376},"previous_names":[],"num_members":2},{"id":"C01CG102ZRP","name":"general","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":true,"unlinked":0,"name_normalized":"general","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + is the one channel that will always include everyone. It\u2019s a great spot + for announcements and team-wide conversations.","creator":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 18:00:57 GMT +- request: + method: get + uri: https://slack.com/api/conversations.list?token=hdbsgf46876534 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 18:00:57 GMT + Server: + - Apache + X-Slack-Req-Id: + - 32a6b2b28f5a007c1e17ff51e552f23c + Referrer-Policy: + - no-referrer + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + X-Slack-Backend: + - r + Access-Control-Allow-Origin: + - "*" + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '0' + Content-Length: + - '55' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-d35h,haproxy-edge-pdx-raf5 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + recorded_at: Thu, 08 Oct 2020 18:00:57 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/send_message.yml b/test/cassettes/send_message.yml new file mode 100644 index 00000000..b3710009 --- /dev/null +++ b/test/cassettes/send_message.yml @@ -0,0 +1,70 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel=C01C099NZ9B + headers: + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 20:46:59 GMT + Server: + - Apache + X-Slack-Req-Id: + - 38034521788f37ec584c215c6d2e3b86 + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '326' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-cn1z,haproxy-edge-pdx-8nlt + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channel":"C01C099NZ9B","ts":"1602190019.000100","message":{"bot_id":"B01C9H7MZS8","type":"message","text":"test + message","user":"U01C3CG762E","ts":"1602190019.000100","team":"T01C099NKQV","bot_profile":{"id":"B01C9H7MZS8","deleted":false,"name":"Slack + CLI project","updated":1602027119,"app_id":"A01BNKYQEET","icons":{"image_36":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_36.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/service_72.png"},"team_id":"T01C099NKQV"}}}' + recorded_at: Thu, 08 Oct 2020 20:46:59 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/send_message_error.yml b/test/cassettes/send_message_error.yml new file mode 100644 index 00000000..351f6f7a --- /dev/null +++ b/test/cassettes/send_message_error.yml @@ -0,0 +1,133 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel=bogus + headers: + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 20:43:01 GMT + Server: + - Apache + X-Slack-Req-Id: + - a90336d918ddaace5fb79748af13136b + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '60' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-6rku,haproxy-edge-pdx-bahq + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"channel_not_found"}' + recorded_at: Thu, 08 Oct 2020 20:43:01 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=test%20message&channel= + headers: + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 20:43:27 GMT + Server: + - Apache + X-Slack-Req-Id: + - 8e5011a21edfae6119dc39f156afd07d + X-Oauth-Scopes: + - channels:read,chat:write,users:read,groups:read,chat:write.public + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '60' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-1obv,haproxy-edge-pdx-xlh0 + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"channel_not_found"}' + recorded_at: Thu, 08 Oct 2020 20:43:28 GMT +recorded_with: VCR 6.0.0 diff --git a/test/cassettes/users_and_channels.yml b/test/cassettes/users_and_channels.yml new file mode 100644 index 00000000..f43aa09a --- /dev/null +++ b/test/cassettes/users_and_channels.yml @@ -0,0 +1,146 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 19:42:53 GMT + Server: + - Apache + X-Slack-Req-Id: + - c8489d248a3e0c7bcdb5dda4cb8f756c + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '1305' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-kg4q,haproxy-edge-pdx-dhzt + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"T01C099NKQV","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/80588\/marketing\/img\/avatars\/slackbot\/avatar-slackbot.png","image_512":"https:\/\/a.slack-edge.com\/80588\/img\/slackbot_512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"U01C3CFAMPC","team_id":"T01C099NKQV","name":"earth_lina_api_projec","deleted":false,"color":"e7392d","real_name":"Earth + - Lina - API Project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight + Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Earth + - Lina - API Project","real_name_normalized":"Earth - Lina - API Project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb6df7c42d8a","api_app_id":"A01CT7LA01E","always_active":false,"bot_id":"B01CT7U95UG","image_24":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b6df7c42d8a304820ae95749339719b6.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0021-512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1602027082},{"id":"U01C3CG762E","team_id":"T01C099NKQV","name":"slack_cli_project","deleted":false,"color":"3c989f","real_name":"Slack + CLI project","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slack + CLI project","real_name_normalized":"Slack CLI project","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gfeb7379424b","api_app_id":"A01BNKYQEET","always_active":false,"bot_id":"B01C9H7MZS8","image_24":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/feb7379424b5c899942463baf6f502c0.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0002-512.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":true,"is_app_user":false,"updated":1602027119},{"id":"U01C3J008KV","team_id":"T01C099NKQV","name":"l.do5147","deleted":false,"color":"9f69e7","real_name":"Lina + Do","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Lina + Do","real_name_normalized":"Lina Do","display_name":"","display_name_normalized":"","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"ef1063468d2b","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_original.jpg","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_24.jpg","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_32.jpg","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_48.jpg","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_72.jpg","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_192.jpg","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_512.jpg","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1411404673618_ef1063468d2b0b3438d3_1024.jpg","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1602026273},{"id":"U01C3J2BULT","team_id":"T01C099NKQV","name":"taylormililani","deleted":false,"color":"4bbe2e","real_name":"Taylor + Tompkins","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Taylor + Tompkins","real_name_normalized":"Taylor Tompkins","display_name":"Taylor + Tompkins","display_name_normalized":"Taylor Tompkins","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"94572139481e","image_original":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_original.png","is_custom_image":true,"image_24":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_24.png","image_32":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_32.png","image_48":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_48.png","image_72":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_72.png","image_192":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_192.png","image_512":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_512.png","image_1024":"https:\/\/avatars.slack-edge.com\/2020-10-06\/1424041252561_94572139481ebc5af1d7_1024.png","status_text_canonical":"","team":"T01C099NKQV"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1602026592}],"cache_ts":1602186173,"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 19:42:54 GMT +- request: + method: get + uri: https://slack.com/api/conversations.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 08 Oct 2020 19:42:54 GMT + Server: + - Apache + X-Slack-Req-Id: + - 351ab97d2fda6b18c9314ee048380b62 + X-Oauth-Scopes: + - chat:write,channels:read,users:read,groups:read,im:read,mpim:read + Access-Control-Expose-Headers: + - x-slack-req-id, retry-after + Access-Control-Allow-Origin: + - "*" + X-Slack-Backend: + - r + X-Content-Type-Options: + - nosniff + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read,groups:read,mpim:read,im:read,read + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, + x-b3-sampled, x-b3-flags + Vary: + - Accept-Encoding + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Content-Length: + - '637' + Content-Type: + - application/json; charset=utf-8 + X-Via: + - haproxy-www-n0tv,haproxy-edge-pdx-r6b3 + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"C01C099NZ9B","name":"random","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"random","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + channel is for... well, everything else. It\u2019s a place for team jokes, + spur-of-the-moment ideas, and funny GIFs. Go wild!","creator":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2},{"id":"C01C9GQEAUU","name":"project","is_channel":true,"is_group":false,"is_im":false,"created":1602026376,"is_archived":false,"is_general":false,"unlinked":0,"name_normalized":"project","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + *channel* is for working on a project. Hold meetings, share docs, and make + decisions together with your team.","creator":"U01C3J008KV","last_set":1602026376},"previous_names":[],"num_members":2},{"id":"C01CG102ZRP","name":"general","is_channel":true,"is_group":false,"is_im":false,"created":1602026270,"is_archived":false,"is_general":true,"unlinked":0,"name_normalized":"general","is_shared":false,"parent_conversation":null,"creator":"U01C3J008KV","is_ext_shared":false,"is_org_shared":false,"shared_team_ids":["T01C099NKQV"],"pending_shared":[],"pending_connected_team_ids":[],"is_pending_ext_shared":false,"is_member":false,"is_private":false,"is_mpim":false,"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"This + is the one channel that will always include everyone. It\u2019s a great spot + for announcements and team-wide conversations.","creator":"U01C3J008KV","last_set":1602026270},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + recorded_at: Thu, 08 Oct 2020 19:42:54 GMT +recorded_with: VCR 6.0.0 diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..cb521013 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,89 @@ +require_relative 'test_helper' + +describe "Channel Class" do + describe "Channel Instantiation" do + before do + @topic = { + "value"=>"plants", + "creator"=>"slackbot", + "last_set"=>1 + } + + @channel = Channel.new( + slack_id: "U015QQ2BXF", + name: "plants_and_gardening", + topic: @topic, + member_count: 2 + ) + end + + it "is an instance of Channel" do + expect(@channel).must_be_kind_of Channel + end + + it "Takes in Channel info" do + expect(@channel).must_respond_to :slack_id + expect(@channel.slack_id).must_equal "U015QQ2BXF" + + expect(@channel).must_respond_to :name + expect(@channel.name).must_equal "plants_and_gardening" + + expect(@channel).must_respond_to :topic + expect(@channel.topic).must_equal @topic + + expect(@channel).must_respond_to :member_count + expect(@channel.member_count).must_equal 2 + end + + it "all of the attributes are correct datatypes" do + expect(@channel.slack_id).must_be_kind_of String + expect(@channel.name).must_be_kind_of String + expect(@channel.topic).must_be_kind_of Hash + expect(@channel.member_count).must_be_kind_of Integer + end + + end + + describe "list_all" do + it "lists all channels" do + VCR.use_cassette("list_all channels") do + + response = Channel.list_all + + expect(response).must_be_kind_of Array + expect(response.first).must_be_instance_of Channel + expect(response.length).must_equal 3 + end + end + + it "correctly list the attributes of the first channel" do + VCR.use_cassette("list_all channels") do + + response = Channel.list_all + + first_channel = response.first + + first_topic = {"value"=>"", "creater"=>"", "last_set"=>0} + + expect(first_channel.slack_id).must_equal "C01C099NZ9B" + expect(first_channel.name).must_equal "random" + expect(first_channel.topic).must_equal first_topic + expect(first_channel.member_count).must_equal 2 + end + end + + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all channels") do + url = "https://slack.com/api/conversations.list" + query_params = {token: "hdbsgf46876534"} + + expect{Channel.get(url, query_params)}.must_raise SlackTokenError + end + end + end + + end + + +end diff --git a/test/recipient_test.rb b/test/recipient_test.rb new file mode 100644 index 00000000..0ab20640 --- /dev/null +++ b/test/recipient_test.rb @@ -0,0 +1,53 @@ +require_relative 'test_helper' + +describe "Recipient Class" do + before do + @recipient = Recipient.new("HD336D", "bobby") + end + + describe "Recipient Instantiation" do + it "is an instance of Recipient" do + expect(@recipient).must_be_kind_of Recipient + end + + it "Takes in Recipient info" do + expect(@recipient).must_respond_to :slack_id + expect(@recipient.slack_id).must_equal "HD336D" + + expect(@recipient).must_respond_to :name + expect(@recipient.name).must_equal "bobby" + end + + it "all of the attributes are correct datatypes" do + expect(@recipient.slack_id).must_be_kind_of String + expect(@recipient.name).must_be_kind_of String + end + + end + + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all channels") do + url = "https://slack.com/api/conversations.list" + query_params = {token: "hdbsgf46876534"} + + expect{Recipient.get(url, query_params)}.must_raise SlackTokenError + end + end + end + + describe "details method" do + it "raises error if invoked through a recipient instance" do + expect{@recipient.details}.must_raise NotImplementedError + end + end + + describe "list all method" do + it "raises error if invoked through a Recipient Class" do + expect{Recipient.list_all}.must_raise NotImplementedError + end + end + + + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 1fcf2bab..6d19559e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,20 +1,22 @@ -require 'simplecov' -SimpleCov.start do - add_filter 'test/' -end +# require 'simplecov' +# SimpleCov.start do +# add_filter 'test/' +# end require 'minitest' require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require 'dotenv' +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" - config.hook_into :webmock -end +require_relative '../lib/user' +require_relative '../lib/channel' +require_relative '../lib/recipient' +require_relative '../lib/workspace' VCR.configure do |config| config.cassette_library_dir = "test/cassettes" # folder where casettes will be located @@ -25,5 +27,7 @@ } # Don't leave our token lying around in a cassette file. - + config.filter_sensitive_data("") do + ENV["SLACK_TOKEN"] + end end diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..c0d05dda --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,91 @@ +require_relative 'test_helper' + +describe "User Class" do + describe "User Instantiation" do + before do + @user = User.new( + slack_id: "U015QQ2BXFZ", + name: "sarah", + real_name: "Sarah Wilson", + status_text: "feeling tired today", + status_emoji: ":she:" + ) + end + + it "is an instance of User" do + expect(@user).must_be_kind_of User + end + + it "Takes in user info" do + expect(@user).must_respond_to :slack_id + expect(@user.slack_id).must_equal "U015QQ2BXFZ" + + expect(@user).must_respond_to :name + expect(@user.name).must_equal "sarah" + + expect(@user).must_respond_to :real_name + expect(@user.real_name).must_equal "Sarah Wilson" + + expect(@user).must_respond_to :status_text + expect(@user.status_text).must_equal "feeling tired today" + + expect(@user).must_respond_to :status_emoji + expect(@user.status_emoji).must_equal ":she:" + end + + it "all of the attributes must be string datatypes" do + expect(@user.slack_id).must_be_kind_of String + expect(@user.name).must_be_kind_of String + expect(@user.real_name).must_be_kind_of String + expect(@user.status_text).must_be_kind_of String + expect(@user.status_emoji).must_be_kind_of String + end + + end + + describe "get method" do + it "raises an error for invalid token" do + VCR.use_cassette("list_all") do + url = "https://slack.com/api/users.list" + query_params = {token: "hdbsgf46876534"} + + expect{User.get(url, query_params)}.must_raise SlackTokenError + end + end + end + + describe "list_all" do + it "lists all users" do + VCR.use_cassette("list_all") do + + response = User.list_all + + expect(response).must_be_kind_of Array + expect(response.first).must_be_instance_of User + expect(response.length).must_equal 5 + end + end + + it "correctly list the attributes of the first user" do + VCR.use_cassette("list_all") do + + response = User.list_all + + first_user = response.first + + expect(first_user.slack_id).must_equal "USLACKBOT" + expect(first_user.name).must_equal "slackbot" + expect(first_user.real_name).must_equal "Slackbot" + expect(first_user.status_text).must_equal "" + expect(first_user.status_emoji).must_equal "" + end + end + + end + + +end + + + + diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..fff48342 --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,104 @@ +require_relative 'test_helper' + +describe "Workspace Class" do + before do + VCR.use_cassette("users and channels") do + @workspace = Workspace.new + end + end + + describe "Workspace Instantiation"do + it "is an instance of Workspace" do + expect(@workspace).must_be_kind_of Workspace + end + + it "be able to read the attributes" do + expect(@workspace).must_respond_to :users + expect(@workspace).must_respond_to :channels + end + + it "all of the attributes are correct datatypes" do + expect(@workspace.users).must_be_kind_of Array + expect(@workspace.channels).must_be_kind_of Array + end + + it "can access the first user and channel" do + first_user = @workspace.users.first + + expect(first_user).must_be_kind_of User + expect(first_user.slack_id).must_equal "USLACKBOT" + expect(first_user.name).must_equal "slackbot" + expect(first_user.real_name).must_equal "Slackbot" + expect(first_user.status_text).must_equal "" + expect(first_user.status_emoji).must_equal "" + + end + + it "can access the first channel" do + first_channel = @workspace.channels.first + first_topic = {"value"=>"", "creater"=>"", "last_set"=>0} + + expect(first_channel).must_be_kind_of Channel + expect(first_channel.slack_id).must_equal "C01C099NZ9B" + expect(first_channel.name).must_equal "random" + expect(first_channel.topic).must_equal first_topic + expect(first_channel.member_count).must_equal 2 + + end + + end + + describe "select user" do + it "correctly selects the user" do + user = @workspace.select_user("USLACKBOT") + expect(user.slack_id).must_equal "USLACKBOT" + end + + it "returns nil if the user is not found" do + user = @workspace.select_user("Bobby") + expect(user).must_be_nil + end + + it "the argument should be case insensitive" do + user = @workspace.select_user("UslackBOT") + expect(user.slack_id).must_equal "USLACKBOT" + end + end + + describe "select channel" do + it "correctly selects the channel" do + channel = @workspace.select_channel("random") + expect(channel.name).must_equal "random" + end + + it "returns nil if the user is not found" do + channel = @workspace.select_channel("plants") + expect(channel).must_be_nil + end + + it "the argument should be case insensitive" do + channel = @workspace.select_channel("RANdom") + expect(channel.name).must_equal "random" + end + end + + describe "send message" do + it "is able to send a message" do + VCR.use_cassette("send message") do + answer = @workspace.send_message("C01C099NZ9B", "test message") + expect(answer).must_equal true + end + end + + it "raises an error for invalid channel" do + VCR.use_cassette("send message error") do + expect{ + @workspace.send_message("bogus", "test message") + }.must_raise SlackApiError + end + end + end + +end + +