diff --git a/.gitignore b/.gitignore index 8d6a243f..b1d9ae7c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,10 @@ /tmp/ # Used by dotenv library to load environment variables. -# .env +.env + +# Ignore environment variables +.env ## Specific to RubyMotion: .dat* @@ -48,9 +51,3 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc - -# Ignore environemnt variables -.env - -# Ignore cassette files -/specs/cassettes/ diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..5ff647ea --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,12 @@ +module Slack + class Channel + attr_reader :channel_name, :topic, :member_count, :slack_id + + def initialize(channel_name:, topic:, member_count:, slack_id:) + @channel_name = channel_name + @topic = topic + @member_count = member_count + @slack_id = slack_id + end + end +end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..174877c7 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,119 @@ -#!/usr/bin/env ruby +require_relative "workspace" +require_relative "user" +require_relative "channel" +require 'httparty' +require "dotenv" +Dotenv.load def main - puts "Welcome to the Ada Slack CLI!" + puts "Welcome to the Ada Slack CLI!\n" + workspace = Slack::Workspace.new + workspace.user_list + workspace.channel_list + + puts "\nDarn Cute Puppers has #{workspace.users.count} users and #{workspace.channels.count} channels.\n" - # TODO project + prompt = "\nPlease select from the following options: + List Users + List Channels + Select User + Select Channel + Quit\n" + puts prompt + + while command = gets.chomp.downcase + case command + when "quit" + exit + when "list users" + puts workspace.print_user_list + puts prompt + when "list channels" + puts workspace.print_channel_list + puts prompt + when "select user" + puts "Please enter a username or Slack ID." + username = gets.chomp.downcase + search_result = workspace.search("user", username) + if search_result == nil + puts "User not found. Returning to main menu..." + puts prompt + break + else + puts "User found." + options = "Please select from the following options: + Show Details + Send Message + Main Menu" + puts options + while selected_command = gets.chomp.downcase + case selected_command + when "main menu" + puts prompt + break + when "show details" + puts workspace.show_details(search_result) + puts options + when "send message" + puts "What message do you want to send?" + message_body = gets.chomp + convert_to_username = workspace.selected.username + workspace.send_message(message_body, "@#{convert_to_username}") + puts "Message sent. Returning to main menu..." + puts prompt + break + else + puts "Invalid input. Returning to main menu..." + puts prompt + break + end + end + end + when "select channel" + puts "Please enter a channel name or Slack ID." + channel_name = gets.chomp.downcase + search_result = workspace.search("channel", channel_name) + if search_result == nil + puts "Channel not found. Returning to main menu..." + puts prompt + break + else + puts "Channel found." + options = "Please select from the following options: + Show Details + Send Message + Main Menu" + puts options + while selected_command = gets.chomp.downcase + case selected_command + when "main menu" + puts prompt + break + when "show details" + puts workspace.show_details(search_result) + puts options + when "send message" + puts "What message do you want to send?" + message_body = gets.chomp + convert_to_channel_name = workspace.selected.channel_name + workspace.send_message(message_body, convert_to_channel_name) + puts "Message sent. Returning to main menu..." + puts prompt + break + else + puts "Invalid input. Returning to main menu..." + puts prompt + break + end + end + end + else + puts "Invalid input. Returning to main menu..." + break + 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..0ae88252 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,11 @@ +module Slack + class User + attr_reader :username, :real_name, :slack_id + + def initialize(username:, real_name:, slack_id:) + @username = username + @real_name = real_name + @slack_id = slack_id + end + end +end diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..0ef2328d --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,118 @@ +require_relative "user" +require_relative "channel" + +module Slack + class SlackApiError < StandardError; end + class Workspace + USER_URL = "https://slack.com/api/users.list" + CHANNEL_URL = "https://slack.com/api/channels.list" + POST_MSG_URL = "https://slack.com/api/chat.postMessage" + + attr_reader :users, :channels, :selected + + def initialize + @users = [] + @channels = [] + @selected = nil + end + + def get_api(url) + query_parameters = { + token: ENV['SLACK_TOKEN'] + } + return HTTParty.get(url, query: query_parameters) + end + + def user_list + api_user_response = get_api(USER_URL) + api_members = api_user_response["members"] + api_members.each do |each_member| + username = each_member["name"] + real_name = each_member["real_name"] + slack_id = each_member["id"] + @users << Slack::User.new(username: username, real_name: real_name, slack_id: slack_id) + end + end + + def print_user_list + user_counter = 0 + result = '' + @users.each do |each_user| + user_counter += 1 + result = result + "User #{user_counter} - username: #{each_user.username}, real name: #{each_user.real_name}, Slack ID: #{each_user.slack_id}\n" + end + return result + end + + def channel_list + api_channel_response = get_api(CHANNEL_URL) + api_channels = api_channel_response["channels"] + api_channels.each do |each_channel| + channel_name = each_channel["name"] + topic = each_channel["topic"]["value"] + member_count = each_channel["num_members"] + slack_id = each_channel["id"] + @channels << Channel.new(channel_name: channel_name, topic: topic, member_count: member_count, slack_id: slack_id) + end + end + + def print_channel_list + channel_counter = 0 + result = '' + @channels.each do |each_channel| + channel_counter += 1 + result = result + "Channel #{channel_counter} - channel name: #{each_channel.channel_name}, topic: #{each_channel.topic}, member count: #{each_channel.member_count}, Slack ID: #{each_channel.slack_id}\n" + end + return result + end + + def search(data_source, query_term) + @selected = nil + if data_source == "user" + @users.each do |current_user| + if current_user.username == query_term || current_user.slack_id == query_term.upcase + @selected = current_user + break + end + end + return @selected + elsif data_source == "channel" + @channels.each do |current_channel| + if current_channel.channel_name == query_term || current_channel.slack_id == query_term.upcase + @selected = current_channel + break + end + end + return @selected + end + end + + def show_details(selected_receiver) + details = "" + if selected_receiver.class == Slack::User + details = details + "Username: #{@selected.username}, Real name: #{@selected.real_name}, Slack ID: #{@selected.slack_id}" + return details + elsif selected_receiver.class == Slack::Channel + details = details + "Channel name: #{@selected.channel_name}, Topic: #{@selected.topic}, Member count: #{@selected.member_count}, Slack ID: #{@selected.slack_id}" + return details + end + end + + def send_message(message, selected_receiver) + response = HTTParty.post( + POST_MSG_URL, + body: { + token: ENV['SLACK_TOKEN'], + text: message, + channel: selected_receiver + }, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } + ) + + unless response.parsed_response["ok"] + raise SlackApiError, "Error when posting #{message} to #{selected_receiver}, error: #{response.parsed_response["error"]}" + end + return response + end + end +end \ No newline at end of file diff --git a/test/cassettes/send_message_channel.yml b/test/cassettes/send_message_channel.yml new file mode 100644 index 00000000..3d57fd13 --- /dev/null +++ b/test/cassettes/send_message_channel.yml @@ -0,0 +1,139 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=This%20post%20should%20not%20work&channel=invalid-channel + 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: + - Thu, 12 Sep 2019 22:40:40 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - e094058c-f000-470e-a3a5-b2bedfd6cf3d + 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-xtep + X-Cache: + - Miss from cloudfront + Via: + - 1.1 2e20768704c71ff3ce2e677251d27f3c.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - g-NaQYyv1aaa-hay89RWQ_2lWk7FWi84nD2CKPmws_rn2CwbYUmiAQ== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"channel_not_found"}' + http_version: + recorded_at: Thu, 12 Sep 2019 22:40:40 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=Hi%20Pupper%20Friends%21&channel=random + 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: + - Thu, 12 Sep 2019 22:40:40 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 7eed6677-9bab-4b11-9ff0-b517f0121a34 + 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-6he5 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 aabd01c4a20dae837d162bd972422efc.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - gM6EEaMzkrb_Vb-oJMTSbBCohlpSfqOSVpSxMWupRRdkjc-6LuJ14g== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"CN689KKBP","ts":"1568328040.000200","message":{"type":"message","subtype":"bot_message","text":"Hi + Pupper Friends!","ts":"1568328040.000200","username":"Leaves - Kristina & + Ga-Young","bot_id":"BN8EPGQ4E"}}' + http_version: + recorded_at: Thu, 12 Sep 2019 22:40:40 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/send_message_user.yml b/test/cassettes/send_message_user.yml new file mode 100644 index 00000000..a71bc3a9 --- /dev/null +++ b/test/cassettes/send_message_user.yml @@ -0,0 +1,273 @@ +--- +http_interactions: +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=Hi%20Pupper%20Friends%21&channel=%40username + 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: + - Thu, 12 Sep 2019 23:37:18 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - fb620464-d989-49ae-bf71-a6a2055466a4 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:user,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-lx8n + X-Cache: + - Miss from cloudfront + Via: + - 1.1 1570d93226c1bbca2ebaad510cff3e0d.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - iXyEQik3RZXlOq-OCuDEKmTRyAAzxitEIcWMPYR8GjrkV0LsHo7T8Q== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"channel_not_found"}' + http_version: + recorded_at: Thu, 12 Sep 2019 23:37:18 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=Hi%20Pupper%20Friends%21&channel=%40kristina.tanya + 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: + - Thu, 12 Sep 2019 23:37:33 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 21228778-49d0-4836-b825-0856eeb738a8 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:user,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-4tyc + X-Cache: + - Miss from cloudfront + Via: + - 1.1 e5147bed59b539c23be4f2e01cf6f6f5.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - f_SQTQpsGwCR6L-qT8cYOAwENDIM8PfZUPdmeZdK7DUs44dO_EknkQ== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DMUPUKT5Z","ts":"1568331453.000200","message":{"type":"message","subtype":"bot_message","text":"Hi + Pupper Friends!","ts":"1568331453.000200","username":"Leaves - Kristina & + Ga-Young","bot_id":"BN8EPGQ4E"}}' + http_version: + recorded_at: Thu, 12 Sep 2019 23:37:33 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=This%20post%20should%20not%20work&channel=%40invaliduser + 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: + - Thu, 12 Sep 2019 23:38:06 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 52c511d7-d9aa-4091-84e7-d2beb85e8eca + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:user,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-ecjy + X-Cache: + - Miss from cloudfront + Via: + - 1.1 d8792dbd3191bbe722eba5b536b979c8.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C2 + X-Amz-Cf-Id: + - Dsx1_-w98m2eu8oblThqQujr061xiYIT_7nFvyxtOul0VXc9hJfR6Q== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"channel_not_found"}' + http_version: + recorded_at: Thu, 12 Sep 2019 23:38:06 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&text=This%20post%20should%20not%20work&channel=%40invalid_user + 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: + - Thu, 12 Sep 2019 23:38:48 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - c39ad379-4606-45dc-80fb-4c21a18c03ea + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:user,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-xqej + X-Cache: + - Miss from cloudfront + Via: + - 1.1 112d82578d402a38d8d02e8b857617e1.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C2 + X-Amz-Cf-Id: + - tnW-RqF5jQDWF_0Yms2gLVy57hMzCjjPVIWgxrK5cJx4o7TwqN3W4g== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"channel_not_found"}' + http_version: + recorded_at: Thu, 12 Sep 2019 23:38:48 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/user.yml b/test/cassettes/user.yml new file mode 100644 index 00000000..e69de29b diff --git a/test/cassettes/workspace.yml b/test/cassettes/workspace.yml new file mode 100644 index 00000000..e69de29b diff --git a/test/cassettes/workspace_channel.yml b/test/cassettes/workspace_channel.yml new file mode 100644 index 00000000..92e8aaf6 --- /dev/null +++ b/test/cassettes/workspace_channel.yml @@ -0,0 +1,82 @@ +--- +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: + - '681' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 21:45:26 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 368d4ab9-c9ad-41a9-87d8-46bf30b69889 + 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-1nva + X-Cache: + - Miss from cloudfront + Via: + - 1.1 324a68a6c25ee50d774953f3e15a611d.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C2 + X-Amz-Cf-Id: + - NldFLHawcoJ5pFvEsCbJu4uRzTmZ_xFCGSpLwbe-Z7RgAzoH8k0_GA== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CMUPUL1R9","name":"pupper-pics","is_channel":true,"created":1568067981,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UMTERDBPU","name_normalized":"pupper-pics","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UMTERDBPU","UMURAK10T","UMURAL35H","UN69JKYQ0","UN69RK75K","UN85KTCHM"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"To + share darn cute pupper pics!","creator":"UMTERDBPU","last_set":1568081755},"previous_names":[],"num_members":6},{"id":"CMWARHPNF","name":"sploots","is_channel":true,"created":1568159409,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UN69RK75K","name_normalized":"sploots","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UMTERDBPU","UMURAK10T","UMURAL35H","UN69JKYQ0","UN69RK75K","UN85KTCHM"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"look + at cute doggy butts and toes","creator":"UN69RK75K","last_set":1568159410},"previous_names":[],"num_members":6},{"id":"CN5PU7BBN","name":"general","is_channel":true,"created":1568067980,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UMTERDBPU","name_normalized":"general","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UMTERDBPU","UMURAK10T","UMURAL35H","UN69JKYQ0","UN69RK75K","UN85KTCHM"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UMTERDBPU","last_set":1568067980},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UMTERDBPU","last_set":1568067980},"previous_names":[],"num_members":6},{"id":"CN689KKBP","name":"random","is_channel":true,"created":1568067980,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UMTERDBPU","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UMTERDBPU","UMURAK10T","UMURAL35H","UN69JKYQ0","UN69RK75K","UN85KTCHM"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UMTERDBPU","last_set":1568067980},"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":"UMTERDBPU","last_set":1568067980},"previous_names":[],"num_members":6}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 21:45:26 GMT +recorded_with: VCR 5.0.0 diff --git a/test/cassettes/workspace_user.yml b/test/cassettes/workspace_user.yml new file mode 100644 index 00000000..9d64162e --- /dev/null +++ b/test/cassettes/workspace_user.yml @@ -0,0 +1,86 @@ +--- +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: + - '1463' + Connection: + - keep-alive + Date: + - Wed, 11 Sep 2019 21:13:49 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 622eef41-585c-4cc4-b37b-717ce3bb3e35 + 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-x0x4 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 ec2a2c75c16156e4d43504606c118b91.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - SEA19-C1 + X-Amz-Cf-Id: + - 8rd_VRwpAz-Vjb5DL9LO2ubPWuPwIHSEL64z8jmk2tJrcePl3yfZ6Q== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TN843TM63","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":"TN843TM63"},"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":"UMTERDBPU","team_id":"TN843TM63","name":"kristina.tanya","deleted":false,"color":"9f69e7","real_name":"Kristina + M","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Kristina + M","real_name_normalized":"Kristina M","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g3e9b0330d1c","first_name":"Kristina","last_name":"M","image_24":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/3e9b0330d1c2e104f1d9dbe0c6d81104.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0021-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568159573,"has_2fa":false},{"id":"UMURAK10T","team_id":"TN843TM63","name":"rvesteinsdottir","deleted":false,"color":"3c989f","real_name":"Raisah","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Raisah","real_name_normalized":"Raisah","display_name":"Raisah","display_name_normalized":"Raisah","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g1166bfab69b","image_24":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/1166bfab69bc4d1c79dcb245ee03288a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0026-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568071907,"has_2fa":false},{"id":"UMURAL35H","team_id":"TN843TM63","name":"dnsanche","deleted":false,"color":"e96699","real_name":"Daniela + Sanchez","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Daniela + Sanchez","real_name_normalized":"Daniela Sanchez","display_name":"Daniela + Sanchez","display_name_normalized":"Daniela Sanchez","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb0d197e7328","image_24":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b0d197e73289b9054faa0517726ff139.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0006-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568072127,"has_2fa":false},{"id":"UN69JKYQ0","team_id":"TN843TM63","name":"cloudylopez","deleted":false,"color":"674b1b","real_name":"Cloudy + Lopez","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Cloudy + Lopez","real_name_normalized":"Cloudy Lopez","display_name":"Cloudy Lopez","display_name_normalized":"Cloudy + Lopez","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g877656756b0","image_24":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/877656756b0aed370014507bf3552664.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0009-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568071916,"has_2fa":false},{"id":"UN69RK75K","team_id":"TN843TM63","name":"gyjinn","deleted":false,"color":"4bbe2e","real_name":"Ga-Young","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Ga-Young","real_name_normalized":"Ga-Young","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g816359af264","image_24":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/816359af2646dc0038a37f6704efd389.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0005-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568130004,"has_2fa":false},{"id":"UN85KTCHM","team_id":"TN843TM63","name":"alicesunhi","deleted":false,"color":"e7392d","real_name":"taro","tz":"America\/Los_Angeles","tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"taro","real_name_normalized":"taro","display_name":"taro","display_name_normalized":"taro","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gb68b3fce35e","image_24":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/b68b3fce35eedec31832996b6e7a6bd3.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F80588%2Fimg%2Favatars%2Fuser_shapes%2Fava_0017-512.png","status_text_canonical":"","team":"TN843TM63"},"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":1568185310,"has_2fa":false}],"cache_ts":1568236429,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 11 Sep 2019 21:13:49 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..19d59774 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,12 @@ +require_relative 'test_helper' + +describe "Channel" do + describe "initialize" do + it "establishes the base attributes when instantiated" do + channel = Slack::Channel.new(channel_name:"#random", topic:"pupper toes", member_count: 6, slack_id:"CMUPUL1R9") + [:channel_name, :topic, :member_count, :slack_id].each do |attribute| + expect(channel).must_respond_to attribute + end + end + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 90aeb408..1cb1db93 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,11 +7,26 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require "webmock/minitest" require 'vcr' +require "dotenv" +require 'httparty' +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require_relative '../lib/user' +require_relative '../lib/channel' +require_relative '../lib/workspace' + VCR.configure do |config| config.cassette_library_dir = "test/cassettes" config.hook_into :webmock -end + 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 + } + config.filter_sensitive_data("") do + ENV["SLACK_TOKEN"] + end +end \ No newline at end of file diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..219c75e2 --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,12 @@ +require_relative 'test_helper' + +describe "User" do + describe "initialize" do + it "establishes the base attributes when instantiated" do + user = Slack::User.new(username: "SlackBot", real_name: "SlackBot", slack_id: "W012A3CDE") + [:username, :real_name, :slack_id].each do |attribute| + expect(user).must_respond_to attribute + end + end + end +end \ No newline at end of file diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..beb19fb6 --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,238 @@ +require_relative 'test_helper' + +describe "Workspace" do + describe "initialize" do + it "can be initialized" do + workspace = Slack::Workspace.new + expect(workspace).must_be_instance_of Slack::Workspace + end + end + + describe "workspace user list methods" do + describe "get_api" do + it "returns a response from Slack api" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + @response = workspace.get_api("https://slack.com/api/users.list") + end + expect(@response).must_be_instance_of HTTParty::Response + end + end + + describe "user_list" do + it "populates the a list of users inside Workspace" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + end + expect(workspace.users).must_be_kind_of Array + expect(workspace.users[0]).must_be_instance_of Slack::User + expect(workspace.users.length).must_equal 7 + end + end + + describe "print_user_list" do + it "prints a string with user info" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + @user_info_string = workspace.print_user_list + end + expect(@user_info_string).must_be_kind_of String + end + end + end + + describe "workspace channel list methods" do + describe "get_api" do + it "returns a response from Slack api" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + @response = workspace.get_api("https://slack.com/api/channels.list") + end + expect(@response).must_be_instance_of HTTParty::Response + end + end + + describe "channel_list" do + it "populates the a list of channels inside Workspace" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + end + expect(workspace.channels).must_be_kind_of Array + expect(workspace.channels[0]).must_be_instance_of Slack::Channel + expect(workspace.channels.length).must_equal 4 + end + end + + describe "print_channel_list" do + it "prints a string with channel info" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + @channel_info_string = workspace.print_channel_list + end + expect(@channel_info_string).must_be_kind_of String + end + end + end + + describe "workspace select methods" do + describe ".search users" do + it "will search through usernames and save that user to selected" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + workspace.search("user", "slackbot") + end + workspace.print_user_list + expect(workspace.selected).must_be_instance_of Slack::User + expect(workspace.selected.username).must_equal "slackbot" + end + + it "if username does not exist it will set the value of @selected to nil" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + workspace.search("user", "slackboot") + end + workspace.print_user_list + expect(workspace.selected).must_be_nil + end + + it "will search through user slack_ids and save that user to selected" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + workspace.search("user", "USLACKBOT") + end + workspace.print_user_list + expect(workspace.selected).must_be_instance_of Slack::User + expect(workspace.selected.slack_id).must_equal "USLACKBOT" + end + + it "if user slack_id does not exist it will set the value of selected to nil" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + workspace.search("user", "USLACKBOOT") + end + workspace.print_user_list + expect(workspace.selected).must_be_nil + end + + describe ".search channels" do + it "will search through channel names and save that channel to selected" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + workspace.search("channel", "random") + end + workspace.print_channel_list + expect(workspace.selected).must_be_instance_of Slack::Channel + expect(workspace.selected.channel_name).must_equal "random" + end + + it "if channel name does not exist it will set the value of selected to nil" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + workspace.search("channel", "randomz") + end + workspace.print_channel_list + expect(workspace.selected).must_be_nil + end + + it "will search through channel slack_ids and save that channel to selected" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + workspace.search("channel", "CN689KKBP") + end + workspace.print_channel_list + expect(workspace.selected).must_be_instance_of Slack::Channel + expect(workspace.selected.slack_id).must_equal "CN689KKBP" + end + + it "if channel slack_id does not exist it will set the value of selected to nil" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + workspace.search("channel", "CN8888888") + end + workspace.print_channel_list + expect(workspace.selected).must_be_nil + end + end + end + end + + describe "workspace show details methods" do + describe ".show_details for user" do + it "will print the details of the selected user" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_user") do + workspace.user_list + workspace.search("user", "USLACKBOT") + @selected = workspace.users[0] + end + expect(workspace.show_details(@selected)).must_be_kind_of String + expect(@selected).must_be_instance_of Slack::User + end + end + + describe ".show_details for channel" do + it "will print the details of the selected channel" do + workspace = Slack::Workspace.new + VCR.use_cassette("workspace_channel") do + workspace.channel_list + workspace.search("channel", "random") + @selected = workspace.channels.last + end + expect(workspace.show_details(@selected)).must_be_kind_of String + expect(@selected).must_be_instance_of Slack::Channel + end + end + end + + describe "workspace send message methods" do + it "if a channel is selected it will send a direct message to a channel" do + workspace = Slack::Workspace.new + VCR.use_cassette("send_message_channel") do + @response = workspace.send_message("Hi Pupper Friends!", "random") + end + expect(@response).must_be_instance_of HTTParty::Response + end + + it "will raise an error when given an invalid channel" do + workspace = Slack::Workspace.new + VCR.use_cassette("send_message_channel") do + exception = expect { + workspace.send_message("This post should not work", "invalid-channel") + }.must_raise Slack::SlackApiError + + expect(exception.message).must_equal 'Error when posting This post should not work to invalid-channel, error: channel_not_found' + end + end + + it "if a user is selected it will send a direct message to a user" do + workspace = Slack::Workspace.new + VCR.use_cassette("send_message_user") do + @response = workspace.send_message("Hi Pupper Friends!", "@kristina.tanya") + end + expect(@response).must_be_instance_of HTTParty::Response + end + + it "will raise an error when given an invalid user" do + workspace = Slack::Workspace.new + VCR.use_cassette("send_message_user") do + exception = expect { + workspace.send_message("This post should not work", "@invalid_user") + }.must_raise Slack::SlackApiError + + expect(exception.message).must_equal 'Error when posting This post should not work to @invalid_user, error: channel_not_found' + end + end + end +end \ No newline at end of file diff --git a/token_verification_test.rb b/token_verification_test.rb new file mode 100644 index 00000000..9788ecb1 --- /dev/null +++ b/token_verification_test.rb @@ -0,0 +1,25 @@ +require 'dotenv' +require 'httparty' + +Dotenv.load + +unless ENV['SLACK_TOKEN'] + puts "Could not load API key, please store in the environment variable 'SLACK_TOKEN'" + exit +end + +URL = "https://slack.com/api/channels.list" + +query_parameters = { + token: ENV['SLACK_TOKEN'] +} + +response = HTTParty.get(URL, query: query_parameters) + +if response['ok'] == false + puts "Something went wrong: #{response['error']}" +else + response["channels"].each do |channel| + p "Channel: #{channel["name"]}" + end +end \ No newline at end of file