lita-default-handlers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f1b1a5bfb8fdadbbdebe5527f1ee37c323eb3e3
4
+ data.tar.gz: 321c892420efece5552b903c567430b0d2f81c4b
5
+ SHA512:
6
+ metadata.gz: e6aca24f50bb044d5ec3460cac57ab7f14d244b7c6b229759cff47232715df1c77c881fcb43bc6cf8003d19acf293e9cc2a922bf14e3a5423f08cb74e2a0d790
7
+ data.tar.gz: b49b1e936bd0e781b4d508dc2082457636ad7d28bf12c3d09009e71d126e3e1293718790aca59e1d7ec8a3ae421acf8cb9cace3a8ff0064317d3816da9dcd242
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rake
5
+ before_install:
6
+ - gem update --system
7
+ services:
8
+ - redis-server
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "lita", path: "../lita"
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013-2015 Jimmy Cuadra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # lita-default-handlers
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lita-default-handlers.png)](http://badge.fury.io/rb/lita-default-handlers)
4
+
5
+ Provides the default set of handlers that ship with Lita itself. The handlers are:
6
+
7
+ * Authorization, for managing authorization groups
8
+ * Help, for listing help strings for other handler routes
9
+ * Info, for basic information about the running robot
10
+ * Room, for making the robot join and part from rooms
11
+ * Users, for finding a user's Lita user ID
12
+
13
+ ## Installation
14
+
15
+ In Lita 5, this gem is a hard dependency of the lita gem itself, so it's not necessary to do anything to install it.
16
+
17
+ In Lita 6:
18
+
19
+ This gem must be included in the Lita project's Gemfile in order to be included, like any other plugin.
20
+ New Lita projects created by `lita new` using Lita 5 or later will include the entry for it in the Gemfile automatically.
21
+ To remove the default handlers, simply remove that line from the Gemfile.
22
+
23
+ ## License
24
+
25
+ [MIT](http://opensource.org/licenses/MIT)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/authorization"
8
+ require "lita/handlers/help"
9
+ require "lita/handlers/info"
10
+ require "lita/handlers/room"
11
+ require "lita/handlers/users"
@@ -0,0 +1,127 @@
1
+ module Lita
2
+ # A namespace to hold all subclasses of {Handler}.
3
+ module Handlers
4
+ # Provides a chat interface for administering authorization groups.
5
+ class Authorization
6
+ extend Handler::ChatRouter
7
+
8
+ route(
9
+ /^auth\s+add/,
10
+ :add,
11
+ command: true,
12
+ restrict_to: :admins,
13
+ help: { t("help.add_key") => t("help.add_value") }
14
+ )
15
+ route(
16
+ /^auth\s+remove/,
17
+ :remove,
18
+ command: true,
19
+ restrict_to: :admins,
20
+ help: { t("help.remove_key") => t("help.remove_value") }
21
+ )
22
+ route(/^auth\s+list/, :list, command: true, restrict_to: :admins, help: {
23
+ t("help.list_key") => t("help.list_value")
24
+ })
25
+
26
+ # Adds a user to an authorization group.
27
+ # @param response [Response] The response object.
28
+ # @return [void]
29
+ def add(response)
30
+ toggle_membership(response, :add_user_to_group, "user_added", "user_already_in")
31
+ end
32
+
33
+ # Removes a user from an authorization group.
34
+ # @param response [Response] The response object.
35
+ # @return [void]
36
+ def remove(response)
37
+ toggle_membership(response, :remove_user_from_group, "user_removed", "user_not_in")
38
+ end
39
+
40
+ # Lists all authorization groups (or only the specified group) and the
41
+ # names of their members.
42
+ # @param response [Response] The response object.
43
+ # @return [void]
44
+ def list(response)
45
+ requested_group = response.args[1]
46
+ output = get_groups_list(response.args[1])
47
+ if output.empty?
48
+ response.reply(empty_state_for_list(requested_group))
49
+ else
50
+ response.reply(output.join("\n"))
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def empty_state_for_list(requested_group)
57
+ if requested_group
58
+ t("empty_state_group", group: requested_group)
59
+ else
60
+ t("empty_state")
61
+ end
62
+ end
63
+
64
+ def get_groups_list(requested_group)
65
+ groups_with_users = robot.auth.groups_with_users
66
+ if requested_group
67
+ requested_group = requested_group.downcase.strip.to_sym
68
+ groups_with_users.select! { |group, _| group == requested_group }
69
+ end
70
+ groups_with_users.map do |group, users|
71
+ user_names = users.map(&:name).join(", ")
72
+ "#{group}: #{user_names}"
73
+ end
74
+ end
75
+
76
+ def toggle_membership(response, method_name, success_key, failure_key)
77
+ return unless valid_message?(response)
78
+
79
+ if robot.auth.public_send(method_name, response.user, @user, @group)
80
+ response.reply t(success_key, user: @user.name, group: @group)
81
+ else
82
+ response.reply t(failure_key, user: @user.name, group: @group)
83
+ end
84
+ end
85
+
86
+ def valid_group?(response, identifier)
87
+ unless identifier && @group
88
+ response.reply "#{t('format')}: #{robot.name} auth add USER GROUP"
89
+ return
90
+ end
91
+
92
+ if @group.downcase.strip == "admins"
93
+ response.reply t("admin_management")
94
+ return
95
+ end
96
+
97
+ true
98
+ end
99
+
100
+ # Validates that incoming messages have the right format and a valid user.
101
+ # Also assigns the user and group to instance variables for the main
102
+ # methods to use later.
103
+ def valid_message?(response)
104
+ _command, identifier, @group = response.args
105
+
106
+ return unless valid_group?(response, identifier)
107
+
108
+ return unless valid_user?(response, identifier)
109
+
110
+ true
111
+ end
112
+
113
+ def valid_user?(response, identifier)
114
+ @user = User.fuzzy_find(identifier)
115
+
116
+ if @user
117
+ true
118
+ else
119
+ response.reply t("no_user_found", identifier: identifier)
120
+ return
121
+ end
122
+ end
123
+ end
124
+
125
+ Lita.register_handler(Authorization)
126
+ end
127
+ end
@@ -0,0 +1,71 @@
1
+ module Lita
2
+ # A namespace to hold all subclasses of {Handler}.
3
+ module Handlers
4
+ # Provides online help about Lita commands for users.
5
+ class Help
6
+ extend Handler::ChatRouter
7
+
8
+ route(/^help\s*(.+)?/, :help, command: true, help: {
9
+ "help" => t("help.help_value"),
10
+ t("help.help_command_key") => t("help.help_command_value")
11
+ })
12
+
13
+ # Outputs help information about Lita commands.
14
+ # @param response [Response] The response object.
15
+ # @return [void]
16
+ def help(response)
17
+ output = build_help(response)
18
+ output = filter_help(output, response)
19
+ response.reply_privately output.join("\n")
20
+ end
21
+
22
+ private
23
+
24
+ # Checks if the user is authorized to at least one of the given groups.
25
+ def authorized?(user, required_groups)
26
+ required_groups.nil? || required_groups.any? do |group|
27
+ robot.auth.user_in_group?(user, group)
28
+ end
29
+ end
30
+
31
+ # Creates an array of help info for all registered routes.
32
+ def build_help(response)
33
+ robot.handlers.map do |handler|
34
+ next unless handler.respond_to?(:routes)
35
+
36
+ handler.routes.map do |route|
37
+ route.help.map do |command, description|
38
+ if authorized?(response.user, route.required_groups)
39
+ help_command(route, command, description)
40
+ end
41
+ end
42
+ end
43
+ end.flatten.compact
44
+ end
45
+
46
+ # Filters the help output by an optional command.
47
+ def filter_help(output, response)
48
+ filter = response.matches[0][0]
49
+
50
+ if filter
51
+ output.select { |line| /(?:@?#{name}[:,]?)?#{filter}/i === line }
52
+ else
53
+ output
54
+ end
55
+ end
56
+
57
+ # Formats an individual command's help message.
58
+ def help_command(route, command, description)
59
+ command = "#{name}: #{command}" if route.command?
60
+ "#{command} - #{description}"
61
+ end
62
+
63
+ # The way the bot should be addressed in order to trigger a command.
64
+ def name
65
+ robot.config.robot.mention_name || robot.config.robot.name
66
+ end
67
+ end
68
+
69
+ Lita.register_handler(Help)
70
+ end
71
+ end
@@ -0,0 +1,62 @@
1
+ module Lita
2
+ # A namespace to hold all subclasses of {Handler}.
3
+ module Handlers
4
+ # Provides information about the currently running robot.
5
+ class Info
6
+ extend Handler::ChatRouter
7
+ extend Handler::HTTPRouter
8
+
9
+ route(/^info$/i, :chat, command: true, help: {
10
+ "info" => t("help.info_value")
11
+ })
12
+
13
+ http.get "/lita/info", :web
14
+
15
+ # Replies with the current version of Lita, the current version of Redis,
16
+ # and Redis memory usage.
17
+ # @param response [Response] The response object.
18
+ # @return [void]
19
+ # @since 3.0.0
20
+ def chat(response)
21
+ response.reply(
22
+ %(Lita #{Lita::VERSION} - https://www.lita.io/),
23
+ %(Redis #{redis_version} - Memory used: #{redis_memory_usage})
24
+ )
25
+ end
26
+
27
+ # Returns JSON with basic information about the robot.
28
+ # @param _request [Rack::Request] The HTTP request.
29
+ # @param response [Rack::Response] The HTTP response.
30
+ # @return [void]
31
+ def web(_request, response)
32
+ response.headers["Content-Type"] = "application/json"
33
+ json = MultiJson.dump(
34
+ adapter: robot.config.robot.adapter,
35
+ lita_version: Lita::VERSION,
36
+ redis_memory_usage: redis_memory_usage,
37
+ redis_version: redis_version,
38
+ robot_mention_name: robot.mention_name,
39
+ robot_name: robot.name
40
+ )
41
+ response.write(json)
42
+ end
43
+
44
+ # A hash of information about Redis.
45
+ def redis_info
46
+ @redis_info ||= redis.info
47
+ end
48
+
49
+ # The current version of Redis.
50
+ def redis_version
51
+ redis_info["redis_version"]
52
+ end
53
+
54
+ # The amount of memory Redis is using.
55
+ def redis_memory_usage
56
+ redis_info["used_memory_human"]
57
+ end
58
+ end
59
+
60
+ Lita.register_handler(Info)
61
+ end
62
+ end
@@ -0,0 +1,34 @@
1
+ module Lita
2
+ # A namespace to hold all subclasses of {Handler}.
3
+ module Handlers
4
+ # Allows administrators to make Lita join and part from rooms.
5
+ # @since 3.0.0
6
+ class Room
7
+ extend Handler::ChatRouter
8
+
9
+ route(/^join\s+(.+)$/i, :join, command: true, restrict_to: :admins, help: {
10
+ t("help.join_key") => t("help.join_value")
11
+ })
12
+
13
+ route(/^part\s+(.+)$/i, :part, command: true, restrict_to: :admins, help: {
14
+ t("help.part_key") => t("help.part_value")
15
+ })
16
+
17
+ # Joins the room with the specified ID.
18
+ # @param response [Response] The response object.
19
+ # @return [void]
20
+ def join(response)
21
+ robot.join(response.args[0])
22
+ end
23
+
24
+ # Parts from the room with the specified ID.
25
+ # @param response [Response] The response object.
26
+ # @return [void]
27
+ def part(response)
28
+ robot.part(response.args[0])
29
+ end
30
+ end
31
+
32
+ Lita.register_handler(Room)
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ module Lita
2
+ module Handlers
3
+ # Provides information on Lita users.
4
+ # @since 4.1.0
5
+ class Users
6
+ extend Handler::ChatRouter
7
+
8
+ route(/^users\s+find\s+(.+)/i, :find, command: true, help: {
9
+ t("help.find_key") => t("help.find_value")
10
+ })
11
+
12
+ # Outputs the name, ID, and mention name of a user matching the search query.
13
+ # @param response [Response] The response object.
14
+ # @return [void]
15
+ def find(response)
16
+ user = User.fuzzy_find(response.args[1])
17
+
18
+ if user
19
+ response.reply(formatted_user(user))
20
+ else
21
+ response.reply(t("find_empty_state"))
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ # Extract and label the relevant user information.
28
+ def formatted_user(user)
29
+ "#{user.name} (ID: #{user.id}, Mention name: #{user.mention_name})"
30
+ end
31
+ end
32
+
33
+ Lita.register_handler(Users)
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-default-handlers"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Jimmy Cuadra"]
5
+ spec.email = ["jimmy@jimmycuadra.com"]
6
+ spec.description = "A set of handlers included by default in Lita."
7
+ spec.summary = "A set of handlers included by default in Lita."
8
+ spec.homepage = "https://github.com/litaio/lita-default-handlers"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_development_dependency "bundler", "~> 1.3"
18
+ spec.add_development_dependency "pry-byebug"
19
+ spec.add_development_dependency "rake"
20
+ spec.add_development_dependency "rack-test"
21
+ spec.add_development_dependency "rspec", ">= 3.0.0"
22
+ spec.add_development_dependency "simplecov"
23
+ spec.add_development_dependency "coveralls"
24
+ end
@@ -0,0 +1,41 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ authorization:
5
+ help:
6
+ add_key: auth add USER GROUP
7
+ add_value: Add USER to authorization group GROUP. Requires admin privileges.
8
+ remove_key: auth remove USER GROUP
9
+ remove_value: Remove USER from authorization group GROUP. Requires admin privileges.
10
+ list_key: "auth list [GROUP]"
11
+ list_value: >-
12
+ List authorization groups and the users in them. If GROUP is supplied,
13
+ only lists that group.
14
+ user_added: "%{user} was added to %{group}."
15
+ user_already_in: "%{user} was already in %{group}."
16
+ user_removed: "%{user} was removed from %{group}."
17
+ user_not_in: "%{user} was not in %{group}."
18
+ empty_state: There are no authorization groups yet.
19
+ empty_state_group: "There is no authorization group named %{group}."
20
+ format: "Format"
21
+ admin_management: Administrators can only be managed via Lita config.
22
+ no_user_found: 'No user was found with the identifier "%{identifier}".'
23
+ help:
24
+ help:
25
+ help_value: Lists help information for terms and command the robot will respond to.
26
+ help_command_key: help COMMAND
27
+ help_command_value: Lists help information for terms or commands that begin with COMMAND.
28
+ info:
29
+ help:
30
+ info_value: Replies with the current version of Lita.
31
+ room:
32
+ help:
33
+ join_key: join ROOM_ID
34
+ join_value: Makes the robot join the room with room ID ROOM_ID.
35
+ part_key: part ROOM_ID
36
+ part_value: Makes the robot part from the room with room ID ROOM_ID.
37
+ users:
38
+ find_empty_state: No matching users found.
39
+ help:
40
+ find_key: users find SEARCH_TERM
41
+ find_value: Find a Lita user by ID, name, or mention name.
@@ -0,0 +1,109 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Authorization, lita_handler: true do
4
+ before do
5
+ allow(robot.auth).to receive(:user_is_admin?).with(user).and_return(true)
6
+ end
7
+
8
+ let(:target_user) { instance_double("Lita::User", id: "1", name: "Carl") }
9
+
10
+ it { is_expected.to route_command("auth add foo bar").to(:add) }
11
+ it { is_expected.to route_command("auth add foo@bar.com baz").to(:add) }
12
+ it { is_expected.to route_command("auth remove foo bar").to(:remove) }
13
+ it { is_expected.to route_command("auth remove foo@bar.com baz").to(:remove) }
14
+ it { is_expected.to route_command("auth list").to(:list) }
15
+ it { is_expected.to route_command("auth list foo").to(:list) }
16
+
17
+ describe "#add" do
18
+ it "replies with the proper format if the require commands are missing" do
19
+ send_command("auth add foo")
20
+ expect(replies.last).to match(/^Format:/)
21
+ end
22
+
23
+ it "replies with a warning if target user is not known" do
24
+ send_command("auth add foo bar")
25
+ expect(replies.last).to match(/No user was found/)
26
+ end
27
+
28
+ it "replies with success if a valid user and group were supplied" do
29
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
30
+ send_command("auth add foo bar")
31
+ expect(replies.last).to eq("#{target_user.name} was added to bar.")
32
+ end
33
+
34
+ it "replies with a warning if the user was already in the group" do
35
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
36
+ send_command("auth add foo bar")
37
+ send_command("auth add foo bar")
38
+ expect(replies.last).to eq("#{target_user.name} was already in bar.")
39
+ end
40
+
41
+ it 'replies with a warning if the group was "admins"' do
42
+ send_command("auth add foo admins")
43
+ expect(replies.last).to match(/Administrators can only be managed/)
44
+ end
45
+ end
46
+
47
+ describe "#remove" do
48
+ before do
49
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
50
+ send_command("auth add foo bar")
51
+ end
52
+
53
+ it "replies with success if a valid user and group were supplied" do
54
+ send_command("auth remove foo bar")
55
+ expect(replies.last).to eq("#{target_user.name} was removed from bar.")
56
+ end
57
+
58
+ it "replies with a warning if the user was already in the group" do
59
+ send_command("auth remove foo bar")
60
+ send_command("auth remove foo bar")
61
+ expect(replies.last).to eq("#{target_user.name} was not in bar.")
62
+ end
63
+
64
+ it 'replies with a warning if the group was "admins"' do
65
+ send_command("auth add foo admins")
66
+ expect(replies.last).to match(/Administrators can only be managed/)
67
+ end
68
+ end
69
+
70
+ describe "#list" do
71
+ context "when there are populated groups" do
72
+ let(:groups) { %i(foo bar) }
73
+ let(:user1) { Lita::User.create(3, name: "Bongo") }
74
+ let(:user2) { Lita::User.create(4, name: "Carl") }
75
+
76
+ before do
77
+ groups.each do |group|
78
+ subject.robot.auth.add_user_to_group(user, user1, group)
79
+ subject.robot.auth.add_user_to_group(user, user2, group)
80
+ end
81
+ end
82
+
83
+ it "lists all authorization groups and their members" do
84
+ send_command("auth list")
85
+ groups.each do |group|
86
+ expect(replies.last).to include(
87
+ "#{group}: #{user1.name}, #{user2.name}"
88
+ )
89
+ end
90
+ end
91
+
92
+ it "lists only the requested group" do
93
+ send_command("auth list foo")
94
+ expect(replies.last).to include("foo")
95
+ expect(replies.last).not_to include("bar")
96
+ end
97
+ end
98
+
99
+ it "replies that there are no groups" do
100
+ send_command("auth list")
101
+ expect(replies.last).to include("no authorization groups yet")
102
+ end
103
+
104
+ it "replies that the specified group doesn't exist" do
105
+ send_command("auth list nothing")
106
+ expect(replies.last).to include("no authorization group named nothing")
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Help, lita_handler: true do
4
+ it { is_expected.to route_command("help").to(:help) }
5
+ it { is_expected.to route_command("help foo").to(:help) }
6
+
7
+ describe "#help" do
8
+ let(:secret_handler_class) do
9
+ Class.new(Lita::Handler) do
10
+ route(/secret/, :secret, restrict_to: :the_nobodies, help: {
11
+ "secret" => "no one should ever see this help message"
12
+ })
13
+ end
14
+ end
15
+
16
+ it "sends help information for all commands" do
17
+ send_command("help")
18
+ expect(replies.last).to match(
19
+ /#{robot.mention_name}: help.+#{robot.mention_name}: help COMMAND/m
20
+ )
21
+ end
22
+
23
+ it "sends help information for commands starting with COMMAND" do
24
+ send_command("help help COMMAND")
25
+ expect(replies.last).to match(/help COMMAND - Lists/)
26
+ expect(replies.last).not_to match(/help - Lists/)
27
+ end
28
+
29
+ it "doesn't crash if a handler doesn't have routes" do
30
+ event_handler = Class.new do
31
+ extend Lita::Handler::EventRouter
32
+ end
33
+
34
+ registry.register_handler(event_handler)
35
+
36
+ expect { send_command("help") }.not_to raise_error
37
+ end
38
+
39
+ describe "restricted routes" do
40
+ let(:authorized_user) do
41
+ user = Lita::User.create(2, name: "Authorized User")
42
+ Lita::Authorization.new(robot).add_user_to_group!(user, :the_nobodies)
43
+ user
44
+ end
45
+
46
+ before { registry.register_handler(secret_handler_class) }
47
+
48
+ it "doesn't show help for commands the user doesn't have access to" do
49
+ send_command("help")
50
+ expect(replies.last).not_to include("secret")
51
+ end
52
+
53
+ it "shows help for restricted routes if the user has access" do
54
+ send_command("help", as: authorized_user)
55
+ expect(replies.last).to include("secret")
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,65 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Info, lita_handler: true do
4
+ it { is_expected.to route_command("info").to(:chat) }
5
+ it { is_expected.to route_http(:get, "/lita/info").to(:web) }
6
+
7
+ let(:request) { double("Rack::Request") }
8
+ let(:response) { Rack::Response.new }
9
+
10
+ describe "#chat" do
11
+ it "responds with the current version of Lita" do
12
+ send_command("info")
13
+ expect(replies.first).to include(Lita::VERSION)
14
+ end
15
+
16
+ it "responds with a link to the website" do
17
+ send_command("info")
18
+ expect(replies.first).to include("lita.io")
19
+ end
20
+
21
+ it "responds with the Redis version and memory usage" do
22
+ send_command("info")
23
+ expect(replies.last).to match(/Redis [\d\.]+ - Memory used: [\d\.]+[BKMG]/)
24
+ end
25
+ end
26
+
27
+ describe "#web" do
28
+ let(:json) { MultiJson.load(response.body.join) }
29
+
30
+ it "returns JSON" do
31
+ subject.web(request, response)
32
+ expect(response.headers["Content-Type"]).to eq("application/json")
33
+ end
34
+
35
+ it "includes the current version of Lita" do
36
+ subject.web(request, response)
37
+ expect(json).to include("lita_version" => Lita::VERSION)
38
+ end
39
+
40
+ it "includes the adapter being used" do
41
+ subject.web(request, response)
42
+ expect(json).to include("adapter" => registry.config.robot.adapter.to_s)
43
+ end
44
+
45
+ it "includes the robot's name" do
46
+ subject.web(request, response)
47
+ expect(json).to include("robot_name" => robot.name)
48
+ end
49
+
50
+ it "includes the robot's mention name" do
51
+ subject.web(request, response)
52
+ expect(json).to include("robot_mention_name" => robot.mention_name)
53
+ end
54
+
55
+ it "includes the Redis version" do
56
+ subject.web(request, response)
57
+ expect(json).to have_key("redis_version")
58
+ end
59
+
60
+ it "includes the Redis memory usage" do
61
+ subject.web(request, response)
62
+ expect(json).to have_key("redis_memory_usage")
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Room, lita_handler: true do
4
+ it { is_expected.to route_command("join #lita.io").to(:join) }
5
+ it { is_expected.to route_command("part #lita.io").to(:part) }
6
+
7
+ before { allow(robot.auth).to receive(:user_is_admin?).with(user).and_return(true) }
8
+
9
+ describe "#join" do
10
+ it "calls Robot#join with the provided ID" do
11
+ expect(robot).to receive(:join).with("#lita.io")
12
+ send_command("join #lita.io")
13
+ end
14
+ end
15
+
16
+ describe "#part" do
17
+ it "calls Robot#part with the provided ID" do
18
+ expect(robot).to receive(:part).with("#lita.io")
19
+ send_command("part #lita.io")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Users, lita_handler: true do
4
+ it { is_expected.to route_command("users find carl").to(:find) }
5
+
6
+ describe "#find" do
7
+ it "finds users by ID" do
8
+ send_command("users find 1")
9
+
10
+ expect(replies.first).to eq("Test User (ID: 1, Mention name: Test User)")
11
+ end
12
+
13
+ it "finds users by name" do
14
+ send_command("users find 'Test User'")
15
+
16
+ expect(replies.first).to eq("Test User (ID: 1, Mention name: Test User)")
17
+ end
18
+
19
+ it "finds users by mention name" do
20
+ Lita::User.create(2, name: "Mr. Pug", mention_name: "carl")
21
+
22
+ send_command("users find carl")
23
+
24
+ expect(replies.first).to eq("Mr. Pug (ID: 2, Mention name: carl)")
25
+ end
26
+
27
+ it "replies with a message when no matches are found" do
28
+ send_command("users find nobody")
29
+
30
+ expect(replies.first).to eq("No matching users found.")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ])
7
+ SimpleCov.start { add_filter "/spec/" }
8
+
9
+ require "lita-default-handlers"
10
+ require "lita/rspec"
11
+
12
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
13
+ # was generated with Lita 4, the compatibility mode should be left disabled.
14
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-default-handlers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Cuadra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A set of handlers included by default in Lita.
112
+ email:
113
+ - jimmy@jimmycuadra.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/lita-default-handlers.rb
125
+ - lib/lita/handlers/authorization.rb
126
+ - lib/lita/handlers/help.rb
127
+ - lib/lita/handlers/info.rb
128
+ - lib/lita/handlers/room.rb
129
+ - lib/lita/handlers/users.rb
130
+ - lita-default-handlers.gemspec
131
+ - locales/en.yml
132
+ - spec/lita/handlers/authorization_spec.rb
133
+ - spec/lita/handlers/help_spec.rb
134
+ - spec/lita/handlers/info_spec.rb
135
+ - spec/lita/handlers/room_spec.rb
136
+ - spec/lita/handlers/users_spec.rb
137
+ - spec/spec_helper.rb
138
+ - templates/.gitkeep
139
+ homepage: https://github.com/litaio/lita-default-handlers
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ lita_plugin_type: handler
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.5.1
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: A set of handlers included by default in Lita.
164
+ test_files:
165
+ - spec/lita/handlers/authorization_spec.rb
166
+ - spec/lita/handlers/help_spec.rb
167
+ - spec/lita/handlers/info_spec.rb
168
+ - spec/lita/handlers/room_spec.rb
169
+ - spec/lita/handlers/users_spec.rb
170
+ - spec/spec_helper.rb