profile_search_controller_test.rb
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require File.dirname(__FILE__) + '/../test_helper'
require 'profile_search_controller'
# Re-raise errors caught by the controller.
class ProfileSearchController; def rescue_action(e) raise e end; end
class ProfileSearchControllerTest < ActionController::TestCase
def setup
@controller = ProfileSearchController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@person = fast_create(Person)
end
attr_reader :person
should 'espape xss attack' do
@controller.expects(:profile).returns(person).at_least_once
get 'index', :profile => person.identifier, :q => '<wslite>'
assert_no_tag :tag => 'wslite'
end
should 'render success in search' do
get :index, :profile => person.identifier, :q => 'something not important'
assert_response :success
end
should 'search for articles' do
article = TextileArticle.create(:name => 'My article', :body => 'Article to test profile search', :profile => person)
get 'index', :profile => person.identifier, :q => 'article to test'
assert_includes assigns(:results), article
end
should 'not display articles from another profile' do
article = TextileArticle.create(:name => 'My article', :body => 'Article to test profile search', :profile => person)
article2 = TextileArticle.create(:name => 'Another article', :body => 'Article from someone else', :profile => fast_create(Person))
get 'index', :profile => person.identifier, :q => 'article'
assert_includes assigns(:results), article
assert_not_includes assigns(:results), article2
end
should 'display search results' do
article1 = fast_create(Article, {:body => '<p>Article to test profile search</p>', :profile_id => person.id}, :search => true)
article2 = fast_create(Article, {:body => '<p>Another article to test profile search</p>', :profile_id => person.id}, :search => true)
get 'index', :profile => person.identifier, :q => 'article'
[article1, article2].each do |article|
assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article.short_lead, :attributes => { :class => /article-details/ }}
end
end
should 'paginate results listing' do
(1..11).each do |i|
TextileArticle.create!(:name => "Article #{i}", :profile => person, :language => 'en')
end
get 'index', :profile => person.identifier, :q => 'Article'
assert_equal 10, assigns('results').size
assert_tag :tag => 'a', :attributes => { :href => "/profile/#{person.identifier}/search?page=2&q=Article", :rel => 'next' }
end
should 'display abstract if given' do
article1 = TextileArticle.create(:name => 'Article 1', :abstract => 'Abstract to test', :body => 'Article to test profile search', :profile => person)
article2 = TextileArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)
get 'index', :profile => person.identifier, :q => 'article to test'
assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article1.abstract, :attributes => { :class => /article-details/ }}
assert_no_tag :tag => 'li', :descendant => { :tag => 'a', :content => article1.body, :attributes => { :class => /article-details/ }}
assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article2.body, :attributes => { :class => /article-details/ }}
end
should 'display nothing if search is blank' do
article1 = TextileArticle.create(:name => 'Article 1', :body => 'Article to test profile search', :profile => person)
article2 = TextileArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)
get 'index', :profile => person.identifier, :q => ''
assert_no_tag :tag => 'ul', :attributes => { :id => 'profile-search-results'}, :descendant => { :tag => 'li' }
end
should 'not display private articles' do
article1 = TextileArticle.create(:name => 'Article 1', :body => 'Article to test profile search', :profile => person, :published => false)
article2 = TextileArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)
get 'index', :profile => person.identifier, :q => 'article to test'
assert_no_tag :tag => 'li', :descendant => { :tag => 'a', :content => article1.body, :attributes => { :class => /article-details/ }}
assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article2.body, :attributes => { :class => /article-details/ }}
end
should 'display number of results found' do
article1 = TextileArticle.create(:name => 'Article 1', :body => 'Article to test profile search', :body => 'Article to test profile search', :profile => person)
article2 = TextileArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)
get 'index', :profile => person.identifier, :q => 'article to test'
assert_tag :tag => 'div', :attributes => { :class => 'results-found-message' }, :content => /2 results found/
end
end