[refs #374] Remove index actions from projects and reps. Add projects#get_id. Update tests. Fix build list show template

This commit is contained in:
konstantin.grabar 2012-08-28 20:57:12 +04:00
parent 4727be33e4
commit 03986b7c21
11 changed files with 65 additions and 53 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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,10 +161,10 @@ 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.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

View File

@ -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

View File

@ -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