Commit 0a8d1cf06d66f706100e2b7aa7dcd640691e4bf2
Committed by
Rafael Manzo
1 parent
867efc6b
Exists in
colab
and in
4 other branches
Models & routes for configuration
Implementing ownership for configuration. Signed-off By: Diego Araújo <diegoamc90@gmail.com> Signed-off By: Fellipe Souto Sampaio <fllsouto@gmail.com>
Showing
7 changed files
with
124 additions
and
2 deletions
Show diff stats
app/controllers/concerns/ownership_authentication.rb
| ... | ... | @@ -25,6 +25,12 @@ module OwnershipAuthentication |
| 25 | 25 | check_reading_group_ownership(params[:reading_group_id]) |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | + def configuration_owner? | |
| 29 | + check_configuration_ownership(params[:configuration_id]) | |
| 30 | + end | |
| 31 | + | |
| 32 | + private | |
| 33 | + | |
| 28 | 34 | def check_project_ownership(id) |
| 29 | 35 | if current_user.project_ownerships.find_by_project_id(id).nil? |
| 30 | 36 | respond_to do |format| |
| ... | ... | @@ -42,4 +48,13 @@ module OwnershipAuthentication |
| 42 | 48 | end |
| 43 | 49 | end |
| 44 | 50 | end |
| 51 | + | |
| 52 | + def check_configuration_ownership(id) | |
| 53 | + if current_user.configuration_ownerships.find_by_configuration_id(id).nil? | |
| 54 | + respond_to do |format| | |
| 55 | + format.html { redirect_to configuration_url(id), notice: "You're not allowed to do this operation" } | |
| 56 | + format.json { head :no_content } | |
| 57 | + end | |
| 58 | + end | |
| 59 | + end | |
| 45 | 60 | end | ... | ... |
| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | +require "validators/kalibro_uniqueness_validator.rb" | |
| 2 | + | |
| 3 | +class Configuration < KalibroGem::Entities::Configuration | |
| 4 | + include KalibroRecord | |
| 5 | + | |
| 6 | + attr_accessor :name | |
| 7 | + validates :name, presence: true, kalibro_uniqueness: true | |
| 8 | + | |
| 9 | +end | |
| 0 | 10 | \ No newline at end of file | ... | ... |
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 :configurations | |
| 16 | + | |
| 15 | 17 | resources :reading_groups do |
| 16 | 18 | resources :readings, except: [:index, :update, :show] |
| 17 | 19 | put '/readings/:id' => 'readings#update', as: :reading_update | ... | ... |
spec/factories/configurations.rb
| 1 | 1 | FactoryGirl.define do |
| 2 | - factory :configuration, class: KalibroGem::Entities::Configuration do | |
| 2 | + factory :configuration, class: Configuration do | |
| 3 | 3 | id 1 |
| 4 | 4 | name "Java" |
| 5 | 5 | description "Code metrics for Java." |
| 6 | 6 | end |
| 7 | 7 | |
| 8 | - factory :another_configuration, class: KalibroGem::Entities::Configuration do | |
| 8 | + factory :another_configuration, class: Configuration do | |
| 9 | 9 | id 12 |
| 10 | 10 | name "Perl" |
| 11 | 11 | description "Code metrics for Perl." | ... | ... |
| ... | ... | @@ -0,0 +1,62 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Configuration do | |
| 4 | + subject { FactoryGirl.build(:configuration) } | |
| 5 | + describe 'methods' do | |
| 6 | + describe 'persisted?' do | |
| 7 | + before :each do | |
| 8 | + Configuration.expects(:exists?).with(subject.id).returns(false) | |
| 9 | + end | |
| 10 | + | |
| 11 | + it 'should return false' do | |
| 12 | + subject.persisted?.should eq(false) | |
| 13 | + end | |
| 14 | + end | |
| 15 | + | |
| 16 | + describe 'update' do | |
| 17 | + before :each do | |
| 18 | + @subject_params = Hash[FactoryGirl.attributes_for(: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 | |
| 19 | + end | |
| 20 | + | |
| 21 | + context 'with valid attributes' do | |
| 22 | + before :each do | |
| 23 | + subject.expects(:save).returns(true) | |
| 24 | + end | |
| 25 | + | |
| 26 | + it 'should return true' do | |
| 27 | + subject.update(@subject_params).should eq(true) | |
| 28 | + end | |
| 29 | + end | |
| 30 | + | |
| 31 | + context 'with invalid attributes' do | |
| 32 | + before :each do | |
| 33 | + subject.expects(:save).returns(false) | |
| 34 | + end | |
| 35 | + | |
| 36 | + it 'should return false' do | |
| 37 | + subject.update(@subject_params).should eq(false) | |
| 38 | + end | |
| 39 | + end | |
| 40 | + end | |
| 41 | + end | |
| 42 | + | |
| 43 | + describe 'validations' do | |
| 44 | + context 'active model validations' do | |
| 45 | + before :each do | |
| 46 | + Configuration.expects(:all).at_least_once.returns([]) | |
| 47 | + end | |
| 48 | + it { should validate_presence_of(:name) } | |
| 49 | + end | |
| 50 | + | |
| 51 | + context 'kalibro validations' do | |
| 52 | + before :each do | |
| 53 | + Configuration.expects(:request).returns(42) | |
| 54 | + end | |
| 55 | + | |
| 56 | + it 'should validate uniqueness' do | |
| 57 | + KalibroUniquenessValidator.any_instance.expects(:validate_each).with(subject, :name, subject.name) | |
| 58 | + subject.save | |
| 59 | + end | |
| 60 | + end | |
| 61 | + end | |
| 62 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,26 @@ |
| 1 | +require "spec_helper" | |
| 2 | + | |
| 3 | +describe ConfigurationsController do | |
| 4 | + describe "routing" do | |
| 5 | + it { should route(:get, '/configurations/new'). | |
| 6 | + to(controller: :configurations, action: :new) } | |
| 7 | + | |
| 8 | + it { should route(:get, '/configurations'). | |
| 9 | + to(controller: :configurations, action: :index) } | |
| 10 | + | |
| 11 | + it { should route(:post, '/configurations'). | |
| 12 | + to(controller: :configurations, action: :create) } | |
| 13 | + | |
| 14 | + it { should route(:get, '/configurations/1'). | |
| 15 | + to(controller: :configurations, action: :show, id: "1") } | |
| 16 | + | |
| 17 | + it { should route(:get, '/configurations/1/edit'). | |
| 18 | + to(controller: :configurations, action: :edit, id: "1") } | |
| 19 | + | |
| 20 | + it { should route(:put, '/configurations/1'). | |
| 21 | + to(controller: :configurations, action: :update, id: "1") } | |
| 22 | + | |
| 23 | + it { should route(:delete, '/configurations/1'). | |
| 24 | + to(controller: :configurations, action: :destroy, id: "1") } | |
| 25 | + end | |
| 26 | +end | |
| 0 | 27 | \ No newline at end of file | ... | ... |