Commit 31c4004d8648940f10b0d055b87232cc2c4ab785

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent f48831f3

adding tests for community api

lib/api/v1/communities.rb
... ... @@ -29,8 +29,11 @@ module API
29 29 end
30 30  
31 31 resource :people do
  32 +
32 33 segment '/:person_id' do
  34 +
33 35 resource :communities do
  36 +
34 37 get do
35 38 person = environment.people.find(params[:person_id])
36 39 communities = select_filtered_collection_of(person, 'communities', params)
... ... @@ -38,13 +41,8 @@ module API
38 41 present communities, :with => Entities::Community
39 42 end
40 43  
41   -# get ':id' do
42   -# person = environment.people.find(params[:person_id])
43   -# article = find_article(person.articles, params[:id])
44   -# present article, :with => Entities::Article
45   -# end
46   -
47 44 end
  45 +
48 46 end
49 47  
50 48 end
... ...
test/unit/api/communities_test.rb
... ... @@ -31,6 +31,25 @@ class CommunitiesTest < ActiveSupport::TestCase
31 31 assert_equal [community1.id], json['communities'].map {|c| c['id']}
32 32 end
33 33  
  34 + should 'not list private communities without permission' do
  35 + community1 = fast_create(Community)
  36 + fast_create(Community, :public_profile => false)
  37 +
  38 + get "/api/v1/communities?#{params.to_query}"
  39 + json = JSON.parse(last_response.body)
  40 + assert_equal [community1.id], json['communities'].map {|c| c['id']}
  41 + end
  42 +
  43 + should 'list private community for members' do
  44 + c1 = fast_create(Community)
  45 + c2 = fast_create(Community, :public_profile => false)
  46 + c1.add_member(person)
  47 +
  48 + get "/api/v1/communities?#{params.to_query}"
  49 + json = JSON.parse(last_response.body)
  50 + assert_equivalent [c1.id, c2.id], json['communities'].map {|c| c['id']}
  51 + end
  52 +
34 53 should 'not get invisible community' do
35 54 community = fast_create(Community, :visible => false)
36 55  
... ...
test/unit/api/test_helper.rb
... ... @@ -11,12 +11,13 @@ class ActiveSupport::TestCase
11 11 def login_api
12 12 @user = User.create!(:login => 'testapi', :password => 'testapi', :password_confirmation => 'testapi', :email => 'test@test.org', :environment => Environment.default)
13 13 @user.activate
  14 + @person = @user.person
14 15  
15 16 post "/api/v1/login?login=testapi&password=testapi"
16 17 json = JSON.parse(last_response.body)
17 18 @private_token = json["private_token"]
18 19 @params = {:private_token => @private_token}
19 20 end
20   - attr_accessor :private_token, :user, :params
  21 + attr_accessor :private_token, :user, :person, :params
21 22  
22 23 end
... ...