Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
injectable_rails_env
Advanced tools
= InjectableRailsEnv
Allows injecting the Rails env into a class for testing purpose.
It is not easy to simulate a special Rails environment like production or development during tests.
First we cannot mock the Rails environment methods like production? as follows:
Rails.env.should_receive(:production?).and_return(true)
Cause the method does not exists and the return value is delivered through method_missing internally.
Even if we could mock the method other code on the way from the specs to the class under test may call Rails.env.production? and our mocking would cause side effects here.
So we have to find another way to simulate the environment and this gem may help.
= Installation
Via Bundler:
gem 'injectable_rails_env'
$ bundle install
= Usage
Include the module into your class under test and replace all occurrences of Rails.env.production? with rails_env_production?.
Example:
class JobsController < ApplicationController include InjectableRailsEnv
before_filter :authenticate
def index
...
end
private
def authenticate
return unless rails_env_production? # intead of Rails.env.production?
redirect_to login_url unless logged_in?
end
end
Now you can simulate the Rails env via rails_env=:
describe JobsController do
context "Rails env is production" do
it "requests authentication" do
@controller.rails_env = "production"
get :index
response.should be_redirect
response.should redirect_to login_url
end
end
context "Rails env is non production" do
it "allows access without authentication" do
@controller.rails_env = "non-production"
get :index
response.should be_success
response.should render_template "index"
end
end
end
Note that this won't work on class level. Consider the following example:
class JobsController < ApplicationController include InjectableRailsEnv
before_filter(:authenticate) if rails_env_production?
...
end
When running the specs the JobsController code is executed before the spec starts and rails_env has not been set. Therefore rails_env_production? will always return the value of Rails.env.production?.
You have to change your code from using rails_env_production? on the class level to using it on the instance level (like in the example above in authenticate).
= Supported Environments
Supported environments are test, development, production, stating, integration and ci. So for each there is a method rails_env_ENV?.
= Contact
For comments and question feel free to contact me: business@thomasbaustert.de
If you are using the plugin, consider recommending me at workingwithrails.com: http://workingwithrails.com/person/6131-thomas-baustert
Copyright (c) 2012 [Thomas Baustert], released under the MIT license
FAQs
Unknown package
We found that injectable_rails_env demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.