Commit 763189acb5555817ba2a5b37938b04c1c5d49e6e

Authored by Leandro Santos
2 parents dbb2c156 87cde6f4

Merge branch 'participa/noosfero-environment-api'

lib/noosfero/api/v1/boxes.rb
@@ -17,10 +17,28 @@ module Noosfero @@ -17,10 +17,28 @@ module Noosfero
17 end 17 end
18 end 18 end
19 end 19 end
20 -  
21 end 20 end
22 21
23 end 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
  33 + else
  34 + env = Environment.find(params[:environment_id])
  35 + end
  36 + present env.boxes, :with => Entities::Box
  37 + end
  38 + end
  39 + end
  40 + end
  41 + end
24 end 42 end
25 43
26 end 44 end
lib/noosfero/api/v1/environments.rb
@@ -9,7 +9,17 @@ module Noosfero @@ -9,7 +9,17 @@ module Noosfero
9 get '/signup_person_fields' do 9 get '/signup_person_fields' do
10 present environment.signup_person_fields 10 present environment.signup_person_fields
11 end 11 end
12 - 12 +
  13 + get ':id' do
  14 + if (params[:id] == "default")
  15 + present Environment.default
  16 + elsif (params[:id] == "context")
  17 + present environment
  18 + else
  19 + present Environment.find(params[:id])
  20 + end
  21 + end
  22 +
13 end 23 end
14 24
15 end 25 end
test/unit/api/boxes_test.rb
@@ -3,18 +3,40 @@ require_relative 'test_helper' @@ -3,18 +3,40 @@ 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] 12 + kinds= %w[Profile Community Person Enterprise Environment]
10 kinds.each do |kind| 13 kinds.each do |kind|
11 should "get_boxes_from_#{kind.downcase.pluralize}" do 14 should "get_boxes_from_#{kind.downcase.pluralize}" do
12 - profile_obj = fast_create(kind.constantize)  
13 - box = fast_create(Box, :owner_id => profile_obj.id, :owner_type => "Profile")  
14 - get "/api/v1/#{kind.downcase.pluralize}/#{profile_obj.id}/boxes?#{params.to_query}" 15 + context_obj = fast_create(kind.constantize)
  16 + box = fast_create(Box, :owner_id => context_obj.id, :owner_type => (kind == 'Environment') ? 'Environment' : 'Profile')
  17 + get "/api/v1/#{kind.downcase.pluralize}/#{context_obj.id}/boxes?#{params.to_query}"
15 json = JSON.parse(last_response.body) 18 json = JSON.parse(last_response.body)
16 assert_equal box.id, json["boxes"].first["id"] 19 assert_equal box.id, json["boxes"].first["id"]
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
test/unit/api/environment_test.rb 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +require_relative 'test_helper'
  2 +
  3 +class EnvironmentTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @person = create_user('testing').person
  7 + end
  8 + attr_reader :person
  9 +
  10 + should 'return the default environment' do
  11 + environment = Environment.default
  12 + get "/api/v1/environment/default"
  13 + json = JSON.parse(last_response.body)
  14 + assert_equal environment.id, json['id']
  15 + end
  16 +
  17 + should 'return created environment' do
  18 + environment = fast_create(Environment)
  19 + default_env = Environment.default
  20 + assert_not_equal environment.id, default_env.id
  21 + get "/api/v1/environment/#{environment.id}"
  22 + json = JSON.parse(last_response.body)
  23 + assert_equal environment.id, json['id']
  24 + end
  25 +
  26 + should 'return context environment' do
  27 + context_env = fast_create(Environment)
  28 + context_env.name = "example org"
  29 + context_env.save
  30 + context_env.domains<< Domain.new(:name => 'example.org')
  31 + default_env = Environment.default
  32 + assert_not_equal context_env.id, default_env.id
  33 + get "/api/v1/environment/context"
  34 + json = JSON.parse(last_response.body)
  35 + assert_equal context_env.id, json['id']
  36 + end
  37 +
  38 +end