Commit 4492ca9fea106282776aca602ae8a7f17871ea87
Exists in
send_email_to_admins
and in
5 other branches
Merge branch 'environment-api-refactoring' into 'master'
Environment api refactoring Fixed environment endpoint data See merge request !874
Showing
4 changed files
with
46 additions
and
7 deletions
Show diff stats
lib/noosfero/api/entities.rb
lib/noosfero/api/helpers.rb
| ... | ... | @@ -30,6 +30,11 @@ require_relative '../../find_by_contents' |
| 30 | 30 | current_user.person unless current_user.nil? |
| 31 | 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 | 38 | def logout |
| 34 | 39 | @current_user = nil |
| 35 | 40 | end | ... | ... |
lib/noosfero/api/v1/environments.rb
| ... | ... | @@ -11,13 +11,15 @@ module Noosfero |
| 11 | 11 | end |
| 12 | 12 | |
| 13 | 13 | get ':id' do |
| 14 | + local_environment = nil | |
| 14 | 15 | if (params[:id] == "default") |
| 15 | - present Environment.default | |
| 16 | + local_environment = Environment.default | |
| 16 | 17 | elsif (params[:id] == "context") |
| 17 | - present environment | |
| 18 | + local_environment = environment | |
| 18 | 19 | else |
| 19 | - present Environment.find(params[:id]) | |
| 20 | + local_environment = Environment.find(params[:id]) | |
| 20 | 21 | end |
| 22 | + present_partial local_environment, :with => Entities::Environment, :is_admin => is_admin?(local_environment) | |
| 21 | 23 | end |
| 22 | 24 | |
| 23 | 25 | end | ... | ... |
test/api/environment_test.rb
| ... | ... | @@ -2,16 +2,45 @@ require_relative 'test_helper' |
| 2 | 2 | |
| 3 | 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 | 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 | 13 | environment = Environment.default |
| 12 | 14 | get "/api/v1/environment/default" |
| 13 | 15 | json = JSON.parse(last_response.body) |
| 14 | 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 | 44 | end |
| 16 | 45 | |
| 17 | 46 | should 'return created environment' do | ... | ... |