mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 15:20:13 +01:00
make functional tests for context controller run
This commit is contained in:
parent
76340b780c
commit
59a4d5ede0
10 changed files with 225 additions and 182 deletions
|
|
@ -25,16 +25,21 @@ class ContextsController < ApplicationController
|
|||
format.html &render_contexts_html
|
||||
format.m &render_contexts_mobile
|
||||
format.xml { render :xml => @all_contexts.to_xml( :except => :user_id ) }
|
||||
format.rss &render_contexts_rss_feed
|
||||
format.atom &render_contexts_atom_feed
|
||||
format.rss do
|
||||
@feed_title = 'Tracks Contexts'
|
||||
@feed_description = "Lists all the contexts for #{current_user.display_name}"
|
||||
end
|
||||
format.atom do
|
||||
@feed_title = 'Tracks Contexts'
|
||||
@feed_description = "Lists all the contexts for #{current_user.display_name}"
|
||||
end
|
||||
format.text do
|
||||
@all_contexts = current_user.contexts.all
|
||||
render :action => 'index', :layout => false, :content_type => Mime::TEXT
|
||||
end
|
||||
format.autocomplete &render_autocomplete
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def show
|
||||
@contexts = current_user.contexts(true)
|
||||
if @context.nil?
|
||||
|
|
@ -60,15 +65,10 @@ class ContextsController < ApplicationController
|
|||
#
|
||||
def create
|
||||
if params[:format] == 'application/xml' && params['exception']
|
||||
render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400
|
||||
render_failure "Expected post format is valid xml like so: <context><name>context name</name></context>.", 400
|
||||
return
|
||||
end
|
||||
@context = current_user.contexts.build
|
||||
params_are_invalid = true
|
||||
if (params['context'] || (params['request'] && params['request']['context']))
|
||||
@context.attributes = params['context'] || params['request']['context']
|
||||
params_are_invalid = false
|
||||
end
|
||||
@context = current_user.contexts.build(params['context'])
|
||||
@saved = @context.save
|
||||
@context_not_done_counts = { @context.id => 0 }
|
||||
respond_to do |format|
|
||||
|
|
@ -76,9 +76,7 @@ class ContextsController < ApplicationController
|
|||
@down_count = current_user.contexts.size
|
||||
end
|
||||
format.xml do
|
||||
if @context.new_record? && params_are_invalid
|
||||
render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400
|
||||
elsif @context.new_record?
|
||||
if @context.new_record?
|
||||
render_failure @context.errors.to_xml, 409
|
||||
else
|
||||
head :created, :location => context_url(@context)
|
||||
|
|
@ -233,21 +231,6 @@ class ContextsController < ApplicationController
|
|||
render :action => 'mobile_show_context'
|
||||
end
|
||||
end
|
||||
|
||||
def render_contexts_rss_feed
|
||||
lambda do
|
||||
render_rss_feed_for current_user.contexts.all, :feed => feed_options,
|
||||
:item => { :description => lambda { |c| @template.summary(c, count_undone_todos_phrase(c)) } }
|
||||
end
|
||||
end
|
||||
|
||||
def render_contexts_atom_feed
|
||||
lambda do
|
||||
render_atom_feed_for current_user.contexts.all, :feed => feed_options,
|
||||
:item => { :description => lambda { |c| @template.summary(c, count_undone_todos_phrase(c)) },
|
||||
:author => lambda { |c| nil } }
|
||||
end
|
||||
end
|
||||
|
||||
def render_autocomplete
|
||||
lambda do
|
||||
|
|
|
|||
|
|
@ -29,10 +29,8 @@ module ContextsHelper
|
|||
})
|
||||
end
|
||||
|
||||
|
||||
def summary(context, undone_todo_count)
|
||||
content_tag(:p, "#{undone_todo_count}. Context is #{context.hidden? ? 'Hidden' : 'Active'}.")
|
||||
def context_summary(context, undone_todo_count)
|
||||
content_tag(:p, "#{undone_todo_count}. Context is #{context.hidden? ? 'Hidden' : 'Active'}.".html_safe)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ module ProjectsHelper
|
|||
})
|
||||
end
|
||||
|
||||
def summary(project)
|
||||
def project_summary(project)
|
||||
project_description = ''
|
||||
project_description += Tracks::Utils.render_text( project.description ) unless project.description.blank?
|
||||
project_description += content_tag(:p,
|
||||
|
|
|
|||
|
|
@ -16,14 +16,6 @@ class Context < ActiveRecord::Base
|
|||
validates_length_of :name, :maximum => 255, :message => "context name must be less than 256 characters"
|
||||
validates_uniqueness_of :name, :message => "already exists", :scope => "user_id"
|
||||
|
||||
def self.feed_options(user)
|
||||
# TODO: move to view or helper
|
||||
{
|
||||
:title => 'Tracks Contexts',
|
||||
:description => "Lists all the contexts for #{user.display_name}"
|
||||
}
|
||||
end
|
||||
|
||||
def self.null_object
|
||||
NullContext.new
|
||||
end
|
||||
|
|
|
|||
12
app/views/contexts/index.atom.builder
Normal file
12
app/views/contexts/index.atom.builder
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
atom_feed do |feed|
|
||||
feed.title(@feed_title)
|
||||
feed.subtitle(@feed_description)
|
||||
feed.updated(@all_contexts.last.updated_at)
|
||||
|
||||
@all_contexts.each do |context|
|
||||
feed.entry(context) do |entry|
|
||||
entry.title(h(context.name))
|
||||
entry.content(context_summary(context, count_undone_todos_phrase(context)), :type => :html)
|
||||
end
|
||||
end
|
||||
end
|
||||
20
app/views/contexts/index.rss.builder
Normal file
20
app/views/contexts/index.rss.builder
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
xml.instruct! :xml, :version => "1.0"
|
||||
xml.rss :version => "2.0" do
|
||||
xml.channel do
|
||||
xml.title @feed_title
|
||||
xml.description @feed_description
|
||||
xml.link contexts_url
|
||||
xml.language 'en-us'
|
||||
xml.ttl 40
|
||||
|
||||
@all_contexts.each do |context|
|
||||
xml.item do
|
||||
xml.title h(context.title)
|
||||
xml.description context_summary(context, count_undone_todos_phrase(context))
|
||||
xml.pubDate context.created_at.to_s(:rfc822)
|
||||
xml.link context_url(context)
|
||||
xml.guid context_url(context)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
27
lib/tracks/config.rb
Normal file
27
lib/tracks/config.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
module Tracks
|
||||
class Config
|
||||
def self.salt
|
||||
SITE_CONFIG['salt']
|
||||
end
|
||||
|
||||
def self.auth_schemes
|
||||
SITE_CONFIG['authentication_schemes'] || []
|
||||
end
|
||||
|
||||
def self.openid_enabled?
|
||||
auth_schemes.include?('open_id')
|
||||
end
|
||||
|
||||
def self.cas_enabled?
|
||||
auth_schemes.include?('cas')
|
||||
end
|
||||
|
||||
def self.prefered_auth?
|
||||
if SITE_CONFIG['prefered_auth']
|
||||
SITE_CONFIG['prefered_auth']
|
||||
else
|
||||
auth_schemes.first
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -25,10 +25,143 @@ class ContextsControllerTest < ActionController::TestCase
|
|||
assert_ajax_create_increments_count '@newcontext'
|
||||
end
|
||||
|
||||
def test_create_via_ajax_with_comma_in_name_does_not_increment_number_of_contexts
|
||||
login_as :other_user
|
||||
assert_ajax_create_does_not_increment_count 'foo,bar'
|
||||
# text feed
|
||||
|
||||
def test_text_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "txt" }
|
||||
assert_equal 'text/plain', @response.content_type
|
||||
assert !(/ /.match(@response.body))
|
||||
end
|
||||
|
||||
def test_text_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_text_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_text_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
# REST xml
|
||||
|
||||
def test_show_xml_renders_context_to_xml
|
||||
login_as :admin_user
|
||||
get :show, { :id => "1", :format => 'xml' }
|
||||
assert_equal contexts(:agenda).to_xml( :except => :user_id ), @response.body
|
||||
end
|
||||
|
||||
def test_show_with_nil_context_returns_404
|
||||
login_as :admin_user
|
||||
get :show, { :id => "0" }
|
||||
assert_equal 'Context not found', @response.body
|
||||
assert_response 404
|
||||
end
|
||||
|
||||
def test_show_xml_with_nil_context_returns_404
|
||||
login_as :admin_user
|
||||
get :show, { :id => "0", :format => 'xml' }
|
||||
assert_response 404
|
||||
assert_xml_select 'error', 'Context not found'
|
||||
end
|
||||
|
||||
# RSS
|
||||
|
||||
def test_rss_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "rss" }
|
||||
assert_equal 'application/rss+xml', @response.content_type
|
||||
#puts @response.body
|
||||
|
||||
assert_xml_select 'rss[version="2.0"]' do
|
||||
assert_select 'channel' do
|
||||
assert_select '>title', 'Tracks Contexts'
|
||||
assert_select '>description', "Lists all the contexts for #{users(:admin_user).display_name}"
|
||||
assert_select 'language', 'en-us'
|
||||
assert_select 'ttl', '40'
|
||||
end
|
||||
assert_select 'item', 10 do
|
||||
assert_select 'title', /.+/
|
||||
assert_select 'description' do
|
||||
assert_select_encoded do
|
||||
assert_select 'p', /\d+ actions. Context is (Active|Hidden)./
|
||||
end
|
||||
end
|
||||
%w(guid link).each do |node|
|
||||
assert_select node, /http:\/\/test.host\/contexts\/.+/
|
||||
end
|
||||
assert_select 'pubDate', contexts(:agenda).created_at.to_s(:rfc822)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_rss_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_rss_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_rss_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
# ATOM
|
||||
|
||||
def test_atom_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "atom" }
|
||||
assert_equal 'application/atom+xml', @response.content_type
|
||||
#puts @response.body
|
||||
|
||||
assert_xml_select 'feed[xmlns="http://www.w3.org/2005/Atom"]' do
|
||||
assert_select '>title', 'Tracks Contexts'
|
||||
assert_select '>subtitle', "Lists all the contexts for #{users(:admin_user).display_name}"
|
||||
assert_select 'entry', 10 do
|
||||
assert_select 'title', /.+/
|
||||
assert_select 'content[type="html"]' do
|
||||
assert_select_encoded do
|
||||
assert_select 'p', /\d+ actions. Context is (Active|Hidden)./
|
||||
end
|
||||
end
|
||||
assert_select 'published', /(#{Regexp.escape(contexts(:agenda).created_at.xmlschema)}|#{Regexp.escape(contexts(:library).created_at.xmlschema)})/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_atom_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_atom_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_atom_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,134 +0,0 @@
|
|||
|
||||
def test_rss_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "rss" }
|
||||
assert_equal 'application/rss+xml', @response.content_type
|
||||
#puts @response.body
|
||||
|
||||
assert_xml_select 'rss[version="2.0"]' do
|
||||
assert_select 'channel' do
|
||||
assert_select '>title', 'Tracks Contexts'
|
||||
assert_select '>description', "Lists all the contexts for #{users(:admin_user).display_name}"
|
||||
assert_select 'language', 'en-us'
|
||||
assert_select 'ttl', '40'
|
||||
end
|
||||
assert_select 'item', 10 do
|
||||
assert_select 'title', /.+/
|
||||
assert_select 'description' do
|
||||
assert_select_encoded do
|
||||
assert_select 'p', /\d+ actions. Context is (Active|Hidden)./
|
||||
end
|
||||
end
|
||||
%w(guid link).each do |node|
|
||||
assert_select node, /http:\/\/test.host\/contexts\/.+/
|
||||
end
|
||||
assert_select 'pubDate', contexts(:agenda).created_at.to_s(:rfc822)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_rss_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_rss_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_rss_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "rss", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_atom_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "atom" }
|
||||
assert_equal 'application/atom+xml', @response.content_type
|
||||
#puts @response.body
|
||||
|
||||
assert_xml_select 'feed[xmlns="http://www.w3.org/2005/Atom"]' do
|
||||
assert_select '>title', 'Tracks Contexts'
|
||||
assert_select '>subtitle', "Lists all the contexts for #{users(:admin_user).display_name}"
|
||||
assert_select 'entry', 10 do
|
||||
assert_select 'title', /.+/
|
||||
assert_select 'content[type="html"]' do
|
||||
assert_select_encoded do
|
||||
assert_select 'p', /\d+ actions. Context is (Active|Hidden)./
|
||||
end
|
||||
end
|
||||
assert_select 'published', /(#{Regexp.escape(contexts(:agenda).created_at.xmlschema)}|#{Regexp.escape(contexts(:library).created_at.xmlschema)})/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_atom_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_atom_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_atom_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "atom", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_text_feed_content
|
||||
login_as :admin_user
|
||||
get :index, { :format => "txt" }
|
||||
assert_equal 'text/plain', @response.content_type
|
||||
assert !(/ /.match(@response.body))
|
||||
end
|
||||
|
||||
def test_text_feed_not_accessible_to_anonymous_user_without_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt" }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_text_feed_not_accessible_to_anonymous_user_with_invalid_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt", :token => 'foo' }
|
||||
assert_response 401
|
||||
end
|
||||
|
||||
def test_text_feed_accessible_to_anonymous_user_with_valid_token
|
||||
login_as nil
|
||||
get :index, { :format => "txt", :token => users(:admin_user).token }
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_show_xml_renders_context_to_xml
|
||||
login_as :admin_user
|
||||
get :show, { :id => "1", :format => 'xml' }
|
||||
assert_equal contexts(:agenda).to_xml( :except => :user_id ), @response.body
|
||||
end
|
||||
|
||||
def test_show_with_nil_context_returns_404
|
||||
login_as :admin_user
|
||||
get :show, { :id => "0" }
|
||||
assert_equal 'Context not found', @response.body
|
||||
assert_response 404
|
||||
end
|
||||
|
||||
def test_show_xml_with_nil_context_returns_404
|
||||
login_as :admin_user
|
||||
get :show, { :id => "0", :format => 'xml' }
|
||||
assert_response 404
|
||||
assert_xml_select 'error', 'Context not found'
|
||||
end
|
||||
|
||||
def protect_against_forgery?
|
||||
false
|
||||
end
|
||||
|
|
@ -47,14 +47,26 @@ class ActionController::TestCase
|
|||
end
|
||||
|
||||
def ajax_create(name)
|
||||
xhr :post, :create, @controller.class.name.downcase.to_sym => {:name => name}
|
||||
xhr :post, :create, get_model_class.downcase => {:name => name}
|
||||
end
|
||||
|
||||
def assert_xml_select(*args, &block)
|
||||
@html_document = xml_document
|
||||
assert_select(*args, &block)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def xml_document
|
||||
@xml_document ||= HTML::Document.new(@response.body, false, true)
|
||||
end
|
||||
|
||||
def get_model_class
|
||||
@controller.class.to_s.tableize.split("_")[0].camelcase.singularize #don't ask... converts ContextsController to Context
|
||||
end
|
||||
|
||||
def get_class_count
|
||||
model = @controller.class.to_s.tableize.split("_")[0].camelcase.singularize #don't ask... converts ContextsController to Context
|
||||
eval("#{model}.count")
|
||||
eval("#{get_model_class}.count")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue