Skip to content

Commit

Permalink
[wip] spec for clearing tokens
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
dacook committed Jan 22, 2025
1 parent ff3af2f commit 5bc0335
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 22 deletions.
76 changes: 54 additions & 22 deletions engines/dfc_provider/spec/services/dfc_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,59 @@
# The absence of errors makes this test pass.
end

it "refreshes the access token and retrieves the FDC catalog", vcr: true do
# A refresh is only attempted if the token is stale.
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago

response = nil
expect {
response = api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to change {
account.token
}.and change {
account.refresh_token
}

json = JSON.parse(response)

graph = DfcIo.import(json)
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
describe "refreshing token when stale" do
before do
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago
end

it "refreshes the access token and retrieves the FDC catalog", vcr: true do
response = nil
expect {
response = api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to change {
account.token
}.and change {
account.refresh_token
}

json = JSON.parse(response)

graph = DfcIo.import(json)
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
end

context "with account tokens" do
before do
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
# TODO try to get a token. currently getting auth error! (invalid_client :: Invalid client or Invalid client credentials)
api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
expect(account.token).not_to be_nil
end

it "clears the token if authentication fails", vcr: true do
allow_any_instance_of(OpenIDConnect::Client).to receive(:access_token!).and_raise(
Rack::OAuth2::Client::Error.new(
1, { error: "invalid_grant", error_description: "session not active" }
)
)

expect {
api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to raise_error(Rack::OAuth2::Client::Error).and change {
account.token
}.to(nil).and change {
account.refresh_token
}.to(nil)
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bc0335

Please sign in to comment.