Commit f49ee41c9416983e5f79e91df408a3f1877bcbb3
Committed by
Rodrigo Souto
1 parent
44dd0a25
Exists in
master
and in
29 other branches
adding enterprises endpoints
Showing
3 changed files
with
104 additions
and
9 deletions
Show diff stats
lib/api/entities.rb
... | ... | @@ -32,7 +32,9 @@ module API |
32 | 32 | end |
33 | 33 | |
34 | 34 | class Person < Profile;end; |
35 | - class Enterprise < Profile;end; | |
35 | + class Enterprise < Profile | |
36 | + root 'enterprises', 'enterprise' | |
37 | + end | |
36 | 38 | class Community < Profile |
37 | 39 | root 'communities', 'community' |
38 | 40 | expose :description | ... | ... |
lib/api/v1/enterprises.rb
... | ... | @@ -17,16 +17,38 @@ module API |
17 | 17 | # GET /enterprises?reference_id=10&limit=10&oldest |
18 | 18 | get do |
19 | 19 | enterprises = select_filtered_collection_of(environment, 'enterprises', params) |
20 | + enterprises = enterprises.visible_for_person(current_person) | |
20 | 21 | present enterprises, :with => Entities::Enterprise |
21 | 22 | end |
22 | 23 | |
23 | - desc "Return one environment by id" | |
24 | + desc "Return one enterprise by id" | |
24 | 25 | get ':id' do |
25 | - present environment.enterprises.find(params[:id]), :with => Entities::Enterprise | |
26 | + enterprise = environment.enterprises.visible.find_by_id(params[:id]) | |
27 | + present enterprise, :with => Entities::Enterprise | |
26 | 28 | end |
27 | 29 | |
28 | 30 | end |
29 | 31 | |
32 | + resource :people do | |
33 | + | |
34 | + segment '/:person_id' do | |
35 | + | |
36 | + resource :enterprises do | |
37 | + | |
38 | + get do | |
39 | + person = environment.people.find(params[:person_id]) | |
40 | + enterprises = select_filtered_collection_of(person, 'enterprises', params) | |
41 | + enterprises = enterprises.visible | |
42 | + present enterprises, :with => Entities::Enterprise | |
43 | + end | |
44 | + | |
45 | + end | |
46 | + | |
47 | + end | |
48 | + | |
49 | + end | |
50 | + | |
51 | + | |
30 | 52 | end |
31 | 53 | end |
32 | 54 | end | ... | ... |
test/unit/api/enterprises_test.rb
... | ... | @@ -6,24 +6,95 @@ class EnterprisesTest < ActiveSupport::TestCase |
6 | 6 | login_api |
7 | 7 | end |
8 | 8 | |
9 | - should 'list enterprises' do | |
10 | - enterprise1 = fast_create(Enterprise) | |
9 | + should 'list all enterprises' do | |
10 | + enterprise1 = fast_create(Enterprise, :public_profile => true) | |
11 | 11 | enterprise2 = fast_create(Enterprise) |
12 | + get "/api/v1/enterprises?#{params.to_query}" | |
13 | + json = JSON.parse(last_response.body) | |
14 | + assert_equivalent [enterprise1.id, enterprise2.id], json['enterprises'].map {|c| c['id']} | |
15 | + end | |
16 | + | |
17 | + should 'not list invisible enterprises' do | |
18 | + enterprise1 = fast_create(Enterprise) | |
19 | + fast_create(Enterprise, :visible => false) | |
20 | + | |
21 | + get "/api/v1/enterprises?#{params.to_query}" | |
22 | + json = JSON.parse(last_response.body) | |
23 | + assert_equal [enterprise1.id], json['enterprises'].map {|c| c['id']} | |
24 | + end | |
25 | + | |
26 | + should 'not list private enterprises without permission' do | |
27 | + enterprise1 = fast_create(Enterprise) | |
28 | + fast_create(Enterprise, :public_profile => false) | |
29 | + | |
30 | + get "/api/v1/enterprises?#{params.to_query}" | |
31 | + json = JSON.parse(last_response.body) | |
32 | + assert_equal [enterprise1.id], json['enterprises'].map {|c| c['id']} | |
33 | + end | |
34 | + | |
35 | + should 'list private enterprise for members' do | |
36 | + c1 = fast_create(Enterprise) | |
37 | + c2 = fast_create(Enterprise, :public_profile => false) | |
38 | + c2.add_member(person) | |
12 | 39 | |
13 | 40 | get "/api/v1/enterprises?#{params.to_query}" |
14 | 41 | json = JSON.parse(last_response.body) |
42 | + assert_equivalent [c1.id, c2.id], json['enterprises'].map {|c| c['id']} | |
43 | + end | |
44 | + | |
45 | + should 'get enterprise' do | |
46 | + enterprise = fast_create(Enterprise) | |
47 | + | |
48 | + get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}" | |
49 | + json = JSON.parse(last_response.body) | |
50 | + assert_equal enterprise.id, json['enterprise']['id'] | |
51 | + end | |
52 | + | |
53 | + should 'not get invisible enterprise' do | |
54 | + enterprise = fast_create(Enterprise, :visible => false) | |
15 | 55 | |
16 | - assert_includes json.map {|c| c['id']}, enterprise1.id | |
17 | - assert_includes json.map {|c| c['id']}, enterprise2.id | |
56 | + get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}" | |
57 | + json = JSON.parse(last_response.body) | |
58 | + assert json['enterprise'].blank? | |
18 | 59 | end |
19 | 60 | |
20 | - should 'return one enterprise by id' do | |
61 | + should 'not get private enterprises without permission' do | |
21 | 62 | enterprise = fast_create(Enterprise) |
63 | + fast_create(Enterprise, :public_profile => false) | |
64 | + | |
65 | + get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}" | |
66 | + json = JSON.parse(last_response.body) | |
67 | + assert_equal enterprise.id, json['enterprise']['id'] | |
68 | + end | |
69 | + | |
70 | + should 'get private enterprise for members' do | |
71 | + enterprise = fast_create(Enterprise, :public_profile => false) | |
72 | + enterprise.add_member(person) | |
22 | 73 | |
23 | 74 | get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}" |
24 | 75 | json = JSON.parse(last_response.body) |
76 | + assert_equal enterprise.id, json['enterprise']['id'] | |
77 | + end | |
78 | + | |
79 | + should 'list person enterprises' do | |
80 | + enterprise = fast_create(Enterprise) | |
81 | + fast_create(Enterprise) | |
82 | + enterprise.add_member(person) | |
83 | + | |
84 | + get "/api/v1/people/#{person.id}/enterprises?#{params.to_query}" | |
85 | + json = JSON.parse(last_response.body) | |
86 | + assert_equivalent [enterprise.id], json['enterprises'].map {|c| c['id']} | |
87 | + end | |
25 | 88 | |
26 | - assert_equal enterprise.id, json['id'] | |
89 | + should 'not list person enterprises invisible' do | |
90 | + c1 = fast_create(Enterprise) | |
91 | + c2 = fast_create(Enterprise, :visible => false) | |
92 | + c1.add_member(person) | |
93 | + c2.add_member(person) | |
94 | + | |
95 | + get "/api/v1/people/#{person.id}/enterprises?#{params.to_query}" | |
96 | + json = JSON.parse(last_response.body) | |
97 | + assert_equivalent [c1.id], json['enterprises'].map {|c| c['id']} | |
27 | 98 | end |
28 | 99 | |
29 | 100 | end | ... | ... |