/
measurement_sessions_controller_spec.rb
121 lines (95 loc) · 3.77 KB
/
measurement_sessions_controller_spec.rb
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
# AirCasting - Share your Air!
# Copyright (C) 2011-2012 HabitatMap, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You can contact the authors by email at <info@habitatmap.org>
require 'spec_helper'
shared_examples_for "session creation" do
let(:session) { mock_model(Session, :notes => [note]) }
let(:note) { FactoryGirl.create(:note, :photo => photo, :number => 10) }
let(:photo) { File.new(Rails.root + "spec" + "fixtures" + "test.jpg") }
let(:photos) { :some_files }
context "when session creation fails" do
let(:create_result) { nil }
it { should respond_with(:bad_request) }
end
context "when session creation succeeds" do
let(:create_result) { session }
it { should respond_with(:ok) }
it 'returns JSON with location of created session' do
json_response.should have_key('location')
end
it 'returns JSON with locations of note photos' do
json_response["notes"].first.should ==
{ "photo_location" => "http://test.host:80" + note.photo.url(:medium), "number" => note.number }
end
end
end
describe Api::MeasurementSessionsController do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "GET 'index'" do
let(:json) { [] }
before do
Session.should_receive(:filtered_json).and_return(json)
get :index, :format => :json, :q => {}
end
it { should respond_with(:ok) }
end
describe "GET 'show'" do
let(:session) { FactoryGirl.create(:mobile_session) }
before do
get :show, :id => session.id, :format => :json
end
it { should respond_with(:ok) }
it "should contain notes" do
json_response['notes'].should == jsonized(session.notes)
end
end
describe "GET 'show_multiple'" do
let(:session1) { FactoryGirl.create(:mobile_session) }
let(:session2) { FactoryGirl.create(:mobile_session) }
before do
get :show_multiple, q: { session_ids: [session1.id, session2.id] }, format: :json
end
it { expect(response.status).to eq 200 }
it 'returns multiple sessions' do
expect(response.body).to eq MobileSession.selected_sessions_json(session_ids: [session1.id, session2.id]).to_json
end
end
describe "POST 'create'" do
let(:builder) { stub }
let(:data) { {type: "MobileSession"} }
before do
ActiveSupport::JSON.should_receive(:decode).with(:session).and_return(data)
SessionBuilder.should_receive(:new).with(data, :some_files, user).and_return(builder)
builder.should_receive(:build!).and_return(create_result)
end
context "when the session is sent without compression" do
before do
post :create, :format => :json, :session => :session, :compression => false, :photos => photos
end
it_should_behave_like "session creation"
end
context "when the session is sent compressed" do
before do
Base64.should_receive(:decode64).with(:zipped_and_encoded).and_return(:zipped)
AirCasting::GZip.should_receive(:inflate).with(:zipped).and_return(:session)
post :create, :format => :json, :session => :zipped_and_encoded, :compression => true, :photos => photos
end
it_should_behave_like "session creation"
end
end
end