Skip to content

Commit ab71232

Browse files
author
Simon Coffey
committed
Add unit coverage for Utils.parse_duration
This adds some more comprehensive unit testing for the .parse_duration helper, since we'd like to modify it for better coverage of the ISO8601 duration spec. Since the helper both parses the duration and adds it to the current time (by default) before returning it, I've used the unix epoch as a reference point instead and formatted the result as an ISO timestamp before asserting, to hopefully make any errors clearer. I've not gone for full coverage, but have tried to test the main edge cases. In subsequent commits I'll refactor the parsing and introduce support for fractional values.
1 parent b55733f commit ab71232

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

test/utils_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
22

33
class UtilsTest < Minitest::Test
4+
describe ".parse_duration" do
5+
DURATIONS_FROM_EPOCH = {
6+
# Basic formats
7+
"P1Y1M1D" => "1971-02-02T00:00:00.000Z",
8+
"PT1H1M1S" => "1970-01-01T01:01:01.000Z",
9+
"P1W" => "1970-01-08T00:00:00.000Z",
10+
"P1Y1M1DT1H1M1S" => "1971-02-02T01:01:01.000Z",
11+
12+
# Nominal wraparounds
13+
"P13M" => "1971-02-01T00:00:00.000Z",
14+
"P31D" => "1970-02-01T00:00:00.000Z",
15+
}
16+
17+
def result(duration, reference = 0)
18+
Time.at(
19+
OneLogin::RubySaml::Utils.parse_duration(duration, reference)
20+
).utc.iso8601(3)
21+
end
22+
23+
DURATIONS_FROM_EPOCH.each do |duration, expected|
24+
it "parses #{duration} to return #{expected} from the given timestamp" do
25+
assert_equal expected, result(duration)
26+
end
27+
end
28+
29+
it "returns the last calendar day of the next month when advancing from a longer month to a shorter one" do
30+
initial_timestamp = Time.iso8601("1970-01-31T00:00:00.000Z").to_i
31+
32+
assert_equal "1970-02-28T00:00:00.000Z", result("P1M", initial_timestamp)
33+
end
34+
end
35+
436
describe ".format_cert" do
537
let(:formatted_certificate) {read_certificate("formatted_certificate")}
638
let(:formatted_chained_certificate) {read_certificate("formatted_chained_certificate")}

0 commit comments

Comments
 (0)