rita 0.1.0 → 5.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +51 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +16 -0
  6. data/CONTRIBUTING.md +18 -0
  7. data/Gemfile +5 -0
  8. data/{LICENSE.txt → LICENSE} +1 -3
  9. data/README.md +17 -24
  10. data/Rakefile +5 -5
  11. data/bin/lita +7 -0
  12. data/lib/lita/adapter.rb +147 -0
  13. data/lib/lita/adapters/shell.rb +126 -0
  14. data/lib/lita/adapters/test.rb +62 -0
  15. data/lib/lita/authorization.rb +112 -0
  16. data/lib/lita/callback.rb +39 -0
  17. data/lib/lita/cli.rb +218 -0
  18. data/lib/lita/configurable.rb +47 -0
  19. data/lib/lita/configuration_builder.rb +247 -0
  20. data/lib/lita/configuration_validator.rb +95 -0
  21. data/lib/lita/default_configuration.rb +122 -0
  22. data/lib/lita/errors.rb +25 -0
  23. data/lib/lita/handler/chat_router.rb +141 -0
  24. data/lib/lita/handler/common.rb +208 -0
  25. data/lib/lita/handler/event_router.rb +84 -0
  26. data/lib/lita/handler/http_router.rb +31 -0
  27. data/lib/lita/handler.rb +15 -0
  28. data/lib/lita/handlers/authorization.rb +129 -0
  29. data/lib/lita/handlers/help.rb +171 -0
  30. data/lib/lita/handlers/info.rb +66 -0
  31. data/lib/lita/handlers/room.rb +36 -0
  32. data/lib/lita/handlers/users.rb +37 -0
  33. data/lib/lita/http_callback.rb +46 -0
  34. data/lib/lita/http_route.rb +83 -0
  35. data/lib/lita/logger.rb +43 -0
  36. data/lib/lita/message.rb +124 -0
  37. data/lib/lita/middleware_registry.rb +39 -0
  38. data/lib/lita/namespace.rb +29 -0
  39. data/lib/lita/plugin_builder.rb +43 -0
  40. data/lib/lita/rack_app.rb +100 -0
  41. data/lib/lita/registry.rb +164 -0
  42. data/lib/lita/response.rb +65 -0
  43. data/lib/lita/robot.rb +273 -0
  44. data/lib/lita/room.rb +119 -0
  45. data/lib/lita/route_validator.rb +82 -0
  46. data/lib/lita/rspec/handler.rb +127 -0
  47. data/lib/lita/rspec/matchers/chat_route_matcher.rb +53 -0
  48. data/lib/lita/rspec/matchers/event_route_matcher.rb +29 -0
  49. data/lib/lita/rspec/matchers/http_route_matcher.rb +34 -0
  50. data/lib/lita/rspec.rb +48 -0
  51. data/lib/lita/source.rb +81 -0
  52. data/lib/lita/store.rb +23 -0
  53. data/lib/lita/target.rb +3 -0
  54. data/lib/lita/template.rb +71 -0
  55. data/lib/lita/template_resolver.rb +52 -0
  56. data/lib/lita/timer.rb +49 -0
  57. data/lib/lita/user.rb +157 -0
  58. data/lib/lita/util.rb +31 -0
  59. data/lib/lita/version.rb +6 -0
  60. data/lib/lita.rb +166 -0
  61. data/lib/rita.rb +2 -7
  62. data/rita.gemspec +50 -0
  63. data/spec/lita/adapter_spec.rb +54 -0
  64. data/spec/lita/adapters/shell_spec.rb +99 -0
  65. data/spec/lita/authorization_spec.rb +122 -0
  66. data/spec/lita/configuration_builder_spec.rb +247 -0
  67. data/spec/lita/configuration_validator_spec.rb +114 -0
  68. data/spec/lita/default_configuration_spec.rb +242 -0
  69. data/spec/lita/handler/chat_router_spec.rb +236 -0
  70. data/spec/lita/handler/common_spec.rb +289 -0
  71. data/spec/lita/handler/event_router_spec.rb +121 -0
  72. data/spec/lita/handler/http_router_spec.rb +155 -0
  73. data/spec/lita/handler_spec.rb +62 -0
  74. data/spec/lita/handlers/authorization_spec.rb +111 -0
  75. data/spec/lita/handlers/help_spec.rb +124 -0
  76. data/spec/lita/handlers/info_spec.rb +67 -0
  77. data/spec/lita/handlers/room_spec.rb +24 -0
  78. data/spec/lita/handlers/users_spec.rb +35 -0
  79. data/spec/lita/logger_spec.rb +28 -0
  80. data/spec/lita/message_spec.rb +178 -0
  81. data/spec/lita/plugin_builder_spec.rb +41 -0
  82. data/spec/lita/response_spec.rb +62 -0
  83. data/spec/lita/robot_spec.rb +285 -0
  84. data/spec/lita/room_spec.rb +136 -0
  85. data/spec/lita/rspec/handler_spec.rb +33 -0
  86. data/spec/lita/rspec_spec.rb +162 -0
  87. data/spec/lita/source_spec.rb +68 -0
  88. data/spec/lita/store_spec.rb +23 -0
  89. data/spec/lita/template_resolver_spec.rb +42 -0
  90. data/spec/lita/template_spec.rb +52 -0
  91. data/spec/lita/timer_spec.rb +32 -0
  92. data/spec/lita/user_spec.rb +167 -0
  93. data/spec/lita/util_spec.rb +18 -0
  94. data/spec/lita_spec.rb +227 -0
  95. data/spec/spec_helper.rb +35 -0
  96. data/spec/templates/basic.erb +1 -0
  97. data/spec/templates/basic.irc.erb +1 -0
  98. data/spec/templates/helpers.erb +1 -0
  99. data/spec/templates/interpolated.erb +1 -0
  100. data/templates/locales/en.yml +137 -0
  101. data/templates/plugin/Gemfile +5 -0
  102. data/templates/plugin/README.tt +29 -0
  103. data/templates/plugin/Rakefile +8 -0
  104. data/templates/plugin/gemspec.tt +27 -0
  105. data/templates/plugin/gitignore +18 -0
  106. data/templates/plugin/lib/lita/plugin_type/plugin.tt +19 -0
  107. data/templates/plugin/lib/plugin.tt +16 -0
  108. data/templates/plugin/locales/en.yml.tt +4 -0
  109. data/templates/plugin/spec/lita/plugin_type/plugin_spec.tt +6 -0
  110. data/templates/plugin/spec/spec_helper.tt +8 -0
  111. data/templates/plugin/templates/gitkeep +0 -0
  112. data/templates/robot/Gemfile +5 -0
  113. data/templates/robot/lita_config.rb +28 -0
  114. metadata +386 -20
  115. data/.standard.yml +0 -3
  116. data/CHANGELOG.md +0 -5
  117. data/CODE_OF_CONDUCT.md +0 -132
  118. data/lib/rita/version.rb +0 -5
  119. data/sig/rita.rbs +0 -4
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ handler = Class.new do
6
+ extend Lita::Handler::HTTPRouter
7
+
8
+ namespace "test"
9
+
10
+ http.get "web", :web
11
+ http.post "path/with/:id", :variable
12
+ http.link "foo", :foo
13
+ http.get "heres/*a/glob/in/a/path", :glob
14
+ http.get ":var/otherwise/identical/path", :constraint, var: /\d+/
15
+ http.get ":var/otherwise/identical/path", :no_constraint
16
+ http.get("block") { |_request, response| response.write("block") }
17
+ http.get "middleware", :middleware
18
+ http.get "boom", :boom
19
+
20
+ def web(_request, response)
21
+ response.write("it worked")
22
+ end
23
+
24
+ def variable(request, response)
25
+ id = request.env["router.params"][:id]
26
+ response.write("id is #{id}")
27
+ end
28
+
29
+ def glob(request, response)
30
+ segments = request.env["router.params"][:a]
31
+ response.write(segments.join("/"))
32
+ end
33
+
34
+ def constraint(_request, response)
35
+ response.write("constraint")
36
+ end
37
+
38
+ def no_constraint(_request, response)
39
+ response.write("no constraint")
40
+ end
41
+
42
+ def middleware(request, response)
43
+ response["Custom-Header"] = request.env["header_value"] if request.env["use_header"]
44
+ response.write("middleware worked") if request.env["custom_rack_middleware_working"]
45
+ end
46
+
47
+ def boom(_request, _response)
48
+ # rubocop:disable Style/StringConcatenation
49
+ 1 + "2"
50
+ # rubocop:enable Style/StringConcatenation
51
+ end
52
+ end
53
+
54
+ describe handler, lita_handler: true do
55
+ it "responds to requests for simple paths" do
56
+ response = http.get("/web")
57
+ expect(response.status).to eq(200)
58
+ expect(response.body).to eq("it worked")
59
+ end
60
+
61
+ it "responds to requests with variable paths" do
62
+ response = http.post("/path/with/some_id")
63
+ expect(response.status).to eq(200)
64
+ expect(response.body).to eq("id is some_id")
65
+ end
66
+
67
+ it "responds to requests with globs in their paths" do
68
+ response = http.get("heres/a/giant/glob/in/a/path")
69
+ expect(response.status).to eq(200)
70
+ expect(response.body).to eq("a/giant")
71
+ end
72
+
73
+ it "responds to requests with variable path constraints" do
74
+ response = http.get("/123/otherwise/identical/path")
75
+ expect(response.status).to eq(200)
76
+ expect(response.body).to eq("constraint")
77
+
78
+ response = http.get("/an/otherwise/identical/path")
79
+ expect(response.status).to eq(200)
80
+ expect(response.body).to eq("no constraint")
81
+ end
82
+
83
+ it "responds to HEAD requests for GET routes" do
84
+ response = http.head("/web")
85
+ expect(response.status).to eq(204)
86
+ expect(response.body).to be_empty
87
+ end
88
+
89
+ it "allows route callbacks to be provided as blocks" do
90
+ response = http.get("/block")
91
+ expect(response.status).to eq(200)
92
+ expect(response.body).to eq("block")
93
+ end
94
+
95
+ context "when the handler raises an exception" do
96
+ it "calls the error handler with the exception as argument" do
97
+ expect(registry.config.robot.error_handler).to receive(:call) do |error, *_args|
98
+ expect(error).to be_an_instance_of(TypeError)
99
+ end
100
+
101
+ expect { http.get("/boom") }.to raise_error(TypeError, "String can't be coerced into Integer")
102
+ end
103
+ end
104
+ end
105
+
106
+ describe handler, lita_handler: true do
107
+ let(:middleware) do
108
+ Class.new do
109
+ def initialize(app)
110
+ @app = app
111
+ end
112
+
113
+ def call(env)
114
+ env["custom_rack_middleware_working"] = true
115
+ @app.call(env)
116
+ end
117
+ end
118
+ end
119
+
120
+ prepend_before { registry.config.http.middleware.push(middleware) }
121
+
122
+ it "uses any custom middlewares registered" do
123
+ response = http.get("/middleware")
124
+ expect(response.body).to eq("middleware worked")
125
+ end
126
+ end
127
+
128
+ describe handler, lita_handler: true do
129
+ let(:middleware) do
130
+ Class.new do
131
+ # rubocop:disable Style/OptionalBooleanParameter
132
+ def initialize(app, use_header = false, &block)
133
+ @app = app
134
+ @use_header = use_header
135
+ @block = block
136
+ end
137
+ # rubocop:enable Style/OptionalBooleanParameter
138
+
139
+ def call(env)
140
+ env["use_header"] = @use_header
141
+ env["header_value"] = @block.call
142
+ @app.call(env)
143
+ end
144
+ end
145
+ end
146
+
147
+ prepend_before do
148
+ registry.config.http.middleware.use(middleware, true) { "header value" }
149
+ end
150
+
151
+ it "uses any custom middlewares registered" do
152
+ response = http.get("/middleware")
153
+ expect(response["Custom-Header"]).to eq("header value")
154
+ end
155
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handler, lita_handler: true do
6
+ before { registry.handlers.delete(described_class) }
7
+
8
+ prepend_before(after_config: true) do
9
+ registry.register_handler(:foo) do
10
+ config :foo_response, required: true, type: String
11
+
12
+ after_config do |config|
13
+ route(/foo/) do |response|
14
+ response.reply(config.foo_response)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ it "includes chat routes" do
21
+ registry.register_handler(:foo) do
22
+ route(/foo/) do |response|
23
+ response.reply("bar")
24
+ end
25
+ end
26
+
27
+ send_message("foo")
28
+
29
+ expect(replies.last).to include("bar")
30
+ end
31
+
32
+ it "includes HTTP routes" do
33
+ registry.register_handler(:foo) do
34
+ http.get "foo" do |_request, response|
35
+ response.write("bar")
36
+ end
37
+ end
38
+
39
+ http_client = Faraday::Connection.new { |c| c.adapter(:rack, Lita::RackApp.new(robot)) }
40
+ response = http_client.get("/foo")
41
+
42
+ expect(response.body).to eq("bar")
43
+ end
44
+
45
+ it "includes event routes" do
46
+ registry.register_handler(:foo) do
47
+ on(:some_event) { robot.send_message("payload received") }
48
+ end
49
+
50
+ expect(robot).to receive(:send_message).with("payload received")
51
+
52
+ robot.trigger(:some_event)
53
+ end
54
+
55
+ it "runs the after_config block configuration is finalized", after_config: true do
56
+ registry.config.handlers.foo.foo_response = "baz"
57
+
58
+ send_message("foo")
59
+
60
+ expect(replies.last).to include("baz")
61
+ end
62
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handlers::Authorization, lita_handler: true do
6
+ before do
7
+ allow(robot.auth).to receive(:user_is_admin?).with(user).and_return(true)
8
+ end
9
+
10
+ let(:target_user) { instance_double("Lita::User", id: "1", name: "Carl") }
11
+
12
+ it { is_expected.to route_command("auth add foo bar").to(:add) }
13
+ it { is_expected.to route_command("auth add foo@bar.com baz").to(:add) }
14
+ it { is_expected.to route_command("auth remove foo bar").to(:remove) }
15
+ it { is_expected.to route_command("auth remove foo@bar.com baz").to(:remove) }
16
+ it { is_expected.to route_command("auth list").to(:list) }
17
+ it { is_expected.to route_command("auth list foo").to(:list) }
18
+
19
+ describe "#add" do
20
+ it "replies with the proper format if the require commands are missing" do
21
+ send_command("auth add foo")
22
+ expect(replies.last).to match(/^Format:/)
23
+ end
24
+
25
+ it "replies with a warning if target user is not known" do
26
+ send_command("auth add foo bar")
27
+ expect(replies.last).to match(/No user was found/)
28
+ end
29
+
30
+ it "replies with success if a valid user and group were supplied" do
31
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
32
+ send_command("auth add foo bar")
33
+ expect(replies.last).to eq("#{target_user.name} was added to bar.")
34
+ end
35
+
36
+ it "replies with a warning if the user was already in the group" do
37
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
38
+ send_command("auth add foo bar")
39
+ send_command("auth add foo bar")
40
+ expect(replies.last).to eq("#{target_user.name} was already in bar.")
41
+ end
42
+
43
+ it 'replies with a warning if the group was "admins"' do
44
+ send_command("auth add foo admins")
45
+ expect(replies.last).to match(/Administrators can only be managed/)
46
+ end
47
+ end
48
+
49
+ describe "#remove" do
50
+ before do
51
+ allow(Lita::User).to receive(:find_by_id).and_return(target_user)
52
+ send_command("auth add foo bar")
53
+ end
54
+
55
+ it "replies with success if a valid user and group were supplied" do
56
+ send_command("auth remove foo bar")
57
+ expect(replies.last).to eq("#{target_user.name} was removed from bar.")
58
+ end
59
+
60
+ it "replies with a warning if the user was already in the group" do
61
+ send_command("auth remove foo bar")
62
+ send_command("auth remove foo bar")
63
+ expect(replies.last).to eq("#{target_user.name} was not in bar.")
64
+ end
65
+
66
+ it 'replies with a warning if the group was "admins"' do
67
+ send_command("auth add foo admins")
68
+ expect(replies.last).to match(/Administrators can only be managed/)
69
+ end
70
+ end
71
+
72
+ describe "#list" do
73
+ context "when there are populated groups" do
74
+ let(:groups) { %i[foo bar] }
75
+ let(:user_1) { Lita::User.create(3, name: "Bongo") }
76
+ let(:user_2) { Lita::User.create(4, name: "Carl") }
77
+
78
+ before do
79
+ groups.each do |group|
80
+ subject.robot.auth.add_user_to_group(user, user_1, group)
81
+ subject.robot.auth.add_user_to_group(user, user_2, group)
82
+ end
83
+ end
84
+
85
+ it "lists all authorization groups and their members" do
86
+ send_command("auth list")
87
+ groups.each do |group|
88
+ expect(replies.last).to include(
89
+ "#{group}: #{user_1.name}, #{user_2.name}"
90
+ )
91
+ end
92
+ end
93
+
94
+ it "lists only the requested group" do
95
+ send_command("auth list foo")
96
+ expect(replies.last).to include("foo")
97
+ expect(replies.last).not_to include("bar")
98
+ end
99
+ end
100
+
101
+ it "replies that there are no groups" do
102
+ send_command("auth list")
103
+ expect(replies.last).to include("no authorization groups yet")
104
+ end
105
+
106
+ it "replies that the specified group doesn't exist" do
107
+ send_command("auth list nothing")
108
+ expect(replies.last).to include("no authorization group named nothing")
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handlers::Help, lita_handler: true do
6
+ it { is_expected.to route_command("help").to(:help) }
7
+ it { is_expected.to route_command("help foo").to(:help) }
8
+
9
+ describe "#help" do
10
+ let(:dummy_handler_class) do
11
+ Class.new(Lita::Handler) do
12
+ def self.name
13
+ "Dummy"
14
+ end
15
+
16
+ route(/secret/, :secret, restrict_to: :the_nobodies, help: {
17
+ "secret" => "This help message should be accompanied by a caveat"
18
+ })
19
+
20
+ def secret(_response); end
21
+ end
22
+ end
23
+
24
+ let(:dummy_handler_class_2) do
25
+ Class.new(Lita::Handler) do
26
+ def self.name
27
+ "Dummy2"
28
+ end
29
+
30
+ namespace "Dummy"
31
+
32
+ route(/foo/, :foo, help: { "foo" => "foo" })
33
+
34
+ def foo(_response); end
35
+ end
36
+ end
37
+
38
+ let(:another_handler) do
39
+ Class.new(Lita::Handler) do
40
+ def self.name
41
+ "Another"
42
+ end
43
+
44
+ route(/bar dummy/, :bar, help: { "bar dummy" => "bar" })
45
+ route(/baz/, :baz, help: { "baz" => "baz dummy" })
46
+
47
+ def bar(_response); end
48
+
49
+ def baz(_response); end
50
+ end
51
+ end
52
+
53
+ before do
54
+ registry.register_handler(dummy_handler_class)
55
+ registry.register_handler(dummy_handler_class_2)
56
+ registry.register_handler(another_handler)
57
+ registry.config.robot.alias = "!"
58
+ end
59
+
60
+ it "lists all installed handlers in alphabetical order with duplicates removed" do
61
+ send_command("help")
62
+ expect(replies.last).to match(
63
+ /^Send the message "!help QUERY".+installed:\n\nanother\ndummy\nhelp$/
64
+ )
65
+ end
66
+
67
+ it "sends help information for all commands under a given handler" do
68
+ send_command("help another")
69
+ expect(replies.last).to match(/bar.+baz/m)
70
+ end
71
+
72
+ it "sends help information for all commands matching a given substring" do
73
+ send_command("help foo")
74
+ expect(replies.last).to match(/foo/)
75
+ end
76
+
77
+ it("sends help information for all relevant commands "\
78
+ "when the given substring matches a handler + individual help messages") do
79
+ send_command("help dummy")
80
+ expect(replies.last).to match(/secret.+foo.+bar.+baz/m)
81
+ end
82
+
83
+ it "uses the mention name when no alias is defined" do
84
+ allow(robot.config.robot).to receive(:alias).and_return(nil)
85
+ send_command("help help")
86
+ expect(replies.last).to match(/#{robot.mention_name}: help/)
87
+ end
88
+
89
+ it "responds with an error if the given substring has no matches" do
90
+ send_command("help asdf")
91
+ expect(replies.last).to eq("No matching handlers, message patterns, or descriptions found.")
92
+ end
93
+
94
+ it "doesn't crash if a handler doesn't have routes" do
95
+ event_handler = Class.new do
96
+ extend Lita::Handler::EventRouter
97
+ end
98
+
99
+ registry.register_handler(event_handler)
100
+
101
+ expect { send_command("help") }.not_to raise_error
102
+ end
103
+
104
+ describe "restricted routes" do
105
+ let(:authorized_user) do
106
+ user = Lita::User.create(2, name: "Authorized User")
107
+ Lita::Authorization.new(robot).add_user_to_group!(user, :the_nobodies)
108
+ user
109
+ end
110
+
111
+ it "shows the unauthorized message for commands the user doesn't have access to" do
112
+ send_command("help secret")
113
+ expect(replies.last).to include("secret")
114
+ expect(replies.last).to include("Unauthorized")
115
+ end
116
+
117
+ it "omits the unauthorized message if the user has access" do
118
+ send_command("help secret", as: authorized_user)
119
+ expect(replies.last).to include("secret")
120
+ expect(replies.last).not_to include("Unauthorized")
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handlers::Info, lita_handler: true do
6
+ it { is_expected.to route_command("info").to(:chat) }
7
+ it { is_expected.to route_http(:get, "/lita/info").to(:web) }
8
+
9
+ let(:request) { double("Rack::Request") }
10
+ let(:response) { Rack::Response.new }
11
+
12
+ describe "#chat" do
13
+ it "responds with the current version of Lita" do
14
+ send_command("info")
15
+ expect(replies.first).to include(Lita::VERSION)
16
+ end
17
+
18
+ it "responds with a link to the website" do
19
+ send_command("info")
20
+ expect(replies.first).to include("lita.io")
21
+ end
22
+
23
+ it "responds with the Redis version and memory usage" do
24
+ send_command("info")
25
+ expect(replies.last).to match(/Redis [\d.]+ - Memory used: [\d.]+[BKMG]/)
26
+ end
27
+ end
28
+
29
+ describe "#web" do
30
+ let(:json) { JSON.parse(response.body.join) }
31
+
32
+ it "returns JSON" do
33
+ subject.web(request, response)
34
+ expect(response.headers["Content-Type"]).to eq("application/json")
35
+ end
36
+
37
+ it "includes the current version of Lita" do
38
+ subject.web(request, response)
39
+ expect(json).to include("lita_version" => Lita::VERSION)
40
+ end
41
+
42
+ it "includes the adapter being used" do
43
+ subject.web(request, response)
44
+ expect(json).to include("adapter" => registry.config.robot.adapter.to_s)
45
+ end
46
+
47
+ it "includes the robot's name" do
48
+ subject.web(request, response)
49
+ expect(json).to include("robot_name" => robot.name)
50
+ end
51
+
52
+ it "includes the robot's mention name" do
53
+ subject.web(request, response)
54
+ expect(json).to include("robot_mention_name" => robot.mention_name)
55
+ end
56
+
57
+ it "includes the Redis version" do
58
+ subject.web(request, response)
59
+ expect(json).to have_key("redis_version")
60
+ end
61
+
62
+ it "includes the Redis memory usage" do
63
+ subject.web(request, response)
64
+ expect(json).to have_key("redis_memory_usage")
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handlers::Room, lita_handler: true do
6
+ it { is_expected.to route_command("join #lita.io").to(:join) }
7
+ it { is_expected.to route_command("part #lita.io").to(:part) }
8
+
9
+ before { allow(robot.auth).to receive(:user_is_admin?).with(user).and_return(true) }
10
+
11
+ describe "#join" do
12
+ it "calls Robot#join with the provided ID" do
13
+ expect(robot).to receive(:join).with("#lita.io")
14
+ send_command("join #lita.io")
15
+ end
16
+ end
17
+
18
+ describe "#part" do
19
+ it "calls Robot#part with the provided ID" do
20
+ expect(robot).to receive(:part).with("#lita.io")
21
+ send_command("part #lita.io")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Handlers::Users, lita_handler: true do
6
+ it { is_expected.to route_command("users find carl").to(:find) }
7
+
8
+ describe "#find" do
9
+ it "finds users by ID" do
10
+ send_command("users find 1")
11
+
12
+ expect(replies.first).to eq("Test User (ID: 1, Mention name: Test User)")
13
+ end
14
+
15
+ it "finds users by name" do
16
+ send_command("users find 'Test User'")
17
+
18
+ expect(replies.first).to eq("Test User (ID: 1, Mention name: Test User)")
19
+ end
20
+
21
+ it "finds users by mention name" do
22
+ Lita::User.create(2, name: "Mr. Pug", mention_name: "carl")
23
+
24
+ send_command("users find carl")
25
+
26
+ expect(replies.first).to eq("Mr. Pug (ID: 2, Mention name: carl)")
27
+ end
28
+
29
+ it "replies with a message when no matches are found" do
30
+ send_command("users find nobody")
31
+
32
+ expect(replies.first).to eq("No matching users found.")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Lita::Logger do
6
+ let(:io) { StringIO.new }
7
+
8
+ it "uses a custom log level" do
9
+ logger = described_class.get_logger(:debug)
10
+ expect(logger.level).to eq(Logger::DEBUG)
11
+ end
12
+
13
+ it "uses the info level if the config is nil" do
14
+ logger = described_class.get_logger(nil)
15
+ expect(logger.level).to eq(Logger::INFO)
16
+ end
17
+
18
+ it "uses the info level if the config level is invalid" do
19
+ logger = described_class.get_logger(:foo)
20
+ expect(logger.level).to eq(Logger::INFO)
21
+ end
22
+
23
+ it "logs messages with a custom format" do
24
+ logger = described_class.get_logger(:debug, io: io)
25
+ logger.fatal "foo"
26
+ expect(io.string).to match(/^\[.+\] FATAL: foo$/)
27
+ end
28
+ end