spec/github_spec.rb in overlay-1.1.0 vs spec/github_spec.rb in overlay-2.0.0

- old
+ new

@@ -1,104 +1,262 @@ require 'spec_helper' +require 'socket' describe Overlay::Github do - before :each do - # Configure the overlay - Overlay.configuration.reset if Overlay.configuration - Overlay.configure do |config| - config.auth = 'test_user:test_password' - config.repositories << Overlay::GithubRepo.new('saarinen', 'overlay', 'master', 'spec', 'spec') + let(:repo_config) do + Overlay::GithubRepo.new( + 'test_org', + 'test_repo', + 'test_user:test_pass', + 'spec', + 'spec' + ) + end - # Configure host port as Rails::Server is not available - # - config.host_port = 3000 + describe "Register Github webhook" do + before :each do + # Configure the overlay + Overlay.configuration.reset if Overlay.configuration + + Overlay.configure do |config| + # Configure host port as Rails::Server is not available + config.host_port = 3000 + end + + + Overlay.configuration.repositories = Set.new [repo_config] end - end - describe "process overlays" do - it "should configure the github_api" do - Overlay::Github.stub(:register_web_hook).and_return - Overlay::Github.stub(:overlay_repo).and_return + it 'should register a webhook with github' do + config = Overlay.configuration.repositories.first + allow(Overlay::Github.instance).to receive(:fork_it).with(:overlay_repo, config).and_return - Overlay::Github.should_receive(:configure).once - Overlay::Github.process_overlays + stub_request(:get, /api.github.com/). + with(:headers => {'Accept'=>'application/vnd.github.v3+json,application/vnd.github.beta+json;q=0.5,application/json;q=0.1', 'Accept-Charset'=>'utf-8', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Github Ruby Gem 0.11.2'}). + to_return(:status => 200, :body => '[]', :headers => {}) + + expect(repo_config.github_api.hooks).to receive(:create).with( + 'test_org', + 'test_repo', + name: 'web', + active: true, + config: {:url => "http://#{Socket.gethostname}:3000/overlay/github/update", :content_type => 'json'} + ).and_return + + Overlay::Github.instance.process_overlays end - it "should verify the user and repo config" do - Overlay::Github.stub(:register_web_hook).and_return - Overlay::Github.stub(:overlay_repo).and_return + it 'should use a configured endpoint' do + config = Overlay.configuration.repositories.first + config.endpoint = "https://www.test.com" - Overlay.configuration.repositories << Overlay::GithubRepo.new(nil, nil, 'master', 'spec', 'spec') + allow(Overlay::Github.instance).to receive(:fork_it).with(:overlay_repo, config).and_return - expect{Overlay::Github.process_overlays}.to raise_error("Respository config missing user") + stub_request(:get, /www.test.com/). + with(:headers => {'Accept'=>'application/vnd.github.v3+json,application/vnd.github.beta+json;q=0.5,application/json;q=0.1', 'Accept-Charset'=>'utf-8', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Github Ruby Gem 0.11.2'}). + to_return(:status => 200, :body => '[]', :headers => {}) - Overlay.configuration.reset + expect(repo_config.github_api.hooks).to receive(:create).with( + 'test_org', + 'test_repo', + name: 'web', + active: true, + config: {:url => "http://#{Socket.gethostname}:3000/overlay/github/update", :content_type => 'json'} + ).and_return - Overlay.configuration.repositories << Overlay::GithubRepo.new("test", nil, 'master', 'spec', 'spec') + Overlay::Github.instance.process_overlays + end + end - expect{Overlay::Github.process_overlays}.to raise_error("Respository config missing repo") + describe 'subscribe to a redis publisher' do + before :each do + # Configure the overlay + Overlay.configuration.reset if Overlay.configuration + + Overlay.configure do |config| + # Configure host port as Rails::Server is not available + config.host_port = 3000 + end + + config = repo_config + config.use_publisher = true + config.redis_server = 'localhost' + config.redis_port = 6734 + config.registration_server = "http://www.test.com" + + Overlay.configuration.repositories = Set.new [config] end - it "should call to add a web_hook" do - Overlay::Github.stub(:overlay_repo).and_return + it 'should call publisher_subscribe' do + config = Overlay.configuration.repositories.first + allow(Overlay::Github.instance).to receive(:fork_it).with(:overlay_repo, config).and_return - Overlay::Github.should_receive(:register_web_hook).once.and_return - Overlay::Github.process_overlays + expect(Overlay::Github.instance).to receive(:publisher_subscribe).with(config).and_return + + Overlay::Github.instance.process_overlays end - it "should overlay the repo" do - Overlay::Github.stub(:register_web_hook).and_return + it 'should send a registration request' do + config = Overlay.configuration.repositories.first + allow(Overlay::Github.instance).to receive(:fork_it).with(:overlay_repo, config).and_return + expect(Overlay::Github.instance).to receive(:fork_it).with(:subscribe_to_channel, "test_key", config).and_return - Overlay::Github.should_receive(:overlay_repo).once.and_return - Overlay::Github.process_overlays + stub_request(:post, /www.test.com/). + with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). + to_return(status: 200, body: "{\"publish_key\": \"test_key\"}", headers: {}) + + Overlay::Github.instance.process_overlays end end - describe "configure github_api" do - it "should call github api configure" do - ::Github.should_receive(:configure).once - Overlay::Github.configure + describe 'process webhook payload' do + let(:repo_config) do + Overlay::GithubRepo.new( + 'test_org', + 'test_repo', + 'test_user:test_pass', + 'lib', + 'lib' + ) end - it "should set a custom site address" do - Overlay.configuration.site = "http://www.example.com" - Overlay::Github.configure - expect(::Github::Repos.new.site).to eq("http://www.example.com") + let(:payload) do + JSON.parse( + "{ + \"ref\":\"refs/heads/master\", + \"after\":\"3524f66a6b374a35a5359905be8a87a3931eba17\", + \"before\":\"57f73e81329ef71a37c55c2f41b48910f9a7c790\", + \"created\":false, + \"deleted\":false, + \"forced\":false, + \"compare\":\"https://api.test.com/test_org/test_repo/compare/57f73e81329e...3524f66a6b37\", + \"commits\":[ + { + \"id\":\"3524f66a6b374a35a5359905be8a87a3931eba17\", + \"distinct\":true, + \"message\":\"test webhook\", + \"timestamp\":\"2014-02-14T22:05:33+00:00\", + \"url\":\"https://api.test.com/test_org/test_repo/commit/3524f66a6b374a35a5359905be8a87a3931eba17\", + \"author\":{ + \"name\":\"Steve Saarinen\", + \"email\":\"ssaarinen@whitepages.com\", + \"username\":\"saarinen\" + }, + \"committer\":{ + \"name\":\"Steve Saarinen\", + \"email\":\"ssaarinen@whitepages.com\", + \"username\":\"saarinen\" + }, + \"added\":[ + + ], + \"added\":[ + \"lib/test/test.rb\" + ], + \"removed\":[ + \"lib/test/test.rb\" + ], + \"modified\":[ + \"lib/test/test.rb\" + ] + } + ], + \"head_commit\":{ + \"id\":\"3524f66a6b374a35a5359905be8a87a3931eba17\", + \"distinct\":true, + \"message\":\"test webhook\", + \"timestamp\":\"2014-02-14T22:05:33+00:00\", + \"url\":\"https://api.test.com/test_org/test_repo/commit/3524f66a6b374a35a5359905be8a87a3931eba17\", + \"author\":{ + \"name\":\"Steve Saarinen\", + \"email\":\"ssaarinen@whitepages.com\", + \"username\":\"saarinen\" + }, + \"committer\":{ + \"name\":\"Steve Saarinen\", + \"email\":\"ssaarinen@whitepages.com\", + \"username\":\"saarinen\" + }, + \"added\":[ + \"lib/test/test.rb\" + ], + \"removed\":[ + \"lib/test/test.rb\" + ], + \"modified\":[ + \"lib/test/test.rb\" + ] + }, + \"repository\":{ + \"id\":1720, + \"name\":\"bing_maps\", + \"url\":\"https://www.test.com/test_org/test_repo\", + \"description\":\"Gem providing Bing Maps API integration\", + \"watchers\":0, + \"stargazers\":0, + \"forks\":0, + \"fork\":true, + \"size\":412, + \"owner\":{ + \"name\":\"saarinen\", + \"email\":\"ssaarinen@whitepages.com\" + }, + \"private\":false, + \"open_issues\":0, + \"has_issues\":false, + \"has_downloads\":true, + \"has_wiki\":true, + \"language\":\"Ruby\", + \"created_at\":1383860934, + \"pushed_at\":1392415533, + \"master_branch\":\"master\" + }, + \"pusher\":{ + \"name\":\"saarinen\", + \"email\":\"ssaarinen@whitepages.com\" + } + }" + ) end - it "should verify basic auth" do - Overlay.configuration.auth = nil - expect{Overlay::Github.configure}.to raise_error("Configuration github_overlays.basic_auth not set") + it 'should make a directory for a new file' do + allow(Overlay::Github.instance).to receive(:clone_file).and_return + allow(File).to receive(:delete).and_return + expect(FileUtils).to receive(:mkdir_p).with("#{Rails.application.root}/lib/test") + + Overlay::Github.instance.process_hook(payload, repo_config) end - it "should set auth" do - Overlay::Github.configure - expect(::Github::Repos.new.basic_auth).to eq('test_user:test_password') + it 'should remove a deleted file' do + allow(Overlay::Github.instance).to receive(:clone_file).and_return + allow(FileUtils).to receive(:mkdir_p).with("#{Rails.application.root}/lib/test") + expect(File).to receive(:delete).with("#{Rails.application.root}/lib/test/test.rb").and_return + + Overlay::Github.instance.process_hook(payload, repo_config) end - end - describe "overlay_repo" do - it "should jump directly to overlay_directory if root set" do - repo_config = Overlay::GithubRepo.new('saarinen', 'overlay', 'master', 'spec', 'spec') - Overlay::Github.should_receive(:overlay_directory).once.with('spec', repo_config).and_return - overlay_thread = Overlay::Github.overlay_repo repo_config - overlay_thread.join + it 'should clone added or modified files' do + allow(FileUtils).to receive(:mkdir_p).with("#{Rails.application.root}/lib/test") + allow(File).to receive(:delete).with("#{Rails.application.root}/lib/test/test.rb").and_return + expect(Overlay::Github.instance).to receive(:clone_file).with("lib/test/test.rb", repo_config).exactly(:twice).and_return + + Overlay::Github.instance.process_hook(payload, repo_config) end - it "should ignore any files in repo root" + it 'should call post_hook after hook is processed' do + allow(FileUtils).to receive(:mkdir_p).with("#{Rails.application.root}/lib/test") + allow(File).to receive(:delete).with("#{Rails.application.root}/lib/test/test.rb").and_return + allow(Overlay::Github.instance).to receive(:clone_file).with("lib/test/test.rb", repo_config).and_return - it "should process all directories in repo root" - end + hook_ran = false - describe "overlay_directory" do - it "should set path correctly based on root" - it "should create path for directory" - it "should call clone_files for all files in directory" - it "should call overlay_directory for all directories" - end + repo_config.after_process_hook do + hook_ran = true + end - describe "clone_file" do - it "should create a file for the cloned file" - it "should corrctly set the file path" + Overlay::Github.instance.process_hook(payload, repo_config) + + expect(hook_ran).to eq(true) + end end end