require 'spec_helper' require 'ember_cli_deploy_redis/web' require 'rack/test' require 'rspec-html-matchers' # require 'tilt/erubis' describe EmberCliDeployRedis::Web do include Rack::Test::Methods include RSpecHtmlMatchers def app described_class end let(:application1) { EmberCliDeployRedis::Application.new('application1') } let(:app1_revision_a) { EmberCliDeployRedis::Revision.new(application1, 'app1:revision_a') } let(:app1_revision_b) { EmberCliDeployRedis::Revision.new(application1, 'app1:revision_b') } let(:application2) { EmberCliDeployRedis::Application.new('application2') } let(:app2_revision_a) { EmberCliDeployRedis::Revision.new(application2, 'app2:revision_a') } let(:app2_revision_b) { EmberCliDeployRedis::Revision.new(application2, 'app2:revision_b') } describe 'GET /' do let(:the_request) { get '/' } before do EmberCliDeployRedis.configure do |config| config.web_application_names = %w(application1 application2) end app1_revision_b.activate! app1_revision_a.activate! app2_revision_b.activate! app2_revision_a.activate! app2_revision_b.activate! end it 'responds with Success' do the_request expect(last_response).to be_ok end it 'includes a row for each application' do the_request expect(last_response.body).to have_tag('tr') do with_tag 'td.col-application a', text: '/applications/application1', text: 'application1' with_tag 'td.col-current-revision', text: /app1:revision_a/ end expect(last_response.body).to have_tag('tr') do with_tag 'td.col-application a', href: '/applications/application2', text: 'application2' with_tag 'td.col-current-revision', text: /app2:revision_b/ end end end describe 'GET /applications/:app_name' do let(:the_request) { get "/applications/application1" } before do app1_revision_b.activate! app1_revision_a.activate! app1_revision_b.activate! end it 'responds with Success' do the_request expect(last_response).to be_ok end it 'includes a row for each revision' do the_request expect(last_response.body).to have_tag('tr') do with_tag 'td.col-revision', text: /app1:revision_b/ end expect(last_response.body).to have_tag('tr') do with_tag 'td.col-revision', text: /app1:revision_a/ end expect(last_response.body).to have_tag('tr') do with_tag 'td.col-revision', text: /app1:revision_b/ end end it 'shows the correct revisions as Active' do the_request expect(last_response.body).to have_tag('tr.revision-app1-revision_b') do with_tag 'td.col-status', text: /\A\s*Active\s*\z/ end end it 'does not show the inactive revisions as Active' do the_request expect(last_response.body).to have_tag('tr.revision-app1-revision_a') do without_tag 'td.col-status', text: /\A\s*Active\s*\z/ end end it 'has a Make Active button on non-active revisions' do the_request expect(last_response.body).to have_tag( 'tr.revision-app1-revision_a td.col-status form', method: 'post', action: '/applications/application1/activate/app1%3Arevision_a', ) do with_tag 'input', value: /Make Active/i end end it 'does not have a Make Active button on active revisions' do the_request expect(last_response.body).to have_tag('tr.revision-app1-revision_b') do without_tag 'td.col-status form' end end context 'without a configured revision_specifier_query_param' do it 'does not have a Test link for any revision' do the_request expect(last_response.body).to_not have_tag('td.col-status a', text: /test this revision/i) end end context 'with a configured revision_specifier_query_param' do before do EmberCliDeployRedis.configure do |config| config.revision_specifier_query_param = '_specified_revision' end end it 'has a Test link for each non-active revision' do the_request expect(last_response.body).to have_tag( 'tr.revision-app1-revision_a td.col-revision a', text: /test this revision/i, href: '/?_specified_revision[application1]=app1%3Arevision_a', ) end it 'does not have a Test link for active revisions' do the_request expect(last_response.body).to have_tag('tr.revision-app1-revision_b') do without_tag 'td.col-status a', text: /test this revision/i end end end end describe 'POST /applications/:app_name/activate/:revision_name' do let(:the_request) { post "/applications/application1/activate/app1%3Arevision_a" } before do app1_revision_a.activate! app1_revision_b.activate! end it 'redirects to the application page' do the_request expect(last_response).to be_redirect follow_redirect! expect(last_request.url).to match %r(/applications/application1$) end it 'makes the revision active' do the_request expect(application1.active_revision).to eq(app1_revision_a) end it 'executes the configured on_revision_activated callback' do callback_was_called = false EmberCliDeployRedis.configure do |config| config.on_revision_activated do |revision| callback_was_called = true expect(revision).to eq(app1_revision_a) end end the_request expect(callback_was_called).to be_truthy end end end