Commit 4492ca9fea106282776aca602ae8a7f17871ea87

Authored by Leandro Santos
2 parents c8eaed18 f7d88514

Merge branch 'environment-api-refactoring' into 'master'

Environment api refactoring

Fixed environment endpoint data

See merge request !874
lib/noosfero/api/entities.rb
@@ -231,6 +231,9 @@ module Noosfero @@ -231,6 +231,9 @@ module Noosfero
231 231
232 class Environment < Entity 232 class Environment < Entity
233 expose :name 233 expose :name
  234 + expose :id
  235 + expose :description
  236 + expose :settings, if: lambda { |instance, options| options[:is_admin] }
234 end 237 end
235 238
236 class Tag < Entity 239 class Tag < Entity
lib/noosfero/api/helpers.rb
@@ -30,6 +30,11 @@ require_relative &#39;../../find_by_contents&#39; @@ -30,6 +30,11 @@ require_relative &#39;../../find_by_contents&#39;
30 current_user.person unless current_user.nil? 30 current_user.person unless current_user.nil?
31 end 31 end
32 32
  33 + def is_admin?(environment)
  34 + return false unless current_user
  35 + return current_person.is_admin?(environment)
  36 + end
  37 +
33 def logout 38 def logout
34 @current_user = nil 39 @current_user = nil
35 end 40 end
lib/noosfero/api/v1/environments.rb
@@ -11,13 +11,15 @@ module Noosfero @@ -11,13 +11,15 @@ module Noosfero
11 end 11 end
12 12
13 get ':id' do 13 get ':id' do
  14 + local_environment = nil
14 if (params[:id] == "default") 15 if (params[:id] == "default")
15 - present Environment.default 16 + local_environment = Environment.default
16 elsif (params[:id] == "context") 17 elsif (params[:id] == "context")
17 - present environment 18 + local_environment = environment
18 else 19 else
19 - present Environment.find(params[:id]) 20 + local_environment = Environment.find(params[:id])
20 end 21 end
  22 + present_partial local_environment, :with => Entities::Environment, :is_admin => is_admin?(local_environment)
21 end 23 end
22 24
23 end 25 end
test/api/environment_test.rb
@@ -2,16 +2,45 @@ require_relative &#39;test_helper&#39; @@ -2,16 +2,45 @@ require_relative &#39;test_helper&#39;
2 2
3 class EnvironmentTest < ActiveSupport::TestCase 3 class EnvironmentTest < ActiveSupport::TestCase
4 4
5 - def setup  
6 - @person = create_user('testing').person 5 + should 'return the default environment' do
  6 + environment = Environment.default
  7 + get "/api/v1/environment/default"
  8 + json = JSON.parse(last_response.body)
  9 + assert_equal environment.id, json['id']
7 end 10 end
8 - attr_reader :person  
9 11
10 - should 'return the default environment' do 12 + should 'not return the default environment settings' do
11 environment = Environment.default 13 environment = Environment.default
12 get "/api/v1/environment/default" 14 get "/api/v1/environment/default"
13 json = JSON.parse(last_response.body) 15 json = JSON.parse(last_response.body)
14 assert_equal environment.id, json['id'] 16 assert_equal environment.id, json['id']
  17 + assert_nil json['settings']
  18 + end
  19 +
  20 + should 'return the default environment settings for admin' do
  21 + login_api
  22 + environment = Environment.default
  23 + environment.add_admin(person)
  24 + get "/api/v1/environment/default?#{params.to_query}"
  25 + json = JSON.parse(last_response.body)
  26 + assert_equal environment.id, json['id']
  27 + assert_equal environment.settings, json['settings']
  28 + end
  29 +
  30 + should 'not return the default environment settings for non admin users' do
  31 + login_api
  32 + environment = Environment.default
  33 + get "/api/v1/environment/default?#{params.to_query}"
  34 + json = JSON.parse(last_response.body)
  35 + assert_equal environment.id, json['id']
  36 + assert_nil json['settings']
  37 + end
  38 +
  39 + should 'return the default environment description' do
  40 + environment = Environment.default
  41 + get "/api/v1/environment/default"
  42 + json = JSON.parse(last_response.body)
  43 + assert_equal environment.description, json['description']
15 end 44 end
16 45
17 should 'return created environment' do 46 should 'return created environment' do