Commit 0b4d7363524f706e00185a29ae04ffbe23d618a7

Authored by Diego Camarinha
Committed by Rafael Manzo
1 parent ce912358

Ownerships and Controller for Configuration.

Signed-off-by: Renan Fichberg <rfichberg@gmail.com>
app/controllers/concerns/ownership_authentication.rb
@@ -26,7 +26,7 @@ module OwnershipAuthentication @@ -26,7 +26,7 @@ module OwnershipAuthentication
26 end 26 end
27 27
28 def mezuro_configuration_owner? 28 def mezuro_configuration_owner?
29 - check_mezuro_configuration_ownership(params[:configuration_id]) 29 + check_mezuro_configuration_ownership(params[:id])
30 end 30 end
31 31
32 private 32 private
@@ -52,7 +52,7 @@ module OwnershipAuthentication @@ -52,7 +52,7 @@ module OwnershipAuthentication
52 def check_mezuro_configuration_ownership(id) 52 def check_mezuro_configuration_ownership(id)
53 if current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(id).nil? 53 if current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(id).nil?
54 respond_to do |format| 54 respond_to do |format|
55 - format.html { redirect_to mezuro_configuration_url(id), notice: "You're not allowed to do this operation" } 55 + format.html { redirect_to mezuro_configurations_url, notice: "You're not allowed to do this operation" }
56 format.json { head :no_content } 56 format.json { head :no_content }
57 end 57 end
58 end 58 end
app/controllers/mezuro_configurations_controller.rb
  1 +include OwnershipAuthentication
  2 +
1 class MezuroConfigurationsController < ApplicationController 3 class MezuroConfigurationsController < ApplicationController
  4 + before_action :authenticate_user!,
  5 + except: [:index, :show]
  6 + before_action :mezuro_configuration_owner?, only: [:edit, :update, :destroy]
2 7
  8 + # GET /mezuro_configurations/new
  9 + def new
  10 + @mezuro_configuration = MezuroConfiguration.new
  11 + end
  12 +
  13 + # GET /mezuro_configurations
  14 + # GET /mezuro_configurations.json
3 def index 15 def index
4 - @configurations = MezuroConfiguration.all 16 + @mezuro_configurations = MezuroConfiguration.all
5 end 17 end
6 - 18 +
  19 + # POST /mezuro_configurations
  20 + # POST /mezuro_configurations.json
  21 + def create
  22 + @mezuro_configuration = MezuroConfiguration.new(mezuro_configuration_params)
  23 + respond_to do |format|
  24 + create_and_redir(format)
  25 + end
  26 + end
  27 +
  28 + # GET /mezuro_configurations/1
  29 + # GET /mezuro_configurations/1.json
7 def show 30 def show
8 - @configuration = MezuroConfiguration.find(params[:id]) 31 + set_mezuro_configuration
  32 + @mezuro_configuration_metric_configurations = @mezuro_configuration.metric_configurations
9 end 33 end
10 34
11 - def new  
12 - @configuration = MezuroConfiguration.new 35 + # GET /mezuro_configurations/1/edit
  36 + # GET /mezuro_configurations/1/edit.json
  37 + def edit
  38 + set_mezuro_configuration
13 end 39 end
14 40
15 - def create  
16 - @configuration = MezuroConfiguration.new(configuration_params)  
17 - if @configuration.save  
18 - redirect_to mezuro_configuration_path(@configuration.id),  
19 - notice: 'Configuração criada com sucesso!'  
20 - else  
21 - render action: :new  
22 - end 41 +
  42 + def update
  43 + set_mezuro_configuration
  44 + if @mezuro_configuration.update(mezuro_configuration_params)
  45 + redirect_to(mezuro_configuration_path(@mezuro_configuration.id))
  46 + else
  47 + render "edit"
  48 + end
23 end 49 end
24 50
25 - def edit  
26 - @configuration = MezuroConfiguration.find(params[:id]) 51 + # DELETE /mezuro_configurations/1
  52 + # DELETE /mezuro_configurations/1.json
  53 + def destroy
  54 + set_mezuro_configuration
  55 + current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(@mezuro_configuration.id).destroy
  56 + @mezuro_configuration.destroy
  57 + respond_to do |format|
  58 + format.html { redirect_to mezuro_configurations_url }
  59 + format.json { head :no_content }
  60 + end
27 end 61 end
28 62
29 private 63 private
  64 + # Use callbacks to share common setup or constraints between actions.
  65 + def set_mezuro_configuration
  66 + @mezuro_configuration = MezuroConfiguration.find(params[:id])
  67 + end
30 68
31 - def configuration_params  
32 - params.  
33 - require(:mezuro_configuration).  
34 - permit(:name, :description) 69 + # Never trust parameters from the scary internet, only allow the white list through.
  70 + def mezuro_configuration_params
  71 + params[:mezuro_configuration]
35 end 72 end
36 73
37 -end 74 + # Extracted code from create action
  75 + def create_and_redir(format)
  76 + if @mezuro_configuration.save
  77 + current_user.mezuro_configuration_ownerships.create mezuro_configuration_id: @mezuro_configuration.id
38 78
  79 + format.html { redirect_to mezuro_configuration_path(@mezuro_configuration.id), notice: 'mezuro_configuration was successfully created.' }
  80 + format.json { render action: 'show', status: :created, location: @mezuro_configuration }
  81 + else
  82 + format.html { render action: 'new' }
  83 + format.json { render json: @mezuro_configuration.errors, status: :unprocessable_entity }
  84 + end
  85 + end
  86 +end
app/models/mezuro_configuration.rb
@@ -5,4 +5,8 @@ class MezuroConfiguration &lt; KalibroGem::Entities::Configuration @@ -5,4 +5,8 @@ class MezuroConfiguration &lt; KalibroGem::Entities::Configuration
5 5
6 attr_accessor :name 6 attr_accessor :name
7 validates :name, presence: true, kalibro_uniqueness: true 7 validates :name, presence: true, kalibro_uniqueness: true
8 -end  
9 \ No newline at end of file 8 \ No newline at end of file
  9 +
  10 + def metric_configurations
  11 + KalibroGem::Entities::MetricConfiguration.metric_configurations_of(self.id)
  12 + end
  13 +end
app/models/mezuro_configuration_ownership.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +class MezuroConfigurationOwnership < ActiveRecord::Base
  2 + belongs_to :user
  3 + validates :mezuro_configuration_id, presence: true
  4 +
  5 + def mezuro_configuration
  6 + MezuroConfiguration.find(mezuro_configuration_id)
  7 + end
  8 +end
app/models/user.rb
@@ -11,6 +11,8 @@ class User &lt; ActiveRecord::Base @@ -11,6 +11,8 @@ class User &lt; ActiveRecord::Base
11 has_many :project_ownerships 11 has_many :project_ownerships
12 12
13 has_many :reading_group_ownerships 13 has_many :reading_group_ownerships
  14 +
  15 + has_many :mezuro_configuration_ownerships
14 # Alert: when adding new parameters to this model, they should also be added to registrations_controller 16 # Alert: when adding new parameters to this model, they should also be added to registrations_controller
15 17
16 def projects 18 def projects
@@ -20,4 +22,8 @@ class User &lt; ActiveRecord::Base @@ -20,4 +22,8 @@ class User &lt; ActiveRecord::Base
20 def reading_groups 22 def reading_groups
21 reading_group_ownerships.map { |reading_group_ownership| reading_group_ownership.reading_group } 23 reading_group_ownerships.map { |reading_group_ownership| reading_group_ownership.reading_group }
22 end 24 end
  25 +
  26 + def mezuro_configurations
  27 + mezuro_configuration_ownerships.map { |mezuro_configuration_ownership| mezuro_configuration_ownership.mezuro_configuration }
  28 + end
23 end 29 end
db/migrate/20140124124835_create_mezuro_configuration_ownerships.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +class CreateMezuroConfigurationOwnerships < ActiveRecord::Migration
  2 + def change
  3 + create_table :mezuro_configuration_ownerships do |t|
  4 + t.integer :user_id
  5 + t.integer :mezuro_configuration_id
  6 +
  7 + t.timestamps
  8 + end
  9 + end
  10 +end
spec/controllers/mezuro_configurations_controller_spec.rb 0 → 100644
@@ -0,0 +1,270 @@ @@ -0,0 +1,270 @@
  1 +require 'spec_helper'
  2 +
  3 +describe MezuroConfigurationsController do
  4 +
  5 + describe 'new' do
  6 + before :each do
  7 + sign_in FactoryGirl.create(:user)
  8 + get :new
  9 + end
  10 +
  11 + it { should respond_with(:success) }
  12 + it { should render_template(:new) }
  13 + end
  14 +
  15 + describe 'create' do
  16 + before do
  17 + sign_in FactoryGirl.create(:user)
  18 + end
  19 +
  20 + context 'with valid fields' do
  21 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
  22 + let(:subject_params) { Hash[FactoryGirl.attributes_for(:mezuro_configuration).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
  23 +
  24 + before :each do
  25 + MezuroConfiguration.any_instance.expects(:save).returns(true)
  26 + end
  27 +
  28 + context 'rendering the show' do
  29 + before :each do
  30 + MezuroConfiguration.expects(:exists?).returns(true)
  31 +
  32 + post :create, :mezuro_configuration => subject_params
  33 + end
  34 +
  35 + it 'should redirect to the show view' do
  36 + response.should redirect_to mezuro_configuration_path(mezuro_configuration)
  37 + end
  38 + end
  39 +
  40 + context 'without rendering the show view' do
  41 + before :each do
  42 + post :create, :mezuro_configuration => subject_params
  43 + end
  44 +
  45 + it { should respond_with(:redirect) }
  46 + end
  47 + end
  48 +
  49 + context 'with an invalid field' do
  50 + before :each do
  51 + @subject = FactoryGirl.build(:mezuro_configuration)
  52 + @subject_params = Hash[FactoryGirl.attributes_for(:mezuro_configuration).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
  53 +
  54 + MezuroConfiguration.expects(:new).at_least_once.with(@subject_params).returns(@subject)
  55 + MezuroConfiguration.any_instance.expects(:save).returns(false)
  56 +
  57 + post :create, :mezuro_configuration => @subject_params
  58 + end
  59 +
  60 + it { should render_template(:new) }
  61 + end
  62 + end
  63 +
  64 + describe 'show' do
  65 + subject { FactoryGirl.build(:mezuro_configuration) }
  66 + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) }
  67 + before :each do
  68 + MezuroConfiguration.expects(:find).with(subject.id.to_s).returns(subject)
  69 + subject.expects(:metric_configurations).returns(metric_configuration)
  70 + get :show, :id => subject.id
  71 + end
  72 +
  73 + it { should render_template(:show) }
  74 + end
  75 +
  76 + describe 'destroy' do
  77 + before do
  78 + @subject = FactoryGirl.build(:mezuro_configuration)
  79 + end
  80 +
  81 + context 'with an User logged in' do
  82 + before do
  83 + sign_in FactoryGirl.create(:user)
  84 + @ownership = FactoryGirl.build(:mezuro_configuration_ownership)
  85 + @ownerships = []
  86 + end
  87 +
  88 + context 'when the user owns the mezuro_configuration' 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 @mezuro_configuration.id is an Integer :(
  94 + @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership)
  95 + @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(@ownership)
  96 +
  97 + User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships)
  98 +
  99 + MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject)
  100 + delete :destroy, :id => @subject.id
  101 + end
  102 +
  103 + it 'should redirect to the mezuro_configurations page' do
  104 + response.should redirect_to mezuro_configurations_url
  105 + end
  106 +
  107 + it { should respond_with(:redirect) }
  108 + end
  109 +
  110 + context "when the user doesn't own the mezuro_configuration" do
  111 + before :each do
  112 + @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(nil)
  113 + User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships)
  114 +
  115 + delete :destroy, :id => @subject.id
  116 + end
  117 +
  118 + it { should redirect_to(mezuro_configurations_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(:mezuro_configuration)
  134 + MezuroConfiguration.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(:mezuro_configuration)
  144 + end
  145 +
  146 + context 'with an User logged in' do
  147 + before do
  148 + @user = FactoryGirl.create(:user)
  149 + @ownership = FactoryGirl.build(:mezuro_configuration_ownership)
  150 + @ownerships = []
  151 +
  152 + User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships)
  153 +
  154 + sign_in @user
  155 + end
  156 +
  157 + context 'when the user owns the mezuro_configuration' do
  158 + before :each do
  159 + MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject)
  160 + @ownerships.expects(:find_by_mezuro_configuration_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 @mezuro_configuration the @subject' do
  168 + assigns(:mezuro_configuration).should eq(@subject)
  169 + end
  170 + end
  171 +
  172 + context 'when the user does not own the mezuro_configuration' do
  173 + before do
  174 + @subject = FactoryGirl.build(:another_mezuro_configuration)
  175 + @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(nil)
  176 +
  177 + get :edit, :id => @subject.id
  178 + end
  179 +
  180 + it { should redirect_to(mezuro_configurations_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(:mezuro_configuration)
  197 + @subject_params = Hash[FactoryGirl.attributes_for(:mezuro_configuration).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 mezuro_configuration' do
  206 + before do
  207 + @ownership = FactoryGirl.build(:mezuro_configuration_ownership)
  208 + @ownerships = []
  209 +
  210 + @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership)
  211 + User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships)
  212 + end
  213 +
  214 + context 'with valid fields' do
  215 + before :each do
  216 + MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject)
  217 + MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(true)
  218 + end
  219 +
  220 + context 'rendering the show' do
  221 + before :each do
  222 + MezuroConfiguration.expects(:exists?).returns(true)
  223 +
  224 + post :update, :id => @subject.id, :mezuro_configuration => @subject_params
  225 + end
  226 +
  227 + it 'should redirect to the show view' do
  228 + response.should redirect_to mezuro_configuration_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, :mezuro_configuration => @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 + MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject)
  244 + MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(false)
  245 +
  246 + post :update, :id => @subject.id, :mezuro_configuration => @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 mezuro_configuration' do
  254 + before :each do
  255 + post :update, :id => @subject.id, :mezuro_configuration => @subject_params
  256 + end
  257 +
  258 + it { should redirect_to mezuro_configurations_path }
  259 + end
  260 + end
  261 +
  262 + context 'with no user logged in' do
  263 + before :each do
  264 + post :update, :id => @subject.id, :mezuro_configuration => @subject_params
  265 + end
  266 +
  267 + it { should redirect_to new_user_session_path }
  268 + end
  269 + end
  270 +end
spec/factories/mezuro_configuration_ownerships.rb
@@ -3,6 +3,6 @@ @@ -3,6 +3,6 @@
3 FactoryGirl.define do 3 FactoryGirl.define do
4 factory :mezuro_configuration_ownership do 4 factory :mezuro_configuration_ownership do
5 user_id 1 5 user_id 1
6 - configuration_id 1 6 + mezuro_configuration_id 1
7 end 7 end
8 end 8 end
spec/models/mezuro_configuration_ownership_spec.rb 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +require 'spec_helper'
  2 +
  3 +describe MezuroConfigurationOwnership do
  4 + describe 'associations' do
  5 + it { should belong_to(:user) }
  6 + end
  7 +
  8 + describe 'methods' do
  9 + describe 'mezuro_configuration' do
  10 + subject {FactoryGirl.build(:mezuro_configuration_ownership)}
  11 + let(:mezuro_configuration) {FactoryGirl.build(:mezuro_configuration)}
  12 +
  13 + before :each do
  14 + MezuroConfiguration.expects(:find).with(subject.mezuro_configuration_id).returns(mezuro_configuration)
  15 + end
  16 +
  17 + it 'should return the mezuro_configuration' do
  18 + subject.mezuro_configuration.should eq(mezuro_configuration)
  19 + end
  20 + end
  21 + end
  22 +end
spec/models/mezuro_configuration_spec.rb
@@ -38,6 +38,17 @@ describe MezuroConfiguration do @@ -38,6 +38,17 @@ describe MezuroConfiguration do
38 end 38 end
39 end 39 end
40 end 40 end
  41 +
  42 + describe 'metric_configurations' do
  43 + subject { FactoryGirl.build(:mezuro_configuration) }
  44 + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) }
  45 +
  46 + it 'should call metric_configurations_of on the Metric Configuration model' do
  47 + KalibroGem::Entities::MetricConfiguration.expects(:metric_configurations_of).with(subject.id).returns([metric_configuration])
  48 +
  49 + subject.metric_configurations.should include(metric_configuration)
  50 + end
  51 + end
41 end 52 end
42 53
43 describe 'validations' do 54 describe 'validations' do
spec/models/user_spec.rb
@@ -12,6 +12,7 @@ describe User do @@ -12,6 +12,7 @@ describe User do
12 describe 'associations' do 12 describe 'associations' do
13 it { should have_many(:project_ownerships) } 13 it { should have_many(:project_ownerships) }
14 it { should have_many(:reading_group_ownerships) } 14 it { should have_many(:reading_group_ownerships) }
  15 + it { should have_many(:mezuro_configuration_ownerships) }
15 end 16 end
16 17
17 describe 'methods' do 18 describe 'methods' do
@@ -44,5 +45,20 @@ describe User do @@ -44,5 +45,20 @@ describe User do
44 subject.reading_groups.should eq([reading_group]) 45 subject.reading_groups.should eq([reading_group])
45 end 46 end
46 end 47 end
  48 +
  49 + describe 'mezuro_configurations' do
  50 + subject { FactoryGirl.build(:user) }
  51 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
  52 + let(:mezuro_configuration_ownership) { FactoryGirl.build(:mezuro_configuration_ownership) }
  53 +
  54 + before :each do
  55 + mezuro_configuration_ownership.expects(:mezuro_configuration).returns(mezuro_configuration)
  56 + subject.expects(:mezuro_configuration_ownerships).returns([mezuro_configuration_ownership])
  57 + end
  58 +
  59 + it 'should return a list of mezuro configurations owned by the user' do
  60 + subject.mezuro_configurations.should eq([mezuro_configuration])
  61 + end
  62 + end
47 end 63 end
48 end 64 end