summaryrefslogtreecommitdiffstats
path: root/spec/registration_spec.rb
blob: 8d04d8967b25b71eb3b70554af1e2d631be0461c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 'allows three songs' do
      expect(last_response.body).to match(/Låt nr. 1/)
      expect(last_response.body).to match(/Låt nr. 2/)
      expect(last_response.body).to match(/Låt nr. 3/)
    end
  end

  describe 'submitting a form' 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\: #{band_params['city']}/)
        expect(last_response).to match(/Webside\: #{band_params['website']}/)
        expect(last_response).to match(/Plateselskap\: #{band_params['label']}/)
        expect(last_response).to match(/Kort bio\: #{band_params['shortbio']}/)

        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(/#{band_params['members']['1']['name']}, #{band_params['members']['1']['instrument']}/)
        expect(last_response).to match(/#{band_params['members']['2']['name']}, #{band_params['members']['2']['instrument']}/)
        expect(last_response).to match(/#{band_params['members']['3']['name']}, #{band_params['members']['3']['instrument']}/)

        expect(last_response).to match(/#{band_params['songs']['1']['title']}/)
        expect(last_response).to match(/Spilletid: #{band_params['songs']['1']['time']}/)
        expect(last_response).to match(/Utøvere: #{band_params['songs']['1']['performers'].split("\n").join(", ")}/)
        expect(last_response).to match(/Låtskrivere: #{band_params['songs']['1']['composers'].split("\n").join(", ")}/)
        expect(last_response).to match(/Merknad: #{band_params['songs']['1']['notes']}/)
      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(/uploads\/[0-9]{4}-[0-9]{2}-[0-9]{2}-#{sanitize(band_params['name'])}-#{sanitize(band_params['city'])}\.pdf/)
      end

      describe 'sends an email to Norsk Urskog' do
        let(:message) { Mail::TestMailer.deliveries.first }

        it 'sends an email to Norsk Urskog' do
          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

        it 'contains the url to the pdf file' do
          expect(message.body).to match(/example.org\/uploads\/[0-9]{4}-[0-9]{2}-[0-9]{2}-#{sanitize(band_params['name'])}-#{sanitize(band_params['city'])}\.pdf/)
        end
      end
    end

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

      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

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

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

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

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

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

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

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