Commit c2c346b464305fcf3c503bf9736aae1d82ed84da
1 parent
6d35008c
Exists in
master
and in
1 other branch
adding badge endpoint
Showing
4 changed files
with
76 additions
and
0 deletions
Show diff stats
lib/gamification_plugin.rb
@@ -58,6 +58,9 @@ class GamificationPlugin < Noosfero::Plugin | @@ -58,6 +58,9 @@ class GamificationPlugin < Noosfero::Plugin | ||
58 | { GamificationPlugin::RankingBlock => {}} | 58 | { GamificationPlugin::RankingBlock => {}} |
59 | end | 59 | end |
60 | 60 | ||
61 | + def self.api_mount_points | ||
62 | + [GamificationPlugin::API] | ||
63 | + end | ||
61 | 64 | ||
62 | ActionDispatch::Reloader.to_prepare do | 65 | ActionDispatch::Reloader.to_prepare do |
63 | Merit.setup do |config| | 66 | Merit.setup do |config| |
@@ -0,0 +1,21 @@ | @@ -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 | + |
@@ -0,0 +1,42 @@ | @@ -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 |