Skip to content

Commit c9f81b9

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 c9f81b9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/utils_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
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+
# Negative duration
13+
"-P1Y1M1DT1H1M1S" => "1968-11-29T22:58:59.000Z",
14+
15+
# Nominal wraparounds
16+
"P13M" => "1971-02-01T00:00:00.000Z",
17+
"P31D" => "1970-02-01T00:00:00.000Z",
18+
}
19+
20+
def result(duration, reference = 0)
21+
Time.at(
22+
OneLogin::RubySaml::Utils.parse_duration(duration, reference)
23+
).utc.iso8601(3)
24+
end
25+
26+
DURATIONS_FROM_EPOCH.each do |duration, expected|
27+
it "parses #{duration} to return #{expected} from the given timestamp" do
28+
assert_equal expected, result(duration)
29+
end
30+
end
31+
32+
it "returns the last calendar day of the next month when advancing from a longer month to a shorter one" do
33+
initial_timestamp = Time.iso8601("1970-01-31T00:00:00.000Z").to_i
34+
35+
assert_equal "1970-02-28T00:00:00.000Z", result("P1M", initial_timestamp)
36+
end
37+
end
38+
439
describe ".format_cert" do
540
let(:formatted_certificate) {read_certificate("formatted_certificate")}
641
let(:formatted_chained_certificate) {read_certificate("formatted_chained_certificate")}

0 commit comments

Comments
 (0)