Skip to content

Commit

Permalink
updated requests settings spec
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 13, 2023
1 parent bfe027c commit a1531a2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gem "net-smtp", require: false

gem "alma_rest_client",
git: "https://github.com/mlibrary/alma_rest_client",
tag: "1.3.1"
tag: "v2.0.0"

gem "yabeda-puma-plugin"
gem "yabeda-prometheus"
Expand Down
15 changes: 11 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
GIT
remote: https://github.com/mlibrary/alma_rest_client
revision: 2014809367ece3f21935bd24b45a12285f61e253
tag: 1.3.1
revision: 9606225d82480b6d1568902813ae9018dd8c1acc
tag: v2.0.0
specs:
alma_rest_client (1.3.1)
alma_rest_client (2.0.0)
activesupport (~> 7.0, >= 4.2)
httparty
faraday
faraday-retry
httpx
rexml

GEM
Expand Down Expand Up @@ -43,11 +45,16 @@ GEM
faraday-follow_redirects (0.3.0)
faraday (>= 1, < 3)
faraday-net_http (3.0.2)
faraday-retry (2.2.0)
faraday (~> 2.0)
hashdiff (1.0.1)
hashie (5.0.0)
http-2-next (0.5.1)
httparty (0.21.0)
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
httpx (0.24.3)
http-2-next (>= 0.4.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
post "/sms" do
patron = Patron.for(uniqname: session[:uniqname])
response = patron.update_sms((params["text-notifications"] == "on") ? params["sms-number"] : "")
if response.code == 200
if response.status == 200
flash[:success] = if params["text-notifications"] == "on"
"<span class='strong'>Success:</span> Your SMS number was successfully updated"
else
Expand Down
6 changes: 3 additions & 3 deletions models/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def self.for(uniqname:, alma_client: AlmaRestClient.client, circ_history_client:
circ_history_data = {"confirmed" => false, "retain_history" => false}
end

if alma_response.code == 200
Patron.new(alma_data: alma_response.parsed_response, circ_history_data: circ_history_data, illiad_data: illiad_data)
if alma_response.status == 200
Patron.new(alma_data: alma_response.body, circ_history_data: circ_history_data, illiad_data: illiad_data)
else
NotInAlma.new(uniqname, circ_history_data)
end
Expand Down Expand Up @@ -61,7 +61,7 @@ def update_sms(sms, client = AlmaRestClient.client, phone = TelephoneNumber.pars
return Error.new(message: "Phone number #{sms} is invalid") unless phone.valid? || sms.empty?
url = "/users/#{uniqname}"
response = client.put(url, body: patron_with_internal_sms(phone.national_number).to_json)
(response.code == 200) ? response : AlmaError.new(response)
(response.status == 200) ? response : AlmaError.new(response)
end

def uniqname
Expand Down
4 changes: 3 additions & 1 deletion models/response/response.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Response
attr_reader :code, :message, :parsed_response
attr_reader :status, :body, :code, :message, :parsed_response
def initialize(code: 200, message: "Success", parsed_response: {})
@code = code
@message = message
@parsed_response = parsed_response
@status = code
@body = parsed_response
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/requests/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end
context "get /settings" do
it "contains 'Settings'" do
stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full")
stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full", output: "{}")
stub_circ_history_get_request(url: "users/tutor")
stub_illiad_get_request(url: "Users/tutor", status: 404)
get "/settings"
Expand All @@ -26,7 +26,7 @@
context "post /settings/history" do
before(:each) do
@patron_json = File.read("./spec/fixtures/mrio_user_alma.json")
stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full", body: @patron_json)
stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full", output: @patron_json)
stub_circ_history_get_request(url: "users/tutor")
stub_illiad_get_request(url: "Users/tutor", status: 404)
end
Expand All @@ -49,7 +49,7 @@
context "post /sms" do
before(:each) do
@patron_json = File.read("./spec/fixtures/mrio_user_alma.json")
@stub = stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full", body: @patron_json)
@stub = stub_alma_get_request(url: "users/tutor?expand=none&user_id_type=all_unique&view=full", output: @patron_json)
stub_circ_history_get_request(url: "users/tutor")
stub_illiad_get_request(url: "Users/tutor", status: 404)
end
Expand Down
61 changes: 32 additions & 29 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration

require "alma_rest_client"
require "rack/test"
require "rspec"
require "pry-byebug"
require "webmock/rspec"
require "simplecov"
require "climate_control"
require "httpx/adapters/webmock"
SimpleCov.start
ENV["APP_ENV"] = "test"

Expand All @@ -32,6 +34,7 @@ def app = Sinatra::Application
end
RSpec.configure do |config|
config.include RSpecMixin
include AlmaRestClient::Test::Helpers
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
Expand Down Expand Up @@ -129,35 +132,35 @@ def app = Sinatra::Application
end
end

[:get, :post, :delete].each do |name|
define_method("stub_alma_#{name}_request") do |url:, body: "{}", status: 200, query: {}, no_return: nil|
req = stub_request(name, "#{ENV["ALMA_API_HOST"]}/almaws/v1/#{url}").with(
headers: {
:accept => "application/json",
:Authorization => "apikey #{ENV["ALMA_API_KEY"]}",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"User-Agent" => "Ruby"
},
query: query
)
req.to_return(body: body, status: status, headers: {content_type: "application/json"}) if no_return.nil?
req
end
end
def stub_alma_put_request(url:, input:, output:, status: 200, no_return: nil)
req = stub_request(:put, "#{ENV["ALMA_API_HOST"]}/almaws/v1/#{url}").with(
body: input,
headers: {
:accept => "application/json",
:Authorization => "apikey #{ENV["ALMA_API_KEY"]}",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"User-Agent" => "Ruby",
"Content-Type" => "application/json"
}
)
req.to_return(body: output, status: status, headers: {content_type: "application/json"}) if no_return.nil?
req
end
# [:get, :post, :delete].each do |name|
# define_method("stub_alma_#{name}_request") do |url:, body: "{}", status: 200, query: {}, no_return: nil|
# req = stub_request(name, "#{ENV["ALMA_API_HOST"]}/almaws/v1/#{url}").with(
# headers: {
# :accept => "application/json",
# :Authorization => "apikey #{ENV["ALMA_API_KEY"]}",
# "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
# "User-Agent" => "Ruby"
# },
# query: query
# )
# req.to_return(body: body, status: status, headers: {content_type: "application/json"}) if no_return.nil?
# req
# end
# end
# def stub_alma_put_request(url:, input:, output:, status: 200, no_return: nil)
# req = stub_request(:put, "#{ENV["ALMA_API_HOST"]}/almaws/v1/#{url}").with(
# body: input,
# headers: {
# :accept => "application/json",
# :Authorization => "apikey #{ENV["ALMA_API_KEY"]}",
# "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
# "User-Agent" => "Ruby",
# "Content-Type" => "application/json"
# }
# )
# req.to_return(body: output, status: status, headers: {content_type: "application/json"}) if no_return.nil?
# req
# end

def stub_illiad_get_request(url:, body: "{}", status: 200, query: nil, no_return: nil)
req = stub_request(:get, "#{ENV["ILLIAD_API_HOST"]}/webplatform/#{url}").with(
Expand Down

0 comments on commit a1531a2

Please sign in to comment.