Skip to content

Commit

Permalink
Merge pull request #200 from sparc-request/jjh-sr-controller-specs
Browse files Browse the repository at this point in the history
Jjh sr controller specs
  • Loading branch information
jayhardee9 committed Nov 9, 2015
2 parents ff29fe9 + 49fb77e commit cbcf92b
Show file tree
Hide file tree
Showing 30 changed files with 2,348 additions and 1,092 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -77,3 +77,7 @@ bin/*

# Ignore spec examples
spec/examples.txt

# Ignore ctags files
.tags
.tags1
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -65,6 +65,7 @@ group :development, :test, :profile do
end

group :development, :test do
gem 'pry'
gem 'rails-erd'
gem 'rspec-rails', '~> 3.3.3'
end
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -86,6 +86,7 @@ GEM
activesupport (>= 3.0)
cocaine (0.5.7)
climate_control (>= 0.0.3, < 1.0)
coderay (1.1.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (3.2.2)
Expand Down Expand Up @@ -188,6 +189,7 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25.1)
mini_portile (0.6.2)
multi_json (1.11.2)
Expand Down Expand Up @@ -235,6 +237,10 @@ GEM
progress_bar (1.0.3)
highline (~> 1.6.1)
options (~> 2.3.0)
pry (0.10.2)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rabl (0.6.14)
activesupport (>= 2.3.14)
multi_json (~> 1.0)
Expand Down Expand Up @@ -335,6 +341,7 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
spring (1.3.6)
spring-commands-rspec (1.0.4)
spring (>= 0.9.1)
Expand Down Expand Up @@ -449,6 +456,7 @@ DEPENDENCIES
pdfkit
prawn (= 0.12.0)
progress_bar
pry
rails (~> 3.2.22)
rails-erd
redcarpet
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -189,6 +189,10 @@ def use_existing_service_request
# Validate @service_request and @sub_service_request (after having
# been set by use_existing_service_request). Renders an error page if
# they were not found.
#
# NOTE: If use_existing_service_request cannot find the ServiceRequest
# or SubServiceRequest, it will throw an error, not render a friendly
# authorization_error. So how is this being used?
def validate_existing_service_request
if @service_request.nil?
authorization_error "The service request you are trying to access can not be found.",
Expand Down
1 change: 1 addition & 0 deletions app/models/protocol.rb
Expand Up @@ -33,6 +33,7 @@ class Protocol < ActiveRecord::Base
has_many :project_roles, :dependent => :destroy
has_many :identities, :through => :project_roles
has_many :service_requests
has_many :sub_service_requests, through: :service_requests
has_many :affiliations, :dependent => :destroy
has_many :impact_areas, :dependent => :destroy
has_many :arms, :dependent => :destroy
Expand Down
1 change: 1 addition & 0 deletions app/models/service_request.rb
Expand Up @@ -27,6 +27,7 @@ class ServiceRequest < ActiveRecord::Base
belongs_to :service_requester, :class_name => "Identity", :foreign_key => "service_requester_id"
belongs_to :protocol
has_many :sub_service_requests, :dependent => :destroy
has_many :subsidies, through: :sub_service_requests
has_many :line_items, :include => [:service], :dependent => :destroy
has_many :charges, :dependent => :destroy
has_many :tokens, :dependent => :destroy
Expand Down
246 changes: 243 additions & 3 deletions spec/controllers/application_controller_spec.rb
Expand Up @@ -20,17 +20,257 @@

require 'rails_helper'

RSpec.describe ApplicationController do
RSpec.describe ApplicationController, type: :controller do
controller do
def index
render json: { }
initialize_service_request
render nothing: true
end

def show
prepare_catalog
render nothing: true
end

def not_navigate
initialize_service_request
render nothing: true
end

def navigate
initialize_service_request
render nothing: true
end
end

describe :current_user do
let_there_be_lane
let_there_be_j

describe '#current_user' do
it 'should call current_identity' do
expect(controller).to receive(:current_identity)
controller.current_user
end
end

describe '#authorize_identity' do
context 'Identity logged in' do
before(:each) do
allow(controller).to receive(:current_user).and_return(jug2)
end

context '@sub_service_request nil and Identity can edit @service_request' do
it 'should authorize Identity' do
controller.instance_variable_set(:@service_request, :service_request)
allow(jug2).to receive(:can_edit_service_request?)
.with(:service_request)
.and_return(true)
expect(controller).to_not receive(:authorization_error)
controller.authorize_identity
end
end

context '@sub_service_request set and Identity can edit @sub_service_request' do
it 'should authorize Identity' do
controller.instance_variable_set(:@sub_service_request, :sub_service_request)
allow(jug2).to receive(:can_edit_sub_service_request?)
.with(:sub_service_request)
.and_return(true)
expect(controller).to_not receive(:authorization_error)
controller.authorize_identity
end
end

context '@sub_service_request nil and Identity cannot edit @service_request' do
it 'should not authorize Identity' do
controller.instance_variable_set(:@service_request, :service_request)
allow(jug2).to receive(:can_edit_service_request?)
.with(:service_request)
.and_return(false)
expect(controller).to receive(:authorization_error)
controller.authorize_identity
end
end

context '@sub_service_request set and Identity cannot edit @sub_service_request' do
it 'should not authorize Identity' do
controller.instance_variable_set(:@sub_service_request, :sub_service_request)
allow(jug2).to receive(:can_edit_sub_service_request?)
.with(:sub_service_request)
.and_return(false)
expect(controller).to receive(:authorization_error)
controller.authorize_identity
end
end
end

context 'Identity not logged in' do
before(:each) do
allow(controller).to receive(:current_user).and_return(nil)
end

context 'ServiceRequest in first_draft and not submitted yet' do
it 'should authorize Identity' do
service_request = instance_double('ServiceRequest', status: 'first_draft', service_requester_id: nil)
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to_not receive(:authorization_error)
controller.authorize_identity
end
end

context 'ServiceRequest status not first_draft' do
it 'should authenticate and authorize Identity' do
service_request = instance_double('ServiceRequest', status: 'draft')
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to_not receive(:authorization_error)
expect(controller).to receive(:authenticate_identity!)
controller.authorize_identity
end
end

context 'ServiceRequest has non-nil service_requester_id and status' do
it 'should authorize Identity' do
service_request = instance_double('ServiceRequest', status: 'draft', service_requester_id: 1)
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to_not receive(:authorization_error)
expect(controller).to receive(:authenticate_identity!)
controller.authorize_identity
end
end

context 'ServiceRequest has nil status' do
it 'should not authorize Identity' do
service_request = instance_double('ServiceRequest', status: nil)
controller.instance_variable_set(:@service_request, service_request)
expect(controller).to receive(:authorization_error)
controller.authorize_identity
end
end
end
end

describe '#prepare_catalog' do
build_service_request_with_study

before(:each) do
# make Institution list for sub_service_request distinct from
# list of all Institutions
create(:institution)
end

context 'session[:sub_service_request_id] present and @sub_service_request non-nil' do
it 'should set @institutions to the @sub_service_request\'s Institutions' do
session[:sub_service_request_id] = sub_service_request.id
controller.instance_variable_set(:@sub_service_request, sub_service_request)
routes.draw { get 'show' => 'anonymous#show' }
get :show
expect(assigns(:institutions)).to eq [institution]
end
end

context 'session[:sub_service_request_id] present but @sub_service_request nil' do
it 'should set @institutions to all Institutions' do
session[:sub_service_request_id] = sub_service_request.id
routes.draw { get 'show' => 'anonymous#show' }
get :show
expect(assigns(:institutions)).to eq Institution.order('`order`')
end
end

context 'session[:sub_service_request_id] absent but @sub_service_request set' do
it 'should set @institutions to all Institutions' do
controller.instance_variable_set(:@sub_service_request, sub_service_request)
routes.draw { get 'show' => 'anonymous#show' }
get :show
expect(assigns(:institutions)).to eq Institution.order('`order`')
end
end
end

describe '#setup_navigation' do
build_service_request_with_study

context 'action is not navigate' do
before(:each) { session[:service_request_id] = service_request.id }

it 'should always set @page to params[:action]' do
routes.draw { get 'not_navigate' => 'anonymous#not_navigate' }
get :not_navigate, current_location: 'http://www.example.com/something/something/darkside'
expect(assigns(:page)).to eq 'not_navigate'

allow(controller.request).to receive(:referrer).and_return('http://www.example.com/foo/bar')
get :not_navigate
expect(assigns(:page)).to eq 'not_navigate'
end
end

context 'action is navigate' do
before(:each) { session[:service_request_id] = service_request.id }

context 'params[:current_location] present' do
it 'should assign @page to page referred to by params[:current_location]' do
routes.draw { get 'navigate' => 'anonymous#navigate' }
get :navigate, current_location: 'my current location'
expect(assigns(:page)).to eq 'my current location'
end
end

context 'params[:current_location] absent' do
it 'should assign @page to page referred to by request referrer' do
routes.draw { get 'navigate' => 'anonymous#navigate' }
allow(controller.request).to receive(:referrer).and_return('http://www.example.com/foo/bar')
get :navigate
expect(assigns(:page)).to eq 'bar'
end
end
end
end

describe '#initialize_service_request' do
build_service_request_with_study

context 'not hitting ServiceRequestsController' do
context 'session[:service_request] absent' do
it 'should throw an error' do
expect { get :index }.to raise_error ActiveRecord::RecordNotFound
end
end

context 'session[:service_request_id] present' do
before(:each) { session[:service_request_id] = service_request.id }

it 'should set @service_request' do
get :index
expect(assigns(:service_request)).to eq service_request
end

context 'session[:sub_service_request_id] present' do
before(:each) do
session[:sub_service_request_id] = sub_service_request.id
get :index
end

it 'should set @sub_service_request' do
expect(assigns(:sub_service_request)).to eq sub_service_request
end

it "should set @line_items to the SubServiceRequest's LineItems" do
expect(assigns(:line_items)).to eq sub_service_request.line_items
end
end

context 'session[:sub_service_request_id] absent' do
before(:each) { get :index }

it 'should not set @sub_service_request' do
expect(assigns(:sub_service_request)).to_not be
end

it "should set @line_items to the ServiceRequest's LineItems" do
expect(assigns(:line_items)).to eq service_request.line_items
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/controllers/identities_controller_spec.rb
Expand Up @@ -44,6 +44,11 @@

stub_controller

before(:each) do
# shouldn't need to mess around with a ServiceRequest
allow(controller).to receive(:initialize_service_request) {}
end

describe 'GET show' do
it 'should should set identity' do
session[:identity_id] = identity.id
Expand Down
9 changes: 7 additions & 2 deletions spec/controllers/search_controller_spec.rb
Expand Up @@ -32,7 +32,7 @@
let!(:core2) { create(:core, parent_id: program.id) }
let!(:unavailable_core) { create(:core, parent_id: program.id, is_available: false) }

let!(:service_request) { FactoryGirl.create(:service_request_without_validations) }
let!(:service_request) { create(:service_request_without_validations) }

let!(:core_ssr) { create(:sub_service_request, service_request_id: service_request.id, organization_id: core.id) }
let!(:core2_ssr) { create(:sub_service_request, service_request_id: service_request.id, organization_id: core2.id) }
Expand Down Expand Up @@ -196,7 +196,7 @@

it "should return a service whose organization is a parent of the sub service request's organization" do
session['service_request_id'] = service_request.id
session['sub_service_request_id'] = core2.id
session['sub_service_request_id'] = core2_ssr.id

get :services, {
format: :js,
Expand Down Expand Up @@ -268,6 +268,11 @@
identity
}

before(:each) do
# shouldn't need to mess around with a ServiceRequest
allow(controller).to receive(:initialize_service_request) {}
end

it 'should return one instance if search returns one instance' do
allow(Identity).to receive(:search) { [ identity ] }
expect(Identity).to receive(:search).with('search term')
Expand Down

0 comments on commit cbcf92b

Please sign in to comment.