Create change_time helper method

Time#change is an activesupport feature

Also: Use seconds instead of Numeric#minute and Numeric#hour
This commit is contained in:
Benjamin Neff 2017-04-04 22:57:18 +02:00
parent ccd4b7eceb
commit 40919b4c69
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
6 changed files with 18 additions and 9 deletions

View file

@ -205,8 +205,8 @@ module DiasporaFederation
guid { Fabricate.sequence(:guid) }
summary "Cool event"
description "You need to see this!"
start { Time.now.utc.change(min: 0).change(sec: 0).change(usec: 0) - 1.hour }
f.end { Time.now.utc.change(min: 0).change(sec: 0).change(usec: 0) + 1.hour }
start { change_time(Time.now.utc, min: 0) - 3600 }
f.end { change_time(Time.now.utc, min: 0) + 3600 }
all_day false
timezone "Europe/Berlin"
end

View file

@ -60,11 +60,11 @@ JSON
describe "#created_at" do
it "has a created_at after parse" do
entity = described_class.from_xml(Nokogiri::XML::Document.parse(xml).root)
expect(entity.created_at).to be_within(1.second).of(Time.now.utc)
expect(entity.created_at).to be_within(1).of(Time.now.utc)
end
it "parses the created_at from the xml if it is included and correctly signed" do
created_at = Time.now.utc.change(usec: 0) - 1.minute
created_at = change_time(Time.now.utc) - 60
comment_data = Fabricate.attributes_for(:comment_entity, author: alice.diaspora_id, parent_guid: parent.guid)
comment_data[:created_at] = created_at
comment_data[:parent] = parent_entity

View file

@ -158,7 +158,7 @@ XML
test2: false,
test3: "456",
test4: 789,
test5: Time.current.utc
test5: Time.now.utc
}
}
@ -208,7 +208,7 @@ XML
describe ".from_json" do
it "parses entity properties from the input JSON data" do
now = Time.now.change(usec: 0).utc
now = change_time(Time.now.utc)
entity_data = <<-JSON
{
"entity_type": "test_complex_entity",
@ -281,7 +281,7 @@ JSON
describe ".from_hash" do
it "parses entity properties from the input data" do
now = Time.now.change(usec: 0).utc
now = change_time(Time.now.utc)
entity_data = {
test1: "abc",
test2: false,

View file

@ -26,7 +26,7 @@ JSON
'{"entity_type": "test_complex_entity"}'
it "returns a hash for the correct JSON input" do
now = Time.now.change(usec: 0).utc
now = change_time(Time.now.utc)
json = <<-JSON
{
"entity_type": "test_complex_entity",

View file

@ -26,3 +26,12 @@ end
def verify_signature(pubkey, signature, signed_string)
pubkey.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(signature), signed_string)
end
# time helper
def change_time(time, options={})
new_hour = options.fetch(:hour, time.hour)
new_min = options.fetch(:min, options[:hour] ? 0 : time.min)
new_sec = options.fetch(:sec, options[:hour] || options[:min] ? 0 : time.sec)
::Time.utc(time.year, time.month, time.day, new_hour, new_min, new_sec)
end

View file

@ -90,7 +90,7 @@ shared_examples "an XML Entity" do |ignored_props=[]|
def validate_property(value, parsed_value)
if value.is_a?(Time)
expect(parsed_value).to eq(value.change(usec: 0))
expect(parsed_value).to eq(change_time(value))
else
expect(parsed_value).to eq(value)
end