Files
openfoodnetwork/spec/services/vine/jwt_service_spec.rb
Maikel Linke 94b75540e4 Replace Timecop with Rails' time helpers
Rails 4.1 added time helpers but we never bothered using them. But now
I'm getting rid of the Timecop dependency and use standard helpers.

Beware though that the new helpers always freeze time. When you travel
to a certain date then the clock stops ticking while Timecop maintained
the passing of time.

The freezing of time could cause problems if you are trying to enforce a
timeout. But all current specs don't seem affected.

In most cases, the freezing will make it easier to avoid flaky specs.
2025-08-22 16:57:04 +10:00

53 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require "spec_helper"
RSpec.describe Vine::JwtService do
describe "#generate_token" do
subject { described_class.new(secret: vine_secret) }
let(:vine_secret) { "some_secret" }
it "generate a jwt token" do
expect(subject.generate_token).to be_a String
end
it "includes issuing body" do
token = subject.generate_token
payload = decode(token, vine_secret)
expect(payload["iss"]).to eq("openfoodnetwork")
end
it "includes issuing time" do
generate_time = Time.zone.now
travel_to(generate_time) do
token = subject.generate_token
payload = decode(token, vine_secret)
expect(payload["iat"].to_i).to eq(generate_time.to_i)
end
end
it "includes expirations time" do
generate_time = Time.zone.now
travel_to(generate_time) do
token = subject.generate_token
payload = decode(token, vine_secret)
expect(payload["exp"].to_i).to eq((generate_time + 1.minute).to_i)
end
end
end
def decode(token, secret)
JWT.decode(
token,
secret,
true, { algorithm: "HS256" }
).first
end
end