|
| 1 | +# frozen_string_literal: true |
1 | 2 | require 'spec_helper'
|
2 | 3 |
|
3 |
| -# note, these tests are complex in order to test the admin security features |
4 |
| -# -- namely, it was necessary to test two contexts for each method: the user |
5 |
| -# being an admin, and not. |
6 | 4 | describe RequirementsController, type: :controller do
|
7 |
| - before(:each) do |
8 |
| - mock_app_config |
9 |
| - @requirement = FactoryGirl.create(:requirement, contact_name: 'Adam Bray') |
10 |
| - end |
| 5 | + # NOTE: many of these are essentially just testing permissions |
| 6 | + before(:each) { mock_app_config } |
11 | 7 | describe 'GET index' do
|
12 | 8 | context 'is admin' do
|
13 | 9 | before(:each) do
|
14 |
| - sign_in FactoryGirl.create(:admin) |
| 10 | + allow(Requirement).to receive(:all).and_return(Requirement.none) |
| 11 | + mock_user_sign_in(UserMock.new(:admin)) |
15 | 12 | get :index
|
16 | 13 | end
|
17 |
| - it { is_expected.to respond_with(:success) } |
18 |
| - it { is_expected.to render_template(:index) } |
19 |
| - it { is_expected.not_to set_flash } |
| 14 | + it_behaves_like 'successful request', :index |
20 | 15 | it 'should populate an array of all requirements' do
|
21 |
| - expect(assigns(:requirements)).to eq([@requirement]) |
| 16 | + expect(Requirement).to have_received(:all).twice |
22 | 17 | end
|
23 | 18 | end
|
24 | 19 | context 'not an admin' do
|
25 |
| - it 'should redirect to root url if not an admin' do |
26 |
| - sign_in FactoryGirl.create(:user) |
| 20 | + before do |
| 21 | + mock_user_sign_in |
27 | 22 | get :index
|
28 |
| - expect(response).to redirect_to(root_url) |
29 |
| - end |
30 |
| - end |
31 |
| - end |
32 |
| - describe 'GET show' do |
33 |
| - context 'is an admin' do |
34 |
| - before(:each) do |
35 |
| - sign_in FactoryGirl.create(:admin) |
36 |
| - get :show, id: @requirement |
37 |
| - end |
38 |
| - it { is_expected.to respond_with(:success) } |
39 |
| - it { is_expected.to render_template(:show) } |
40 |
| - it { is_expected.not_to set_flash } |
41 |
| - it 'should set @requirement to the selected requirement' do |
42 |
| - expect(assigns(:requirement)).to eq(@requirement) |
43 |
| - end |
44 |
| - end |
45 |
| - context 'not an admin' do |
46 |
| - it 'should redirect to root url if not an admin' do |
47 |
| - sign_in FactoryGirl.create(:user) |
48 |
| - get :show, id: @requirement |
49 |
| - expect(response).to redirect_to(root_url) |
50 | 23 | end
|
| 24 | + it_behaves_like 'redirected request' |
51 | 25 | end
|
52 | 26 | end
|
| 27 | + |
53 | 28 | describe 'GET new' do
|
54 | 29 | context 'is admin' do
|
55 | 30 | before(:each) do
|
56 |
| - sign_in FactoryGirl.create(:admin) |
| 31 | + allow(Requirement).to receive(:new) |
| 32 | + mock_user_sign_in(UserMock.new(:admin)) |
57 | 33 | get :new
|
58 | 34 | end
|
59 |
| - it { is_expected.to respond_with(:success) } |
60 |
| - it { is_expected.to render_template(:new) } |
61 |
| - it { is_expected.not_to set_flash } |
| 35 | + it_behaves_like 'successful request', :new |
62 | 36 | it 'assigns a new requirement to @requirement' do
|
63 |
| - expect(assigns(:requirement)).to be_new_record |
64 |
| - expect(assigns(:requirement).is_a?(Requirement)).to be_truthy |
| 37 | + expect(Requirement).to have_received(:new).twice |
65 | 38 | end
|
66 | 39 | end
|
67 | 40 | context 'not an admin' do
|
68 |
| - it 'should redirect to root url if not an admin' do |
69 |
| - sign_in FactoryGirl.create(:user) |
| 41 | + before do |
| 42 | + mock_user_sign_in |
70 | 43 | get :new
|
71 |
| - expect(response).to redirect_to(root_url) |
72 |
| - end |
73 |
| - end |
74 |
| - end |
75 |
| - describe 'GET edit' do |
76 |
| - context 'is admin' do |
77 |
| - before(:each) do |
78 |
| - sign_in FactoryGirl.create(:admin) |
79 |
| - get :edit, id: @requirement |
80 |
| - end |
81 |
| - it 'should set @requirement to the selected requirement' do |
82 |
| - expect(assigns(:requirement)).to eq(@requirement) |
83 |
| - end |
84 |
| - it { is_expected.to respond_with(:success) } |
85 |
| - it { is_expected.to render_template(:edit) } |
86 |
| - it { is_expected.not_to set_flash } |
87 |
| - end |
88 |
| - context 'not admin' do |
89 |
| - it 'should redirect to root url if not an admin' do |
90 |
| - sign_in FactoryGirl.create(:user) |
91 |
| - get :edit, id: @requirement |
92 |
| - expect(response).to redirect_to(root_url) |
93 | 44 | end
|
| 45 | + it_behaves_like 'redirected request' |
94 | 46 | end
|
95 | 47 | end
|
96 |
| - describe 'PUT update' do |
| 48 | + |
| 49 | + describe 'POST create' do |
97 | 50 | context 'is admin' do
|
98 |
| - before(:each) do |
99 |
| - sign_in FactoryGirl.create(:admin) |
100 |
| - end |
101 |
| - context 'with valid attributes' do |
| 51 | + before(:each) { mock_user_sign_in(UserMock.new(:admin)) } |
| 52 | + context 'successful save' do |
| 53 | + let!(:req) { FactoryGirl.build_stubbed(:requirement) } |
102 | 54 | before(:each) do
|
103 |
| - put :update, |
104 |
| - id: @requirement, |
105 |
| - requirement: FactoryGirl.attributes_for(:requirement, |
106 |
| - contact_name: 'John Doe') |
| 55 | + allow(Requirement).to receive(:new).and_return(req) |
| 56 | + allow(req).to receive(:save).and_return(true) |
| 57 | + post :create, requirement: { contact_name: 'name' } |
107 | 58 | end
|
108 |
| - it 'should set @requirement to the correct requirement' do |
109 |
| - expect(assigns(:requirement)).to eq(@requirement) |
110 |
| - end |
111 |
| - it 'should update the attributes of @requirement' do |
112 |
| - @requirement.reload |
113 |
| - expect(@requirement.contact_name).to eq('John Doe') |
| 59 | + it 'saves a new requirement' do |
| 60 | + expect(Requirement).to have_received(:new).twice |
| 61 | + expect(req).to have_received(:save) |
114 | 62 | end
|
115 |
| - it { is_expected.to redirect_to(@requirement) } |
| 63 | + it { is_expected.to redirect_to(req) } |
116 | 64 | it { is_expected.to set_flash }
|
117 | 65 | end
|
118 | 66 | context 'with invalid attributes' do
|
119 | 67 | before(:each) do
|
120 |
| - put :update, |
121 |
| - id: @requirement, |
122 |
| - requirement: FactoryGirl.attributes_for(:requirement, |
123 |
| - contact_name: '') |
| 68 | + req = RequirementMock.new(save: false) |
| 69 | + allow(Requirement).to receive(:new).and_return(req) |
| 70 | + post :create, requirement: { contact_name: 'name' } |
124 | 71 | end
|
125 |
| - it 'should not update the attributes of @requirement' do |
126 |
| - @requirement.reload |
127 |
| - expect(@requirement.contact_name).not_to eq('') |
128 |
| - expect(@requirement.contact_name).to eq('Adam Bray') |
129 |
| - end |
130 |
| - it { is_expected.to render_template(:edit) } |
131 | 72 | it { is_expected.not_to set_flash }
|
| 73 | + it { is_expected.to render_template(:new) } |
132 | 74 | end
|
133 | 75 | end
|
134 | 76 | context 'not admin' do
|
135 |
| - it 'should redirect to root url if not an admin' do |
136 |
| - sign_in FactoryGirl.create(:user) |
137 |
| - get :update, |
138 |
| - id: @requirement, |
139 |
| - requirement: FactoryGirl.attributes_for(:requirement) |
140 |
| - expect(response).to redirect_to(root_url) |
| 77 | + before do |
| 78 | + mock_user_sign_in |
| 79 | + post :create, requirement: { contact_name: 'name' } |
141 | 80 | end
|
| 81 | + it_behaves_like 'redirected request' |
142 | 82 | end
|
143 | 83 | end
|
144 |
| - describe 'POST create' do |
| 84 | + |
| 85 | + describe 'PUT update' do |
145 | 86 | context 'is admin' do
|
146 |
| - before(:each) do |
147 |
| - sign_in FactoryGirl.create(:admin) |
148 |
| - end |
| 87 | + before(:each) { mock_user_sign_in(UserMock.new(:admin)) } |
149 | 88 | context 'with valid attributes' do
|
| 89 | + let!(:req) { FactoryGirl.build_stubbed(:requirement) } |
150 | 90 | before(:each) do
|
151 |
| - post :create, requirement: FactoryGirl.attributes_for(:requirement) |
| 91 | + allow(Requirement).to receive(:find).with(req.id.to_s).and_return(req) |
| 92 | + allow(req).to receive(:update_attributes).and_return(true) |
| 93 | + put :update, id: req.id, requirement: { contact_name: 'Name' } |
152 | 94 | end
|
153 |
| - it 'saves a new requirement' do |
154 |
| - expect do |
155 |
| - post :create, requirement: FactoryGirl.attributes_for(:requirement) |
156 |
| - end.to change(Requirement, :count).by(1) |
| 95 | + it 'should update the attributes of @requirement' do |
| 96 | + expect(req).to have_received(:update_attributes) |
157 | 97 | end
|
158 |
| - it { is_expected.to redirect_to(Requirement.last) } |
| 98 | + it { is_expected.to redirect_to(req) } |
159 | 99 | it { is_expected.to set_flash }
|
160 | 100 | end
|
161 | 101 | context 'with invalid attributes' do
|
| 102 | + let!(:req) { RequirementMock.new(traits: [:findable]) } |
162 | 103 | before(:each) do
|
163 |
| - post :create, |
164 |
| - requirement: FactoryGirl.attributes_for(:requirement, |
165 |
| - contact_name: nil) |
166 |
| - end |
167 |
| - it 'fails to save a new requirment' do |
168 |
| - expect do |
169 |
| - post :create, |
170 |
| - requirement: FactoryGirl.attributes_for(:requirement, |
171 |
| - contact_name: nil) |
172 |
| - end.not_to change(Requirement, :count) |
| 104 | + allow(req).to receive(:update_attributes).and_return(false) |
| 105 | + put :update, id: req.id, requirement: { contact_name: 'Name' } |
173 | 106 | end
|
| 107 | + it { is_expected.to render_template(:edit) } |
174 | 108 | it { is_expected.not_to set_flash }
|
175 |
| - it { is_expected.to render_template(:new) } |
176 | 109 | end
|
177 | 110 | end
|
178 | 111 | context 'not admin' do
|
179 |
| - it 'should redirect to root url if not an admin' do |
180 |
| - sign_in FactoryGirl.create(:user) |
181 |
| - post :create, requirement: FactoryGirl.attributes_for(:requirement) |
182 |
| - expect(response).to redirect_to(root_url) |
| 112 | + before do |
| 113 | + mock_user_sign_in |
| 114 | + put :update, id: 1, requirement: { contact_name: 'Name' } |
183 | 115 | end
|
| 116 | + it_behaves_like 'redirected request' |
184 | 117 | end
|
185 | 118 | end
|
| 119 | + |
186 | 120 | describe 'DELETE destroy' do
|
187 | 121 | context 'is admin' do
|
| 122 | + let!(:req) { RequirementMock.new(traits: [:findable]) } |
188 | 123 | before(:each) do
|
189 |
| - sign_in FactoryGirl.create(:admin) |
190 |
| - end |
191 |
| - it 'assigns the selected requirement to @requirement' do |
192 |
| - delete :destroy, id: @requirement |
193 |
| - expect(assigns(:requirement)).to eq(@requirement) |
| 124 | + mock_user_sign_in(UserMock.new(:admin)) |
| 125 | + delete :destroy, id: req.id |
194 | 126 | end
|
195 |
| - it 'removes @requirement from the database' do |
196 |
| - expect do |
197 |
| - delete :destroy, id: @requirement |
198 |
| - end.to change(Requirement, :count).by(-1) |
| 127 | + it 'destroys the requirement' do |
| 128 | + expect(req).to have_received(:destroy).with(:force) |
199 | 129 | end
|
200 | 130 | it 'should redirect to the requirements index page' do
|
201 |
| - delete :destroy, id: @requirement |
202 | 131 | expect(response).to redirect_to requirements_url
|
203 | 132 | end
|
204 | 133 | end
|
205 | 134 | context 'not admin' do
|
206 |
| - it 'should redirect to root url if not an admin' do |
207 |
| - sign_in FactoryGirl.create(:user) |
208 |
| - delete :destroy, id: @requirement |
209 |
| - expect(response).to redirect_to(root_url) |
| 135 | + before do |
| 136 | + mock_user_sign_in |
| 137 | + delete :destroy, id: 1 |
210 | 138 | end
|
| 139 | + it_behaves_like 'redirected request' |
211 | 140 | end
|
212 | 141 | end
|
213 | 142 | end
|
0 commit comments