-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to stub Rails credential key? #2099
Comments
As
It'd be better to use it as designed and setup a test environment set of credentials. |
Thanks @JonRowe I edited the post to actually focus on the issue (described in your first example).
I tried indeed the following but it keeps the value defined in
|
I just did a test on a fresh_app And I had no issue. My credentials file looks like this: aws:
access_key_id: 123
secret_access_key: 345
olimart: 'from_config'
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: xxxx require 'rails_helper'
describe 'Credentials' do
it 'stubs credentials' do
allow(Rails.application.credentials).to receive(:olimart).and_return('123')
expect(Rails.application.credentials.olimart).to eq('123')
end
end no issues. |
@benoittgt What's your master key? Amazing. The exact same code fails on my end. I appreciate your help. |
@benoittgt I'll leave it up to you to decide if this should be reopened :) |
don't think so. Just need to find out why but in the meantime we came up with a workaround. |
Actually, I have the exact same issue, and I cannot figure out why, @olimart, what is the workaround you implemented ? |
shared_context 'encrypted configuration' do
def stub_credential(key, value)
allow(Rails.application).to receive(:credentials).and_return(OpenStruct.new(key.to_sym => value))
end
end
# usage
stub_credential(:my_credential, 123) |
what is the usage for a deeper structure, for example if the credentials.yml file has
(sorry, the above should be indented as per a would you suggest: |
allow(Rails.application).to receive_message_chain(:test, :level, :MY_DEEP_ENV_VAR => 67890) |
Rubocop was complaining about
|
My 2 cents def stub_credential(**credentials)
allow(Rails.application).to receive(:credentials).and_return(
OpenStruct.new(**credentials)
)
end
stub_credential(credential_one: 1, credential_two: 2) |
This worked for me, note the use of allow(Rails.application).to receive(:credentials).and_return(Rails.application.credentials.deep_merge(test: { stripe: { trial_periods: { standard_monthly_uk: 30 } } })) |
We reserve our issue tracker for features and bugs. |
You will find it hard to mock out code run in class definitions, RSpec doesn't control the loading of classes (this is typically something Rails does for you) so by the time the class is loaded the constant is already set, you can use the |
@JonRowe Thanks |
I found an elegant way to stub a deeply nested rails credential in Rspec, commenting here so that someone else can benefit from it. Suppose you have the following in your rails credentials.yml file
You can mock it so as
The first line will ensure that other nested keys are not mocked and will be called as originally expected. |
Similar to stubbing environment variable, can we stub Rails credential key?
What Ruby, Rails and RSpec versions are you using?
Ruby version: 2.6.1
Rails version: 5.2.2
Rspec version: 3.8
Observed behaviour
allow(Rails.application.credentials).to receive(:my_token).and_return('')
Expected behaviour
The text was updated successfully, but these errors were encountered: