Added test for RootRouteConstraint and conferences#current

This commit is contained in:
divyanshumehta 2017-03-19 09:17:31 +05:30
parent 529b8eb710
commit ee7684d845
5 changed files with 60 additions and 5 deletions

View file

@ -12,7 +12,7 @@ class ConferencesController < ApplicationController
def show; end def show; end
def current def current
current = Conference.first current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).first
redirect_to conference_path(current.short_title) redirect_to conference_path(current.short_title)
end end

View file

@ -1,4 +1,4 @@
require "root_route_constraint" require 'root_route_constraint'
Osem::Application.routes.draw do Osem::Application.routes.draw do
if ENV['OSEM_ICHAIN_ENABLED'] == 'true' if ENV['OSEM_ICHAIN_ENABLED'] == 'true'

View file

@ -1,10 +1,19 @@
class RootRouteConstraint class RootRouteConstraint
def initialize def initialize
@current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc) @current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).reorder(start_date: :asc)
end end
##
# Checks if only one conference is live and has a public splashpage
# If any conference is live it checks how many live conferences are there
# ====Returns
# * +true+ -> only one conferene is live AND the conference has public splashpage
# * +false+ -> no or more than one conferences are live or the only live conferece has no public splashpage
def matches?(*) def matches?(*)
return unless @current.present? && @current.first.splashpage.present? if @current.present? && @current.first.splashpage.present?
@current.count == 1 && @current.first.splashpage.public? @current.count == 1 && @current.first.splashpage.public?
else
false
end
end end
end end

View file

@ -26,6 +26,13 @@ describe ConferencesController do
end end
end end
describe 'GET #current' do
it 'redirects to first conference splashpage' do
get :current
expect(response).to redirect_to(conference_path(Conference.first.short_title))
end
end
describe 'OPTIONS #index' do describe 'OPTIONS #index' do
it 'Response code is 200' do it 'Response code is 200' do
process :index, 'OPTIONS' process :index, 'OPTIONS'

View file

@ -0,0 +1,39 @@
require 'spec_helper'
describe RootRouteConstraint do
describe '#matches?' do
it 'returns false, if no conference is live' do
constraint = RootRouteConstraint.new
expect(constraint.matches?).to eq false
end
it 'returns false, when one conference is live but no splashpage' do
create(:conference)
constraint = RootRouteConstraint.new
expect(constraint.matches?).to eq false
end
it 'returns false, when one conference is live but no public splashpage' do
conference = create(:full_conference)
conference.splashpage.public = false
conference.splashpage.save
constraint = RootRouteConstraint.new
expect(constraint.matches?).to eq false
end
it 'returns true, when one conference is live and has public splashpage' do
create(:full_conference)
constraint = RootRouteConstraint.new
expect(constraint.matches?).to eq true
end
it 'returns false, if more than one conference is live' do
create(:full_conference)
create(:full_conference)
constraint = RootRouteConstraint.new
expect(constraint.matches?).to eq false
end
end
end