Skip to content

Commit

Permalink
Use Lita 4 configuration system.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Oct 23, 2014
1 parent 7c5ce04 commit bc92f61
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 54 deletions.
21 changes: 6 additions & 15 deletions lib/lita/handlers/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ module Lita
module Handlers
# Provides Travis CI webhooks for Lita.
class Travis < Handler
def self.default_config(config)
config.token = nil
config.repos = {}
config.default_rooms = nil
end
config :token, type: String, required: true
config :repos, type: Hash, default: {}
config :default_rooms, type: [Array, String]

http.post "/travis", :receive

Expand Down Expand Up @@ -56,8 +54,8 @@ def notify_rooms(repo, data)
end

def rooms_for_repo(repo)
rooms = Lita.config.handlers.travis.repos[repo]
default_rooms = Lita.config.handlers.travis.default_rooms
rooms = config.repos[repo]
default_rooms = config.default_rooms

if rooms
Array(rooms)
Expand All @@ -70,14 +68,7 @@ def rooms_for_repo(repo)
end

def validate_repo(repo, auth_hash)
token = Lita.config.handlers.travis.token

unless token
Lita.logger.warn(t("no_token"))
return
end

unless Digest::SHA2.hexdigest("#{repo}#{token}") == auth_hash
unless Digest::SHA2.hexdigest("#{repo}#{config.token}") == auth_hash
Lita.logger.warn(t("auth_failed"), repo: repo)
return
end
Expand Down
6 changes: 3 additions & 3 deletions lita-travis.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "lita-travis"
spec.version = "0.2.0"
spec.version = "1.0.0"
spec.authors = ["Jimmy Cuadra"]
spec.email = ["[email protected]"]
spec.description = %q{A Lita handler for receiving notifications from Travis CI.}
Expand All @@ -14,11 +14,11 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "lita", ">= 2.5"
spec.add_runtime_dependency "lita", ">= 4.0"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", ">= 3.0.0.beta2"
spec.add_development_dependency "rspec", ">= 3.0.0"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "coveralls"
end
3 changes: 0 additions & 3 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ en:
no_room_configured: >-
Notification from Travis CI for %{repo} ignored because no rooms were specified in the
config.repos hash, and no default rooms were specified in config.default_rooms.
no_token: >-
Notification from Travis CI could not be validated because Lita.config.handlers.token
is not set.
46 changes: 13 additions & 33 deletions spec/lita/handlers/travis_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
require "spec_helper"

describe Lita::Handlers::Travis, lita_handler: true do
it { routes_http(:post, "/travis").to(:receive) }

describe ".default_config" do
it "sets token to nil" do
expect(Lita.config.handlers.travis.token).to be_nil
end

it "sets repos to an empty hash" do
expect(Lita.config.handlers.travis.repos).to eq({})
end
end
it { is_expected.to route_http(:post, "/travis").to(:receive) }

describe "#receive" do
let(:request) do
Expand Down Expand Up @@ -54,10 +44,14 @@
JSON
end

before do
registry.config.handlers.travis.repos = {}
end

context "happy path" do
before do
Lita.config.handlers.travis.token = "abc123"
Lita.config.handlers.travis.repos["foo/bar"] = "#baz"
registry.config.handlers.travis.token = "abc123"
registry.config.handlers.travis.repos["foo/bar"] = "#baz"
allow(request).to receive(:env).and_return(valid_env)
allow(params).to receive(:[]).with("payload").and_return(valid_payload)
end
Expand All @@ -71,25 +65,10 @@
end
end

context "with a missing token" do
before do
Lita.config.handlers.travis.repos["foo/bar"] = "#baz"
allow(request).to receive(:env).and_return(valid_env)
allow(params).to receive(:[]).with("payload").and_return(valid_payload)
end

it "logs a warning that the token is not set" do
expect(Lita.logger).to receive(:warn) do |warning|
expect(warning).to include("token is not set")
end
subject.receive(request, response)
end
end

context "with an invalid authorization header" do
before do
Lita.config.handlers.travis.token = "abc123"
Lita.config.handlers.travis.repos["foo/bar"] = "#baz"
registry.config.handlers.travis.token = "abc123"
registry.config.handlers.travis.repos["foo/bar"] = "#baz"
allow(request).to receive(:env).and_return(invalid_env)
allow(params).to receive(:[]).with("payload").and_return(valid_payload)
end
Expand All @@ -104,8 +83,8 @@

context "with only config.default_rooms set" do
before do
Lita.config.handlers.travis.token = "abc123"
Lita.config.handlers.travis.default_rooms = "#default"
registry.config.handlers.travis.token = "abc123"
registry.config.handlers.travis.default_rooms = "#default"
allow(request).to receive(:env).and_return(valid_env)
allow(params).to receive(:[]).with("payload").and_return(valid_payload)
end
Expand All @@ -121,7 +100,8 @@

context "without setting a value for the repo in config.repos and no default" do
before do
Lita.config.handlers.travis.token = "abc123"
registry.config.handlers.travis.token = "abc123"
allow(registry.config.handlers.travis).to receive(:default_rooms).and_return(nil)
allow(request).to receive(:env).and_return(valid_env)
allow(params).to receive(:[]).with("payload").and_return(valid_payload)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

require "lita-travis"
require "lita/rspec"

Lita.version_3_compatibility_mode = false

0 comments on commit bc92f61

Please sign in to comment.