Skip to content

Commit

Permalink
Fix timezone dependent tests. Randomize timezone for all tests.
Browse files Browse the repository at this point in the history
We had a few tests that would break if they happened to be run on a
computer in the Denver timezone. Fix those tests so they will work
regardless of the host machine's timezone.

Also add time zone randomization to our test suite to better ensure our
tests aren't timezone dependent.
  • Loading branch information
GUI committed Dec 8, 2016
1 parent 1bdfbb8 commit b026e42
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ gem "faker", "~> 1.6.6"
# Concurrency helpers.
gem "concurrent-ruby", "~> 1.0.2"

# Time zone randomization for tests.
gem "zonebie", "~> 0.6.1"

# Color output
gem "rainbow", "~> 2.1.0"

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ GEM
websocket-extensions (0.1.2)
xpath (2.0.0)
nokogiri (~> 1.3)
zonebie (0.6.1)

PLATFORMS
ruby
Expand Down Expand Up @@ -176,6 +177,7 @@ DEPENDENCIES
rake (~> 11.3.0)
rubocop (~> 0.46.0)
typhoeus (~> 1.1.2)
zonebie (~> 0.6.1)

BUNDLED WITH
1.13.6
27 changes: 19 additions & 8 deletions test/admin_ui/test_legacy_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def test_drilldown
"end_at" => "2015-01-18",
"interval" => "hour",
"query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}",
"tz" => "America/Denver",
}, fragment_uri.query_values)
}.merge(expected_tz_param), fragment_uri.query_values)
end

def test_logs
Expand All @@ -49,8 +48,7 @@ def test_logs
"end_at" => "2015-01-18",
"interval" => "hour",
"query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}",
"tz" => "America/Denver",
}, fragment_uri.query_values)
}.merge(expected_tz_param), fragment_uri.query_values)
end

def test_users
Expand All @@ -68,8 +66,7 @@ def test_users
"start_at" => "2015-01-15",
"end_at" => "2015-01-18",
"query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}",
"tz" => "America/Denver",
}, fragment_uri.query_values)
}.merge(expected_tz_param), fragment_uri.query_values)
end

def test_map
Expand All @@ -88,7 +85,21 @@ def test_map
"end_at" => "2015-01-18",
"query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}",
"region" => "US",
"tz" => "America/Denver",
}, fragment_uri.query_values)
}.merge(expected_tz_param), fragment_uri.query_values)
end

private

def expected_tz_param
# In all our redirect tests, we pass in "America/Denver" for the timezone
# parameter. If the user's current timezone happens to be America/Denver,
# then the "tz" parameter won't be in the redirect (since Ember doesn't add
# default parameters into the URL). However, if we're running in any other
# timezone, this non-default "tz" parameter should be part of the redirect.
if(ENV["TZ"] == "America/Denver")
{}
else
{ "tz" => "America/Denver" }
end
end
end
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
ENV["PATH"],
].join(":")

# Set a random time zone to ensure tests aren't time zone specific.
Zonebie.set_random_timezone

# Set the TZ environment variable to ensure other processes (like the Capybara
# browser tests) are run in the same random time zone.
ENV["TZ"] = ::Time.zone.tzinfo.identifier

# Load all the support files. Load models first, so they're defined for other
# helpers.
Dir[File.join(API_UMBRELLA_SRC_ROOT, "test/support/models/*.rb")].each { |f| require f }
Expand Down

0 comments on commit b026e42

Please sign in to comment.