profiles_test.rb
3.26 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
require_relative 'test_helper'
class ProfilesTest < ActiveSupport::TestCase
def setup
Profile.delete_all
end
should 'list all profiles' do
login_api
person1 = fast_create(Person)
person2 = fast_create(Person)
community = fast_create(Community)
get "/api/v1/profiles?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [person.id, person1.id, person2.id, community.id], json.map {|p| p['id']}
end
should 'get person from profile id' do
login_api
some_person = fast_create(Person)
get "/api/v1/profiles/#{some_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal some_person.id, json['id']
end
should 'get community from profile id' do
login_api
community = fast_create(Community)
get "/api/v1/profiles/#{community.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal community.id, json['id']
end
group_kinds = %w(community enterprise)
group_kinds.each do |kind|
should "delete #{kind} from profile id with permission" do
profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
give_permission(@person, 'destroy_profile', profile)
assert_not_nil Profile.find_by_id profile.id
delete "/api/v1/profiles/#{profile.id}?#{params.to_query}"
assert_equal 200, last_response.status
assert_nil Profile.find_by_id profile.id
end
should "not delete #{kind} from profile id without permission" do
profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
assert_not_nil Profile.find_by_id profile.id
delete "/api/v1/profiles/#{profile.id}?#{params.to_query}"
assert_equal 403, last_response.status
assert_not_nil Profile.find_by_id profile.id
end
end
should 'person delete itself' do
delete "/api/v1/profiles/#{@person.id}?#{params.to_query}"
assert_equal 200, last_response.status
assert_nil Profile.find_by_id @person.id
end
should 'only admin delete other people' do
profile = fast_create(Person, :environment_id => environment.id)
assert_not_nil Profile.find_by_id profile.id
delete "/api/v1/profiles/#{profile.id}?#{params.to_query}"
assert_equal 403, last_response.status
assert_not_nil Profile.find_by_id profile.id
environment.add_admin(@person)
delete "/api/v1/profiles/#{profile.id}?#{params.to_query}"
assert_equal 200, last_response.status
assert_nil Profile.find_by_id profile.id
end
should 'anonymous list all profiles' do
person1 = fast_create(Person)
person2 = fast_create(Person)
community = fast_create(Community)
get "/api/v1/profiles"
json = JSON.parse(last_response.body)
assert_equivalent [person1.id, person2.id, community.id], json.map {|p| p['id']}
end
should 'anonymous get person from profile id' do
some_person = fast_create(Person)
get "/api/v1/profiles/#{some_person.id}"
json = JSON.parse(last_response.body)
assert_equal some_person.id, json['id']
end
should 'anonymous get community from profile id' do
community = fast_create(Community)
get "/api/v1/profiles/#{community.id}"
json = JSON.parse(last_response.body)
assert_equal community.id, json['id']
end
end