api_test.rb
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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)
GamificationPlugin.gamification_set_rules(@environment)
create_all_point_rules
end
should 'get 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['badges'].first['name']
end
should 'get my level' do
badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
person.add_badge(badge.id)
get "/api/v1/gamification_plugin/my/level?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_not_nil json['level']
assert_not_nil json['percent']
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 'get level of the public person' do
another_person = create(User, :environment => environment).person
another_person.visible=true
another_person.save
get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_not_nil json['level']
assert_not_nil json['percent']
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
should 'not get level of the private person' do
another_person = create(User, :environment_id => environment.id).person
another_person.visible=false
another_person.save
get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
JSON.parse(last_response.body)
assert_equal 404, last_response.status
end
should 'get amount of environment badges grouped by name' do
3.times { GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') }
get "/api/v1/gamification_plugin/badges"
json = JSON.parse(last_response.body)
assert_equal 3, json['test_badge']
end
should 'get my points' do
article = create(TextArticle, :profile_id => @person.id, :author => @person)
create(Comment, :source_id => article.id, :author => fast_create(Person))
get "/api/v1/gamification_plugin/my/points?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points']
end
should 'get my points filtered by type' do
article = create(TextArticle, :profile_id => @person.id, :author => @person)
create(Comment, :source_id => article.id, :author => fast_create(Person))
params[:type] = 'article_author'
get "/api/v1/gamification_plugin/my/points_by_type?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author), json['points']
end
should 'get my points filtered by profile' do
community = fast_create(Community)
create_point_rule_definition('article_author', community)
create(TextArticle, :profile_id => @person.id, :author => @person)
create(TextArticle, :profile_id => community.id, :author => @person)
params[:profile] = community.identifier
get "/api/v1/gamification_plugin/my/points_by_profile?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author), json['points']
end
should 'get my points excluding points earned in profiles' do
community = fast_create(Community)
create_point_rule_definition('article_author', community)
create(TextArticle, :profile_id => @person.id, :author => @person)
create(TextArticle, :profile_id => community.id, :author => @person)
get "/api/v1/gamification_plugin/my/points_out_of_profiles?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 2*default_point_weight(:article_author), json['points']
end
should 'get points of a person' do
article = create(TextArticle, :profile_id => @person.id, :author => @person)
create(Comment, :source_id => article.id, :author => fast_create(Person))
get "/api/v1/gamification_plugin/people/#{person.id}/points?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points']
end
should 'get points of a person filtered by type' do
article = create(TextArticle, :profile_id => @person.id, :author => @person)
create(Comment, :source_id => article.id, :author => fast_create(Person))
params[:type] = 'article_author'
get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_type?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author), json['points']
end
should 'get points of a person filtered by profile' do
community = fast_create(Community)
create_point_rule_definition('article_author', community)
create(TextArticle, :profile_id => @person.id, :author => @person)
create(TextArticle, :profile_id => community.id, :author => @person)
params[:profile] = community.identifier
get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_profile?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal default_point_weight(:article_author), json['points']
end
should 'get points of a person excluding points earned in profiles' do
community = fast_create(Community)
create_point_rule_definition('article_author', community)
create(TextArticle, :profile_id => @person.id, :author => @person)
create(TextArticle, :profile_id => community.id, :author => @person)
get "/api/v1/gamification_plugin/people/#{@person.id}/points_out_of_profiles?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 2*default_point_weight(:article_author), json['points']
end
end