-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms_helper.rb
More file actions
136 lines (110 loc) · 3.91 KB
/
forms_helper.rb
File metadata and controls
136 lines (110 loc) · 3.91 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
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
require 'calnet_helper'
RSpec.shared_examples 'an authenticated form' do |form_class:, allowed_patron_types:, submit_path:, success_path:, valid_form_params:|
attr_reader :form_name
let(:alma_api_key) { 'totally-fake-key' }
forbidden_patron_types = Alma::Type.all.reject { |t| allowed_patron_types.include?(t) }
missing_field_params = valid_form_params.transform_values { |v| v.is_a?(Hash) ? {} : nil }
before do
allow(Rails.application.config).to receive(:alma_api_key).and_return(alma_api_key)
@form_name = form_class.to_s.underscore
end
def new_form_path
send("new_#{form_name}_path")
end
def index_path
send("#{form_name}s_path")
end
def show_path(id)
send("#{form_name}_path", id)
end
it 'requires login' do
get new_form_path
expect(response).to have_http_status(:unauthorized)
end
allowed_patron_types.each do |type|
it "allows #{Alma::Type.name_of(type)}" do
patron_id = Alma::Type.sample_id_for(type)
with_patron_login(patron_id) do
get new_form_path
expect(response).to have_http_status(:ok)
end
end
end
forbidden_patron_types.each do |type|
it "forbids #{Alma::Type.name_of(type)}" do
patron_id = Alma::Type.sample_id_for(type)
with_patron_login(patron_id) do
get new_form_path
expect(response).to have_http_status(:forbidden)
end
end
end
it 'forbids users without patron records' do
patron_id = 'does_not_exist'
with_patron_login(patron_id) do
get new_form_path
expect(response).to have_http_status(:forbidden)
expected_msg = /This form is only available to active library patrons/
expect(response.body).to match(expected_msg)
end
end
it 'forbids users when active patron lookup returns Alma 404' do
patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
allow(AlmaServices::Patron).to receive(:get_user).with(patron_id)
.and_raise(Error::AlmaRecordNotFoundError, '404')
with_patron_login(patron_id) do
get new_form_path
expect(response).to have_http_status(:forbidden)
end
end
it 'forbids users when active patron lookup returns Alma 400' do
patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
allow(AlmaServices::Patron).to receive(:get_user).with(patron_id)
.and_raise(Error::AlmaRecordNotFoundError, '400')
with_patron_login(patron_id) do
get new_form_path
expect(response).to have_http_status(:forbidden)
end
end
describe 'with a valid user' do
attr_reader :patron_id
attr_reader :patron
attr_reader :user
before do
@patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
@user = login_as_patron(patron_id)
@patron = Alma::User.find(patron_id)
end
after do
logout!
end
it 'handles patron API errors' do
expect(Alma::User).to receive(:find).with(patron_id).and_raise(Error::PatronApiError)
get new_form_path
expect(response).to have_http_status(:service_unavailable)
end
it 'respects patron blocks' do
expect(Alma::User).to receive(:find).with(patron_id).and_return(patron)
expect(patron).to receive(:blocks).and_return('block all the things')
get new_form_path
expect(response).to have_http_status(:forbidden)
end
it 'rejects a submission with missing fields' do
post(submit_path, params: missing_field_params)
expect(response).to redirect_to(new_form_path)
end
it 'accepts a submission' do
post(submit_path, params: valid_form_params)
expect(response).to redirect_to(success_path)
end
it 'redirects from :index to :new' do
get index_path
expect(response).to redirect_to(new_form_path)
end
it 'redirects from :show to :new' do
some_meaningless_id = Time.now.to_i.to_s
get show_path(some_meaningless_id)
expect(response).to redirect_to(new_form_path)
end
end
end