Merge pull request #375 from warpc/263-repository_refactoring

[Refs #263] Refactoring repository. Add tests
This commit is contained in:
Alexander Machehin 2012-04-02 10:44:28 -07:00
commit dfa538abef
10 changed files with 157 additions and 251 deletions

View File

@ -1,11 +1,6 @@
# -*- encoding : utf-8 -*-
class RepositoriesController < ApplicationController
before_filter :authenticate_user!
before_filter :find_repository, :except => [:index, :new, :create]
before_filter :find_platform, :only => [:show, :destroy, :add_project, :remove_project]
before_filter :get_paths, :only => [:show, :new, :create, :add_project, :remove_project]
before_filter :find_platforms, :only => [:new, :create]
before_filter :build_repository_stub, :only => [:new, :create]
load_and_authorize_resource :platform
load_and_authorize_resource :repository, :through => :platform, :shallow => true
@ -26,10 +21,9 @@ class RepositoriesController < ApplicationController
def destroy
@repository.destroy
platform_id = @repository.platform_id
flash[:notice] = t("flash.repository.destroyed")
redirect_to platform_repositories_path(platform_id)
redirect_to platform_repositories_path(@repository.platform)
end
def create
@ -37,7 +31,7 @@ class RepositoriesController < ApplicationController
@repository.platform_id = params[:platform_id]
if @repository.save
flash[:notice] = t('flash.repository.saved')
redirect_to @repositories_path
redirect_to platform_repository_path(@platform, @repository)
else
flash[:error] = t('flash.repository.save_error')
flash[:warning] = @repository.errors.full_messages.join('. ')
@ -87,7 +81,7 @@ class RepositoriesController < ApplicationController
@total_projects = @projects.count
@projects = @projects.search(params[:sSearch]).search_order if params[:sSearch].present?
@total_project = @projects.count
@projects = @projects.order(order)#.includes(:owner) #WTF????
@projects = @projects.order(order)
render :partial => (params[:added] == "true") ? 'project' : 'proj_ajax', :layout => false
end
@ -98,40 +92,4 @@ class RepositoriesController < ApplicationController
redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.project_removed')
end
protected
def get_paths
if params[:user_id]
@user = User.find params[:user_id]
@repositories_path = user_repositories_path @user
@new_repository_path = new_user_repository_path @user
elsif params[:group_id]
@group = Group.find params[:group_id]
@repositories_path = group_repositories_path @group
@new_repository_path = new_group_repository_path @group
elsif params[:platform_id]
@platform = Platform.find params[:platform_id]
@repositories_path = platform_repositories_path @platform
@new_repository_path = new_platform_repository_path @platform
else
@repositories_path = repositories_path
@new_repository_path = new_repository_path
end
end
def find_platform
@platform = @repository.platform
end
def find_platforms
@platforms = Platform.all
end
def find_repository
@repository = Repository.find(params[:id])
end
def build_repository_stub
@repository = Repository.build_stub(Platform.find(params[:platform_id]))
end
end

View File

@ -13,7 +13,8 @@ class Repository < ActiveRecord::Base
before_create :xml_rpc_create, :unless => lambda {Thread.current[:skip]}
before_destroy :xml_rpc_destroy, :unless => lambda {Thread.current[:skip]}
attr_accessible :description, :name
attr_accessible :name, :description
attr_readonly :name, :platform_id
def base_clone(attrs = {})
clone.tap do |c| # dup

View File

@ -4,17 +4,5 @@
%h3= t("layout.repositories.new_header")
= form_for :repository, :url => @repositories_path, :html => { :class => :form } do |f|
= form_for :repository, :url => platform_repositories_path(@platform), :html => { :class => :form } do |f|
= render :partial => "form", :locals => {:f => f}
-#.block
.secondary-navigation
%ul.wat-cf
%li.first= link_to t("layout.repositories.list"), @repositories_path + "#platforms"
%li.active= link_to t("layout.repositories.new"), @new_repository_path
.content
%h2.title= t("layout.repositories.new_header")
.inner
= form_for :repository, :url => @repositories_path, :html => { :class => :form } do |f|
= render :partial => "form", :locals => {:f => f}
-# content_for :sidebar, render(:partial => 'sidebar')

View File

@ -4,7 +4,7 @@
%h3.fix= "#{t("layout.repositories.about")}: #{@repository.name}"
%p= @platform.description
%p= @repository.description
%br
%br
@ -12,49 +12,4 @@
- if can? :add_project, @repository
= link_to t("layout.projects.add"), add_project_platform_repository_path(@platform, @repository), :class => 'button'
= render :partial => 'proj_list'#, :object => @projects
=# will_paginate @projects
-#.block
.secondary-navigation
%ul.wat-cf
%li.first= link_to t("layout.repositories.list"), @repositories_path + "#repositories"
%li= link_to t("layout.repositories.new"), @new_repository_path
%li.active= link_to t("layout.repositories.show"), repository_path(@repository)
.content
.inner
%p
%b
= t("activerecord.attributes.repository.name")
\:
= @repository.name
%p
%b
= t("activerecord.attributes.repository.description")
\:
= @repository.description
%p
%b
= t("activerecord.attributes.repository.platform")
\:
= link_to @repository.platform.description, url_for(@repository.platform)
.wat-cf
= link_to image_tag("x.png", :alt => t("layout.delete")) + " " + t("layout.delete"), @repository_path, :method => "delete", :class => "button", :confirm => t("layout.repositories.confirm_delete") if can? :destroy, @repository
-#%a{ :name => "projects" }
-#.block
.secondary-navigation
%ul.wat-cf
%li.first.active= link_to t("layout.projects.list"), repository_path(@repository) + "#projects"
%li= link_to t("layout.projects.add"), url_for(:controller => :repositories, :action => :add_project)
.content
%h2.title
= t("layout.projects.list_header")
.inner
= render :partial => 'shared/search_form'
= render :partial => 'proj_list1', :object => @projects
.actions-bar.wat-cf
.actions
= will_paginate @projects, :param_name => :project_page
-# content_for :sidebar, render(:partial => 'sidebar')
= render :partial => 'proj_list'

View File

@ -1,19 +1,83 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'
shared_examples_for 'not destroy personal repository' do
it 'should not be able to destroy personal repository' do
lambda { delete :destroy, :id => @personal_repository.id, :platform_id =>
@personal_repository.platform.id}.should change{ Repository.count }.by(0)
response.should redirect_to(forbidden_path)
end
end
shared_examples_for 'user with change projects in repository rights' do
it 'should be able to see add_project page' do
get :add_project, :id => @repository.id, :platform_id => @platform.id
response.should render_template(:projects_list)
end
it 'should be able to add project to repository' do
get :add_project, :id => @repository.id, :platform_id => @platform.id, :project_id => @project.id
response.should redirect_to(platform_repository_path(@repository.platform, @repository))
@repository.projects.should include (@project)
end
it 'should be able to remove project from repository' do
get :remove_project, :id => @repository.id, :platform_id => @platform.id, :project_id => @project.id
response.should redirect_to(platform_repository_path(@repository.platform, @repository))
@repository.projects.should_not include (@project)
end
end
shared_examples_for 'registered user' do
it 'should be able to perform index action' do
get :index
response.should render_template(:index)
end
it 'should be able to perform show action' do
get :show, :id => @repository.id
response.should render_template(:show)
end
end
shared_examples_for 'platform admin user' do
it_should_behave_like 'registered user'
it 'should be able to perform new action' do
get :new, :platform_id => @platform.id
response.should render_template(:new)
end
it 'should be able to create repository' do
lambda { post :create, @create_params }.should change{ Repository.count }.by(1)
response.should redirect_to(platform_repository_path(@platform, Repository.last))
end
it 'should be able to destroy repository in main platform' do
lambda { delete :destroy, :id => @repository.id }.should change{ Repository.count }.by(-1)
response.should redirect_to(platform_repositories_path(@repository.platform))
end
it_should_behave_like 'user with change projects in repository rights'
it_should_behave_like 'not destroy personal repository'
end
describe RepositoriesController do
before(:each) do
before(:each) do
stub_rsync_methods
@repository = FactoryGirl.create(:repository)
@personal_repository = FactoryGirl.create(:personal_repository)
@platform = FactoryGirl.create(:platform)
@repository = FactoryGirl.create(:repository, :platform => @platform)
@personal_repository = FactoryGirl.create(:personal_repository)
@project = FactoryGirl.create(:project)
@another_user = FactoryGirl.create(:user)
@create_params = {:repository => {:name => 'pro', :description => 'pro2'}, :platform_id => @platform.id}
end
end
context 'for guest' do
context 'for guest' do
[:index, :create].each do |action|
it "should not be able to perform #{ action } action" do
get action
@ -30,33 +94,33 @@ describe RepositoriesController do
end
context 'for admin' do
before(:each) do
@admin = FactoryGirl.create(:admin)
set_session_for(@admin)
end
it 'should be able to perform new action' do
get :new, :platform_id => @platform.id
response.should render_template(:new)
before(:each) do
@admin = FactoryGirl.create(:admin)
set_session_for(@admin)
end
it 'should be able to perform create action' do
post :create, @create_params
response.should redirect_to(platform_repositories_path(@platform.id))
it_should_behave_like 'platform admin user'
end
context 'for platform owner user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@repository.platform.update_attribute(:owner, @user)
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
end
it 'should change objects count after create action' do
lambda { post :create, @create_params }.should change{ Repository.count }.by(1)
end
it_should_behave_like 'repository user with admin rights'
it_should_behave_like 'platform admin user'
end
context 'for anyone except admin' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
end
context 'for user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
end
it_should_behave_like 'registered user'
it 'should not be able to perform new action' do
get :new, :platform_id => @platform.id
@ -65,49 +129,29 @@ describe RepositoriesController do
it 'should not be able to perform create action' do
post :create, @create_params
lambda { post :create, @create_params }.should change{ Repository.count }.by(0)
response.should redirect_to(forbidden_path)
end
it 'should not be able to destroy repository in main platform' do
delete :destroy, :id => @repository.id
response.should redirect_to(forbidden_path)
lambda { delete :destroy, :id => @repository.id }.should_not change{ Repository.count }.by(-1)
end
it 'should not change objects count after create action' do
lambda { post :create, @create_params }.should change{ Repository.count }.by(0)
it 'should not be able to add project to repository' do
get :add_project, :id => @repository.id, :platform_id => @platform.id, :project_id => @project.id
response.should redirect_to(forbidden_path)
@repository.projects.should_not include (@project)
end
it 'should not be able to remove project from repository' do
get :remove_project, :id => @repository.id, :platform_id => @platform.id, :project_id => @project.id
response.should redirect_to(forbidden_path)
@repository.projects.should_not include (@project)
end
it_should_behave_like 'not destroy personal repository'
end
context 'for owner user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@repository.platform.update_attribute(:owner, @user)
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin')
end
it_should_behave_like 'repository user with owner rights'
end
context 'for reader user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@repository.platform.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader')
end
it_should_behave_like 'repository user with reader rights'
it 'should not be able to perform add_project action' do
get :add_project, :id => @repository.id
response.should redirect_to(forbidden_path)
end
it 'should not be able to perform add_project action with project_id param' do
get :add_project, :id => @repository.id, :project_id => @project.id
response.should redirect_to(forbidden_path)
end
it 'should not be able to perform destroy action' do
delete :destroy, :id => @repository.id
response.should redirect_to(forbidden_path)
end
end
end

View File

@ -11,4 +11,9 @@ FactoryGirl.define do
factory :platform_with_repos, :parent => :platform do
repositories {|r| [r.association(:repository)]}
end
factory :personal_platform, :parent => :platform do
platform_type 'personal'
end
end

View File

@ -7,10 +7,7 @@ FactoryGirl.define do
end
factory :personal_repository, :parent => :repository do
after_create {|r|
r.platform.platform_type = 'personal'
r.platform.visibility = 'hidden'
r.platform.save!
}
association :platform, :factory => :personal_platform
end
end

View File

@ -15,4 +15,39 @@ describe Repository do
@platform.repositories.count.should eql(1)
end
end
before(:all) do
stub_rsync_methods
Platform.delete_all
User.delete_all
Repository.delete_all
FileUtils.rm_rf(APP_CONFIG['root_path'])
# Need for validate_uniqueness_of check
FactoryGirl.create(:repository)
end
it { should belong_to(:platform) }
it { should have_many(:project_to_repositories)}
it { should have_many(:projects).through(:project_to_repositories)}
it { should validate_presence_of(:name)}
it { should validate_uniqueness_of(:name).case_insensitive.scoped_to(:platform_id) }
it { should validate_format_of(:name).with('basic_repository-name-1234') }
it { should validate_format_of(:name).not_with('.!') }
it { should validate_format_of(:name).not_with('Main') }
it { should validate_presence_of(:description) }
it { should have_readonly_attribute(:name) }
it { should have_readonly_attribute(:platform_id) }
it { should_not allow_mass_assignment_of(:platform) }
it { should_not allow_mass_assignment_of(:platform_id) }
after(:all) do
Platform.delete_all
User.delete_all
Repository.delete_all
FileUtils.rm_rf(APP_CONFIG['root_path'])
end
end

View File

@ -1,14 +0,0 @@
# -*- encoding : utf-8 -*-
shared_examples_for 'not destroy personal repository' do
it 'should not be able to destroy personal repository' do
delete :destroy, :id => @personal_repository.id
response.should redirect_to(forbidden_path)
end
end
shared_examples_for 'destroy personal repository' do
it 'should be able to perform destroy action' do
delete :destroy, :id => @repository.id
response.should redirect_to(platform_repositories_path(@repository.platform.id))
end
end

View File

@ -1,63 +0,0 @@
# -*- encoding : utf-8 -*-
shared_examples_for 'repository user with reader rights' do
it 'should be able to perform index action' do
get :index
response.should render_template(:index)
end
it 'should be able to perform show action' do
get :show, :id => @repository.id
response.should render_template(:show)
end
end
shared_examples_for 'repository user with owner rights' do
it 'should be able to perform add_project action' do
get :add_project, :id => @repository.id
response.should render_template(:projects_list)
end
it 'should be able to perform add_project action with project_id param' do
get :add_project, :id => @repository.id, :project_id => @project.id
response.should redirect_to(platform_repository_path(@repository.platform, @repository))
end
it_should_behave_like 'repository user with add project rights'
it 'should be able to perform remove_project action' do
get :remove_project, :id => @repository.id, :project_id => @project.id
response.should redirect_to(platform_repository_path(@repository.platform, @repository))
end
it_should_behave_like 'repository user with remove project rights'
it 'should be able to perform destroy action' do
delete :destroy, :id => @repository.id
response.should redirect_to(platform_repositories_path(@repository.platform))
end
it 'should change objects count after destroy action' do
lambda { delete :destroy, :id => @repository.id }.should change{ Repository.count }.by(-1)
end
it_should_behave_like 'repository user with reader rights'
end
shared_examples_for 'repository user with admin rights' do
it_should_behave_like 'repository user with owner rights'
it_should_behave_like 'destroy personal repository'
end
shared_examples_for 'repository user with add project rights' do
it 'should be able to add project to repository' do
get :add_project, :id => @repository.id, :project_id => @project.id
@repository.projects.exists? :id => @project.id
end
end
shared_examples_for 'repository user with remove project rights' do
it 'should be able to remove project from repository' do
get :remove_project, :id => @repository.id, :project_id => @project.id
!@repository.projects.exists? :id => @project.id
end
end