[Git][noosfero/noosfero][master] 6 commits: Add api method
Leandro Nunes
gitlab at mg.gitlab.com
Mon Jun 19 11:12:14 BRT 2017
Leandro Nunes pushed to branch master at Noosfero / noosfero
Commits:
5394722e by Marcelo Júnior at 2017-06-13T15:40:54-03:00
Add api method
- - - - -
87bc316f by Marcelo Júnior at 2017-06-14T18:04:12-03:00
Add check to prevent unwanted block_type parameter
- - - - -
efdc8302 by Marcelo Júnior at 2017-06-14T18:05:15-03:00
Add unit tests
- - - - -
27c50b90 by Marcelo Júnior at 2017-06-14T19:20:01-03:00
improve block filtering
- - - - -
7a91d277 by Marcelo Júnior at 2017-06-16T14:46:53-03:00
Update unit tests
- - - - -
a67be042 by Leandro Nunes at 2017-06-19T14:11:50+00:00
Merge branch 'api-block-preview' into 'master'
Api block preview
See merge request !1240
- - - - -
2 changed files:
- app/api/v1/blocks.rb
- test/api/blocks_test.rb
Changes:
=====================================
app/api/v1/blocks.rb
=====================================
--- a/app/api/v1/blocks.rb
+++ b/app/api/v1/blocks.rb
@@ -3,6 +3,22 @@ module Api
class Blocks < Grape::API
+ resource :profiles do
+ segment '/:profile_id' do
+ resource :blocks do
+ resource :preview do
+ get do
+ block_type = params[:block_type]
+ return forbidden! unless Object.const_defined?(block_type) && block_type.constantize <= Block
+ profile = environment.profiles.find_by(id: params[:profile_id])
+ block = block_type.constantize.new(:box => Box.new(:owner => profile))
+ present_partial block, :with => Entities::Block, display_api_content: true
+ end
+ end
+ end
+ end
+ end
+
resource :blocks do
get ':id' do
block = Block.find(params["id"])
=====================================
test/api/blocks_test.rb
=====================================
--- a/test/api/blocks_test.rb
+++ b/test/api/blocks_test.rb
@@ -182,4 +182,45 @@ class BlocksTest < ActiveSupport::TestCase
assert_equal 0, block.images.size
end
+ should 'return forbidden when block type is not a constant declared' do
+ params[:block_type] = 'FakeBlock'
+ get "/api/v1/profiles/#{person.id}/blocks/preview?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+ assert_equal json["message"], "403 Forbidden"
+ end
+
+ should 'return forbidden when block type is a constant declared but is not derived from Block' do
+ params[:block_type] = 'Article'
+ get "/api/v1/profiles/#{person.id}/blocks/preview?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+ assert_equal json["message"], "403 Forbidden"
+ end
+
+ should 'be able to get preview of CommunitiesBlock' do
+ community = fast_create(Community, :environment_id => environment.id)
+ community.add_admin(person)
+ params[:block_type] = 'CommunitiesBlock'
+ get "/api/v1/profiles/#{person.id}/blocks/preview?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+ assert_includes json["api_content"]['communities'].map{ |community| community['id'] }, community.id
+ end
+
+ should 'be able to get preview of RawHTMLBlock' do
+ params[:block_type] = 'RawHTMLBlock'
+ get "/api/v1/profiles/#{person.id}/blocks/preview?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+ assert_nil json["api_content"]['html']
+ end
+
+ should 'be able to get preview of RecentDocumentsBlock' do
+ article1 = fast_create(Article, :profile_id => user.person.id, :name => "Article 1")
+ article2 = fast_create(Article, :profile_id => user.person.id, :name => "Article 2")
+ params[:block_type] = 'RecentDocumentsBlock'
+ get "/api/v1/profiles/#{user.person.id}/blocks/preview?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+ assert_equal 2, json["api_content"]["articles"].size
+ assert_includes json["api_content"]['articles'].map{ |article| article['id'] }, article1.id
+ assert_includes json["api_content"]['articles'].map{ |article| article['id'] }, article2.id
+ end
+
end
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/f583c41fdfae49153c7f9437621d77e5e2610eab...a67be04210804f6d2061a664917eb58fd42b9dcf
---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/f583c41fdfae49153c7f9437621d77e5e2610eab...a67be04210804f6d2061a664917eb58fd42b9dcf
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170619/cfff8b5c/attachment-0001.html>
More information about the Noosfero-dev
mailing list