From f3db00832afd5e53215a69874dc7a917c1b029d8 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Tue, 31 Jul 2012 11:35:27 +0400 Subject: [PATCH] [refs #374] Api v1 base functional and specs --- app/controllers/api/v1/base_controller.rb | 3 + .../api/v1/build_lists_controller.rb | 70 +++ app/controllers/application_controller.rb | 34 +- .../projects/build_lists_controller.rb | 56 +-- .../v1}/build_lists/index.json.erb | 6 +- app/views/api/v1/build_lists/show.json.erb | 42 ++ app/views/projects/build_lists/show.json.erb | 38 -- config/routes.rb | 12 + .../api/v1/build_lists_controller_spec.rb | 474 ++++++++++++++++++ 9 files changed, 640 insertions(+), 95 deletions(-) create mode 100644 app/controllers/api/v1/base_controller.rb create mode 100644 app/controllers/api/v1/build_lists_controller.rb rename app/views/{projects => api/v1}/build_lists/index.json.erb (55%) create mode 100644 app/views/api/v1/build_lists/show.json.erb delete mode 100644 app/views/projects/build_lists/show.json.erb create mode 100644 spec/controllers/api/v1/build_lists_controller_spec.rb diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 000000000..06a950d65 --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -0,0 +1,3 @@ +# -*- encoding : utf-8 -*- +class Api::V1::BaseController < ApplicationController +end diff --git a/app/controllers/api/v1/build_lists_controller.rb b/app/controllers/api/v1/build_lists_controller.rb new file mode 100644 index 000000000..5db970341 --- /dev/null +++ b/app/controllers/api/v1/build_lists_controller.rb @@ -0,0 +1,70 @@ +# -*- encoding : utf-8 -*- +class Api::V1::BuildListsController < Api::V1::BaseController + before_filter :authenticate_user! + skip_before_filter :authenticate_user!, :only => [:show, :index] if APP_CONFIG['anonymous_access'] + + load_and_authorize_resource :project, :only => :index + load_and_authorize_resource :build_list, :only => [:create, :cancel, :publish, :reject_publish]#, :shallow => true + + def index + filter = BuildList::Filter.new(nil, current_user, params[:filter] || {}) + @build_lists = filter.find.scoped(:include => [:save_to_platform, :project, :user, :arch]) + @build_lists = @build_lists.recent.paginate :page => params[:page], :per_page => params[:per_page] + end + + def create + notices, errors = [], [] + json_report = {:build_lists => []} + project = Project.find(params[:build_list][:project_id]) + platform = Platform.find params[:build_list][:save_to_platform_id] + params[:build_list][:auto_publish] = false if platform.released + + Arch.where(:id => params[:arches]).each do |arch| + Platform.main.where(:id => params[:build_for_platforms]).each do |build_for_platform| + build_list = project.build_lists.build(params[:build_list]) + build_list.commit_hash = project.repo.commits(build_list.project_version.match(/^latest_(.+)/).to_a.last || + build_list.project_version).first.id if build_list.project_version + build_list.build_for_platform = build_for_platform; build_list.arch = arch; build_list.user = current_user + build_list.include_repos = build_list.include_repos.select {|ir| build_list.build_for_platform.repository_ids.include? ir.to_i} + build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority + flash_options = {:project_version => build_list.project_version, :arch => arch.name, :build_for_platform => build_for_platform.name} + if build_list.save + msg = t("flash.build_list.saved", flash_options) + notices << msg + else + msg = t("flash.build_list.save_error", flash_options) + errors << msg + end + json_report[:build_lists] << {"id" => build_list.id, "message" => msg} + end + end + + errors << t("flash.build_list.no_arch_or_platform_selected") if errors.blank? and notices.blank? + render :json => json_report.to_json + end + + def cancel + if @build_list.cancel + render :json => {:is_canceled => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.cancel_success')} + else + render :json => {:is_canceled => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.cancel_fail')} + end + end + + def publish + if @build_list.publish + render :json => {:is_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.publish_success')} + else + render :json => {:is_published => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.publish_fail')} + end + end + + def reject_publish + if @build_list.reject_publish + render :json => {:is_reject_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.reject_publish_success')} + else + render :json => {:is_reject_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => t('layout.build_lists.reject_publish_fail')} + end + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6e2eb67f9..ebc41ec94 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,25 +21,25 @@ class ApplicationController < ActionController::Base end end - rescue_from Exception do |exception| - respond_to do |format| - format.json { render :json => {:message => t("flash.500_message")}.to_json } - format.html { redirect_to '/500.html', :alert => t("flash.500_message") } - end - end + #rescue_from Exception do |exception| + # respond_to do |format| + # format.json { render :json => {:message => t("flash.500_message")}.to_json } + # format.html { redirect_to '/500.html', :alert => t("flash.500_message") } + # end + #end rescue_from Grit::NoSuchPathError, :with => :not_found - [ActiveRecord::RecordNotFound, - ActionController::RoutingError, - ActionController::UnknownController, - ActionController::UnknownAction].each do |ext| - rescue_from ext do |exception| - respond_to do |format| - format.json { render :json => {:message => t("flash.404_message")}.to_json } - format.html { redirect_to '/404.html', :alert => t("flash.404_message") } - end - end - end + #[ActiveRecord::RecordNotFound, + # ActionController::RoutingError, + # ActionController::UnknownController, + # ActionController::UnknownAction].each do |ext| + # rescue_from ext do |exception| + # respond_to do |format| + # format.json { render :json => {:message => t("flash.404_message")}.to_json } + # format.html { redirect_to '/404.html', :alert => t("flash.404_message") } + # end + # end + # end protected diff --git a/app/controllers/projects/build_lists_controller.rb b/app/controllers/projects/build_lists_controller.rb index a38a020cb..ce7364d52 100644 --- a/app/controllers/projects/build_lists_controller.rb +++ b/app/controllers/projects/build_lists_controller.rb @@ -41,10 +41,8 @@ class Projects::BuildListsController < Projects::BaseController def create notices, errors = [], [] - json_report = {:build_lists => []} @platform = Platform.find params[:build_list][:save_to_platform_id] params[:build_list][:auto_publish] = false if @platform.released - Arch.where(:id => params[:arches]).each do |arch| Platform.main.where(:id => params[:build_for_platforms]).each do |build_for_platform| @build_list = @project.build_lists.build(params[:build_list]) @@ -55,29 +53,20 @@ class Projects::BuildListsController < Projects::BaseController @build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority flash_options = {:project_version => @build_list.project_version, :arch => arch.name, :build_for_platform => build_for_platform.name} if @build_list.save - msg = t("flash.build_list.saved", flash_options) - notices << msg + notices << t("flash.build_list.saved", flash_options) else - msg = t("flash.build_list.save_error", flash_options) - errors << msg + errors << t("flash.build_list.save_error", flash_options) end - json_report[:build_lists] << {"id" => @build_list.id, "message" => msg} end end - errors << t("flash.build_list.no_arch_or_platform_selected") if errors.blank? and notices.blank? - respond_to do |format| - format.html { - if errors.present? - @build_list ||= BuildList.new - flash[:error] = errors.join('
').html_safe - render :action => :new - else - flash[:notice] = notices.join('
').html_safe - redirect_to project_build_lists_path(@project) - end - } - format.json { render :json => json_report.to_json } + if errors.present? + @build_list ||= BuildList.new + flash[:error] = errors.join('
').html_safe + render :action => :new + else + flash[:notice] = notices.join('
').html_safe + redirect_to project_build_lists_path(@project) end end @@ -97,29 +86,20 @@ class Projects::BuildListsController < Projects::BaseController end def cancel - respond_to do |format| - if @build_list.cancel - format.html { redirect_to :back, :notice => t('layout.build_lists.cancel_success') } - format.json { render :json => {:is_canceled => true, :message => t('layout.build_lists.cancel_success')} } - else - format.html { redirect_to :back, :notice => t('layout.build_lists.cancel_fail') } - format.json { render :json => {:is_canceled => false, :message => t('layout.build_lists.cancel_fail')} } - end + if @build_list.cancel + redirect_to :back, :notice => t('layout.build_lists.cancel_success') + else + redirect_to :back, :notice => t('layout.build_lists.cancel_fail') end end def publish_build - respond_to do |format| - if params[:status].to_i == 0 # ok - @build_list.published - format.json { render :json => {:is_published => true, :message => t('layout.build_lists.publish_success')} } - else - @build_list.fail_publish - format.json { render :json => {:is_published => false, :message => t('layout.build_lists.publish_fail')} } - end - - format.html { render :nothing => true, :status => 200 } + if params[:status].to_i == 0 # ok + @build_list.published + else + @build_list.fail_publish end + render :nothing => true, :status => 200 end def status_build diff --git a/app/views/projects/build_lists/index.json.erb b/app/views/api/v1/build_lists/index.json.erb similarity index 55% rename from app/views/projects/build_lists/index.json.erb rename to app/views/api/v1/build_lists/index.json.erb index b83136a07..36229d464 100644 --- a/app/views/projects/build_lists/index.json.erb +++ b/app/views/api/v1/build_lists/index.json.erb @@ -5,8 +5,10 @@ "id": <%=build_list.id%>, "bs_id": <%=build_list.bs_id.to_json%>, "name": <%=build_list.name%>, - "status": <%=build_list.status%> + "status": <%=build_list.status%>, + "url": <%=api_v1_build_list_path(build_list, :format => :json)%> } <% end %> - ] + ], + "url": <%=api_v1_build_lists_path(:format => :json)%> } diff --git a/app/views/api/v1/build_lists/show.json.erb b/app/views/api/v1/build_lists/show.json.erb new file mode 100644 index 000000000..18f617897 --- /dev/null +++ b/app/views/api/v1/build_lists/show.json.erb @@ -0,0 +1,42 @@ +<%# TODO: May be will be better use .to_json(:only => ...) method for @build_list object %> +{ + "build_list": { + "id": <%=@build_list.id%>, + "bs_id": <%=@build_list.bs_id.to_json%>, + "name": "<%=@build_list.name%>", + "container_path": "<%=@build_list.container_path.to_json%>", + "status": <%=@build_list.status%>, + "project_version": "<%=@build_list.project_version%>", + "project_name": "<%=@build_list.project.name%>", + "project_id": <%=@build_list.project_id%>, + "build_for_platform_id": <%=@build_list.build_for_platform_id%>, + "build_for_platform_name": "<%=@build_list.build_for_platform.name%>", + "save_to_platform_id": <%=@build_list.save_to_platform_id%>, + "save_to_platform_name": "<%=@build_list.save_to_platform.name%>", + "arch_name": "<%=@build_list.arch.name%>", + "notified_at": "<%=@build_list.notified_at.to_json%>", + "is_circle": <%=@build_list.is_circle%>, + "update_type": "<%=@build_list.update_type%>", + "build_requires": <%=@build_list.build_requires%>, + "auto_publish": <%=@build_list.auto_publish%>, + "package_version": <%=@build_list.package_version%>, + "commit_hash": <%=@build_list.commit_hash.to_json%>, + "duration": <%=@build_list.duration.to_json%>, + "mass_build_id": <%=@build_list.mass_build_id.to_json%>, + "advisory_id": <%=@build_list.advisory_id.to_json%>, + "user": { + "id": <%=@build_list.user_id%>, + "name": "<%=@build_list.user.name%>", + "url": "<%=user_path(@build_list.user)%>" + }, + "additional_repos": [ + <% @build_list.additional_repos.each do |repo| %> + { + "id": <%=repo.id%>, + "name": "<%=repo.name%>" + } + <% end if @build_list.additional_repos %> + ], + "url": <%=api_v1_build_list_path(@build_list, :format => :json)%> + } +} diff --git a/app/views/projects/build_lists/show.json.erb b/app/views/projects/build_lists/show.json.erb deleted file mode 100644 index 9873a8204..000000000 --- a/app/views/projects/build_lists/show.json.erb +++ /dev/null @@ -1,38 +0,0 @@ -<%# TODO: May be will be better use .to_json(:only => ...) method for @build_list object %> -{ - "id": <%=@build_list.id%>, - "bs_id": <%=@build_list.bs_id.to_json%>, - "name": "<%=@build_list.name%>", - "container_path": "<%=@build_list.container_path.to_json%>", - "status": <%=@build_list.status%>, - "project_version": "<%=@build_list.project_version%>", - "project_name": "<%=@build_list.project.name%>", - "project_id": <%=@build_list.project_id%>, - "build_for_platform_id": <%=@build_list.build_for_platform_id%>, - "build_for_platform_name": "<%=@build_list.build_for_platform.name%>", - "save_to_platform_id": <%=@build_list.save_to_platform_id%>, - "save_to_platform_name": "<%=@build_list.save_to_platform.name%>", - "arch_name": "<%=@build_list.arch.name%>", - "notified_at": "<%=@build_list.notified_at.to_json%>", - "is_circle": <%=@build_list.is_circle%>, - "update_type": "<%=@build_list.update_type%>", - "build_requires": <%=@build_list.build_requires%>, - "auto_publish": <%=@build_list.auto_publish%>, - "package_version": <%=@build_list.package_version%>, - "commit_hash": <%=@build_list.commit_hash.to_json%>, - "duration": <%=@build_list.duration.to_json%>, - "mass_build_id": <%=@build_list.mass_build_id.to_json%>, - "advisory_id": <%=@build_list.advisory_id.to_json%>, - "user": { - "id": <%=@build_list.user_id%>, - "name": "<%=@build_list.user.name%>" - }, - "additional_repos": [ - <% @build_list.additional_repos.each do |repo| %> - { - "id": <%=repo.id%>, - "name": "<%=repo.name%>" - } - <% end if @build_list.additional_repos %> - ] -} diff --git a/config/routes.rb b/config/routes.rb index 1b391b6e0..27af97c4e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,18 @@ Rosa::Application.routes.draw do end devise_for :users, :controllers => {:omniauth_callbacks => 'users/omniauth_callbacks'} + namespace :api do + namespace :v1 do + resources :build_lists, :only => [:index, :create, :show] do + member { + get :publish + get :reject_publish + get :cancel + } + end + end + end + resources :search, :only => [:index] get '/forbidden' => 'pages#forbidden', :as => 'forbidden' diff --git a/spec/controllers/api/v1/build_lists_controller_spec.rb b/spec/controllers/api/v1/build_lists_controller_spec.rb new file mode 100644 index 000000000..ac867335e --- /dev/null +++ b/spec/controllers/api/v1/build_lists_controller_spec.rb @@ -0,0 +1,474 @@ +# -*- encoding : utf-8 -*- +require 'spec_helper' + +shared_examples_for 'show build list' do + it 'should be able to perform show action' do + get :show, @show_params + response.should render_template("api/v1/build_lists/show") + end + + it 'should be able to perform index action in project scope' do + get :index, :format => :json + response.should render_template("api/v1/build_lists/index") + end +end + +shared_examples_for 'not show build list' do + it 'should not be able to perform show action' do + get :show, @show_params + response.body.should == {"message" => "Access violation to this page!"}.to_json + end + + it 'should not be able to perform index action in project scope' do + get :index, :format => :json + response.body.should == {"message" => "Access violation to this page!"}.to_json + end +end + +shared_examples_for 'create build list' do + before {test_git_commit(@project)} + + it 'should create one more build list' do + lambda { post :create, @create_params.deep_merge(:build_list => {:project_version => "latest_master"}) }.should change{ BuildList.count }.by(1) + end + + it 'should save correct commit_hash for branch based build' do + post :create, @create_params.deep_merge(:build_list => {:project_version => "latest_master"}) + @project.build_lists.last.commit_hash.should == @project.repo.commits('master').last.id + end + + it 'should save correct commit_hash for tag based build' do + system("cd #{@project.repo.path} && git tag 4.7.5.3") # TODO REDO through grit + post :create, @create_params.deep_merge(:build_list => {:project_version => "4.7.5.3"}) + @project.build_lists.last.commit_hash.should == @project.repo.commits('4.7.5.3').last.id + end +end + +shared_examples_for 'not create build list' do + it 'should not be able to perform create action' do + post :create, @create_params + response.body.should == {"message" => "Access violation to this page!"}.to_json + end + + it 'should not create one more build list' do + lambda { post :create, @create_params }.should change{ BuildList.count }.by(0) + end +end + +describe Api::V1::BuildListsController do + before(:each) do + stub_symlink_methods + # TODO: What a fuck?! Arches doesn't clear after tests finish O_o + Arch.destroy_all + @build_list = FactoryGirl.create(:build_list_core) + @project = @build_list.project + end + + context 'crud' do + before(:each) do + platform = FactoryGirl.create(:platform_with_repos) + @create_params = { + :build_list => { + :project_id => @project.id, + :save_to_platform_id => platform.id, + :update_type => 'security', + :include_repos => [platform.repositories.first.id] + }, + :arches => [FactoryGirl.create(:arch).id], + :build_for_platforms => [platform.id], + :project_id => @project.id, + :format => :json + } + any_instance_of(Project, :versions => ['v1.0', 'v2.0']) + end + + context 'for guest' do + if APP_CONFIG['anonymous_access'] + it 'should be able to perform index action' do + get :index, :format => :json + response.should be_success + end + else + it 'should not be able to perform index action' do + get :index, :format => :json + response.status.should == 401 + end + end + end + + context 'for user' do + before(:each) do + @owner_user = @project.owner + @member_user = FactoryGirl.create(:user) + rel = @project.relations.build(:role => 'reader') + rel.actor = @member_user + rel.save + @user = FactoryGirl.create(:user) + set_session_for(@user) + @show_params = {:id => @build_list.id, :format => :json} + end + + context "do cancel" do + def do_cancel + get :cancel, :id => @build_list, :format => :json + end + + context 'if user is project owner' do + before(:each) {set_session_for(@owner_user)} + + context "if it has :build_pending status" do + it "should return correct json message" do + @build_list.update_attribute(:status, BuildList::BUILD_PENDING) + do_cancel + response.body.should == {:is_canceled => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.cancel_success')}.to_json + end + + it "should cancel build list" do + @build_list.update_attribute(:status, BuildList::BUILD_PENDING) + do_cancel + @build_list.reload.status.should == BuildList::BUILD_CANCELED + end + end + + context "if it has another status" do + it "should return correct json error message" do + @build_list.update_attribute(:status, BuildServer::PROJECT_NOT_FOUND) + do_cancel + # TODO: May be it sends access violation msg!!! + # If we not remove can_cancel? from ability.rb it will be only access violation! + response.body.should == {:is_canceled => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.cancel_fail')}.to_json + end + + it "should not cancel build list" do + @build_list.update_attribute(:status, BuildServer::PROJECT_NOT_FOUND) + do_cancel + @build_list.reload.status.should == BuildServer::PROJECT_NOT_FOUND + end + end + end + + context 'if user is not project owner' do + before(:each) do + @build_list.update_attribute(:status, BuildList::BUILD_PENDING) + do_cancel + end + + it "should return access violation message" do + response.body.should == {"message" => "Access violation to this page!"}.to_json + end + + it "should not cancel build list" do + @build_list.reload.status.should == BuildList::BUILD_PENDING + end + end + end + + context "do publish" do + def do_publish + get :publish, :id => @build_list, :format => :json + end + + context 'if user is project owner' do + before(:each) do + set_session_for(@owner_user) + @build_list.update_attribute(:status, BuildList::FAILED_PUBLISH) + do_publish + end + + context "if it has :failed_publish status" do + it "should return correct json message" do + response.body.should == {:is_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.publish_success')}.to_json + end + + it "should cancel build list" do + @build_list.reload.status.should == BuildList::BUILD_PUBLISH + end + end + + context "if it has another status" do + before(:each) do + @build_list.update_attribute(:status, BuildServer::PROJECT_NOT_FOUND) + do_publish + end + + it "should return correct json error message" do + # TODO: May be it sends access violation msg!!! + # If we not remove can_cancel? from ability.rb it will be only access violation! + response.body.should == {:is_published => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.publish_fail')}.to_json + end + + it "should not cancel build list" do + @build_list.reload.status.should == BuildServer::PROJECT_NOT_FOUND + end + end + end + + context 'if user is not project owner' do + before(:each) do + @build_list.update_attribute(:status, BuildList::FAILED_PUBLISH) + do_publish + end + + it "should return access violation message" do + response.body.should == {"message" => "Access violation to this page!"}.to_json + end + + it "should not cancel build list" do + @build_list.reload.status.should == BuildList::FAILED_PUBLISH + end + end + end + + context "do reject_publish" do + before(:each) do + any_instance_of(BuildList, :current_duration => 100) + end + + def do_reject_publish + get :reject_publish, :id => @build_list, :format => :json + end + + context 'if user is project owner' do + before(:each) do + set_session_for(@owner_user) + @build_list.update_attribute(:status, BuildServer::SUCCESS) + @build_list.save_to_platform.update_attribute(:released, true) + do_reject_publish + end + + context "if it has :failed_publish status" do + it "should return correct json message" do + response.body.should == {:is_reject_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.reject_publish_success')}.to_json + end + + it "should cancel build list" do + @build_list.reload.status.should == BuildList::BUILD_PUBLISH + end + end + + context "if it has another status" do + before(:each) do + @build_list.update_attribute(:status, BuildServer::PROJECT_NOT_FOUND) + do_reject_publish + end + + it "should return correct json error message" do + # TODO: May be it sends access violation msg!!! + # If we not remove can_cancel? from ability.rb it will be only access violation! + response.body.should == {:is_reject_published => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.reject_publish_fail')}.to_json + end + + it "should not cancel build list" do + @build_list.reload.status.should == BuildServer::PROJECT_NOT_FOUND + end + end + end + + context 'if user is not project owner' do + before(:each) do + @build_list.update_attribute(:status, BuildServer::SUCCESS) + @build_list.save_to_platform.update_attribute(:released, true) + do_reject_publish + end + + it "should return access violation message" do + response.body.should == {"message" => "Access violation to this page!"}.to_json + end + + it "should not cancel build list" do + do_reject_publish + @build_list.reload.status.should == BuildServer::SUCCESS + end + end + end + + context 'for all build lists' do + before(:each) do + @build_list1 = FactoryGirl.create(:build_list_core) + @build_list2 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :visibility => 'hidden')) + @build_list3 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :owner => @user, :visibility => 'hidden')) + @build_list4 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :visibility => 'hidden')) + @build_list4.project.relations.create :role => 'reader', :actor_id => @user.id, :actor_type => 'User' + end + + it 'should be able to perform index action' do + get :index, :format => :json + response.should be_success + end + + it 'should show only accessible build_lists' do + get :index, :filter => {:ownership => 'index'}, :format => :json + assigns(:build_lists).should include(@build_list1) + assigns(:build_lists).should_not include(@build_list2) + assigns(:build_lists).should include(@build_list3) + assigns(:build_lists).should include(@build_list4) + end + end + + context 'for open project' do + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + + context 'if user is project owner' do + before(:each) {set_session_for(@owner_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'create build list' + end + + context 'if user is project read member' do + before(:each) {set_session_for(@member_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + end + end + + context 'for hidden project' do + before(:each) do + @project.visibility = 'hidden' + @project.save! + end + + it_should_behave_like 'not show build list' + it_should_behave_like 'not create build list' + + context 'if user is project owner' do + before(:each) {set_session_for(@owner_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'create build list' + end + + context 'if user is project read member' do + before(:each) {set_session_for(@member_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + end + end + end + + context 'for group' do + before(:each) do + @owner_group = FactoryGirl.create(:group) + @owner_user = FactoryGirl.create(:user) + @owner_group.actors.create :role => 'admin', :actor_id => @owner_user.id, :actor_type => 'User' + @member_group = FactoryGirl.create(:group) + @member_user = FactoryGirl.create(:user) + @member_group.actors.create :role => 'reader', :actor_id => @member_user.id, :actor_type => 'User' + + @group = FactoryGirl.create(:group) + @user = FactoryGirl.create(:user) + @group.actors.create :role => 'reader', :actor_id => @user.id, :actor_type => 'User' + + @project = FactoryGirl.create(:project, :owner => @owner_group) + @project.relations.create :role => 'reader', :actor_id => @member_group.id, :actor_type => 'Group' + + @build_list = FactoryGirl.create(:build_list_core, :project => @project) + + set_session_for(@user) + @show_params = {:id => @build_list.id, :format => :json} + end + + context 'for all build lists' do + before(:each) do + @build_list1 = FactoryGirl.create(:build_list_core) + @build_list2 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :visibility => 'hidden')) + @build_list3 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :owner => @group, :visibility => 'hidden')) + @build_list4 = FactoryGirl.create(:build_list_core, :project => FactoryGirl.create(:project, :visibility => 'hidden')) + @build_list4.project.relations.create :role => 'reader', :actor_id => @group.id, :actor_type => 'Group' + end + + it 'should be able to perform index action' do + get :index, :format => :json + response.should be_success + end + + it 'should show only accessible build_lists' do + get :index, :filter => {:ownership => 'index'}, :format => :json + assigns(:build_lists).should include(@build_list1) + assigns(:build_lists).should_not include(@build_list2) + assigns(:build_lists).should include(@build_list3) + assigns(:build_lists).should include(@build_list4) + end + end + + context 'for open project' do + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + + context 'if user is group owner' do + before(:each) {set_session_for(@owner_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'create build list' + end + + context 'if user is group read member' do + before(:each) {set_session_for(@member_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + end + end + + context 'for hidden project' do + before(:each) do + #@project.visibility = 'hidden' + #@project.save + @build_list.project.update_attribute(:visibility, 'hidden') + end + + it_should_behave_like 'not show build list' + it_should_behave_like 'not create build list' + + context 'if user is group owner' do + before(:each) {set_session_for(@owner_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'create build list' + end + + context 'if user is group read member' do + before(:each) {set_session_for(@member_user)} + it_should_behave_like 'show build list' + it_should_behave_like 'not create build list' + end + end + + end + end + + context 'filter' do + + before(:each) do + set_session_for FactoryGirl.create(:admin) + + @build_list1 = FactoryGirl.create(:build_list_core) + @build_list2 = FactoryGirl.create(:build_list_core) + @build_list3 = FactoryGirl.create(:build_list_core) + @build_list4 = FactoryGirl.create(:build_list_core, :updated_at => (Time.now - 1.day), + :project => @build_list3.project, :save_to_platform => @build_list3.save_to_platform, + :arch => @build_list3.arch) + end + + it 'should filter by bs_id' do + get :index, :filter => {:bs_id => @build_list1.bs_id, :project_name => 'fdsfdf', :any_other_field => 'do not matter'}, :format => :json + assigns[:build_lists].should include(@build_list1) + assigns[:build_lists].should_not include(@build_list2) + assigns[:build_lists].should_not include(@build_list3) + end + + it 'should filter by project_name' do + get :index, :filter => {:project_name => @build_list2.project.name, :ownership => 'index'}, :format => :json + assigns[:build_lists].should_not include(@build_list1) + assigns[:build_lists].should include(@build_list2) + assigns[:build_lists].should_not include(@build_list3) + end + + it 'should filter by project_name and start_date' do + get :index, :filter => {:project_name => @build_list3.project.name, :ownership => 'index', + :"updated_at_start(1i)" => @build_list3.updated_at.year.to_s, + :"updated_at_start(2i)" => @build_list3.updated_at.month.to_s, + :"updated_at_start(3i)" => @build_list3.updated_at.day.to_s}, :format => :json + assigns[:build_lists].should_not include(@build_list1) + assigns[:build_lists].should_not include(@build_list2) + assigns[:build_lists].should include(@build_list3) + assigns[:build_lists].should_not include(@build_list4) + end + end +end