Commit 87cde6f4b2c7c05d95e93562fa5ea740087f5003
1 parent
b00143d8
Exists in
staging
and in
32 other branches
refactoring boxes routes for environment
Showing
2 changed files
with
43 additions
and
7 deletions
Show diff stats
lib/noosfero/api/v1/boxes.rb
| @@ -4,7 +4,7 @@ module Noosfero | @@ -4,7 +4,7 @@ module Noosfero | ||
| 4 | 4 | ||
| 5 | class Boxes < Grape::API | 5 | class Boxes < Grape::API |
| 6 | 6 | ||
| 7 | - kinds = %w[profile community person enterprise environment] | 7 | + kinds = %w[profile community person enterprise] |
| 8 | kinds.each do |kind| | 8 | kinds.each do |kind| |
| 9 | 9 | ||
| 10 | resource kind.pluralize.to_sym do | 10 | resource kind.pluralize.to_sym do |
| @@ -12,18 +12,32 @@ module Noosfero | @@ -12,18 +12,32 @@ module Noosfero | ||
| 12 | segment "/:#{kind}_id" do | 12 | segment "/:#{kind}_id" do |
| 13 | resource :boxes do | 13 | resource :boxes do |
| 14 | get do | 14 | get do |
| 15 | - if (kind == "environment") | ||
| 16 | - container = Environment.find(params["environment_id"]) | 15 | + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) |
| 16 | + present profile.boxes, :with => Entities::Box | ||
| 17 | + end | ||
| 18 | + end | ||
| 19 | + end | ||
| 20 | + end | ||
| 21 | + | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + resource :environments do | ||
| 25 | + [ '/default', '/context', ':environment_id' ].each do |route| | ||
| 26 | + segment route do | ||
| 27 | + resource :boxes do | ||
| 28 | + get do | ||
| 29 | + if (route.match(/default/)) | ||
| 30 | + env = Environment.default | ||
| 31 | + elsif (route.match(/context/)) | ||
| 32 | + env = environment | ||
| 17 | else | 33 | else |
| 18 | - container = environment.send(kind.pluralize).find(params["#{kind}_id"]) | 34 | + env = Environment.find(params[:environment_id]) |
| 19 | end | 35 | end |
| 20 | - present container.boxes, :with => Entities::Box | 36 | + present env.boxes, :with => Entities::Box |
| 21 | end | 37 | end |
| 22 | end | 38 | end |
| 23 | end | 39 | end |
| 24 | - | ||
| 25 | end | 40 | end |
| 26 | - | ||
| 27 | end | 41 | end |
| 28 | end | 42 | end |
| 29 | 43 |
test/unit/api/boxes_test.rb
| @@ -3,7 +3,10 @@ require_relative 'test_helper' | @@ -3,7 +3,10 @@ require_relative 'test_helper' | ||
| 3 | class BoxesTest < ActiveSupport::TestCase | 3 | class BoxesTest < ActiveSupport::TestCase |
| 4 | 4 | ||
| 5 | def setup | 5 | def setup |
| 6 | + @controller = AccountController.new | ||
| 7 | + @request = ActionController::TestRequest.new | ||
| 6 | login_api | 8 | login_api |
| 9 | +# @request = ActionController::TestRequest.new | ||
| 7 | end | 10 | end |
| 8 | 11 | ||
| 9 | kinds= %w[Profile Community Person Enterprise Environment] | 12 | kinds= %w[Profile Community Person Enterprise Environment] |
| @@ -17,4 +20,23 @@ class BoxesTest < ActiveSupport::TestCase | @@ -17,4 +20,23 @@ class BoxesTest < ActiveSupport::TestCase | ||
| 17 | end | 20 | end |
| 18 | end | 21 | end |
| 19 | 22 | ||
| 23 | + should 'get boxes from default environment' do | ||
| 24 | + Environment.delete_all | ||
| 25 | + environment = fast_create(Environment, :is_default => true) | ||
| 26 | + box = fast_create(Box, :owner_id => environment.id, :owner_type => 'Environment') | ||
| 27 | + get "/api/v1/environments/default/boxes?#{params.to_query}" | ||
| 28 | + json = JSON.parse(last_response.body) | ||
| 29 | + assert_equal box.id, json["boxes"].first["id"] | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + should 'get boxes from context environment' do | ||
| 33 | + env = fast_create(Environment, :is_default => true) | ||
| 34 | + env2 = fast_create(Environment).domains << Domain.new(:name => 'test.host') | ||
| 35 | + box = fast_create(Box, :owner_id => environment.id, :owner_type => 'Environment') | ||
| 36 | + get "/api/v1/environments/context/boxes?#{params.to_query}" | ||
| 37 | + | ||
| 38 | + json = JSON.parse(last_response.body) | ||
| 39 | + assert_equal box.id, json["boxes"].first["id"] | ||
| 40 | + end | ||
| 41 | + | ||
| 20 | end | 42 | end |