spec/lita/handlers/github_commits_spec.rb in lita-github-commits-0.0.3 vs spec/lita/handlers/github_commits_spec.rb in lita-github-commits-0.1.0

- old
+ new

@@ -1,8 +1,12 @@ require "spec_helper" +require_relative "payload" describe Lita::Handlers::GithubCommits, lita_handler: true do + + include Payload + it { routes_http(:post, "/github-commits").to(:receive) } describe "#receive" do let(:request) do @@ -14,417 +18,147 @@ let(:response) { Rack::Response.new } let(:params) { double("Hash") } - let(:valid_payload) do - <<-JSON.chomp -{ - "after":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "before":"17c497ccc7cca9c2f735aa07e9e3813060ce9a6a", - "commits":[ - { - "added":[ - - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"c441029cf673f84c8b7db52d0a5944ee5c52ff89", - "message":"Test", - "modified":[ - "README.md" - ], - "removed":[ - - ], - "timestamp":"2013-02-22T13:50:07-08:00", - "url":"https://github.com/octokitty/testing/commit/c441029cf673f84c8b7db52d0a5944ee5c52ff89" - }, - { - "added":[ - - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"36c5f2243ed24de58284a96f2a643bed8c028658", - "message":"This is me testing the windows client.", - "modified":[ - "README.md" - ], - "removed":[ - - ], - "timestamp":"2013-02-22T14:07:13-08:00", - "url":"https://github.com/octokitty/testing/commit/36c5f2243ed24de58284a96f2a643bed8c028658" - }, - { - "added":[ - "words/madame-bovary.txt" - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "message":"Rename madame-bovary.txt to words/madame-bovary.txt", - "modified":[ - - ], - "removed":[ - "madame-bovary.txt" - ], - "timestamp":"2013-03-12T08:14:29-07:00", - "url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb" - } - ], - "compare":"https://github.com/octokitty/testing/compare/17c497ccc7cc...1481a2de7b2a", - "created":false, - "deleted":false, - "forced":false, - "head_commit":{ - "added":[ - "words/madame-bovary.txt" - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "message":"Rename madame-bovary.txt to words/madame-bovary.txt", - "modified":[ - - ], - "removed":[ - "madame-bovary.txt" - ], - "timestamp":"2013-03-12T08:14:29-07:00", - "url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb" - }, - "pusher":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian" - }, - "ref":"refs/heads/master", - "repository":{ - "created_at":1332977768, - "description":"", - "fork":false, - "forks":0, - "has_downloads":true, - "has_issues":true, - "has_wiki":true, - "homepage":"", - "id":3860742, - "language":"Ruby", - "master_branch":"master", - "name":"testing", - "open_issues":2, - "owner":{ - "email":"lolwut@noway.biz", - "name":"octokitty" - }, - "private":false, - "pushed_at":1363295520, - "size":2156, - "stargazers":1, - "url":"https://github.com/octokitty/testing", - "watchers":1 - } -} - JSON + context "request with commits" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(valid_payload) end + it "sends a notification message to the applicable rooms" do + expect(robot).to receive(:send_message) do |target, message| + expect(target.room).to eq("#baz") + expect(message).to eq( + "[GitHub] Got 3 new commits from Garen Torikian on octokitty/testing on the master branch") + end + subject.receive(request, response) + end + end - - let(:created_payload) do - <<-JSON.chomp -{ - "after":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "before":"17c497ccc7cca9c2f735aa07e9e3813060ce9a6a", - "commits":[], - "compare":"https://github.com/octokitty/testing/compare/17c497ccc7cc...1481a2de7b2a", - "created":true, - "deleted":false, - "forced":false, - "head_commit":{ - "added":[ - "words/madame-bovary.txt" - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "message":"Rename madame-bovary.txt to words/madame-bovary.txt", - "modified":[ - - ], - "removed":[ - "madame-bovary.txt" - ], - "timestamp":"2013-03-12T08:14:29-07:00", - "url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb" - }, - "pusher":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian" - }, - "ref":"refs/heads/master", - "repository":{ - "created_at":1332977768, - "description":"", - "fork":false, - "forks":0, - "has_downloads":true, - "has_issues":true, - "has_wiki":true, - "homepage":"", - "id":3860742, - "language":"Ruby", - "master_branch":"master", - "name":"testing", - "open_issues":2, - "owner":{ - "email":"lolwut@noway.biz", - "name":"octokitty" - }, - "private":false, - "pushed_at":1363295520, - "size":2156, - "stargazers":1, - "url":"https://github.com/octokitty/testing", - "watchers":1 - } -} - JSON + context "request with one commit" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return( + valid_payload_one_commit) end - - let(:deleted_payload) do - <<-JSON.chomp -{ - "after":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "before":"17c497ccc7cca9c2f735aa07e9e3813060ce9a6a", - "commits":[], - "compare":"https://github.com/octokitty/testing/compare/17c497ccc7cc...1481a2de7b2a", - "created":false, - "deleted":true, - "forced":false, - "head_commit":{ - "added":[ - "words/madame-bovary.txt" - ], - "author":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "committer":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian", - "username":"octokitty" - }, - "distinct":true, - "id":"1481a2de7b2a7d02428ad93446ab166be7793fbb", - "message":"Rename madame-bovary.txt to words/madame-bovary.txt", - "modified":[ - - ], - "removed":[ - "madame-bovary.txt" - ], - "timestamp":"2013-03-12T08:14:29-07:00", - "url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb" - }, - "pusher":{ - "email":"lolwut@noway.biz", - "name":"Garen Torikian" - }, - "ref":"refs/heads/master", - "repository":{ - "created_at":1332977768, - "description":"", - "fork":false, - "forks":0, - "has_downloads":true, - "has_issues":true, - "has_wiki":true, - "homepage":"", - "id":3860742, - "language":"Ruby", - "master_branch":"master", - "name":"testing", - "open_issues":2, - "owner":{ - "email":"lolwut@noway.biz", - "name":"octokitty" - }, - "private":false, - "pushed_at":1363295520, - "size":2156, - "stargazers":1, - "url":"https://github.com/octokitty/testing", - "watchers":1 - } -} - JSON + it "sends a singular commit notification message to the applicable rooms" do + expect(robot).to receive(:send_message) do |target, message| + expect(target.room).to eq("#baz") + expect(message).to eq( + "[GitHub] Got 1 new commit from Garen Torikian on octokitty/testing on the master branch") + end + subject.receive(request, response) end + end - let(:ping_payload) do - <<-JSON.chomp -{ - "zen":"Non-blocking is better than blocking", - "hook_id":12345 -} - JSON + context "request with commits" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(valid_payload_diff_committer) end - - context "request with commits" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return(valid_payload) + it "sends a notification message to the applicable rooms" do + expect(robot).to receive(:send_message) do |target, message| + expect(message).to eq( + "[GitHub] Got 3 new commits authored by Garen Torikian and " + + "committed by Repository Owner on octokitty/testing on the master branch") end - - it "sends a notification message to the applicable rooms" do - expect(robot).to receive(:send_message) do |target, message| - expect(target.room).to eq("#baz") - expect(message).to include("[GitHub] Got") - end - subject.receive(request, response) - end + subject.receive(request, response) end + end + context "create payload" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(created_payload) + end - context "create payload" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return(created_payload) + it "sends a notification message to the applicable rooms" do + expect(robot).to receive(:send_message) do |target, message| + expect(target.room).to eq("#baz") + expect(message).to include("[GitHub] Garen Torikian created") end - - it "sends a notification message to the applicable rooms" do - expect(robot).to receive(:send_message) do |target, message| - expect(target.room).to eq("#baz") - expect(message).to include("[GitHub] Garen Torikian created") - end - subject.receive(request, response) - end + subject.receive(request, response) end + end - context "delete payload" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return(deleted_payload) - end + context "delete payload" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(deleted_payload) + end - it "sends a notification message to the applicable rooms" do - expect(robot).to receive(:send_message) do |target, message| - expect(target.room).to eq("#baz") - expect(message).to include("[GitHub] Garen Torikian deleted") - end - subject.receive(request, response) + it "sends a notification message to the applicable rooms" do + expect(robot).to receive(:send_message) do |target, message| + expect(target.room).to eq("#baz") + expect(message).to include("[GitHub] Garen Torikian deleted") end + subject.receive(request, response) end + end - context "bad payload" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return("yaryary") - end + context "bad payload" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return("yaryary") + end - it "sends a notification message to the applicable rooms" do - expect(Lita.logger).to receive(:error) do |error| - expect(error).to include("Could not parse JSON payload from Github") - end - subject.receive(request, response) + it "sends a notification message to the applicable rooms" do + expect(Lita.logger).to receive(:error) do |error| + expect(error).to include("Could not parse JSON payload from Github") end + subject.receive(request, response) end + end - context "ping event" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return(ping_payload) - allow(request).to receive(:env).and_return({"HTTP_X_GITHUB_EVENT" => "ping"}) - end + context "ping event" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(ping_payload) + allow(request).to receive(:env).and_return({"HTTP_X_GITHUB_EVENT" => "ping"}) + end - it "handles the ping event" do - expect(Lita.logger).to_not receive(:error) - expect(response).to receive(:write).with("Working!") - subject.receive(request, response) - end + it "handles the ping event" do + expect(Lita.logger).to_not receive(:error) + expect(response).to receive(:write).with("Working!") + subject.receive(request, response) end + end - context "unknown event" do - before do - Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" - allow(params).to receive(:[]).with("payload").and_return(ping_payload) - allow(request).to receive(:env).and_return({"HTTP_X_GITHUB_EVENT" => "fakefake"}) - end + context "unknown event" do + before do + Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz" + allow(params).to receive(:[]).with("payload").and_return(ping_payload) + allow(request).to receive(:env).and_return({"HTTP_X_GITHUB_EVENT" => "fakefake"}) + end - it "handles the ping event" do - expect(Lita.logger).to_not receive(:error) - expect(response).to_not receive(:write) - subject.receive(request, response) - end + it "handles the ping event" do + expect(Lita.logger).to_not receive(:error) + expect(response).to_not receive(:write) + subject.receive(request, response) end + end - context "improper config" do - before do - allow(params).to receive(:[]).with("payload").and_return(deleted_payload) - end + context "improper config" do + before do + allow(params).to receive(:[]).with("payload").and_return(deleted_payload) + end - it "sends a notification message to the applicable rooms" do - expect(Lita.logger).to receive(:warn) do |warning| - expect(warning).to include("Notification from GitHub Commits for unconfigured project") - end - subject.receive(request, response) + it "sends a notification message to the applicable rooms" do + expect(Lita.logger).to receive(:warn) do |warning| + expect(warning).to include("Notification from GitHub Commits for unconfigured project") end + subject.receive(request, response) end - - end + + end end