aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/info_request_batch_spec.rb
blob: 787b812cfe64877b596370ff784098dc8e8c2a85 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe InfoRequestBatch, "when validating" do

    before do
        @info_request_batch = FactoryGirl.build(:info_request_batch)
    end

    it 'should require a user' do
        @info_request_batch.user = nil
        @info_request_batch.valid?.should be_false
        @info_request_batch.errors.full_messages.should == ["User can't be blank"]
    end

    it 'should require a title' do
        @info_request_batch.title = nil
        @info_request_batch.valid?.should be_false
        @info_request_batch.errors.full_messages.should == ["Title can't be blank"]
    end

    it 'should require a body' do
        @info_request_batch.body = nil
        @info_request_batch.valid?.should be_false
        @info_request_batch.errors.full_messages.should == ["Body can't be blank"]
    end

end

describe InfoRequestBatch, "when finding an existing batch" do

    before do
        @info_request_batch = FactoryGirl.create(:info_request_batch, :title => 'Matched title',
                                                                      :body => 'Matched body')
        @first_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch)
        @second_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch)
    end

    it 'should return a batch with the same user, title and body sent to one of the same public bodies' do
        InfoRequestBatch.find_existing(@info_request_batch.user,
                                       @info_request_batch.title,
                                       @info_request_batch.body,
                                       [@first_request.public_body_id]).should_not be_nil
    end

    it 'should not return a batch with the same title and body sent to another public body' do
        InfoRequestBatch.find_existing(@info_request_batch.user,
                                       @info_request_batch.title,
                                       @info_request_batch.body,
                                       [FactoryGirl.create(:public_body).id]).should be_nil
    end

    it 'should not return a batch sent the same public bodies with a different title and body' do
        InfoRequestBatch.find_existing(@info_request_batch.user,
                                       'Other title',
                                       'Other body',
                                       [@first_request.public_body_id]).should be_nil
    end

    it 'should not return a batch sent to one of the same public bodies with the same title and body by
        a different user' do
        InfoRequestBatch.find_existing(FactoryGirl.create(:user),
                                       @info_request_batch.title,
                                       @info_request_batch.body,
                                       [@first_request.public_body_id]).should be_nil
    end
end