Skip to content

Commit b9a7ee1

Browse files
kescherCodeClearlyClaire
authored andcommitted
Merge commit from fork
* Fix domain blocks/rationales being visible to unapproved/unconfirmed users * Fix domain blocks/rationales being visible to suspended users Co-authored-by: Claire <claire.github-309c@sitedethib.com> * Allow moved users to view domain blocks * Add authorization specs for `/api/v1/instance/domain_blocks` spec * Fix tests * Fix incorrect test setup --------- Co-authored-by: Claire <claire.github-309c@sitedethib.com>
1 parent d074683 commit b9a7ee1

2 files changed

Lines changed: 94 additions & 9 deletions

File tree

app/controllers/api/v1/instances/domain_blocks_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def show_domain_blocks_for_all?
3131
end
3232

3333
def show_domain_blocks_to_user?
34-
Setting.show_domain_blocks == 'users' && user_signed_in?
34+
Setting.show_domain_blocks == 'users' && user_signed_in? && current_user.functional_or_moved?
3535
end
3636

3737
def set_domain_blocks
@@ -47,6 +47,6 @@ def always_show_rationale?
4747
end
4848

4949
def show_rationale_for_user?
50-
Setting.show_domain_blocks_rationale == 'users' && user_signed_in?
50+
Setting.show_domain_blocks_rationale == 'users' && user_signed_in? && current_user.functional_or_moved?
5151
end
5252
end

spec/requests/api/v1/instances/domain_blocks_spec.rb

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
RSpec.describe 'Domain Blocks' do
66
describe 'GET /api/v1/instance/domain_blocks' do
7-
before do
8-
Fabricate(:domain_block)
9-
end
7+
let(:user) { Fabricate(:user) }
8+
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id).token }
9+
10+
before { Fabricate(:domain_block) }
1011

1112
context 'with domain blocks set to all' do
1213
before { Setting.show_domain_blocks = 'all' }
@@ -30,11 +31,95 @@
3031
context 'with domain blocks set to users' do
3132
before { Setting.show_domain_blocks = 'users' }
3233

33-
it 'returns http not found' do
34-
get api_v1_instance_domain_blocks_path
34+
context 'without authentication token' do
35+
it 'returns http not found' do
36+
get api_v1_instance_domain_blocks_path
3537

36-
expect(response)
37-
.to have_http_status(404)
38+
expect(response)
39+
.to have_http_status(404)
40+
end
41+
end
42+
43+
context 'with authentication token' do
44+
context 'with unapproved user' do
45+
before { user.update(approved: false) }
46+
47+
it 'returns http not found' do
48+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
49+
50+
expect(response)
51+
.to have_http_status(404)
52+
end
53+
end
54+
55+
context 'with unconfirmed user' do
56+
before { user.update(confirmed_at: nil) }
57+
58+
it 'returns http not found' do
59+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
60+
61+
expect(response)
62+
.to have_http_status(404)
63+
end
64+
end
65+
66+
context 'with disabled user' do
67+
before { user.update(disabled: true) }
68+
69+
it 'returns http not found' do
70+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
71+
72+
expect(response)
73+
.to have_http_status(404)
74+
end
75+
end
76+
77+
context 'with suspended user' do
78+
before { user.account.update(suspended_at: Time.zone.now) }
79+
80+
it 'returns http not found' do
81+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
82+
83+
expect(response)
84+
.to have_http_status(403)
85+
end
86+
end
87+
88+
context 'with moved user' do
89+
before { user.account.update(moved_to_account_id: Fabricate(:account).id) }
90+
91+
it 'returns http success' do
92+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
93+
94+
expect(response)
95+
.to have_http_status(200)
96+
97+
expect(response.content_type)
98+
.to start_with('application/json')
99+
100+
expect(response.parsed_body)
101+
.to be_present
102+
.and(be_an(Array))
103+
.and(have_attributes(size: 1))
104+
end
105+
end
106+
107+
context 'with normal user' do
108+
it 'returns http success' do
109+
get api_v1_instance_domain_blocks_path, headers: { 'Authorization' => "Bearer #{token}" }
110+
111+
expect(response)
112+
.to have_http_status(200)
113+
114+
expect(response.content_type)
115+
.to start_with('application/json')
116+
117+
expect(response.parsed_body)
118+
.to be_present
119+
.and(be_an(Array))
120+
.and(have_attributes(size: 1))
121+
end
122+
end
38123
end
39124
end
40125

0 commit comments

Comments
 (0)