Skip to content

Commit 004d07e

Browse files
Use authenticate_with_role in ref card; update specs
1 parent 082b972 commit 004d07e

6 files changed

Lines changed: 94 additions & 72 deletions

File tree

app/controllers/reference_card_forms_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def validate_recaptcha!
8484
end
8585

8686
def require_admin!
87-
authenticate!
88-
@user_is_admin = current_user.any_role?(Role.stackpass_admin, :framework_admin)
89-
raise Error::ForbiddenError unless @user_is_admin
87+
@user_is_admin = authenticate_with_role!(Role.stackpass_admin, :framework_admin)
9088
end
9189
end

spec/models/user_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
it 'returns false when the user does not have the role' do
180180
user = User.new(uid: '12345')
181181

182-
allow(FrameworkUsers).to receive(:hardcoded_admin?).with('12345').and_return(false)
182+
allow(FrameworkUsers).to receive(:TEST_UID?).with('12345').and_return(false)
183183
allow(FrameworkUsers).to receive(:find_by).with(lcasid: '12345').and_return(nil)
184184

185185
expect(user.role?(:framework_admin)).to be(false)
@@ -203,7 +203,7 @@
203203
it 'returns false when the user has none of the requested roles' do
204204
user = User.new(uid: '12345')
205205

206-
allow(FrameworkUsers).to receive(:hardcoded_admin?).with('12345').and_return(false)
206+
allow(FrameworkUsers).to receive(:TEST_UID?).with('12345').and_return(false)
207207
allow(FrameworkUsers).to receive(:find_by).with(lcasid: '12345').and_return(nil)
208208

209209
expect(user.any_role?(:alma_admin, :framework_admin)).to be(false)
Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
require 'forms_helper'
22

33
describe :forms_proxy_borrower_admin, type: :request do
4-
context 'specs with hardcoded admin' do
5-
before do
6-
mock_login(CalnetHelper::TEST_UID)
7-
end
8-
9-
it 'removes an admin user' do
10-
# First, create the user (directly)
11-
user = FrameworkUsers.create(lcasid: 112_233, name: 'John Doe', role: 'Admin')
12-
Assignment.create(framework_users: user, role: Role.proxyborrow_admin)
13-
14-
# Then, delete via the controller
15-
delete "/forms/proxy-borrower/delete_admin/#{user.id}"
16-
expect(response).to redirect_to(forms_proxy_borrower_admin_users_path)
17-
get(forms_proxy_borrower_admin_users_path)
18-
expect(response.body).to include('Removed John Doe from administrator list')
19-
expect(Assignment.count).to eq(0)
20-
end
21-
22-
it 'adds an admin user' do
23-
post '/forms/proxy-borrower/add_admin', params: { lcasid: '12345678', name: 'Jane Doe' }
24-
25-
expect(response).to redirect_to(forms_proxy_borrower_admin_users_path)
26-
get(forms_proxy_borrower_admin_users_path)
27-
28-
expect(response).to have_http_status(:ok)
29-
expect(response.body).to include('Jane Doe')
30-
31-
created_user = FrameworkUsers.find_by(lcasid: '12345678')
32-
expect(created_user).not_to be_nil
33-
34-
assignment = Assignment.find_by(framework_users_id: created_user.id, role_id: Role.proxyborrow_admin.id)
35-
expect(assignment).not_to be_nil
36-
end
4+
let(:admin_role) { Role.proxyborrow_admin }
5+
6+
before do
7+
mock_login(CalnetHelper::TEST_UID)
8+
end
9+
10+
it 'removes an admin user' do
11+
user = FrameworkUsers.create(lcasid: 112_233, name: 'John Doe', role: 'Admin')
12+
Assignment.create(framework_users: user, role: admin_role)
13+
14+
delete "/forms/proxy-borrower/delete_admin/#{user.id}"
15+
16+
expect(response).to redirect_to(forms_proxy_borrower_admin_users_path)
17+
18+
get forms_proxy_borrower_admin_users_path
19+
20+
expect(response.body).to include('Removed John Doe from administrator list')
21+
expect(Assignment.count).to eq(0)
22+
end
23+
24+
it 'adds an admin user' do
25+
post '/forms/proxy-borrower/add_admin',
26+
params: { lcasid: '12345678', name: 'Jane Doe' }
27+
28+
expect(response).to redirect_to(forms_proxy_borrower_admin_users_path)
29+
30+
get forms_proxy_borrower_admin_users_path
31+
32+
expect(response).to have_http_status(:ok)
33+
expect(response.body).to include('Jane Doe')
34+
35+
created_user = FrameworkUsers.find_by(lcasid: '12345678')
36+
37+
expect(created_user).not_to be_nil
38+
expect(
39+
Assignment.find_by(framework_users: created_user, role: admin_role)
40+
).not_to be_nil
3741
end
3842
end

spec/system/proxy_borrower_admin_system_spec.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,38 @@
33

44
describe :forms_proxy_borrower_admin, type: :system do
55

6-
context 'specs with hardcoded admin' do
6+
context 'specs with proxyborrower admin' do
77
before do
8-
# First create a DSP Rep and assignment
9-
user = FrameworkUsers.create(lcasid: 112_233, name: 'John Doe', role: 'Admin')
10-
@assignment = Assignment.create(framework_users_id: user.id, role_id: Role.proxyborrow_admin.id)
8+
admin = FrameworkUsers.create(
9+
lcasid: CalnetHelper::TEST_UID,
10+
name: 'Test Admin',
11+
role: 'Admin'
12+
)
13+
14+
Assignment.create(
15+
framework_users: admin,
16+
role: Role.proxyborrow_admin
17+
)
18+
19+
user = FrameworkUsers.create(
20+
lcasid: 112_233,
21+
name: 'John Doe',
22+
role: 'Admin'
23+
)
24+
25+
@assignment = Assignment.create(
26+
framework_users: user,
27+
role: Role.proxyborrow_admin
28+
)
1129

12-
# These functions require admin privledges:
1330
mock_login(CalnetHelper::TEST_UID)
1431

15-
# Go to the Admin Users View Page:
1632
visit forms_proxy_borrower_admin_users_path
1733
end
1834

1935
it 'removes an admin user' do
2036
accept_confirm 'Are you sure you want to delete John Doe?' do
21-
click_link 'Remove'
37+
click_link 'Remove', match: :first
2238
end
2339
expect(page).to have_no_content('<div class="col user-col">John Doe')
2440
expect(page).to have_content('Removed John Doe from administrator list')

spec/system/proxy_borrower_dsp_system_spec.rb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
require 'time'
44

55
describe :forms_proxy_borrower_dsp, type: :system do
6-
attr_reader :patron_id
7-
attr_reader :patron
8-
attr_reader :user
9-
106
let(:alma_api_key) { 'totally-fake-key' }
11-
127
let(:field_prefix) { 'proxy_borrower_requests_' }
13-
14-
before(:all) do
15-
# Calculate and define the max date and an invalid date:
16-
@max_date = ProxyBorrowerRequests.max_term
17-
18-
# Thou shalt pass parameters as Dates, since we use native date fields now:
19-
@invalid_date = Time.zone.today - 1.day
20-
end
8+
let(:max_date) { ProxyBorrowerRequests.max_term }
9+
let(:invalid_date) { Time.zone.today - 1.day }
10+
let(:patron_id) { Alma::Type.sample_id_for(Alma::Type::UNDERGRAD_SLE) }
2111

2212
before do
23-
@patron_id = Alma::Type.sample_id_for(Alma::Type::UNDERGRAD_SLE)
24-
@user = login_as_patron(patron_id)
13+
login_as_patron(patron_id)
14+
2515
allow(Rails.application.config).to receive(:alma_api_key).and_return(alma_api_key)
2616

2717
req_url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{patron_id}?expand=fees&view=full"
@@ -30,8 +20,6 @@
3020
.with(headers: { 'Accept' => 'application/json', 'Authorization' => "apikey #{alma_api_key}" })
3121
.to_return(status: 200, body: File.new("spec/data/alma_patrons/#{patron_id}.json"))
3222

33-
@patron = Alma::User.find(patron_id)
34-
3523
visit forms_proxy_borrower_dsp_path
3624
end
3725

@@ -62,7 +50,7 @@
6250
fill_in("#{field_prefix}research_last", with: ' ')
6351
fill_in("#{field_prefix}research_first", with: ' ')
6452
fill_in("#{field_prefix}dsp_rep", with: ' ') # TODO: add server-side validation for this (currently only JS)
65-
fill_in("#{field_prefix}date_term", with: @max_date)
53+
fill_in("#{field_prefix}date_term", with: max_date)
6654

6755
submit_button = find(:xpath, "//input[@type='submit']")
6856
submit_button.click
@@ -77,7 +65,7 @@
7765
fill_in("#{field_prefix}research_first", with: 'John')
7866
fill_in("#{field_prefix}dsp_rep", with: 'Jane Roe')
7967

80-
fill_in("#{field_prefix}date_term", with: @invalid_date)
68+
fill_in("#{field_prefix}date_term", with: invalid_date)
8169

8270
submit_button = find(:xpath, "//input[@type='submit']")
8371
submit_button.click
@@ -90,7 +78,7 @@
9078
fill_in("#{field_prefix}research_last", with: 'Doe')
9179
fill_in("#{field_prefix}research_first", with: 'John')
9280
fill_in("#{field_prefix}dsp_rep", with: 'Jane Roe')
93-
fill_in("#{field_prefix}date_term", with: @max_date)
81+
fill_in("#{field_prefix}date_term", with: max_date)
9482

9583
submit_button = find(:xpath, "//input[@type='submit']")
9684
submit_button.click

spec/system/stack_pass_admin_system_spec.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@
22
require 'calnet_helper'
33

44
describe :forms_stack_pass_admin, type: :system do
5-
context 'specs with hardcoded admin' do
5+
context 'specs with stack pass admin' do
66
before do
7-
# First create an admin and assignment
8-
user = FrameworkUsers.create(lcasid: 112_233, name: 'John Doe', role: 'Admin')
9-
Assignment.create(framework_users_id: user.id, role_id: Role.stackpass_admin.id)
7+
admin = FrameworkUsers.create(
8+
lcasid: CalnetHelper::TEST_UID,
9+
name: 'Test Admin',
10+
role: 'Admin'
11+
)
12+
13+
Assignment.create(
14+
framework_users: admin,
15+
role: Role.stackpass_admin
16+
)
17+
18+
user = FrameworkUsers.create(
19+
lcasid: 112_233,
20+
name: 'John Doe',
21+
role: 'Admin'
22+
)
23+
24+
Assignment.create(
25+
framework_users: user,
26+
role: Role.stackpass_admin
27+
)
1028

11-
# These functions require admin privledges:
1229
mock_login(CalnetHelper::TEST_UID)
1330

14-
# Go to the Admin Users View Page:
1531
visit forms_stack_pass_admin_users_path
1632
end
1733

1834
it 'removes an admin user' do
1935
accept_confirm 'Are you sure you want to delete John Doe?' do
20-
click_link 'Remove'
36+
click_link 'Remove', match: :first
2137
end
2238
expect(page).to have_no_content('<div class="col user-col">John Doe')
2339
expect(page).to have_content('Removed John Doe from administrator list')

0 commit comments

Comments
 (0)