Commit 8e1ebf5b8a87dc8a8ad3f976ebfdc33c8cb967d4
1 parent
f1587a22
Exists in
fix_sign_up_form
adding more unit tests for activities api endpoint
Showing
2 changed files
with
66 additions
and
13 deletions
Show diff stats
app/api/entities.rb
... | ... | @@ -296,6 +296,8 @@ module Api |
296 | 296 | class Activity < Entity |
297 | 297 | root 'activities', 'activity' |
298 | 298 | expose :id, :created_at, :updated_at |
299 | + expose :user, :using => Profile | |
300 | + | |
299 | 301 | expose :target do |activity, opts| |
300 | 302 | type_map = {Profile => ::Profile, ArticleBase => ::Article}.find {|h| activity.target.kind_of?(h.last)} |
301 | 303 | type_map.first.represent(activity.target) unless type_map.nil? |
... | ... | @@ -303,12 +305,9 @@ module Api |
303 | 305 | expose :params, :if => lambda { |activity, options| activity.kind_of?(ActionTracker::Record)} |
304 | 306 | expose :content, :if => lambda { |activity, options| activity.kind_of?(Scrap)} |
305 | 307 | expose :verb do |activity, options| |
306 | - activity.kind_of?(Scrap) ? 'scrap' : activity.verb | |
308 | + activity.kind_of?(Scrap) ? 'leave_scrap' : activity.verb | |
307 | 309 | end |
308 | 310 | |
309 | - expose :user, :using => Profile# do |activity, opts| | |
310 | -# activity.kind_of?(Scrap) ? activity.sender : activity.user | |
311 | -# end | |
312 | 311 | end |
313 | 312 | |
314 | 313 | class Role < Entity | ... | ... |
test/api/activities_test.rb
... | ... | @@ -8,7 +8,7 @@ class ActivitiesTest < ActiveSupport::TestCase |
8 | 8 | end |
9 | 9 | |
10 | 10 | should 'get own activities' do |
11 | - create_activity(person) | |
11 | + create_activity(:target => person) | |
12 | 12 | |
13 | 13 | get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" |
14 | 14 | json = JSON.parse(last_response.body) |
... | ... | @@ -19,7 +19,7 @@ class ActivitiesTest < ActiveSupport::TestCase |
19 | 19 | |
20 | 20 | should 'not get private community activities' do |
21 | 21 | community = fast_create(Community, :public_profile => false) |
22 | - create_activity(community) | |
22 | + create_activity(:target => community) | |
23 | 23 | |
24 | 24 | get "/api/v1/profiles/#{community.id}/activities?#{params.to_query}" |
25 | 25 | json = JSON.parse(last_response.body) |
... | ... | @@ -40,7 +40,7 @@ class ActivitiesTest < ActiveSupport::TestCase |
40 | 40 | |
41 | 41 | should 'get community activities for member' do |
42 | 42 | community = fast_create(Community) |
43 | - create_activity(community) | |
43 | + create_activity(:target => community) | |
44 | 44 | community.add_member(person) |
45 | 45 | |
46 | 46 | get "/api/v1/profiles/#{community.id}/activities?#{params.to_query}" |
... | ... | @@ -50,7 +50,7 @@ class ActivitiesTest < ActiveSupport::TestCase |
50 | 50 | |
51 | 51 | should 'not get other person activities' do |
52 | 52 | other_person = fast_create(Person) |
53 | - create_activity(other_person) | |
53 | + create_activity(:target => other_person) | |
54 | 54 | |
55 | 55 | get "/api/v1/profiles/#{other_person.id}/activities?#{params.to_query}" |
56 | 56 | json = JSON.parse(last_response.body) |
... | ... | @@ -61,7 +61,7 @@ class ActivitiesTest < ActiveSupport::TestCase |
61 | 61 | should 'get friend activities' do |
62 | 62 | other_person = fast_create(Person) |
63 | 63 | other_person.add_friend(person) |
64 | - create_activity(other_person) | |
64 | + create_activity(:target => other_person) | |
65 | 65 | |
66 | 66 | get "/api/v1/profiles/#{other_person.id}/activities?#{params.to_query}" |
67 | 67 | json = JSON.parse(last_response.body) |
... | ... | @@ -70,16 +70,70 @@ class ActivitiesTest < ActiveSupport::TestCase |
70 | 70 | |
71 | 71 | should 'get activities for non logged user in a public community' do |
72 | 72 | community = fast_create(Community) |
73 | - create_activity(community) | |
73 | + create_activity(:target => community) | |
74 | 74 | community.add_member(person) |
75 | 75 | get "/api/v1/profiles/#{community.id}/activities?#{params.to_query}" |
76 | 76 | json = JSON.parse(last_response.body) |
77 | 77 | assert_equivalent community.activities.map(&:activity).map(&:id), json["activities"].map{|c| c["id"]} |
78 | 78 | end |
79 | 79 | |
80 | - def create_activity(target) | |
81 | - activity = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => target | |
82 | - ProfileActivity.create! profile_id: target.id, activity: activity | |
80 | + should 'not crash api if an scrap activity is in the list' do | |
81 | + create_activity(:target => person) | |
82 | + create(Scrap, :sender_id => person.id, :receiver_id => person.id) | |
83 | + | |
84 | + assert_nothing_raised NoMethodError do | |
85 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
86 | + end | |
87 | + end | |
88 | + | |
89 | + should 'scrap activity be returned in acitivities list' do | |
90 | + create_activity(:target => person) | |
91 | + create(Scrap, :sender_id => person.id, :receiver_id => person.id) | |
92 | + | |
93 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
94 | + json = JSON.parse(last_response.body) | |
95 | + | |
96 | + assert_equivalent person.activities.map(&:activity).map(&:id), json["activities"].map{|c| c["id"]} | |
97 | + end | |
98 | + | |
99 | + should 'always return the activity verb parameter' do | |
100 | + ActionTracker::Record.destroy_all | |
101 | + ProfileActivity.destroy_all | |
102 | + create_activity(:target => person) | |
103 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
104 | + json = JSON.parse(last_response.body) | |
105 | + assert_equal 'create_article', json["activities"].last['verb'] | |
106 | + end | |
107 | + | |
108 | + should 'scrap activity return leave_scrap verb' do | |
109 | + ActionTracker::Record.destroy_all | |
110 | + create(TinyMceArticle, :name => 'Tracked Article 1', :profile_id => person.id) | |
111 | + create(Scrap, :sender_id => person.id, :receiver_id => person.id) | |
112 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
113 | + json = JSON.parse(last_response.body) | |
114 | + assert_equivalent ['create_article', 'leave_scrap'], json["activities"].map{|a|a['verb']} | |
115 | + end | |
116 | + | |
117 | + should 'the content be returned in scrap activities' do | |
118 | + ActionTracker::Record.destroy_all | |
119 | + content = 'some content' | |
120 | + create(Scrap, :sender_id => person.id, :receiver_id => person.id, :content => content) | |
121 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
122 | + json = JSON.parse(last_response.body) | |
123 | + assert_equal content, json["activities"].last['content'] | |
124 | + end | |
125 | + | |
126 | + should 'not return the content in other kind of activities except scrap' do | |
127 | + ActionTracker::Record.destroy_all | |
128 | + create_activity(:target => person) | |
129 | + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}" | |
130 | + json = JSON.parse(last_response.body) | |
131 | + assert_nil json["activities"].last['content'] | |
132 | + end | |
133 | + | |
134 | + def create_activity(params = {}) | |
135 | + params[:verb] ||= 'create_article' | |
136 | + ActionTracker::Record.create! :verb => params[:verb], :user => person, :target => params[:target] | |
83 | 137 | end |
84 | 138 | |
85 | 139 | end | ... | ... |