Commit 52d3c365ad5286a1ef2abe469c60dd0e1de801d5
Exists in
ratings_minor_fixes
and in
3 other branches
Merge branch 'destroy-article-api' into 'master'
Added remove article in API See merge request !920
Showing
2 changed files
with
31 additions
and
0 deletions
Show diff stats
app/api/v1/articles.rb
| @@ -54,6 +54,17 @@ module Api | @@ -54,6 +54,17 @@ module Api | ||
| 54 | present_partial article, :with => Entities::Article | 54 | present_partial article, :with => Entities::Article |
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | + delete ':id' do | ||
| 58 | + article = environment.articles.find(params[:id]) | ||
| 59 | + return forbidden! unless article.allow_delete?(current_person) | ||
| 60 | + begin | ||
| 61 | + article.destroy | ||
| 62 | + { :success => true } | ||
| 63 | + rescue Exception => exception | ||
| 64 | + render_api_error!(_('The article couldn\'t be removed due to some problem. Please contact the administrator.'), 400) | ||
| 65 | + end | ||
| 66 | + end | ||
| 67 | + | ||
| 57 | desc 'Report a abuse and/or violent content in a article by id' do | 68 | desc 'Report a abuse and/or violent content in a article by id' do |
| 58 | detail 'Submit a abuse (in general, a content violation) report about a specific article' | 69 | detail 'Submit a abuse (in general, a content violation) report about a specific article' |
| 59 | params Entities::Article.documentation | 70 | params Entities::Article.documentation |
test/api/articles_test.rb
| @@ -7,6 +7,26 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -7,6 +7,26 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 7 | login_api | 7 | login_api |
| 8 | end | 8 | end |
| 9 | 9 | ||
| 10 | + should 'remove article' do | ||
| 11 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
| 12 | + delete "/api/v1/articles/#{article.id}?#{params.to_query}" | ||
| 13 | + json = JSON.parse(last_response.body) | ||
| 14 | + | ||
| 15 | + assert_not_equal 401, last_response.status | ||
| 16 | + assert_equal true, json['success'] | ||
| 17 | + | ||
| 18 | + assert !Article.exists?(article.id) | ||
| 19 | + end | ||
| 20 | + | ||
| 21 | + should 'not remove article without permission' do | ||
| 22 | + otherPerson = fast_create(Person, :name => "Other Person") | ||
| 23 | + article = fast_create(Article, :profile_id => otherPerson.id, :name => "Some thing") | ||
| 24 | + delete "/api/v1/articles/#{article.id}?#{params.to_query}" | ||
| 25 | + json = JSON.parse(last_response.body) | ||
| 26 | + assert_equal 403, last_response.status | ||
| 27 | + assert Article.exists?(article.id) | ||
| 28 | + end | ||
| 29 | + | ||
| 10 | should 'list articles' do | 30 | should 'list articles' do |
| 11 | article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | 31 | article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") |
| 12 | get "/api/v1/articles/?#{params.to_query}" | 32 | get "/api/v1/articles/?#{params.to_query}" |