From c2c346b464305fcf3c503bf9736aae1d82ed84da Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 31 Aug 2015 16:47:40 -0300 Subject: [PATCH] adding badge endpoint --- lib/gamification_plugin.rb | 3 +++ lib/gamification_plugin/api.rb | 21 +++++++++++++++++++++ lib/gamification_plugin/entities.rb | 10 ++++++++++ test/unit/api_test.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 0 deletions(-) create mode 100644 lib/gamification_plugin/api.rb create mode 100644 lib/gamification_plugin/entities.rb create mode 100644 test/unit/api_test.rb diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index ff1e5b2..c399c22 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -58,6 +58,9 @@ class GamificationPlugin < Noosfero::Plugin { GamificationPlugin::RankingBlock => {}} end + def self.api_mount_points + [GamificationPlugin::API] + end ActionDispatch::Reloader.to_prepare do Merit.setup do |config| diff --git a/lib/gamification_plugin/api.rb b/lib/gamification_plugin/api.rb new file mode 100644 index 0000000..db37378 --- /dev/null +++ b/lib/gamification_plugin/api.rb @@ -0,0 +1,21 @@ +class GamificationPlugin::API < Grape::API + + resource :gamification_plugin do + + resource :my do + get 'badges' do + present current_person.badges + end + end + + resource :people do + get ':id/badges' do + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) + return not_found! if person.blank? + present person.badges + end + + end + end +end + diff --git a/lib/gamification_plugin/entities.rb b/lib/gamification_plugin/entities.rb new file mode 100644 index 0000000..674fcc4 --- /dev/null +++ b/lib/gamification_plugin/entities.rb @@ -0,0 +1,10 @@ +#module Noosfero +# module API +# module Entities +# class Badge < Entity +# root 'badges', 'badge' +# expose :name +# end +# end +# end +#end diff --git a/test/unit/api_test.rb b/test/unit/api_test.rb new file mode 100644 index 0000000..48f50e4 --- /dev/null +++ b/test/unit/api_test.rb @@ -0,0 +1,42 @@ +require_relative '../test_helper' +require_relative '../../../../test/unit/api/test_helper' + +class APITest < ActiveSupport::TestCase + + def setup + login_api + environment = Environment.default + environment.enable_plugin(GamificationPlugin) + end + + should 'get badges my own badges' do + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') + person.add_badge(badge.id) + get "/api/v1/gamification_plugin/my/badges?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 'test_badge', json.first['name'] + end + + should 'get badges of the public person' do + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') + another_person = create(User, :environment => environment).person + another_person.visible=true + another_person.save + another_person.add_badge(badge.id) + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 'test_badge', json.first['name'] + end + + should 'not get badges of the private person' do + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') + another_person = create(User, :environment_id => environment.id).person + another_person.visible=false + another_person.save + another_person.add_badge(badge.id) + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 404, last_response.status + end + +end -- libgit2 0.21.2