Commit c584e1d2b6393e21a9bd1c163b0ccd7a8df728ea

Authored by Leandro Santos
2 parents 68b1e8b7 c218a18f

Merge branch 'new_branch_name'

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,31 @@
  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 + get 'level' do
  10 + {:level => current_person.level, :percent => current_person.gamification_plugin_level_percent}
  11 + end
  12 +
  13 + end
  14 +
  15 + resource :people do
  16 + get ':id/badges' do
  17 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  18 + return not_found! if person.blank?
  19 + present person.badges
  20 + end
  21 +
  22 + get ':id/level' do
  23 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  24 + return not_found! if person.blank?
  25 + {:level => person.level, :percent => person.gamification_plugin_level_percent}
  26 + end
  27 +
  28 + end
  29 + end
  30 +end
  31 +
... ...
test/unit/api_test.rb 0 → 100644
... ... @@ -0,0 +1,70 @@
  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 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 my level' do
  21 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  22 + person.add_badge(badge.id)
  23 + get "/api/v1/gamification_plugin/my/level?#{params.to_query}"
  24 + json = JSON.parse(last_response.body)
  25 + assert_not_nil json['level']
  26 + assert_not_nil json['percent']
  27 + end
  28 +
  29 + should 'get badges of the public person' do
  30 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  31 + another_person = create(User, :environment => environment).person
  32 + another_person.visible=true
  33 + another_person.save
  34 + another_person.add_badge(badge.id)
  35 + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}"
  36 + json = JSON.parse(last_response.body)
  37 + assert_equal 'test_badge', json.first['name']
  38 + end
  39 +
  40 + should 'get level of the public person' do
  41 + another_person = create(User, :environment => environment).person
  42 + another_person.visible=true
  43 + another_person.save
  44 + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
  45 + json = JSON.parse(last_response.body)
  46 + assert_not_nil json['level']
  47 + assert_not_nil json['percent']
  48 + end
  49 +
  50 + should 'not get badges of the private person' do
  51 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  52 + another_person = create(User, :environment_id => environment.id).person
  53 + another_person.visible=false
  54 + another_person.save
  55 + another_person.add_badge(badge.id)
  56 + get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}"
  57 + json = JSON.parse(last_response.body)
  58 + assert_equal 404, last_response.status
  59 + end
  60 +
  61 + should 'not get level of the private person' do
  62 + another_person = create(User, :environment_id => environment.id).person
  63 + another_person.visible=false
  64 + another_person.save
  65 + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
  66 + json = JSON.parse(last_response.body)
  67 + assert_equal 404, last_response.status
  68 + end
  69 +
  70 +end
... ...