Commit 11954a66bc21de8f92b00b3550416042f06d45c4

Authored by André Guedes
Committed by Rodrigo Souto
1 parent 44d8600f

Created task API mount point tests

Signed-off-by: André Bernardes <andrebsguedes@gmail.com>
Signed-off-by: Eduardo Vital <vitaldu@gmail.com>
Showing 1 changed file with 156 additions and 360 deletions   Show diff stats
test/unit/api/task_test.rb
... ... @@ -12,6 +12,7 @@ class TasksTest &lt; ActiveSupport::TestCase
12 12 attr_accessor :person, :community, :environment
13 13  
14 14 should 'list tasks of environment' do
  15 + environment.add_admin(person)
15 16 task = create(Task, :requestor => person, :target => environment)
16 17 get "/api/v1/tasks?#{params.to_query}"
17 18 json = JSON.parse(last_response.body)
... ... @@ -26,364 +27,159 @@ class TasksTest &lt; ActiveSupport::TestCase
26 27 assert_equal task.id, json["task"]["id"]
27 28 end
28 29  
29   -# should 'not return environmet task if user has no permission to view it' do
30   -# person = fast_create(Person)
31   -# task = create(Task, :requestor => person, :target => environment)
32   -#
33   -# get "/api/v1/tasks/#{task.id}?#{params.to_query}"
34   -# assert_equal 403, last_response.status
35   -# end
36   -#
37   -# #############################
38   -# # Community Tasks #
39   -# #############################
40   -#
41   -# should 'return task by community' do
42   -# community = fast_create(Community)
43   -# task = create(Task, :requestor => person, :target => community)
44   -# get "/api/v1/communities/#{community.id}/tasks/#{task.id}?#{params.to_query}"
45   -# json = JSON.parse(last_response.body)
46   -# assert_equal task.id, json["task"]["id"]
47   -# end
48   -#
49   -# should 'not return task by community if user has no permission to view it' do
50   -# community = fast_create(Community)
51   -# task = create(Task, :requestor => person, :target => community)
52   -# assert !person.is_member_of?(community)
53   -#
54   -# get "/api/v1/communities/#{community.id}/tasks/#{task.id}?#{params.to_query}"
55   -# assert_equal 403, last_response.status
56   -# end
57   -#
58   -## should 'not list forbidden article when listing articles by community' do
59   -## community = fast_create(Community)
60   -## article = fast_create(Article, :profile_id => community.id, :name => "Some thing", :published => false)
61   -## assert !article.published?
62   -##
63   -## get "/api/v1/communities/#{community.id}/articles?#{params.to_query}"
64   -## json = JSON.parse(last_response.body)
65   -## assert_not_includes json['articles'].map {|a| a['id']}, article.id
66   -## end
67   -#
68   -# should 'create task in a community' do
69   -# community = fast_create(Community)
70   -# give_permission(person, 'post_content', community)
71   -# post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
72   -# json = JSON.parse(last_response.body)
73   -# assert_not_nil json["task"]["id"]
74   -# end
75   -#
76   -# should 'do not create article if user has no permission to post content' do
77   -#assert false
78   -## community = fast_create(Community)
79   -## give_permission(user.person, 'invite_members', community)
80   -## params[:article] = {:name => "Title"}
81   -## post "/api/v1/communities/#{community.id}/articles?#{params.to_query}"
82   -## assert_equal 403, last_response.status
83   -# end
84   -#
85   -## should 'create article with parent' do
86   -## community = fast_create(Community)
87   -## community.add_member(user.person)
88   -## article = fast_create(Article)
89   -##
90   -## params[:article] = {:name => "Title", :parent_id => article.id}
91   -## post "/api/v1/communities/#{community.id}/articles?#{params.to_query}"
92   -## json = JSON.parse(last_response.body)
93   -## assert_equal article.id, json["article"]["parent"]["id"]
94   -## end
95   -#
96   -# should 'create task defining the requestor as current profile logged in' do
97   -# community = fast_create(Community)
98   -# community.add_member(person)
99   -#
100   -# post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
101   -# json = JSON.parse(last_response.body)
102   -#
103   -# assert_equal person, Task.last.requestor
104   -# end
105   -#
106   -# should 'create task defining the target as the community' do
107   -# community = fast_create(Community)
108   -# community.add_member(person)
109   -#
110   -# post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
111   -# json = JSON.parse(last_response.body)
112   -#
113   -# assert_equal community, Task.last.target
114   -# end
115   -#
116   -## #############################
117   -## # Person Articles #
118   -## #############################
119   -##
120   -## should 'return article by person' do
121   -## person = fast_create(Person)
122   -## article = fast_create(Article, :profile_id => person.id, :name => "Some thing")
123   -## get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}"
124   -## json = JSON.parse(last_response.body)
125   -## assert_equal article.id, json["article"]["id"]
126   -## end
127   -##
128   -## should 'not return article by person if user has no permission to view it' do
129   -## person = fast_create(Person)
130   -## article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)
131   -## assert !article.published?
132   -##
133   -## get "/api/v1/people/#{person.id}/articles/#{article.id}?#{params.to_query}"
134   -## assert_equal 403, last_response.status
135   -## end
136   -##
137   -## should 'not list forbidden article when listing articles by person' do
138   -## person = fast_create(Person)
139   -## article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)
140   -## assert !article.published?
141   -## get "/api/v1/people/#{person.id}/articles?#{params.to_query}"
142   -## json = JSON.parse(last_response.body)
143   -## assert_not_includes json['articles'].map {|a| a['id']}, article.id
144   -## end
145   -##
146   -## should 'create article in a person' do
147   -## params[:article] = {:name => "Title"}
148   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
149   -## json = JSON.parse(last_response.body)
150   -## assert_equal "Title", json["article"]["title"]
151   -## end
152   -##
153   -## should 'person do not create article if user has no permission to post content' do
154   -## person = fast_create(Person)
155   -## params[:article] = {:name => "Title"}
156   -## post "/api/v1/people/#{person.id}/articles?#{params.to_query}"
157   -## assert_equal 403, last_response.status
158   -## end
159   -##
160   -## should 'person create article with parent' do
161   -## article = fast_create(Article)
162   -##
163   -## params[:article] = {:name => "Title", :parent_id => article.id}
164   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
165   -## json = JSON.parse(last_response.body)
166   -## assert_equal article.id, json["article"]["parent"]["id"]
167   -## end
168   -##
169   -## should 'person create article with content type passed as parameter' do
170   -## Article.delete_all
171   -## params[:article] = {:name => "Title"}
172   -## params[:content_type] = 'TextArticle'
173   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
174   -## json = JSON.parse(last_response.body)
175   -##
176   -## assert_kind_of TextArticle, Article.last
177   -## end
178   -##
179   -## should 'person create article of TinyMceArticle type if no content type is passed as parameter' do
180   -## params[:article] = {:name => "Title"}
181   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
182   -## json = JSON.parse(last_response.body)
183   -##
184   -## assert_kind_of TinyMceArticle, Article.last
185   -## end
186   -##
187   -## should 'person not create article with invalid article content type' do
188   -## params[:article] = {:name => "Title"}
189   -## params[:content_type] = 'Person'
190   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
191   -## json = JSON.parse(last_response.body)
192   -##
193   -## assert_equal 403, last_response.status
194   -## end
195   -##
196   -## should 'person create article defining the correct profile' do
197   -## params[:article] = {:name => "Title"}
198   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
199   -## json = JSON.parse(last_response.body)
200   -##
201   -## assert_equal user.person, Article.last.profile
202   -## end
203   -##
204   -## should 'person create article defining the created_by' do
205   -## params[:article] = {:name => "Title"}
206   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
207   -## json = JSON.parse(last_response.body)
208   -##
209   -## assert_equal user.person, Article.last.created_by
210   -## end
211   -##
212   -## should 'person create article defining the last_changed_by' do
213   -## params[:article] = {:name => "Title"}
214   -## post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}"
215   -## json = JSON.parse(last_response.body)
216   -##
217   -## assert_equal user.person, Article.last.last_changed_by
218   -## end
219   -##
220   -## #############################
221   -## # Enterprise Articles #
222   -## #############################
223   -##
224   -## should 'return article by enterprise' do
225   -## enterprise = fast_create(Enterprise)
226   -## article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing")
227   -## get "/api/v1/enterprises/#{enterprise.id}/articles/#{article.id}?#{params.to_query}"
228   -## json = JSON.parse(last_response.body)
229   -## assert_equal article.id, json["article"]["id"]
230   -## end
231   -##
232   -## should 'not return article by enterprise if user has no permission to view it' do
233   -## enterprise = fast_create(Enterprise)
234   -## article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing", :published => false)
235   -## assert !article.published?
236   -##
237   -## get "/api/v1/enterprises/#{enterprise.id}/articles/#{article.id}?#{params.to_query}"
238   -## assert_equal 403, last_response.status
239   -## end
240   -##
241   -## should 'not list forbidden article when listing articles by enterprise' do
242   -## enterprise = fast_create(Enterprise)
243   -## article = fast_create(Article, :profile_id => enterprise.id, :name => "Some thing", :published => false)
244   -## assert !article.published?
245   -##
246   -## get "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
247   -## json = JSON.parse(last_response.body)
248   -## assert_not_includes json['articles'].map {|a| a['id']}, article.id
249   -## end
250   -##
251   -## should 'create article in a enterprise' do
252   -## enterprise = fast_create(Enterprise)
253   -## give_permission(user.person, 'post_content', enterprise)
254   -## params[:article] = {:name => "Title"}
255   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
256   -## json = JSON.parse(last_response.body)
257   -## assert_equal "Title", json["article"]["title"]
258   -## end
259   -##
260   -## should 'enterprise: do not create article if user has no permission to post content' do
261   -## enterprise = fast_create(Enterprise)
262   -## give_permission(user.person, 'invite_members', enterprise)
263   -## params[:article] = {:name => "Title"}
264   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
265   -## assert_equal 403, last_response.status
266   -## end
267   -##
268   -## should 'enterprise: create article with parent' do
269   -## enterprise = fast_create(Enterprise)
270   -## enterprise.add_member(user.person)
271   -## article = fast_create(Article)
272   -##
273   -## params[:article] = {:name => "Title", :parent_id => article.id}
274   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
275   -## json = JSON.parse(last_response.body)
276   -## assert_equal article.id, json["article"]["parent"]["id"]
277   -## end
278   -##
279   -## should 'enterprise: create article with content type passed as parameter' do
280   -## enterprise = fast_create(Enterprise)
281   -## enterprise.add_member(user.person)
282   -##
283   -## Article.delete_all
284   -## params[:article] = {:name => "Title"}
285   -## params[:content_type] = 'TextArticle'
286   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
287   -## json = JSON.parse(last_response.body)
288   -##
289   -## assert_kind_of TextArticle, Article.last
290   -## end
291   -##
292   -## should 'enterprise: create article of TinyMceArticle type if no content type is passed as parameter' do
293   -## enterprise = fast_create(Enterprise)
294   -## enterprise.add_member(user.person)
295   -##
296   -## params[:article] = {:name => "Title"}
297   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
298   -## json = JSON.parse(last_response.body)
299   -##
300   -## assert_kind_of TinyMceArticle, Article.last
301   -## end
302   -##
303   -## should 'enterprise: not create article with invalid article content type' do
304   -## enterprise = fast_create(Enterprise)
305   -## enterprise.add_member(user.person)
306   -##
307   -## params[:article] = {:name => "Title"}
308   -## params[:content_type] = 'Person'
309   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
310   -## json = JSON.parse(last_response.body)
311   -##
312   -## assert_equal 403, last_response.status
313   -## end
314   -##
315   -## should 'enterprise: create article defining the correct profile' do
316   -## enterprise = fast_create(Enterprise)
317   -## enterprise.add_member(user.person)
318   -##
319   -## params[:article] = {:name => "Title"}
320   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
321   -## json = JSON.parse(last_response.body)
322   -##
323   -## assert_equal enterprise, Article.last.profile
324   -## end
325   -##
326   -## should 'enterprise: create article defining the created_by' do
327   -## enterprise = fast_create(Enterprise)
328   -## enterprise.add_member(user.person)
329   -##
330   -## params[:article] = {:name => "Title"}
331   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
332   -## json = JSON.parse(last_response.body)
333   -##
334   -## assert_equal user.person, Article.last.created_by
335   -## end
336   -##
337   -## should 'enterprise: create article defining the last_changed_by' do
338   -## enterprise = fast_create(Enterprise)
339   -## enterprise.add_member(user.person)
340   -##
341   -## params[:article] = {:name => "Title"}
342   -## post "/api/v1/enterprises/#{enterprise.id}/articles?#{params.to_query}"
343   -## json = JSON.parse(last_response.body)
344   -##
345   -## assert_equal user.person, Article.last.last_changed_by
346   -## end
347   -##
348   -## should 'list article children with partial fields' do
349   -## article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
350   -## child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")
351   -## params[:fields] = [:title]
352   -## get "/api/v1/articles/#{article.id}/children?#{params.to_query}"
353   -## json = JSON.parse(last_response.body)
354   -## assert_equal ['title'], json['articles'].first.keys
355   -## end
356   -##
357   -## should 'suggest article children' do
358   -## article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
359   -## params[:target_id] = user.person.id
360   -## params[:article] = {:name => "Article name", :body => "Article body"}
361   -## assert_difference "SuggestArticle.count" do
362   -## post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}"
363   -## end
364   -## json = JSON.parse(last_response.body)
365   -## assert_equal 'SuggestArticle', json['type']
366   -## end
367   -##
368   -## should 'suggest event children' do
369   -## article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
370   -## params[:target_id] = user.person.id
371   -## params[:article] = {:name => "Article name", :body => "Article body", :type => "Event"}
372   -## assert_difference "SuggestArticle.count" do
373   -## post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}"
374   -## end
375   -## json = JSON.parse(last_response.body)
376   -## assert_equal 'SuggestArticle', json['type']
377   -## end
378   -##
379   -## should 'update hit attribute of article children' do
380   -## a1 = fast_create(Article, :profile_id => user.person.id)
381   -## a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id)
382   -## a3 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id)
383   -## get "/api/v1/articles/#{a1.id}/children?#{params.to_query}"
384   -## json = JSON.parse(last_response.body)
385   -## assert_equal [1, 1], json['articles'].map { |a| a['hits']}
386   -## assert_equal [0, 1, 1], [a1.reload.hits, a2.reload.hits, a3.reload.hits]
387   -## end
388   -##
  30 + should 'not return environmet task if user has no permission to view it' do
  31 + person = fast_create(Person)
  32 + task = create(Task, :requestor => person, :target => environment)
  33 +
  34 + get "/api/v1/tasks/#{task.id}?#{params.to_query}"
  35 + assert_equal 403, last_response.status
  36 + end
  37 +
  38 + #############################
  39 + # Community Tasks #
  40 + #############################
  41 +
  42 + should 'return task by community' do
  43 + community = fast_create(Community)
  44 + community.add_admin(person)
  45 +
  46 + task = create(Task, :requestor => person, :target => community)
  47 + assert person.is_member_of?(community)
  48 +
  49 + get "/api/v1/communities/#{community.id}/tasks/#{task.id}?#{params.to_query}"
  50 + json = JSON.parse(last_response.body)
  51 + assert_equal task.id, json["task"]["id"]
  52 + end
  53 +
  54 + should 'not return task by community if user has no permission to view it' do
  55 + community = fast_create(Community)
  56 + task = create(Task, :requestor => person, :target => community)
  57 + assert !person.is_member_of?(community)
  58 +
  59 + get "/api/v1/communities/#{community.id}/tasks/#{task.id}?#{params.to_query}"
  60 + assert_equal 403, last_response.status
  61 + end
  62 +
  63 + should 'create task in a community' do
  64 + community = fast_create(Community)
  65 + give_permission(person, 'perform_task', community)
  66 + post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
  67 + json = JSON.parse(last_response.body)
  68 + assert_not_nil json["task"]["id"]
  69 + end
  70 +
  71 + should 'not create task in a community' do
  72 + community = fast_create(Community)
  73 + post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}&content_type=ApproveArticle"
  74 + assert_equal 400, last_response.status
  75 + end
  76 +
  77 + should 'create task defining the requestor as current profile logged in' do
  78 + community = fast_create(Community)
  79 + community.add_member(person)
  80 +
  81 + post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
  82 + json = JSON.parse(last_response.body)
  83 +
  84 + assert_equal person, Task.last.requestor
  85 + end
  86 +
  87 + should 'create task defining the target as the community' do
  88 + community = fast_create(Community)
  89 + community.add_member(person)
  90 +
  91 + post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}"
  92 + json = JSON.parse(last_response.body)
  93 +
  94 + assert_equal community, Task.last.target
  95 + end
  96 +
  97 + #############################
  98 + # Person Tasks #
  99 + #############################
  100 +
  101 + should 'return task by person' do
  102 + task = create(Task, :requestor => person, :target => person)
  103 + get "/api/v1/people/#{person.id}/tasks/#{task.id}?#{params.to_query}"
  104 + json = JSON.parse(last_response.body)
  105 + assert_equal task.id, json["task"]["id"]
  106 + end
  107 +
  108 + should 'not return task by person if user has no permission to view it' do
  109 + some_person = fast_create(Person)
  110 + task = create(Task, :requestor => person, :target => some_person)
  111 +
  112 + get "/api/v1/people/#{some_person.id}/tasks/#{task.id}?#{params.to_query}"
  113 + assert_equal 403, last_response.status
  114 + end
  115 +
  116 + should 'create task for person' do
  117 + post "/api/v1/people/#{person.id}/tasks?#{params.to_query}"
  118 + json = JSON.parse(last_response.body)
  119 + assert_not_nil json["task"]["id"]
  120 + end
  121 +
  122 + should 'create task for another person' do
  123 + some_person = fast_create(Person)
  124 + post "/api/v1/people/#{some_person.id}/tasks?#{params.to_query}"
  125 + json = JSON.parse(last_response.body)
  126 +
  127 + assert_equal some_person, Task.last.target
  128 + end
  129 +
  130 + should 'create task defining the target as a person' do
  131 + post "/api/v1/people/#{person.id}/tasks?#{params.to_query}"
  132 + json = JSON.parse(last_response.body)
  133 +
  134 + assert_equal person, Task.last.target
  135 + end
  136 +
  137 + #############################
  138 + # Enterprise Tasks #
  139 + #############################
  140 +
  141 + should 'return task by enterprise' do
  142 + enterprise = fast_create(Enterprise)
  143 + enterprise.add_admin(person)
  144 +
  145 + task = create(Task, :requestor => person, :target => enterprise)
  146 + assert person.is_member_of?(enterprise)
  147 +
  148 + get "/api/v1/enterprises/#{enterprise.id}/tasks/#{task.id}?#{params.to_query}"
  149 + json = JSON.parse(last_response.body)
  150 + assert_equal task.id, json["task"]["id"]
  151 + end
  152 +
  153 + should 'not return task by enterprise if user has no permission to view it' do
  154 + enterprise = fast_create(Enterprise)
  155 + task = create(Task, :requestor => person, :target => enterprise)
  156 + assert !person.is_member_of?(enterprise)
  157 +
  158 + get "/api/v1/enterprises/#{enterprise.id}/tasks/#{task.id}?#{params.to_query}"
  159 + assert_equal 403, last_response.status
  160 + end
  161 +
  162 + should 'create task in a enterprise' do
  163 + enterprise = fast_create(Enterprise)
  164 + give_permission(person, 'perform_task', enterprise)
  165 + post "/api/v1/enterprises/#{enterprise.id}/tasks?#{params.to_query}"
  166 + json = JSON.parse(last_response.body)
  167 + assert_not_nil json["task"]["id"]
  168 + end
  169 +
  170 + should 'not create task in a enterprise' do
  171 + enterprise = fast_create(Enterprise)
  172 + post "/api/v1/enterprises/#{enterprise.id}/tasks?#{params.to_query}&content_type=ApproveArticle"
  173 + assert_equal 400, last_response.status
  174 + end
  175 +
  176 + should 'create task defining the target as the enterprise' do
  177 + enterprise = fast_create(Enterprise)
  178 + enterprise.add_member(person)
  179 +
  180 + post "/api/v1/enterprises/#{enterprise.id}/tasks?#{params.to_query}"
  181 + json = JSON.parse(last_response.body)
  182 +
  183 + assert_equal enterprise, Task.last.target
  184 + end
389 185 end
... ...