2016-10-11 17:54:28 +02:00
|
|
|
# frozen_string_literal: true
|
2017-06-14 22:16:21 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
require_relative "helper"
|
|
|
|
require "rack/test"
|
|
|
|
require "web/server"
|
2014-05-16 21:35:16 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
describe "the server" do
|
2014-05-16 21:35:16 +02:00
|
|
|
include Rack::Test::Methods
|
|
|
|
|
2020-05-08 15:50:09 +02:00
|
|
|
let(:app) { Web::Server.freeze }
|
2018-03-09 01:01:08 +01:00
|
|
|
|
|
|
|
def json
|
|
|
|
Oj.load(last_response.body)
|
|
|
|
end
|
2014-05-16 21:35:16 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "handles unfound pages" do
|
|
|
|
get "/foo"
|
|
|
|
_(last_response.status).must_equal(404)
|
2014-05-16 21:35:16 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "will not process an invalid date" do
|
|
|
|
get "/2010-31-01"
|
|
|
|
_(last_response).must_be(:unprocessable?)
|
2014-05-16 21:35:16 +02:00
|
|
|
end
|
|
|
|
|
2024-11-21 13:15:06 +01:00
|
|
|
it "will not process an invalid amount" do
|
|
|
|
get "/latest?amount=0&from=USD&to=EUR"
|
|
|
|
_(last_response).must_be(:unprocessable?)
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "will not process a date before 2000" do
|
|
|
|
get "/1999-01-01"
|
|
|
|
_(last_response).must_be(:not_found?)
|
2014-05-16 21:35:16 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "will not process an unavailable base" do
|
|
|
|
get "/latest?base=UAH"
|
|
|
|
_(last_response).must_be(:not_found?)
|
2016-10-12 00:37:29 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "handles malformed queries" do
|
|
|
|
get "/latest?base=USD?callback=?"
|
|
|
|
_(last_response).must_be(:not_found?)
|
2014-05-16 21:39:14 +02:00
|
|
|
end
|
2015-09-15 13:02:49 +02:00
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "does not return stale dates" do
|
2018-10-03 15:48:30 +02:00
|
|
|
Day.db.transaction do
|
2024-11-19 20:06:47 +01:00
|
|
|
get "/latest"
|
|
|
|
date = json["date"]
|
2018-10-03 15:48:30 +02:00
|
|
|
Day.latest.delete
|
2024-11-19 20:06:47 +01:00
|
|
|
get "/latest"
|
|
|
|
_(json["date"]).wont_equal(date)
|
2015-09-15 13:02:49 +02:00
|
|
|
raise Sequel::Rollback
|
|
|
|
end
|
|
|
|
end
|
2024-11-21 14:45:44 +01:00
|
|
|
|
|
|
|
it "will not process circular conversions" do
|
|
|
|
get "/latest?from=EUR&to=EUR"
|
|
|
|
_(last_response).must_be(:unprocessable?)
|
|
|
|
get "/latest?from=USD&to=USD"
|
|
|
|
_(last_response).must_be(:unprocessable?)
|
|
|
|
end
|
2014-05-16 21:35:16 +02:00
|
|
|
end
|