Commit 518e6a90fa20bccb50e33739fc25c18c8d114de6
Committed by
Rodrigo Souto
1 parent
1107cc69
Exists in
master
and in
29 other branches
adding tests for article api
Showing
2 changed files
with
105 additions
and
32 deletions
Show diff stats
lib/api/v1/articles.rb
... | ... | @@ -3,6 +3,8 @@ module API |
3 | 3 | class Articles < Grape::API |
4 | 4 | before { authenticate! } |
5 | 5 | |
6 | + ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} | |
7 | + | |
6 | 8 | resource :articles do |
7 | 9 | |
8 | 10 | # Collect articles |
... | ... | @@ -16,7 +18,7 @@ module API |
16 | 18 | # GET host/api/v1/articles?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317 |
17 | 19 | get do |
18 | 20 | articles = select_filtered_collection_of(environment, 'articles', params) |
19 | - articles = articles.display_filter(current_user.person, nil) | |
21 | + articles = articles.display_filter(current_person, nil) | |
20 | 22 | present articles, :with => Entities::Article |
21 | 23 | end |
22 | 24 | |
... | ... | @@ -29,7 +31,7 @@ module API |
29 | 31 | get ':id/children' do |
30 | 32 | article = find_article(environment.articles, params[:id]) |
31 | 33 | articles = select_filtered_collection_of(article, 'children', params) |
32 | - articles = articles.display_filter(current_user.person, nil) | |
34 | + articles = articles.display_filter(current_person, nil) | |
33 | 35 | present articles, :with => Entities::Article |
34 | 36 | end |
35 | 37 | |
... | ... | @@ -46,11 +48,11 @@ module API |
46 | 48 | get do |
47 | 49 | community = environment.communities.find(params[:community_id]) |
48 | 50 | articles = select_filtered_collection_of(community, 'articles', params) |
49 | - articles = articles.display_filter(current_user.person, community) | |
51 | + articles = articles.display_filter(current_person, community) | |
50 | 52 | present articles, :with => Entities::Article |
51 | 53 | end |
52 | 54 | |
53 | - get '/:id' do | |
55 | + get ':id' do | |
54 | 56 | community = environment.communities.find(params[:community_id]) |
55 | 57 | article = find_article(community.articles, params[:id]) |
56 | 58 | present article, :with => Entities::Article |
... | ... | @@ -60,9 +62,11 @@ module API |
60 | 62 | # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body |
61 | 63 | post do |
62 | 64 | community = environment.communities.find(params[:community_id]) |
63 | - return forbidden! unless current_user.person.can_post_content?(community) | |
65 | + return forbidden! unless current_person.can_post_content?(community) | |
64 | 66 | |
65 | 67 | klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] |
68 | + return forbidden! unless ARTICLE_TYPES.include?(klass_type) | |
69 | + | |
66 | 70 | article = klass_type.constantize.new(params[:article]) |
67 | 71 | article.last_changed_by = current_person |
68 | 72 | article.created_by= current_person | ... | ... |
test/unit/api/articles_test.rb
... | ... | @@ -39,33 +39,6 @@ class ArticlesTest < ActiveSupport::TestCase |
39 | 39 | assert_equal 403, last_response.status |
40 | 40 | end |
41 | 41 | |
42 | - should 'return article by community' do | |
43 | - community = fast_create(Community) | |
44 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing") | |
45 | - get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
46 | - json = JSON.parse(last_response.body) | |
47 | - assert_equal article.id, json["article"]["id"] | |
48 | - end | |
49 | - | |
50 | - should 'not return article by community if user has no permission to view it' do | |
51 | - community = fast_create(Community) | |
52 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
53 | - assert !article.published? | |
54 | - | |
55 | - get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
56 | - assert_equal 403, last_response.status | |
57 | - end | |
58 | - | |
59 | - should 'not list forbidden article when listing articles by community' do | |
60 | - community = fast_create(Community) | |
61 | - article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
62 | - assert !article.published? | |
63 | - | |
64 | - get "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
65 | - json = JSON.parse(last_response.body) | |
66 | - assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
67 | - end | |
68 | - | |
69 | 42 | should 'list article children' do |
70 | 43 | article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") |
71 | 44 | child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") |
... | ... | @@ -109,6 +82,33 @@ class ArticlesTest < ActiveSupport::TestCase |
109 | 82 | assert_not_includes json['articles'].map {|a| a['id']}, child.id |
110 | 83 | end |
111 | 84 | |
85 | + should 'return article by community' do | |
86 | + community = fast_create(Community) | |
87 | + article = fast_create(Article, :profile_id => community.id, :name => "Some thing") | |
88 | + get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
89 | + json = JSON.parse(last_response.body) | |
90 | + assert_equal article.id, json["article"]["id"] | |
91 | + end | |
92 | + | |
93 | + should 'not return article by community if user has no permission to view it' do | |
94 | + community = fast_create(Community) | |
95 | + article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
96 | + assert !article.published? | |
97 | + | |
98 | + get "/api/v1/communities/#{community.id}/articles/#{article.id}?#{params.to_query}" | |
99 | + assert_equal 403, last_response.status | |
100 | + end | |
101 | + | |
102 | + should 'not list forbidden article when listing articles by community' do | |
103 | + community = fast_create(Community) | |
104 | + article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false) | |
105 | + assert !article.published? | |
106 | + | |
107 | + get "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
108 | + json = JSON.parse(last_response.body) | |
109 | + assert_not_includes json['articles'].map {|a| a['id']}, article.id | |
110 | + end | |
111 | + | |
112 | 112 | should 'create article in a community' do |
113 | 113 | community = fast_create(Community) |
114 | 114 | give_permission(user.person, 'post_content', community) |
... | ... | @@ -137,4 +137,73 @@ class ArticlesTest < ActiveSupport::TestCase |
137 | 137 | assert_equal article.id, json["article"]["parent"]["id"] |
138 | 138 | end |
139 | 139 | |
140 | + should 'create article with content type passed as parameter' do | |
141 | + community = fast_create(Community) | |
142 | + community.add_member(user.person) | |
143 | + | |
144 | + Article.delete_all | |
145 | + params[:article] = {:name => "Title"} | |
146 | + params[:content_type] = 'TextArticle' | |
147 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
148 | + json = JSON.parse(last_response.body) | |
149 | + | |
150 | + assert_kind_of TextArticle, Article.last | |
151 | + end | |
152 | + | |
153 | + should 'create article of TinyMceArticle type if no content type is passed as parameter' do | |
154 | + community = fast_create(Community) | |
155 | + community.add_member(user.person) | |
156 | + | |
157 | + params[:article] = {:name => "Title"} | |
158 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
159 | + json = JSON.parse(last_response.body) | |
160 | + | |
161 | + assert_kind_of TinyMceArticle, Article.last | |
162 | + end | |
163 | + | |
164 | + should 'not create article with invalid article content type' do | |
165 | + community = fast_create(Community) | |
166 | + community.add_member(user.person) | |
167 | + | |
168 | + params[:article] = {:name => "Title"} | |
169 | + params[:content_type] = 'Person' | |
170 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
171 | + json = JSON.parse(last_response.body) | |
172 | + | |
173 | + assert_equal 403, last_response.status | |
174 | + end | |
175 | + | |
176 | + should 'create article defining the correct profile' do | |
177 | + community = fast_create(Community) | |
178 | + community.add_member(user.person) | |
179 | + | |
180 | + params[:article] = {:name => "Title"} | |
181 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
182 | + json = JSON.parse(last_response.body) | |
183 | + | |
184 | + assert_equal community, Article.last.profile | |
185 | + end | |
186 | + | |
187 | + should 'create article defining the created_by' do | |
188 | + community = fast_create(Community) | |
189 | + community.add_member(user.person) | |
190 | + | |
191 | + params[:article] = {:name => "Title"} | |
192 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
193 | + json = JSON.parse(last_response.body) | |
194 | + | |
195 | + assert_equal user.person, Article.last.created_by | |
196 | + end | |
197 | + | |
198 | + should 'create article defining the last_changed_by' do | |
199 | + community = fast_create(Community) | |
200 | + community.add_member(user.person) | |
201 | + | |
202 | + params[:article] = {:name => "Title"} | |
203 | + post "/api/v1/communities/#{community.id}/articles?#{params.to_query}" | |
204 | + json = JSON.parse(last_response.body) | |
205 | + | |
206 | + assert_equal user.person, Article.last.last_changed_by | |
207 | + end | |
208 | + | |
140 | 209 | end | ... | ... |