Commit c1f0c0f15553ec5e716d85016fa66df762af6326
Committed by
Rafael Manzo
1 parent
c0ccf5f0
Exists in
colab
and in
4 other branches
Reading group controller.
Signed-off-by: Fellipe Souto <fllsouto@gmail.com> Signed-off-by: Renan Fichberg <rfichberg@gmail.com>
Showing
5 changed files
with
389 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,85 @@ |
1 | +include OwnershipAuthentication | |
2 | + | |
3 | +class ReadingGroupsController < ApplicationController | |
4 | + before_action :authenticate_user!, | |
5 | + except: [:index, :show] | |
6 | + before_action :reading_group_owner?, only: [:edit, :update, :destroy] | |
7 | + | |
8 | + # GET /reading_groups/new | |
9 | + def new | |
10 | + @reading_group = ReadingGroup.new | |
11 | + end | |
12 | + | |
13 | + # GET /reading_groups | |
14 | + # GET /reading_groups.json | |
15 | + def index | |
16 | + @reading_groups = ReadingGroup.all | |
17 | + end | |
18 | + | |
19 | + # POST /reading_groups | |
20 | + # POST /reading_groups.json | |
21 | + def create | |
22 | + @reading_group = ReadingGroup.new(reading_group_params) | |
23 | + respond_to do |format| | |
24 | + create_and_redir(format) | |
25 | + end | |
26 | + end | |
27 | + | |
28 | + # GET /reading_group/1 | |
29 | + # GET /reading_group/1.json | |
30 | + def show | |
31 | + set_reading_group | |
32 | + @reading_group_readings = @reading_group.readings | |
33 | + end | |
34 | + | |
35 | + # GET /reading_groups/1/edit | |
36 | + # GET /reading_groups/1/edit.json | |
37 | + def edit | |
38 | + set_reading_group | |
39 | + end | |
40 | + | |
41 | + def update | |
42 | + set_reading_group | |
43 | + if @reading_group.update(reading_group_params) | |
44 | + redirect_to(reading_group_path(@reading_group.id)) | |
45 | + else | |
46 | + render "edit" | |
47 | + end | |
48 | + end | |
49 | + | |
50 | + # DELETE /reading_group/1 | |
51 | + # DELETE /reading_group/1.json | |
52 | + def destroy | |
53 | + set_reading_group | |
54 | + current_user.reading_group_ownerships.find_by_reading_group_id(@reading_group.id).destroy | |
55 | + @reading_group.destroy | |
56 | + respond_to do |format| | |
57 | + format.html { redirect_to reading_groups_url } | |
58 | + format.json { head :no_content } | |
59 | + end | |
60 | + end | |
61 | + | |
62 | + private | |
63 | + # Use callbacks to share common setup or constraints between actions. | |
64 | + def set_reading_group | |
65 | + @reading_group = ReadingGroup.find(params[:id]) | |
66 | + end | |
67 | + | |
68 | + # Never trust parameters from the scary internet, only allow the white list through. | |
69 | + def reading_group_params | |
70 | + params[:reading_group] | |
71 | + end | |
72 | + | |
73 | + # Extracted code from create action | |
74 | + def create_and_redir(format) | |
75 | + if @reading_group.save | |
76 | + current_user.reading_group_ownerships.create reading_group_id: @reading_group.id | |
77 | + | |
78 | + format.html { redirect_to reading_group_path(@reading_group.id), notice: 'Reading Group was successfully created.' } | |
79 | + format.json { render action: 'show', status: :created, location: @reading_group } | |
80 | + else | |
81 | + format.html { render action: 'new' } | |
82 | + format.json { render json: @reading_group.errors, status: :unprocessable_entity } | |
83 | + end | |
84 | + end | |
85 | +end | ... | ... |
config/routes.rb
... | ... | @@ -12,6 +12,8 @@ Mezuro::Application.routes.draw do |
12 | 12 | get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process |
13 | 13 | end |
14 | 14 | |
15 | + resources :reading_groups | |
16 | + | |
15 | 17 | #resources :modules |
16 | 18 | get '/modules/:id/metric_history' => 'modules#metric_history' |
17 | 19 | post '/modules/:id/tree' => 'modules#load_module_tree' | ... | ... |
... | ... | @@ -0,0 +1,270 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe ReadingGroupsController do | |
4 | + describe 'new' do | |
5 | + before :each do | |
6 | + sign_in FactoryGirl.create(:user) | |
7 | + get :new | |
8 | + end | |
9 | + | |
10 | + it { should respond_with(:success) } | |
11 | + it { should render_template(:new) } | |
12 | + end | |
13 | + | |
14 | + describe 'create' do | |
15 | + before do | |
16 | + sign_in FactoryGirl.create(:user) | |
17 | + end | |
18 | + | |
19 | + context 'with valid fields' do | |
20 | + let(:reading_group) { FactoryGirl.build(:reading_group) } | |
21 | + let(:subject_params) { Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | |
22 | + | |
23 | + before :each do | |
24 | + ReadingGroup.any_instance.expects(:save).returns(true) | |
25 | + end | |
26 | + | |
27 | + context 'rendering the show' do | |
28 | + before :each do | |
29 | + ReadingGroup.expects(:exists?).returns(true) | |
30 | + | |
31 | + post :create, :reading_group => subject_params | |
32 | + end | |
33 | + | |
34 | + it 'should redirect to the show view' do | |
35 | + response.should redirect_to reading_group_path(reading_group) | |
36 | + end | |
37 | + end | |
38 | + | |
39 | + context 'without rendering the show view' do | |
40 | + before :each do | |
41 | + post :create, :reading_group => subject_params | |
42 | + end | |
43 | + | |
44 | + it { should respond_with(:redirect) } | |
45 | + end | |
46 | + end | |
47 | + | |
48 | + context 'with an invalid field' do | |
49 | + before :each do | |
50 | + @subject = FactoryGirl.build(:reading_group) | |
51 | + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | |
52 | + | |
53 | + ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject) | |
54 | + ReadingGroup.any_instance.expects(:save).returns(false) | |
55 | + | |
56 | + post :create, :reading_group => @subject_params | |
57 | + end | |
58 | + | |
59 | + it { should render_template(:new) } | |
60 | + end | |
61 | + end | |
62 | + | |
63 | + describe 'show' do | |
64 | + subject { FactoryGirl.build(:reading_group) } | |
65 | + let(:reading) { FactoryGirl.build(:reading) } | |
66 | + before :each do | |
67 | + ReadingGroup.expects(:find).with(subject.id.to_s).returns(subject) | |
68 | + subject.expects(:readings).returns(reading) | |
69 | + get :show, :id => subject.id | |
70 | + end | |
71 | + | |
72 | + it { should render_template(:show) } | |
73 | + end | |
74 | + | |
75 | + describe 'destroy' do | |
76 | + before do | |
77 | + @subject = FactoryGirl.build(:reading_group) | |
78 | + end | |
79 | + | |
80 | + context 'with an User logged in' do | |
81 | + before do | |
82 | + sign_in FactoryGirl.create(:user) | |
83 | + @ownership = FactoryGirl.build(:reading_group_ownership) | |
84 | + @ownerships = [] | |
85 | + | |
86 | + end | |
87 | + | |
88 | + context 'when the user owns the reading group' do | |
89 | + before :each do | |
90 | + @ownership.expects(:destroy) | |
91 | + @subject.expects(:destroy) | |
92 | + | |
93 | + #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :( | |
94 | + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) | |
95 | + @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership) | |
96 | + | |
97 | + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) | |
98 | + | |
99 | + ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) | |
100 | + delete :destroy, :id => @subject.id | |
101 | + end | |
102 | + | |
103 | + it 'should redirect to the reading groups page' do | |
104 | + response.should redirect_to reading_groups_url | |
105 | + end | |
106 | + | |
107 | + it { should respond_with(:redirect) } | |
108 | + end | |
109 | + | |
110 | + context "when the user doesn't own the reading group" do | |
111 | + before :each do | |
112 | + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) | |
113 | + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) | |
114 | + | |
115 | + delete :destroy, :id => @subject.id | |
116 | + end | |
117 | + | |
118 | + it { should redirect_to(reading_group_path) } | |
119 | + end | |
120 | + end | |
121 | + | |
122 | + context 'with no User logged in' do | |
123 | + before :each do | |
124 | + delete :destroy, :id => @subject.id | |
125 | + end | |
126 | + | |
127 | + it { should redirect_to new_user_session_path } | |
128 | + end | |
129 | + end | |
130 | + | |
131 | + describe 'index' do | |
132 | + before :each do | |
133 | + @subject = FactoryGirl.build(:reading_group) | |
134 | + ReadingGroup.expects(:all).returns([@subject]) | |
135 | + get :index | |
136 | + end | |
137 | + | |
138 | + it { should render_template(:index) } | |
139 | + end | |
140 | + | |
141 | + describe 'edit' do | |
142 | + before do | |
143 | + @subject = FactoryGirl.build(:reading_group) | |
144 | + end | |
145 | + | |
146 | + context 'with an User logged in' do | |
147 | + before do | |
148 | + @user = FactoryGirl.create(:user) | |
149 | + @ownership = FactoryGirl.build(:reading_group_ownership) | |
150 | + @ownerships = [] | |
151 | + | |
152 | + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) | |
153 | + | |
154 | + sign_in @user | |
155 | + end | |
156 | + | |
157 | + context 'when the user owns the reading group' do | |
158 | + before :each do | |
159 | + ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) | |
160 | + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) | |
161 | + | |
162 | + get :edit, :id => @subject.id | |
163 | + end | |
164 | + | |
165 | + it { should render_template(:edit) } | |
166 | + | |
167 | + it 'should assign to @reading group the @subject' do | |
168 | + assigns(:reading_group).should eq(@subject) | |
169 | + end | |
170 | + end | |
171 | + | |
172 | + context 'when the user does not own the reading group' do | |
173 | + before do | |
174 | + @subject = FactoryGirl.build(:another_reading_group) | |
175 | + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) | |
176 | + | |
177 | + get :edit, :id => @subject.id | |
178 | + end | |
179 | + | |
180 | + it { should redirect_to(reading_group_path) } | |
181 | + it { should set_the_flash[:notice].to("You're not allowed to do this operation") } | |
182 | + end | |
183 | + end | |
184 | + | |
185 | + context 'with no user logged in' do | |
186 | + before :each do | |
187 | + get :edit, :id => @subject.id | |
188 | + end | |
189 | + | |
190 | + it { should redirect_to new_user_session_path } | |
191 | + end | |
192 | + end | |
193 | + | |
194 | + describe 'update' do | |
195 | + before do | |
196 | + @subject = FactoryGirl.build(:reading_group) | |
197 | + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | |
198 | + end | |
199 | + | |
200 | + context 'when the user is logged in' do | |
201 | + before do | |
202 | + sign_in FactoryGirl.create(:user) | |
203 | + end | |
204 | + | |
205 | + context 'when user owns the reading group' do | |
206 | + before do | |
207 | + @ownership = FactoryGirl.build(:reading_group_ownership) | |
208 | + @ownerships = [] | |
209 | + | |
210 | + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) | |
211 | + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) | |
212 | + end | |
213 | + | |
214 | + context 'with valid fields' do | |
215 | + before :each do | |
216 | + ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) | |
217 | + ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true) | |
218 | + end | |
219 | + | |
220 | + context 'rendering the show' do | |
221 | + before :each do | |
222 | + ReadingGroup.expects(:exists?).returns(true) | |
223 | + | |
224 | + post :update, :id => @subject.id, :reading_group => @subject_params | |
225 | + end | |
226 | + | |
227 | + it 'should redirect to the show view' do | |
228 | + response.should redirect_to reading_group_path(@subject) | |
229 | + end | |
230 | + end | |
231 | + | |
232 | + context 'without rendering the show view' do | |
233 | + before :each do | |
234 | + post :update, :id => @subject.id, :reading_group => @subject_params | |
235 | + end | |
236 | + | |
237 | + it { should respond_with(:redirect) } | |
238 | + end | |
239 | + end | |
240 | + | |
241 | + context 'with an invalid field' do | |
242 | + before :each do | |
243 | + ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) | |
244 | + ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) | |
245 | + | |
246 | + post :update, :id => @subject.id, :reading_group => @subject_params | |
247 | + end | |
248 | + | |
249 | + it { should render_template(:edit) } | |
250 | + end | |
251 | + end | |
252 | + | |
253 | + context 'when the user does not own the reading group' do | |
254 | + before :each do | |
255 | + post :update, :id => @subject.id, :reading_group => @subject_params | |
256 | + end | |
257 | + | |
258 | + it { should redirect_to reading_group_path } | |
259 | + end | |
260 | + end | |
261 | + | |
262 | + context 'with no user logged in' do | |
263 | + before :each do | |
264 | + post :update, :id => @subject.id, :reading_group => @subject_params | |
265 | + end | |
266 | + | |
267 | + it { should redirect_to new_user_session_path } | |
268 | + end | |
269 | + end | |
270 | +end | ... | ... |
spec/factories/reading_groups.rb
... | ... | @@ -0,0 +1,26 @@ |
1 | +require "spec_helper" | |
2 | + | |
3 | +describe ReadingGroupsController do | |
4 | + describe "routing" do | |
5 | + it { should route(:get, '/reading_groups/new'). | |
6 | + to(controller: :reading_groups, action: :new) } | |
7 | + | |
8 | + it { should route(:get, '/reading_groups'). | |
9 | + to(controller: :reading_groups, action: :index) } | |
10 | + | |
11 | + it { should route(:post, '/reading_groups'). | |
12 | + to(controller: :reading_groups, action: :create) } | |
13 | + | |
14 | + it { should route(:get, '/reading_groups/1'). | |
15 | + to(controller: :reading_groups, action: :show, id: "1") } | |
16 | + | |
17 | + it { should route(:get, '/reading_groups/1/edit'). | |
18 | + to(controller: :reading_groups, action: :edit, id: "1") } | |
19 | + | |
20 | + it { should route(:put, '/reading_groups/1'). | |
21 | + to(controller: :reading_groups, action: :update, id: "1") } | |
22 | + | |
23 | + it { should route(:delete, '/reading_groups/1'). | |
24 | + to(controller: :reading_groups, action: :destroy, id: "1") } | |
25 | + end | |
26 | +end | ... | ... |