From 03986b7c219566d70ba363cc3d2a8b5fdef3c319 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Tue, 28 Aug 2012 20:57:12 +0400 Subject: [PATCH] [refs #374] Remove index actions from projects and reps. Add projects#get_id. Update tests. Fix build list show template --- app/controllers/api/v1/projects_controller.rb | 10 ++++-- .../api/v1/repositories_controller.rb | 4 --- app/models/ability.rb | 1 + .../api/v1/build_lists/show.json.jbuilder | 11 ++++-- .../api/v1/platforms/index.json.jbuilder | 2 +- .../api/v1/projects/get_id.json.jbuilder | 9 +++++ .../api/v1/repositories/index.json.jbuilder | 10 ------ config/routes.rb | 6 ++-- db/schema.rb | 16 ++++----- .../api/v1/projects_controller_spec.rb | 34 ++++++++++++++----- .../api/v1/repositories_controller_spec.rb | 15 +------- 11 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 app/views/api/v1/projects/get_id.json.jbuilder delete mode 100644 app/views/api/v1/repositories/index.json.jbuilder diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index fe6e4244d..dca7fc7b5 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -1,9 +1,13 @@ # -*- encoding : utf-8 -*- class Api::V1::ProjectsController < Api::V1::BaseController before_filter :authenticate_user! - load_and_authorize_resource# :id_param => :project_name # to force member actions load + load_and_authorize_resource - def index - @projects = Project.accessible_by(current_ability, :membered) + def get_id + if @project = Project.find_by_owner_and_name(params[:owner], params[:name]) + authorize! :show, @project + else + render :json => {:message => t("flash.404_message")}.to_json + end end end diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index b46042579..76c22f036 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -3,8 +3,4 @@ class Api::V1::RepositoriesController < Api::V1::BaseController before_filter :authenticate_user! load_and_authorize_resource :repository, :through => :platform, :shallow => true - - def index - @repositories = @repositories.paginate(:page => params[:page]) - end end diff --git a/app/models/ability.rb b/app/models/ability.rb index 33fd95408..47ab633be 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -14,6 +14,7 @@ class Ability # Shared rights between guests and registered users can :show, Project, :visibility => 'open' + can :get_id, Project can :archive, Project, :visibility => 'open' can :read, Issue, :project => {:visibility => 'open'} can :search, BuildList diff --git a/app/views/api/v1/build_lists/show.json.jbuilder b/app/views/api/v1/build_lists/show.json.jbuilder index 2fd8088b4..076f4540c 100644 --- a/app/views/api/v1/build_lists/show.json.jbuilder +++ b/app/views/api/v1/build_lists/show.json.jbuilder @@ -10,6 +10,7 @@ json.build_list do |json| json.project do |json_project| json_project.(@build_list.project, :id, :name) json_project.fullname @build_list.project.name_with_owner + json_project.url api_v1_project_path(@build_list.project, :format => :json) end json.save_to_repository do |json_save_to_repository| @@ -17,16 +18,20 @@ json.build_list do |json| json_save_to_repository.platform do |json_str_platform| json_str_platform.(@build_list.save_to_repository.platform, :id, :name) + json_str_platform.url api_v1_platform_path(@build_list.save_to_repository.platform, :format => :json) end + + json_save_to_repository.url api_v1_repository_path(@build_list.save_to_repository, :format => :json) end json.build_for_platform do |json_build_for_platform| json_build_for_platform.(@build_list.build_for_platform, :id, :name) + json_build_for_platform.url api_v1_platform_path(@build_list.build_for_platform, :format => :json) end - json.user do |json_user| - json_user.(@build_list.user, :id, :name) - json_user.url user_path(@build_list.user) + json.owner do |json_owner| + json_owner.(@build_list.user, :id, :name) + json_owner.url @build_list.project.owner_type == 'User' ? user_path(@build_list.project.owner.uname) : group_path(@build_list.project.owner.uname) end inc_repos = Repository.includes(:platform).where(:id => @build_list.include_repos) diff --git a/app/views/api/v1/platforms/index.json.jbuilder b/app/views/api/v1/platforms/index.json.jbuilder index 3c6f70e76..39b1e93df 100644 --- a/app/views/api/v1/platforms/index.json.jbuilder +++ b/app/views/api/v1/platforms/index.json.jbuilder @@ -5,7 +5,7 @@ json.platforms @platforms do |json, platform| json_owner.type platform.owner_type json_owner.url platform.owner_type == 'User' ? user_path(platform.owner) : group_path(platform.owner) end - json.url api_v1_platform_path(platform, :format => :json) + json.url api_v1_platform_path(platform.name, :format => :json) end json.url api_v1_platforms_path(:format => :json) diff --git a/app/views/api/v1/projects/get_id.json.jbuilder b/app/views/api/v1/projects/get_id.json.jbuilder new file mode 100644 index 000000000..c4274d1fb --- /dev/null +++ b/app/views/api/v1/projects/get_id.json.jbuilder @@ -0,0 +1,9 @@ +json.project do |json| + json.(@project, :id, :name, :visibility) + json.owner do |json_owner| + json_owner.(@project.owner, :id, :name) + json_owner.type @project.owner_type + json_owner.url @project.owner_type == 'User' ? user_path(@project.owner) : group_path(@project.owner) + end + json.url api_v1_project_path(@project, :format => :json) +end diff --git a/app/views/api/v1/repositories/index.json.jbuilder b/app/views/api/v1/repositories/index.json.jbuilder deleted file mode 100644 index 896b1a182..000000000 --- a/app/views/api/v1/repositories/index.json.jbuilder +++ /dev/null @@ -1,10 +0,0 @@ -json.repositories @repositories do |json, repository| - json.(repository, :id, :name) - json.platform do |json_platform| - json_platform.(repository.platform, :id, :name) - json_platform.url api_v1_platform_path(repository.platform) - end - json.url api_v1_repository_path(repository, :format => :json) -end - -json.url api_v1_repositories_path(:format => :json) diff --git a/config/routes.rb b/config/routes.rb index 8c206b7a3..f6f7e50c8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,8 +20,10 @@ Rosa::Application.routes.draw do } end resources :platforms, :only => [:index, :show] - resources :repositories, :only => [:index, :show] - resources :projects, :only => [:index, :show] + resources :repositories, :only => [:show] + resources :projects, :only => [:show] do + collection { get :get_id } + end end end diff --git a/db/schema.rb b/db/schema.rb index 388cb9e4c..cae031337 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -17,8 +17,8 @@ ActiveRecord::Schema.define(:version => 20120730214052) do t.integer "user_id", :null => false t.string "kind" t.text "data" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "advisories", :force => true do |t| @@ -161,12 +161,12 @@ ActiveRecord::Schema.define(:version => 20120730214052) do end create_table "flash_notifies", :force => true do |t| - t.text "body_ru", :null => false - t.text "body_en", :null => false - t.string "status", :null => false - t.boolean "published", :default => true, :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.text "body_ru" + t.text "body_en" + t.string "status" + t.boolean "published" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "groups", :force => true do |t| diff --git a/spec/controllers/api/v1/projects_controller_spec.rb b/spec/controllers/api/v1/projects_controller_spec.rb index 32b7e3e68..4b949cfdb 100644 --- a/spec/controllers/api/v1/projects_controller_spec.rb +++ b/spec/controllers/api/v1/projects_controller_spec.rb @@ -3,11 +3,6 @@ require 'spec_helper' shared_examples_for "api projects user with reader rights" do include_examples "api projects user with show rights" - - it "should render index template" do - get :index, :format => :json - render_template(:index) - end end shared_examples_for "api projects user with reader rights for hidden project" do @@ -27,10 +22,15 @@ shared_examples_for "api projects user without reader rights for hidden project" end shared_examples_for "api projects user without show rights" do - it "should not show project data" do + it "should show access violation instead of project data" do get :show, :id => @project.id, :format => :json response.body.should == {"message" => "Access violation to this page!"}.to_json end + + it "should access violation instead of project data by get_id" do + get :get_id, :name => @project.name, :owner => @project.owner.uname, :format => :json + response.body.should == {"message" => "Access violation to this page!"}.to_json + end end shared_examples_for "api projects user with show rights" do @@ -38,6 +38,24 @@ shared_examples_for "api projects user with show rights" do get :show, :id => @project.id, :format => :json render_template(:show) end + + context 'project find by get_id' do + it "should find project by name and owner name" do + @project.reload + get :get_id, :name => @project.name, :owner => @project.owner.uname, :format => :json + assigns[:project].id.should == @project.id + end + + it "should not find project by non existing name and owner name" do + get :get_id, :name => 'NONE_EXISTING_NAME', :owner => @project.owner.uname, :format => :json + assigns[:project].should be_blank + end + + it "should render 404 for non existing name and owner name" do + get :get_id, :name => 'NONE_EXISTING_NAME', :owner => @project.owner.uname, :format => :json + response.body.should == {:message => I18n.t("flash.404_message")}.to_json + end + end end describe Api::V1::ProjectsController do @@ -52,8 +70,8 @@ describe Api::V1::ProjectsController do #TODO: Find out how it works (why it only sets 401 status without redirect on .json) context 'for guest' do - it 'should not be able to perform index action' do - get :index, :format => :json + it 'should not be able to perform get_id action' do + get :get_id, :format => :json response.status.should == 401 end diff --git a/spec/controllers/api/v1/repositories_controller_spec.rb b/spec/controllers/api/v1/repositories_controller_spec.rb index da8fb583e..740f765a3 100644 --- a/spec/controllers/api/v1/repositories_controller_spec.rb +++ b/spec/controllers/api/v1/repositories_controller_spec.rb @@ -2,15 +2,7 @@ require 'spec_helper' shared_examples_for 'api repository user with reader rights' do - it 'should be able to perform index action' do - get :index, :platform_id => @platform.id, :format => :json - response.should render_template(:index) - end - - it 'should be able to perform show action' do - get :show, :id => @repository.id, :format => :json - response.should render_template(:show) - end + it_should_behave_like 'api repository user with show rights' end shared_examples_for 'api repository user with reader rights for hidden platform' do @@ -55,11 +47,6 @@ describe Api::V1::RepositoriesController do end context 'for guest' do - it "should not be able to perform index action" do - get :index, :platform_id => @platform, :format => :json - response.status.should == 401 - end - it "should not be able to perform show action" do get :show, :id => @repository.id, :format => :json response.status.should == 401