|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +class MockArchiveFile |
| 4 | + |
| 5 | + def get!(result) |
| 6 | + @result = result |
| 7 | + end |
| 8 | + |
| 9 | + def log_denied_attempt!(request_hash: metadata) |
| 10 | + end |
| 11 | + |
| 12 | + def to_s |
| 13 | + "MockArchiveFile" |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +RSpec.describe ArchiveController do |
| 20 | + |
| 21 | + describe "#user_is_authorized?" do |
| 22 | + before { |
| 23 | + allow(subject).to receive(:set_variables) |
| 24 | + } |
| 25 | + |
| 26 | + situations = [{auth_user: true, recaptcha: true, result: true}, |
| 27 | + {auth_user: true, recaptcha: false, result: false}, |
| 28 | + {auth_user: false, recaptcha: true, result: false}, |
| 29 | + {auth_user: false, recaptcha: false, result: false}] |
| 30 | + |
| 31 | + situations.each do |situation| |
| 32 | + context "when authenticated_user? is #{situation[:auth_user]} and recaptcha_success? is #{situation[:recaptcha]}" do |
| 33 | + before { |
| 34 | + allow(subject).to receive(:authenticated_user?).and_return situation[:auth_user] |
| 35 | + allow(subject).to receive(:recaptcha_success?).and_return situation[:recaptcha] |
| 36 | + } |
| 37 | + |
| 38 | + it "sets variables and returns #{situation[:result]}" do |
| 39 | + |
| 40 | + expect(subject).to receive(:authenticated_user?).and_return situation[:auth_user] |
| 41 | + if (situation[:auth_user]) |
| 42 | + expect(subject).to receive(:recaptcha_success?).and_return situation[:recaptcha] |
| 43 | + end |
| 44 | + |
| 45 | + expect(subject.user_is_authorized?).to eq situation[:result] |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + after { |
| 51 | + expect(subject).to have_received(:set_variables) |
| 52 | + } |
| 53 | + end |
| 54 | + |
| 55 | + |
| 56 | + describe "#status" do |
| 57 | + context "when user is authorized" do |
| 58 | + before { |
| 59 | + allow(subject).to receive(:user_is_authorized?).and_return true |
| 60 | + subject.instance_variable_set(:@archive_file, OpenStruct.new(display_status:"User is Authorized")) |
| 61 | + allow(subject).to receive(:render).with({plain: "User is Authorized"}).and_return "User is Authorized" |
| 62 | + } |
| 63 | + it "returns string" do |
| 64 | + expect(subject.status).to eq "User is Authorized" |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context "when user is not authorized" do |
| 69 | + before { |
| 70 | + allow(subject).to receive(:user_is_authorized?).and_return false |
| 71 | + allow(subject).to receive(:render).with({plain: "action unavailable", status: 403}).and_return "action unavailable 403" |
| 72 | + } |
| 73 | + it "returns 403 status" do |
| 74 | + expect(subject.status).to eq "action unavailable 403" |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + |
| 80 | + describe "#download_request" do |
| 81 | + mock_archive_file = MockArchiveFile.new |
| 82 | + before { |
| 83 | + allow(subject).to receive(:root_url).and_return "root url" |
| 84 | + subject.instance_variable_set(:@archive_file, mock_archive_file) |
| 85 | + } |
| 86 | + |
| 87 | + context "when user is authorized" do |
| 88 | + before { |
| 89 | + allow(subject).to receive(:user_is_authorized?).and_return true |
| 90 | + } |
| 91 | + |
| 92 | + context "when file_path is present in @archive_file" do |
| 93 | + before { |
| 94 | + allow(subject).to receive(:request_metadata).and_return :file_path => "file path", :filename => "file name" |
| 95 | + allow(subject).to receive(:download_filename).with("file name").and_return "download filename" |
| 96 | + } |
| 97 | + it "calls send_file" do |
| 98 | + expect(subject).to receive(:send_file).with("file path", filename: "download filename") |
| 99 | + subject.download_request |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context "when file_path is not present in @archive_file" do |
| 104 | + |
| 105 | + context "when message is not present in @archive_file" do |
| 106 | + before { |
| 107 | + allow(subject).to receive(:request_metadata).and_return :file_path => "" |
| 108 | + allow(Rails.logger).to receive(:error).with("Message missing from MockArchiveFile result: {:file_path=>\"\"}") |
| 109 | + } |
| 110 | + it "redirects with default error message" do |
| 111 | + expect(Rails.logger).to receive(:error).with("Message missing from MockArchiveFile result: {:file_path=>\"\"}") |
| 112 | + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", notice: "Request failed. Please seek technical support.") |
| 113 | + subject.download_request |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context "when message is present in @archive_file and alert is not" do |
| 118 | + before { |
| 119 | + allow(subject).to receive(:request_metadata).and_return :message => "message" |
| 120 | + } |
| 121 | + it "redirects with notice" do |
| 122 | + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", notice: "message") |
| 123 | + subject.download_request |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + context "when message is present in @archive_file and so is alert" do |
| 128 | + before { |
| 129 | + allow(subject).to receive(:request_metadata).and_return :message => "message", :alert => "alert" |
| 130 | + } |
| 131 | + it "redirects with alert" do |
| 132 | + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", alert: "message") |
| 133 | + subject.download_request |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + context "when user is not authorized" do |
| 140 | + before { |
| 141 | + allow(subject).to receive(:user_is_authorized?).and_return false |
| 142 | + allow(subject).to receive(:request_metadata).and_return "request metadata" |
| 143 | + subject.instance_variable_set(:@failure_description, "failure description") |
| 144 | + } |
| 145 | + it "logs denied attempt and calls redirect_back with @failure_description" do |
| 146 | + expect(mock_archive_file).to receive(:log_denied_attempt!).with(request_hash: "request metadata") |
| 147 | + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", alert: "failure description") |
| 148 | + subject.download_request |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + |
| 154 | + # private methods start |
| 155 | + |
| 156 | + describe "#variable_params" do |
| 157 | + before { |
| 158 | + allow(subject.params).to receive(:permit).with(:collection, :object, :format, :request, :user_email, :file_set_id, 'g-recaptcha-response'.to_sym, 'g-recaptcha-response-data'.to_sym => [:sda_request]) |
| 159 | + |
| 160 | + } |
| 161 | + it "calls params.permit" do |
| 162 | + expect(subject.params.permit(:collection, :object, :format, :request, :user_email, :file_set_id, 'g-recaptcha-response'.to_sym, 'g-recaptcha-response-data'.to_sym => [:sda_request])) |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + |
| 167 | + describe "#set_variables" do |
| 168 | + before { |
| 169 | + allow(subject).to receive(:params).and_return :collection => "The Collection", :user_email => "[email protected]", :file_set_id => "Q-3333" |
| 170 | + allow(subject).to receive(:variable_params).and_return :object => "I Object", :format => "formatted" |
| 171 | + allow(ArchiveFile).to receive(:new).with(collection: "The Collection", object: "I Object.formatted").and_return "archive file" |
| 172 | + } |
| 173 | + |
| 174 | + it "sets instance variables" do |
| 175 | + subject.send(:set_variables) |
| 176 | + |
| 177 | + expect(subject.instance_variable_get(:@collection)).to eq "The Collection" |
| 178 | + expect(subject.instance_variable_get(:@object)).to eq "I Object.formatted" |
| 179 | + expect(subject.instance_variable_get(:@archive_file)).to eq "archive file" |
| 180 | + expect(subject.instance_variable_get(:@user_email)).to eq "[email protected]" |
| 181 | + expect(subject.instance_variable_get(:@file_set_id)).to eq "Q-3333" |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + |
| 186 | + describe "#download_filename" do |
| 187 | + context "when user-displayed filename is not available" do |
| 188 | + before { |
| 189 | + allow(FileSet).to receive(:search_with_conditions).and_return [] |
| 190 | + } |
| 191 | + it "returns the default filename (the parameter entered)" do |
| 192 | + expect(subject.send(:download_filename, "V. Basic File")).to eq "V. Basic File" |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + context "when user-displayed filename is available" do |
| 197 | + before { |
| 198 | + subject.instance_variable_set(:@file_set_id, "B-2000") |
| 199 | + allow(FileSet).to receive(:search_with_conditions).with(id: "B-2000").and_return [Hash.new(label_ssi: "Descriptively labelled file with emojis")] |
| 200 | + } |
| 201 | + it "returns the user-displayed filename" do |
| 202 | + expect(subject.send(:download_filename, "V. Basic File")).to eq label_ssi: "Descriptively labelled file with emojis" |
| 203 | + end |
| 204 | + end |
| 205 | + end |
| 206 | + |
| 207 | + |
| 208 | + describe "#authenticated_user?" do |
| 209 | + context "when user authentication is not required" do |
| 210 | + before { |
| 211 | + allow(Settings.archive_api).to receive(:require_user_authentication).and_return false |
| 212 | + } |
| 213 | + it "returns true" do |
| 214 | + expect(subject.send(:authenticated_user?)).to eq true |
| 215 | + |
| 216 | + expect(subject.instance_variable_get(:@failure_description)).to be_blank |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + context "when user authentication is required" do |
| 221 | + before { |
| 222 | + allow(Settings.archive_api).to receive(:require_user_authentication).and_return true |
| 223 | + allow(subject).to receive(:user_signed_in?).and_return true |
| 224 | + } |
| 225 | + it "returns result of user_signed_in?" do |
| 226 | + expect(subject.send(:authenticated_user?)).to eq true |
| 227 | + |
| 228 | + expect(subject.instance_variable_get(:@failure_description)).to eq "Action available only to signed-in users." |
| 229 | + end |
| 230 | + end |
| 231 | + end |
| 232 | + |
| 233 | + |
| 234 | + describe "#recaptcha_success?" do |
| 235 | + context "when recaptcha not in use" do |
| 236 | + before { |
| 237 | + allow(Settings.archive_api).to receive(:use_recaptcha).and_return false |
| 238 | + } |
| 239 | + it "returns true" do |
| 240 | + expect(subject.send(:recaptcha_success?)).to eq true |
| 241 | + expect(subject.instance_variable_get(:@failure_description)).to be_blank |
| 242 | + end |
| 243 | + end |
| 244 | + |
| 245 | + context "when recaptcha in use" do |
| 246 | + before { |
| 247 | + allow(Settings.archive_api).to receive(:use_recaptcha).and_return true |
| 248 | + } |
| 249 | + |
| 250 | + context "when recaptcha v3 is successful" do |
| 251 | + before { |
| 252 | + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 253 | + secret_key: Settings.recaptcha.v3.secret_key) |
| 254 | + .and_return true |
| 255 | + } |
| 256 | + it "returns true" do |
| 257 | + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 258 | + secret_key: Settings.recaptcha.v3.secret_key).and_return true |
| 259 | + expect(subject).not_to receive(:verify_recaptcha) |
| 260 | + expect(subject.send(:recaptcha_success?)).to eq true |
| 261 | + end |
| 262 | + end |
| 263 | + |
| 264 | + context "when recaptcha v2 is successful" do |
| 265 | + before { |
| 266 | + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 267 | + secret_key: Settings.recaptcha.v3.secret_key) |
| 268 | + .and_return false |
| 269 | + allow(subject).to receive(:verify_recaptcha).and_return true |
| 270 | + } |
| 271 | + it "returns true" do |
| 272 | + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 273 | + secret_key: Settings.recaptcha.v3.secret_key).and_return false |
| 274 | + expect(subject).to receive(:verify_recaptcha).and_return true |
| 275 | + expect(subject.send(:recaptcha_success?)).to eq true |
| 276 | + end |
| 277 | + end |
| 278 | + |
| 279 | + context "when recaptcha v3 and v2 are both unsuccessful" do |
| 280 | + before { |
| 281 | + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 282 | + secret_key: Settings.recaptcha.v3.secret_key) |
| 283 | + .and_return false |
| 284 | + allow(subject).to receive(:verify_recaptcha).and_return false |
| 285 | + } |
| 286 | + it "returns false" do |
| 287 | + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, |
| 288 | + secret_key: Settings.recaptcha.v3.secret_key).and_return false |
| 289 | + expect(subject).to receive(:verify_recaptcha).and_return false |
| 290 | + expect(subject.send(:recaptcha_success?)).to eq false |
| 291 | + end |
| 292 | + end |
| 293 | + |
| 294 | + after { |
| 295 | + expect(subject.instance_variable_get(:@failure_description)).to eq 'Action requires successful recaptcha completion.' |
| 296 | + } |
| 297 | + end |
| 298 | + end |
| 299 | + |
| 300 | + |
| 301 | + describe "#request_metadata" do |
| 302 | + before { |
| 303 | + allow(Time).to receive(:now).and_return "It's time..." |
| 304 | + subject.instance_variable_set(:@user_email, "User's email") |
| 305 | + subject.instance_variable_set(:@file_set_id, "A-1000") |
| 306 | + } |
| 307 | + |
| 308 | + it "returns hashset" do |
| 309 | + result_hash = {:time => "It's time...", :user_email => "User's email", :file_set_id => "A-1000"} |
| 310 | + expect(subject.send(:request_metadata)).to eq result_hash |
| 311 | + end |
| 312 | + end |
| 313 | + |
| 314 | +end |
| 315 | + |
| 316 | + |
0 commit comments