lita 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,9 +39,8 @@ describe Lita::Config do
39
39
  it "raises an exception if lita_config.rb raises an exception" do
40
40
  allow(File).to receive(:exist?).and_return(true)
41
41
  allow(described_class).to receive(:load) { Lita.non_existent_method }
42
- expect do
43
- described_class.load_user_config
44
- end.to raise_error(Lita::ConfigError)
42
+ expect(Lita.logger).to receive(:fatal).with(/could not be processed/)
43
+ expect { described_class.load_user_config }.to raise_error(SystemExit)
45
44
  end
46
45
  end
47
46
  end
@@ -12,6 +12,7 @@ describe Lita::Handler do
12
12
  allow(message).to receive(:scan).and_return(matches)
13
13
  allow(message).to receive(:command?).and_return(false)
14
14
  allow(message).to receive(:source).and_return(source)
15
+ allow(message).to receive(:user).and_return(user)
15
16
  message
16
17
  end
17
18
 
@@ -19,10 +20,13 @@ describe Lita::Handler do
19
20
 
20
21
  let(:source) { double("Source") }
21
22
 
23
+ let(:user) { double("User") }
24
+
22
25
  let(:handler_class) do
23
26
  Class.new(described_class) do
24
27
  route(/\w{3}/, to: :foo)
25
28
  route(/\w{4}/, to: :blah, command: true)
29
+ route(/secret/, to: :secret, restrict_to: :admins)
26
30
 
27
31
  def foo(matches)
28
32
  end
@@ -30,6 +34,9 @@ describe Lita::Handler do
30
34
  def blah(matches)
31
35
  end
32
36
 
37
+ def secret(matches)
38
+ end
39
+
33
40
  def self.name
34
41
  "Lita::Handlers::Test"
35
42
  end
@@ -71,6 +78,20 @@ describe Lita::Handler do
71
78
  expect_any_instance_of(handler_class).not_to receive(:blah)
72
79
  handler_class.dispatch(robot, message)
73
80
  end
81
+
82
+ it "dispatches to restricted routes if the user is in the auth group" do
83
+ allow(message).to receive(:body).and_return("secret")
84
+ allow(Lita::Authorization).to receive(:user_in_group?).and_return(true)
85
+ expect_any_instance_of(handler_class).to receive(:secret)
86
+ handler_class.dispatch(robot, message)
87
+ end
88
+
89
+ it "doesn't route unauthorized users' messages to restricted routes" do
90
+ allow(message).to receive(:body).and_return("secret")
91
+ allow(Lita::Authorization).to receive(:user_in_group?).and_return(false)
92
+ expect_any_instance_of(handler_class).not_to receive(:secret)
93
+ handler_class.dispatch(robot, message)
94
+ end
74
95
  end
75
96
 
76
97
  describe "#args" do
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Authorization, lita: true do
4
+ before { allow(robot).to receive(:send_messages) }
5
+
6
+ it { routes("#{robot.name}: auth add foo bar").to(:add) }
7
+ it { routes("#{robot.name}: auth add foo@bar.com baz").to(:add) }
8
+ it { routes("#{robot.name}: auth remove foo bar").to(:remove) }
9
+ it { routes("#{robot.name}: auth remove foo@bar.com baz").to(:remove) }
10
+
11
+ describe ".help" do
12
+ it "returns a hash of command help" do
13
+ expect(described_class.help).to be_a(Hash)
14
+ end
15
+ end
16
+
17
+ describe "#add" do
18
+ before do
19
+ allow(Lita::Authorization).to receive(:user_is_admin?).and_return(true)
20
+ end
21
+
22
+ it "replies with the proper format if the require commands are missing" do
23
+ expect_reply(/^Format:/)
24
+ send_test_message("#{robot.name}: auth add foo")
25
+ end
26
+
27
+ it "replies with a warning if target user is not known" do
28
+ expect_reply(/No user was found/)
29
+ send_test_message("#{robot.name}: auth add foo bar")
30
+ end
31
+
32
+ it "replies with success if the valid user ID and group were supplied" do
33
+ allow(Lita::User).to receive(:find_by_id).and_return(user)
34
+ expect_reply("#{user.name} was added to bar.")
35
+ send_test_message("#{robot.name}: auth add foo bar")
36
+ end
37
+
38
+ it "replies with success if the valid user ID and group were supplied" do
39
+ allow(Lita::User).to receive(:find_by_name).and_return(user)
40
+ expect_reply("#{user.name} was added to bar.")
41
+ send_test_message("#{robot.name}: auth add foo bar")
42
+ end
43
+
44
+ it "replies with a warning if the user was already in the group" do
45
+ allow(Lita::User).to receive(:find_by_id).and_return(user)
46
+ send_test_message("#{robot.name}: auth add foo bar")
47
+ expect_reply("#{user.name} was already in bar.")
48
+ send_test_message("#{robot.name}: auth add foo bar")
49
+ end
50
+ end
51
+
52
+ describe "#remove" do
53
+ before do
54
+ allow(Lita::Authorization).to receive(:user_is_admin?).and_return(true)
55
+ allow(Lita::User).to receive(:find_by_id).and_return(user)
56
+ send_test_message("#{robot.name}: auth add foo bar")
57
+ end
58
+
59
+ it "replies with success if the valid user ID and group were supplied" do
60
+ expect_reply("#{user.name} was removed from bar.")
61
+ send_test_message("#{robot.name}: auth remove foo bar")
62
+ end
63
+
64
+ it "replies with a warning if the user was already in the group" do
65
+ send_test_message("#{robot.name}: auth remove foo bar")
66
+ expect_reply("#{user.name} was not in bar.")
67
+ send_test_message("#{robot.name}: auth remove foo bar")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Help, lita: true do
4
+ it { routes("#{robot.name}: help").to(:help) }
5
+ it { routes("#{robot.name}: help foo").to(:help) }
6
+
7
+ describe "#help" do
8
+ it "sends help information for all commands" do
9
+ expect_reply(/help - Lists.+help COMMAND - Lists/m)
10
+ send_test_message("#{robot.name}: help")
11
+ end
12
+
13
+ it "sends help information for commands starting with COMMAND" do
14
+ expect_reply(/help COMMAND - Lists/)
15
+ expect_no_reply(/help - Lists/)
16
+ send_test_message("#{robot.name}: help help command")
17
+ end
18
+ end
19
+ end
@@ -59,4 +59,11 @@ describe Lita::Message do
59
59
  subject.scan
60
60
  end
61
61
  end
62
+
63
+ describe "#user" do
64
+ it "delegates to #source" do
65
+ expect(subject.source).to receive(:user)
66
+ subject.user
67
+ end
68
+ end
62
69
  end
@@ -1,11 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Robot do
4
- it "raises an exception if the specified adapter can't be found" do
4
+ it "logs and quits if the specified adapter can't be found" do
5
5
  adapter_registry = double("adapter_registry")
6
6
  allow(Lita).to receive(:adapters).and_return(adapter_registry)
7
7
  allow(adapter_registry).to receive(:[]).and_return(nil)
8
- expect { subject }.to raise_error(Lita::UnknownAdapterError)
8
+ expect(Lita.logger).to receive(:fatal).with(/Unknown adapter/)
9
+ expect { subject }.to raise_error(SystemExit)
9
10
  end
10
11
 
11
12
  describe "#receive" do
@@ -25,18 +26,45 @@ describe Lita::Robot do
25
26
  expect_any_instance_of(Lita::Adapters::Shell).to receive(:run)
26
27
  subject.run
27
28
  end
29
+
30
+ it "rescues interrupts and calls #shut_down" do
31
+ allow_any_instance_of(
32
+ Lita::Adapters::Shell
33
+ ).to receive(:run).and_raise(Interrupt)
34
+ expect_any_instance_of(Lita::Adapters::Shell).to receive(:shut_down)
35
+ subject.run
36
+ end
28
37
  end
29
38
 
30
39
  describe "#send_message" do
31
- let(:message) { double("Message") }
40
+ let(:source) { double("Source") }
32
41
 
33
42
  it "delegates to the adapter" do
34
43
  expect_any_instance_of(
35
44
  Lita::Adapters::Shell
36
45
  ).to receive(:send_messages).with(
37
- message, ["foo", "bar"]
46
+ source, ["foo", "bar"]
38
47
  )
39
- subject.send_messages(message, "foo", "bar")
48
+ subject.send_messages(source, "foo", "bar")
49
+ end
50
+ end
51
+
52
+ describe "#set_topic" do
53
+ let(:source) { double("Source") }
54
+
55
+ it "delegates to the adapter" do
56
+ expect_any_instance_of(Lita::Adapters::Shell).to receive(:set_topic).with(
57
+ source,
58
+ "New topic"
59
+ )
60
+ subject.set_topic(source, "New topic")
61
+ end
62
+ end
63
+
64
+ describe "#shut_down" do
65
+ it "gracefully stops the adapter" do
66
+ expect_any_instance_of(Lita::Adapters::Shell).to receive(:shut_down)
67
+ subject.shut_down
40
68
  end
41
69
  end
42
70
  end
@@ -5,9 +5,11 @@ handler_class = Class.new(Lita::Handler) do
5
5
  route(/\w{4}/, to: :blah, command: true)
6
6
 
7
7
  def foo(matches)
8
+ reply "baz"
8
9
  end
9
10
 
10
11
  def blah(matches)
12
+ reply "bongo", "wongo"
11
13
  end
12
14
 
13
15
  def self.name
@@ -15,9 +17,33 @@ handler_class = Class.new(Lita::Handler) do
15
17
  end
16
18
  end
17
19
 
18
- describe handler_class, lita_handler: true do
20
+ describe handler_class, lita: true do
19
21
  it { routes("foo").to(:foo) }
20
22
  it { routes("#{robot.name}: blah").to(:blah) }
21
23
  it { doesnt_route("blah").to(:blah) }
22
24
  it { does_not_route("blah").to(:blah) }
25
+
26
+ describe "#foo" do
27
+ it "replies with baz" do
28
+ expect_reply("baz")
29
+ send_test_message("foo")
30
+ end
31
+
32
+ it "doesn't reply with blam" do
33
+ expect_no_reply("blam")
34
+ send_test_message("foo")
35
+ end
36
+ end
37
+
38
+ describe "#blah" do
39
+ it "replies with bongo and wongo" do
40
+ expect_replies("bongo", "wongo")
41
+ send_test_message("#{robot.name}: blah")
42
+ end
43
+
44
+ it "doesn't reply with foo and bar" do
45
+ expect_no_replies("foo", "bar")
46
+ send_test_message("#{robot.name}: blah")
47
+ end
48
+ end
23
49
  end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::User, lita: true do
4
+ describe ".create" do
5
+ it "creates and returns new users" do
6
+ user = described_class.create(1, name: "Carl")
7
+ expect(user.id).to eq("1")
8
+ expect(user.name).to eq("Carl")
9
+ persisted_user = described_class.find(1)
10
+ expect(user).to eq(persisted_user)
11
+ end
12
+ end
13
+
14
+ describe ".find" do
15
+ before { described_class.create(1, name: "Carl") }
16
+
17
+ it "returns existing users" do
18
+ expect_any_instance_of(described_class).not_to receive(:save)
19
+ user = described_class.find(1, name: "Carl")
20
+ expect(user.id).to eq("1")
21
+ expect(user.name).to eq("Carl")
22
+ end
23
+ end
24
+
25
+ describe ".find_by_name" do
26
+ it "returns nil if no user matches the provided name" do
27
+ expect(described_class.find_by_name("Carl")).to be_nil
28
+ end
29
+
30
+ it "returns existing users" do
31
+ described_class.create(1, name: "Carl")
32
+ user = described_class.find_by_name("Carl")
33
+ expect(user.id).to eq("1")
34
+ end
35
+ end
36
+
37
+ describe "#save" do
38
+ subject { described_class.new(1, name: "Carl") }
39
+
40
+ it "saves an ID to name mapping for the user in Redis" do
41
+ subject.save
42
+ expect(described_class.redis.hgetall("id:1")).to eq("name" => "Carl")
43
+ end
44
+
45
+ it "saves a name to ID mapping for the user in Redis" do
46
+ subject.save
47
+ expect(described_class.redis.get("name:Carl")).to eq("1")
48
+ end
49
+ end
50
+
51
+ describe "#==" do
52
+ it "considers two users equal if they share an ID and name" do
53
+ user1 = described_class.new(1, name: "Carl")
54
+ user2 = described_class.new(1, name: "Carl")
55
+ expect(user1).to eq(user2)
56
+ end
57
+ end
58
+ end
data/spec/lita_spec.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita do
4
+ before do
5
+ Lita.instance_variable_set(:@config, nil)
6
+ Lita.instance_variable_set(:@logger, nil)
7
+ end
8
+
4
9
  it "memoizes a hash of Adapters" do
5
10
  adapter_class = double("Adapter")
6
11
  described_class.register_adapter(:foo, adapter_class)
@@ -12,7 +17,9 @@ describe Lita do
12
17
  handler_class = double("Handler")
13
18
  described_class.register_handler(handler_class)
14
19
  described_class.register_handler(handler_class)
15
- expect(described_class.handlers.to_a).to eq([handler_class])
20
+ original_size = described_class.handlers.to_a.size
21
+ new_size = (described_class.handlers.to_a - [handler_class]).size
22
+ expect(new_size).to eq(original_size - 1)
16
23
  expect(described_class.handlers).to eql(described_class.handlers)
17
24
  end
18
25
 
@@ -28,21 +35,44 @@ describe Lita do
28
35
  end
29
36
  end
30
37
 
38
+ describe ".logger" do
39
+ it "memoizes the logger" do
40
+ expect(described_class.logger).to be_a(Logger)
41
+ expect(described_class.logger).to eql(described_class.logger)
42
+ end
43
+
44
+ it "uses a custom log level" do
45
+ Lita.config.robot.log_level = :debug
46
+ expect(described_class.logger.level).to eq(Logger::DEBUG)
47
+ end
48
+
49
+ it "uses the info level if the config is nil" do
50
+ Lita.config.robot.log_level = nil
51
+ expect(described_class.logger.level).to eq(Logger::INFO)
52
+ end
53
+
54
+ it "uses the info level if the config level is invalid" do
55
+ Lita.config.robot.log_level = :foo
56
+ expect(described_class.logger.level).to eq(Logger::INFO)
57
+ end
58
+
59
+ it "logs messages with a custom format" do
60
+ stderr = StringIO.new
61
+ stub_const("STDERR", stderr)
62
+ Lita.logger.fatal "foo"
63
+ expect(stderr.string).to match(%r{^\[.+\] FATAL: foo$})
64
+ end
65
+ end
66
+
31
67
  describe ".redis" do
32
68
  it "memoizes a Redis::Namespace" do
33
- expect(described_class.redis.namespace).to eq(
34
- described_class::REDIS_NAMESPACE
35
- )
69
+ expect(described_class.redis).to respond_to(:namespace)
36
70
  expect(described_class.redis).to eql(described_class.redis)
37
71
  end
38
72
  end
39
73
 
40
74
  describe ".run" do
41
- it "laods the user config" do
42
- expect(Lita::Config).to receive(:load_user_config)
43
- allow_any_instance_of(Lita::Robot).to receive(:run)
44
- described_class.run
45
- end
75
+ before { Lita.config }
46
76
 
47
77
  it "runs a new Robot" do
48
78
  expect_any_instance_of(Lita::Robot).to receive(:run)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Cuadra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-15 00:00:00.000000000 Z
11
+ date: 2013-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,6 +118,7 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - .gitignore
120
120
  - .travis.yml
121
+ - CHANGELOG.md
121
122
  - Gemfile
122
123
  - README.md
123
124
  - Rakefile
@@ -125,26 +126,33 @@ files:
125
126
  - lib/lita.rb
126
127
  - lib/lita/adapter.rb
127
128
  - lib/lita/adapters/shell.rb
129
+ - lib/lita/authorization.rb
128
130
  - lib/lita/cli.rb
129
131
  - lib/lita/config.rb
130
- - lib/lita/errors.rb
131
132
  - lib/lita/handler.rb
133
+ - lib/lita/handlers/authorization.rb
134
+ - lib/lita/handlers/help.rb
132
135
  - lib/lita/message.rb
133
136
  - lib/lita/robot.rb
134
137
  - lib/lita/rspec.rb
135
138
  - lib/lita/source.rb
139
+ - lib/lita/user.rb
136
140
  - lib/lita/version.rb
137
141
  - lita.gemspec
138
142
  - skeleton/Gemfile
139
143
  - skeleton/lita_config.rb
140
144
  - spec/lita/adapter_spec.rb
141
145
  - spec/lita/adapters/shell_spec.rb
146
+ - spec/lita/authorization_spec.rb
142
147
  - spec/lita/config_spec.rb
143
148
  - spec/lita/handler_spec.rb
149
+ - spec/lita/handlers/authorization_spec.rb
150
+ - spec/lita/handlers/help_spec.rb
144
151
  - spec/lita/message_spec.rb
145
152
  - spec/lita/robot_spec.rb
146
153
  - spec/lita/rspec_spec.rb
147
154
  - spec/lita/source_spec.rb
155
+ - spec/lita/user_spec.rb
148
156
  - spec/lita_spec.rb
149
157
  - spec/spec_helper.rb
150
158
  homepage: https://github.com/jimmycuadra/lita
@@ -159,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
167
  requirements:
160
168
  - - '>='
161
169
  - !ruby/object:Gem::Version
162
- version: '0'
170
+ version: 2.0.0
163
171
  required_rubygems_version: !ruby/object:Gem::Requirement
164
172
  requirements:
165
173
  - - '>='
@@ -174,12 +182,16 @@ summary: A multi-service chat bot with extendable behavior.
174
182
  test_files:
175
183
  - spec/lita/adapter_spec.rb
176
184
  - spec/lita/adapters/shell_spec.rb
185
+ - spec/lita/authorization_spec.rb
177
186
  - spec/lita/config_spec.rb
178
187
  - spec/lita/handler_spec.rb
188
+ - spec/lita/handlers/authorization_spec.rb
189
+ - spec/lita/handlers/help_spec.rb
179
190
  - spec/lita/message_spec.rb
180
191
  - spec/lita/robot_spec.rb
181
192
  - spec/lita/rspec_spec.rb
182
193
  - spec/lita/source_spec.rb
194
+ - spec/lita/user_spec.rb
183
195
  - spec/lita_spec.rb
184
196
  - spec/spec_helper.rb
185
197
  has_rdoc: