summaryrefslogblamecommitdiffstats
path: root/spec/registration_spec.rb
blob: 89e2fa577aad5f8ce22421395fd06725dd7365fc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
               
                             
 







                             
                   
             


                          

                                    

                                          
                                                                        


                                          
                                                                   


                                        
                                                                 



                           





                                       
                                          
                                              
                     
                                             





                                      



                                                                           
                                             
                                                                           




                                                                                



                                                                                           









                                                                                                  
         
 
                                  
                                                                                    
                                                                                           


                                            


                                                            
                                                                                                        
         
       
 
                                                     
                                                    
 







                                                                            
 


                                                                         

         











                                                            















                                                                 

     
require "prawn"
require 'rack/session/cookie'

describe RegistrationApp do
  include Rack::Test::Methods

  def app
    RegistrationApp
  end

  describe 'GET index' do
    before :each do
      get '/'
    end

    it 'should succeed' do
      expect(last_response).to be_ok
    end

    it 'displays the registration form' do
      expect(last_response.body).to match(/form id="registration-form"/)
    end

    it 'displays the add member button' do
      expect(last_response.body).to match(/id="add-member-button"/)
    end

    it 'displays the add song button' do
      expect(last_response.body).to match(/id="add-song-button"/)
    end
  end

  describe 'POST submit' do
    include Mail::Matchers

    before :each do
      Mail::TestMailer.deliveries.clear
    end

    context 'with a valid registration' do
      let(:band_params) { create_band_params }
      before :each do
        post '/submit', 'band' => band_params
      end

      it 'should succeed' do
        expect(last_response).to be_ok
      end

      it 'should not display any errors' do
        expect(last_response.body).not_to include("Det er feil i skjemaet")
      end

      it 'displays submitted data to user' do
        expect(last_response).to match(/Bandnavn\: #{band_params['name']}/)
        expect(last_response).to match(/Hjemsted\: Oslo/)
        expect(last_response).to match(/Webside\: http\:\/\/imbalance.no/)
        expect(last_response).to match(/Plateselskap\: Calculated Imperfection/)
        expect(last_response).to match(/Kort bio\: Thrash metal from Norway/)

        expect(last_response).to match(/Kontaktperson: #{band_params['contact']['name']}/)
        expect(last_response).to match(/Kontaktadresse: #{band_params['contact']['addr']}/)
        expect(last_response).to match(/Telefon: #{band_params['contact']['phone']}/)
        expect(last_response).to match(/Epost: #{band_params['contact']['email']}/)

        expect(last_response).to match(/Harald Eilertsen, Bass\/Vocals/)
        expect(last_response).to match(/Welle, Drums/)
        expect(last_response).to match(/Thormodr, Guitar/)

        expect(last_response).to match(/Bestial by Nature/)
        expect(last_response).to match(/Spilletid: 02:80/)
        expect(last_response).to match(/Utøvere: Harald Eilertsen, Thormod Steinert, Lars Welle/)
        expect(last_response).to match(/Låtskrivere: Harald Eilertsen, Thormod Steinert/)
        expect(last_response).to match(/Merknad: Rævrukkje rum kjurr!/)
      end

      it "generates a PDF file" do
        expect(Prawn::Document).to have_received('new').with({ :page_size => "A4" })
        expect(@doc_spy).to have_received('render_file').with("#{band_params['name']}.pdf")
      end

      it 'sends an email to Norsk Urskog' do
        message = Mail::TestMailer.deliveries.first
        expect(Mail::TestMailer.deliveries).not_to be_empty
        expect(message.to).to include('haraldei@anduin.net')
        expect(message.subject).to match(/Registrering av band #{band_params['name']} til Norsk Urskog/)
      end
    end

    shared_examples_for('form with errors') do |args|
      let(:band_params) { create_band_params(args) }

      describe "form with errors" do
        before :each do
          post '/submit', 'band' => band_params
        end

        it 'should go back to the registration form' do
          expect(last_response.body).to match(/form id="registration-form"/)
        end

        it 'should display an error message' do
          expect(last_response.body).to include("Det er feil i skjemaet")
        end
      end
    end

    context 'with no songs' do
      it_behaves_like('form with errors', {:songs => 0})
    end

    context 'without a band name' do
      it_behaves_like('form with errors', {:name => ''})
    end

    context 'with a band name with only spaces' do
      it_behaves_like('form with errors', {:name => '    '})
    end

    context 'with no contact' do
      it_behaves_like('form with errors', {:contact_name => ''})
    end

    context 'with no contact address' do
      it_behaves_like('form with errors', {:contact_addr => ''})
    end

    context 'with no contact phone' do
      it_behaves_like('form with errors', {:contact_phone => ''})
    end

    context 'with no contact email' do
      it_behaves_like('form with errors', {:contact_phone => ''})
    end
  end
end