cirandas.net

ref: master

test/api/enterprises_test.rb


  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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require_relative 'test_helper'

class EnterprisesTest < ActiveSupport::TestCase

  def setup
    Enterprise.delete_all
    create_and_activate_user
  end

  should 'logger user list only enterprises' do
    login_api
    community = fast_create(Community, :environment_id => environment.id) # should not list this community
    enterprise = fast_create(Enterprise, :environment_id => environment.id, :public_profile => true)
    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_includes json.map {|c| c['id']}, enterprise.id
    assert_not_includes json.map {|c| c['id']}, community.id
  end

  should 'anonymous list only enterprises' do
    community = fast_create(Community, :environment_id => environment.id) # should not list this community
    enterprise = fast_create(Enterprise, :environment_id => environment.id, :public_profile => true)
    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_includes json.map {|c| c['id']}, enterprise.id
    assert_not_includes json.map {|c| c['id']}, community.id
  end

  should 'anonymous list all enterprises' do
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id, :public_profile => true)
    enterprise2 = fast_create(Enterprise, :environment_id => environment.id)
    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [enterprise1.id, enterprise2.id], json.map {|c| c['id']}
  end

  should 'logger user list all enterprises' do
    login_api
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id, :public_profile => true)
    enterprise2 = fast_create(Enterprise, :environment_id => environment.id)
    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [enterprise1.id, enterprise2.id], json.map {|c| c['id']}
  end

  should 'not list invisible enterprises' do
    login_api
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :visible => false)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [enterprise1.id], json.map {|c| c['id']}
  end

  should 'not, anonymous list invisible enterprises' do
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :visible => false)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [enterprise1.id], json.map {|c| c['id']}
  end

  should 'not, logged user list invisible enterprises' do
    login_api
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :visible => false)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [enterprise1.id], json.map {|c| c['id']}
  end

  should 'anonymous list private enterprises' do
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id)
    enterprise2 = fast_create(Enterprise, :environment_id => environment.id, :public_profile => false)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [enterprise1.id, enterprise2.id], json.map {|c| c['id']}
  end

  should 'logged user list private enterprises' do
    login_api
    enterprise1 = fast_create(Enterprise, :environment_id => environment.id)
    enterprise2 = fast_create(Enterprise, :environment_id => environment.id, :public_profile => false)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [enterprise1.id, enterprise2.id], json.map {|c| c['id']}
  end

  should 'logged user list private enterprise for members' do
    login_api
    c1 = fast_create(Enterprise, :environment_id => environment.id)
    c2 = fast_create(Enterprise, :environment_id => environment.id, :public_profile => false)
    c2.add_member(person)

    get "/api/v1/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [c1.id, c2.id], json.map {|c| c['id']}
  end

  should 'anonymous get enterprise' do
    enterprise = fast_create(Enterprise, :environment_id => environment.id)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal enterprise.id, json['id']
  end

  should 'logged user get enterprise' do
    login_api
    enterprise = fast_create(Enterprise, :environment_id => environment.id)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal enterprise.id, json['id']
  end

  should 'not, logger user get invisible enterprise' do
    login_api
    enterprise = fast_create(Enterprise, :visible => false)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    assert_equal Api::Status::NOT_FOUND, last_response.status
  end

  should 'not, anonymous get invisible enterprise' do
    enterprise = fast_create(Enterprise, :visible => false)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    assert_equal Api::Status::NOT_FOUND, last_response.status
  end

  should 'not get private enterprises without permission' do
    login_api
    enterprise = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :environment_id => environment.id, :public_profile => false)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal enterprise.id, json['id']
  end

  should 'not, anonymous get private enterprises' do
    enterprise = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :environment_id => environment.id, :public_profile => false)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal enterprise.id, json['id']
  end

  should 'get private enterprise for members' do
    login_api
    enterprise = fast_create(Enterprise, :public_profile => false)
    enterprise.add_member(person)

    get "/api/v1/enterprises/#{enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal enterprise.id, json['id']
  end

  should 'list person enterprises' do
    login_api
    enterprise = fast_create(Enterprise, :environment_id => environment.id)
    fast_create(Enterprise, :environment_id => environment.id)
    enterprise.add_member(person)

    get "/api/v1/people/#{person.id}/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [enterprise.id], json.map {|c| c['id']}
  end

  should 'not list person enterprises invisible' do
    login_api
    c1 = fast_create(Enterprise, :environment_id => environment.id)
    c2 = fast_create(Enterprise, :environment_id => environment.id, :visible => false)
    c1.add_member(person)
    c2.add_member(person)

    get "/api/v1/people/#{person.id}/enterprises?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [c1.id], json.map {|c| c['id']}
  end

  should 'display public custom fields to anonymous' do
    CustomField.create!(:name => "Rating", :format => "string", :customized_type => "Enterprise", :active => true, :environment => Environment.default)
    some_enterprise = fast_create(Enterprise)
    some_enterprise.custom_values = { "Rating" => { "value" => "Five stars", "public" => "true"} }
    some_enterprise.save!

    get "/api/v1/enterprises/#{some_enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert json['additional_data'].has_key?('Rating')
    assert_equal "Five stars", json['additional_data']['Rating']
  end

  should 'not display public custom fields to anonymous' do
    CustomField.create!(:name => "Rating", :format => "string", :customized_type => "Enterprise", :active => true, :environment => Environment.default)
    some_enterprise = fast_create(Enterprise)
    some_enterprise.custom_values = { "Rating" => { "value" => "Five stars", "public" => "false"} }
    some_enterprise.save!

    get "/api/v1/enterprises/#{some_enterprise.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    refute json['additional_data'].has_key?('Rating')
  end

end