Commit c2c346b464305fcf3c503bf9736aae1d82ed84da

Authored by Leandro Santos
1 parent 6d35008c

adding badge endpoint

lib/gamification_plugin.rb
... ... @@ -58,6 +58,9 @@ class GamificationPlugin < Noosfero::Plugin
58 58 { GamificationPlugin::RankingBlock => {}}
59 59 end
60 60  
  61 + def self.api_mount_points
  62 + [GamificationPlugin::API]
  63 + end
61 64  
62 65 ActionDispatch::Reloader.to_prepare do
63 66 Merit.setup do |config|
... ...
lib/gamification_plugin/api.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +class GamificationPlugin::API < Grape::API
  2 +
  3 + resource :gamification_plugin do
  4 +
  5 + resource :my do
  6 + get 'badges' do
  7 + present current_person.badges
  8 + end
  9 + end
  10 +
  11 + resource :people do
  12 + get ':id/badges' do
  13 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  14 + return not_found! if person.blank?
  15 + present person.badges
  16 + end
  17 +
  18 + end
  19 + end
  20 +end
  21 +
... ...
lib/gamification_plugin/entities.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +#module Noosfero
  2 +# module API
  3 +# module Entities
  4 +# class Badge < Entity
  5 +# root 'badges', 'badge'
  6 +# expose :name
  7 +# end
  8 +# end
  9 +# end
  10 +#end
... ...
test/unit/api_test.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +require_relative '../test_helper'
  2 +require_relative '../../../../test/unit/api/test_helper'
  3 +
  4 +class APITest < ActiveSupport::TestCase
  5 +
  6 + def setup
  7 + login_api
  8 + environment = Environment.default
  9 + environment.enable_plugin(GamificationPlugin)
  10 + end
  11 +
  12 + should 'get badges my own badges' do
  13 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  14 + person.add_badge(badge.id)
  15 + get "/api/v1/gamification_plugin/my/badges?#{params.to_query}"
  16 + json = JSON.parse(last_response.body)
  17 + assert_equal 'test_badge', json.first['name']
  18 + end
  19 +
  20 + should 'get badges of the public person' do
  21 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  22 + another_person = create(User, :environment => environment).person
  23 + another_person.visible=true
  24 + another_person.save
  25 + another_person.add_badge(badge.id)
  26 + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}"
  27 + json = JSON.parse(last_response.body)
  28 + assert_equal 'test_badge', json.first['name']
  29 + end
  30 +
  31 + should 'not get badges of the private person' do
  32 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  33 + another_person = create(User, :environment_id => environment.id).person
  34 + another_person.visible=false
  35 + another_person.save
  36 + another_person.add_badge(badge.id)
  37 + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}"
  38 + json = JSON.parse(last_response.body)
  39 + assert_equal 404, last_response.status
  40 + end
  41 +
  42 +end
... ...