Commit 261afb0d5cedc1633680685b56a72553d18c548d
Committed by
Rodrigo Souto
1 parent
7164ac60
Exists in
master
and in
29 other branches
API: adding initial task endpoint
Showing
7 changed files
with
582 additions
and
7 deletions
Show diff stats
app/models/task.rb
| @@ -265,6 +265,19 @@ class Task < ActiveRecord::Base | @@ -265,6 +265,19 @@ class Task < ActiveRecord::Base | ||
| 265 | 265 | ||
| 266 | include Spammable | 266 | include Spammable |
| 267 | 267 | ||
| 268 | + #FIXME make this test | ||
| 269 | + def display_to?(user = nil) | ||
| 270 | + return true if self.target == user | ||
| 271 | + return false if !self.target.kind_of?(Environment) && self.target.person? | ||
| 272 | + | ||
| 273 | + if self.target.kind_of?(Environment) | ||
| 274 | + user.is_admin?(self.target) | ||
| 275 | + else | ||
| 276 | + self.target.members.by_role(self.target.roles.reject {|r| !r.has_permission?('perform_task')}).include?(user) | ||
| 277 | + end | ||
| 278 | + end | ||
| 279 | + | ||
| 280 | + | ||
| 268 | protected | 281 | protected |
| 269 | 282 | ||
| 270 | # This method must be overrided in subclasses, and its implementation must do | 283 | # This method must be overrided in subclasses, and its implementation must do |
db/schema.rb
| @@ -729,24 +729,26 @@ ActiveRecord::Schema.define(:version => 20150525101430) do | @@ -729,24 +729,26 @@ ActiveRecord::Schema.define(:version => 20150525101430) do | ||
| 729 | create_table "users", :force => true do |t| | 729 | create_table "users", :force => true do |t| |
| 730 | t.string "login" | 730 | t.string "login" |
| 731 | t.string "email" | 731 | t.string "email" |
| 732 | - t.string "crypted_password", :limit => 40 | ||
| 733 | - t.string "salt", :limit => 40 | 732 | + t.string "crypted_password", :limit => 40 |
| 733 | + t.string "salt", :limit => 40 | ||
| 734 | t.datetime "created_at" | 734 | t.datetime "created_at" |
| 735 | t.datetime "updated_at" | 735 | t.datetime "updated_at" |
| 736 | t.string "remember_token" | 736 | t.string "remember_token" |
| 737 | t.datetime "remember_token_expires_at" | 737 | t.datetime "remember_token_expires_at" |
| 738 | t.text "terms_of_use" | 738 | t.text "terms_of_use" |
| 739 | - t.string "terms_accepted", :limit => 1 | 739 | + t.string "terms_accepted", :limit => 1 |
| 740 | t.integer "environment_id" | 740 | t.integer "environment_id" |
| 741 | t.string "password_type" | 741 | t.string "password_type" |
| 742 | - t.boolean "enable_email", :default => false | ||
| 743 | - t.string "last_chat_status", :default => "" | ||
| 744 | - t.string "chat_status", :default => "" | 742 | + t.boolean "enable_email", :default => false |
| 743 | + t.string "last_chat_status", :default => "" | ||
| 744 | + t.string "chat_status", :default => "" | ||
| 745 | t.datetime "chat_status_at" | 745 | t.datetime "chat_status_at" |
| 746 | - t.string "activation_code", :limit => 40 | 746 | + t.string "activation_code", :limit => 40 |
| 747 | t.datetime "activated_at" | 747 | t.datetime "activated_at" |
| 748 | t.string "return_to" | 748 | t.string "return_to" |
| 749 | t.datetime "last_login_at" | 749 | t.datetime "last_login_at" |
| 750 | + t.string "private_token" | ||
| 751 | + t.datetime "private_token_generated_at" | ||
| 750 | end | 752 | end |
| 751 | 753 | ||
| 752 | create_table "validation_infos", :force => true do |t| | 754 | create_table "validation_infos", :force => true do |t| |
lib/noosfero/api/api.rb
| @@ -44,6 +44,7 @@ module Noosfero | @@ -44,6 +44,7 @@ module Noosfero | ||
| 44 | mount V1::People | 44 | mount V1::People |
| 45 | mount V1::Enterprises | 45 | mount V1::Enterprises |
| 46 | mount V1::Categories | 46 | mount V1::Categories |
| 47 | + mount V1::Tasks | ||
| 47 | mount Session | 48 | mount Session |
| 48 | 49 | ||
| 49 | # hook point which allow plugins to add Grape::API extensions to API::API | 50 | # hook point which allow plugins to add Grape::API extensions to API::API |
lib/noosfero/api/entities.rb
lib/noosfero/api/helpers.rb
| @@ -50,6 +50,11 @@ module Noosfero | @@ -50,6 +50,11 @@ module Noosfero | ||
| 50 | article.display_to?(current_user.person) ? article : forbidden! | 50 | article.display_to?(current_user.person) ? article : forbidden! |
| 51 | end | 51 | end |
| 52 | 52 | ||
| 53 | + def find_task(tasks, id) | ||
| 54 | + task = tasks.find(id) | ||
| 55 | + task.display_to?(current_user.person) ? task : forbidden! | ||
| 56 | + end | ||
| 57 | + | ||
| 53 | def make_conditions_with_parameter(params = {}) | 58 | def make_conditions_with_parameter(params = {}) |
| 54 | parsed_params = parser_params(params) | 59 | parsed_params = parser_params(params) |
| 55 | conditions = {} | 60 | conditions = {} |
| @@ -0,0 +1,164 @@ | @@ -0,0 +1,164 @@ | ||
| 1 | +module Noosfero | ||
| 2 | + module API | ||
| 3 | + module V1 | ||
| 4 | + class Tasks < Grape::API | ||
| 5 | +# before { authenticate! } | ||
| 6 | + | ||
| 7 | +# ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} | ||
| 8 | + | ||
| 9 | + resource :tasks do | ||
| 10 | + | ||
| 11 | + # Collect tasks | ||
| 12 | + # | ||
| 13 | + # Parameters: | ||
| 14 | + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created | ||
| 15 | + # oldest - Collect the oldest articles. If nothing is passed the newest articles are collected | ||
| 16 | + # limit - amount of articles returned. The default value is 20 | ||
| 17 | + # | ||
| 18 | + # Example Request: | ||
| 19 | + # GET host/api/v1/tasks?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317 | ||
| 20 | + get do | ||
| 21 | + #FIXME check for permission | ||
| 22 | + tasks = select_filtered_collection_of(environment, 'tasks', params) | ||
| 23 | + present tasks, :with => Entities::Task, :fields => params[:fields] | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + desc "Return the task id" | ||
| 27 | + get ':id' do | ||
| 28 | + task = find_task(environment.tasks, params[:id]) | ||
| 29 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + resource :communities do | ||
| 36 | + segment '/:community_id' do | ||
| 37 | + resource :tasks do | ||
| 38 | + get do | ||
| 39 | + #FIXME check for permission | ||
| 40 | + community = environment.communities.find(params[:community_id]) | ||
| 41 | + tasks = select_filtered_collection_of(community, 'tasks', params) | ||
| 42 | + present tasks, :with => Entities::Task, :fields => params[:fields] | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + get ':id' do | ||
| 46 | + community = environment.communities.find(params[:community_id]) | ||
| 47 | + task = find_task(community.tasks, params[:id]) | ||
| 48 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | + # Example Request: | ||
| 52 | + # POST api/v1/communites/:community_id/articles?private_token=234298743290432&article[name]=title&article[body]=body | ||
| 53 | + post do | ||
| 54 | + community = environment.communities.find(params[:community_id]) | ||
| 55 | +#FIXME see the correct permission | ||
| 56 | + return forbidden! unless current_person.can_post_content?(community) | ||
| 57 | +#FIXME check the task type before create | ||
| 58 | + klass_type= params[:content_type].nil? ? 'Task' : params[:content_type] | ||
| 59 | +# return forbidden! unless ARTICLE_TYPES.include?(klass_type) | ||
| 60 | +# | ||
| 61 | + task = klass_type.constantize.new(params[:task]) | ||
| 62 | + task.requestor = current_person | ||
| 63 | + task.target = community | ||
| 64 | + | ||
| 65 | + if !task.save | ||
| 66 | + render_api_errors!(task.errors.full_messages) | ||
| 67 | + end | ||
| 68 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 69 | + end | ||
| 70 | + | ||
| 71 | + end | ||
| 72 | + end | ||
| 73 | + | ||
| 74 | + end | ||
| 75 | + | ||
| 76 | + resource :people do | ||
| 77 | + segment '/:person_id' do | ||
| 78 | + resource :tasks do | ||
| 79 | + get do | ||
| 80 | +# person = environment.people.find(params[:person_id]) | ||
| 81 | +# articles = select_filtered_collection_of(person, 'articles', params) | ||
| 82 | +# articles = articles.display_filter(current_person, person) | ||
| 83 | +tasks = Task.all | ||
| 84 | + present tasks, :with => Entities::Task, :fields => params[:fields] | ||
| 85 | + end | ||
| 86 | + | ||
| 87 | + get ':id' do | ||
| 88 | +# person = environment.people.find(params[:person_id]) | ||
| 89 | +# article = find_article(person.articles, params[:id]) | ||
| 90 | +task = Task.first | ||
| 91 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 92 | + end | ||
| 93 | + | ||
| 94 | + post do | ||
| 95 | +# person = environment.people.find(params[:person_id]) | ||
| 96 | +# return forbidden! unless current_person.can_post_content?(person) | ||
| 97 | +# | ||
| 98 | +# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | ||
| 99 | +# return forbidden! unless ARTICLE_TYPES.include?(klass_type) | ||
| 100 | +# | ||
| 101 | +# article = klass_type.constantize.new(params[:article]) | ||
| 102 | +# article.last_changed_by = current_person | ||
| 103 | +# article.created_by= current_person | ||
| 104 | +# article.profile = person | ||
| 105 | +# | ||
| 106 | +# if !article.save | ||
| 107 | +# render_api_errors!(article.errors.full_messages) | ||
| 108 | +# end | ||
| 109 | +task = Task.first | ||
| 110 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 111 | + end | ||
| 112 | + | ||
| 113 | + end | ||
| 114 | + end | ||
| 115 | + | ||
| 116 | + end | ||
| 117 | + | ||
| 118 | + resource :enterprises do | ||
| 119 | + segment '/:enterprise_id' do | ||
| 120 | + resource :tasks do | ||
| 121 | + get do | ||
| 122 | +# enterprise = environment.enterprises.find(params[:enterprise_id]) | ||
| 123 | +# articles = select_filtered_collection_of(enterprise, 'articles', params) | ||
| 124 | +# articles = articles.display_filter(current_person, enterprise) | ||
| 125 | +tasks = Task.all | ||
| 126 | + present tasks, :with => Entities::Task, :fields => params[:fields] | ||
| 127 | + end | ||
| 128 | + | ||
| 129 | + get ':id' do | ||
| 130 | +# enterprise = environment.enterprises.find(params[:enterprise_id]) | ||
| 131 | +# article = find_article(enterprise.articles, params[:id]) | ||
| 132 | +task = Task.first | ||
| 133 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 134 | + end | ||
| 135 | + | ||
| 136 | + post do | ||
| 137 | +# enterprise = environment.enterprises.find(params[:enterprise_id]) | ||
| 138 | +# return forbidden! unless current_person.can_post_content?(enterprise) | ||
| 139 | +# | ||
| 140 | +# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | ||
| 141 | +# return forbidden! unless ARTICLE_TYPES.include?(klass_type) | ||
| 142 | +# | ||
| 143 | +# article = klass_type.constantize.new(params[:article]) | ||
| 144 | +# article.last_changed_by = current_person | ||
| 145 | +# article.created_by= current_person | ||
| 146 | +# article.profile = enterprise | ||
| 147 | +# | ||
| 148 | +# if !article.save | ||
| 149 | +# render_api_errors!(article.errors.full_messages) | ||
| 150 | +# end | ||
| 151 | +task = Task.first | ||
| 152 | + present task, :with => Entities::Task, :fields => params[:fields] | ||
| 153 | + end | ||
| 154 | + | ||
| 155 | + end | ||
| 156 | + end | ||
| 157 | + | ||
| 158 | + end | ||
| 159 | + | ||
| 160 | + | ||
| 161 | + end | ||
| 162 | + end | ||
| 163 | + end | ||
| 164 | +end |
| @@ -0,0 +1,389 @@ | @@ -0,0 +1,389 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/test_helper' | ||
| 2 | + | ||
| 3 | +class TasksTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + login_api | ||
| 7 | + @person = user.person | ||
| 8 | + @community = fast_create(Community) | ||
| 9 | + @environment = Environment.default | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + attr_accessor :person, :community, :environment | ||
| 13 | + | ||
| 14 | + should 'list tasks' do | ||
| 15 | + task = fast_create(Task, :requestor_id => environment.id, :target_id => community.id) | ||
| 16 | + get "/api/v1/tasks?#{params.to_query}" | ||
| 17 | + json = JSON.parse(last_response.body) | ||
| 18 | + assert_includes json["tasks"].map { |a| a["id"] }, task.id | ||
| 19 | + end | ||
| 20 | + | ||
| 21 | + should 'return environment task by id' do | ||
| 22 | + environment.add_admin(person) | ||
| 23 | + task = create(Task, :requestor => person, :target => environment) | ||
| 24 | + get "/api/v1/tasks/#{task.id}?#{params.to_query}" | ||
| 25 | + json = JSON.parse(last_response.body) | ||
| 26 | + assert_equal task.id, json["task"]["id"] | ||
| 27 | + end | ||
| 28 | + | ||
| 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 | +# | ||
| 389 | +end |