Commit 0aabc96cb2ab2bd8f5bb86651919c56312a11831

Authored by Leandro Santos
2 parents 98328aff 4492ca9f
Exists in staging and in 1 other branch production

Merge branch 'master' into staging

lib/noosfero/api/entities.rb
@@ -234,6 +234,9 @@ module Noosfero @@ -234,6 +234,9 @@ module Noosfero
234 234
235 class Environment < Entity 235 class Environment < Entity
236 expose :name 236 expose :name
  237 + expose :id
  238 + expose :description
  239 + expose :settings, if: lambda { |instance, options| options[:is_admin] }
237 end 240 end
238 241
239 class Tag < Entity 242 class Tag < Entity
lib/noosfero/api/helpers.rb
@@ -41,6 +41,11 @@ require_relative &#39;../../find_by_contents&#39; @@ -41,6 +41,11 @@ require_relative &#39;../../find_by_contents&#39;
41 current_user.person unless current_user.nil? 41 current_user.person unless current_user.nil?
42 end 42 end
43 43
  44 + def is_admin?(environment)
  45 + return false unless current_user
  46 + return current_person.is_admin?(environment)
  47 + end
  48 +
44 def logout 49 def logout
45 @current_user = nil 50 @current_user = nil
46 end 51 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