Rolify fixtures setup for rails app - ruby-on-rails

Have an app where i'm using Rolify and Pundit for authorization and Minitest/fixtures for testing.
When i setup a user as administrator using fixtures, i'm not getting back a true for an assertion.
/fixtures/users.yml
administrator:
email: admin#example.com
roles: admin
/fixtures/roles.yml
admin:
id: 1
name: admin
test.rb
it "is administrator" do
User.first.has_role?('admin').must_equal true
end
The test comes back as false, i've tried the admin with a symbol and still get the same thing.
When a user in that development environment has admin, i get true when i ask if the role is admin.
Test framework is minitest and my user model is rolified

Alright so the problem was related to setting up roles.yml correctly. I was using id: above and if you just use users: it works
/fixtures/users.yml
administrator:
email: admin#example.com
roles: admin
/fixtures/roles.yml
admin:
users: administrator
name: admin
test.rb
it "is administrator" do
User.first.has_role?('admin').must_equal true
end

Related

Devise user created with Rails generator cannot sign in

I'm working on a Rails Engine which will depend on Devise.
I created an installer using Rails generator which you can find here. The installer creates the first credentials in order to access the administration panel later.
The problem is that I cannot sign in with those credentials. Weirdly then, if I delete the account and recreate it via Rails console, they do work. I think that's due to something happening during the installer.
Here a snippet of it:
# Binda installer generator ...
def setup_devise
return if Rails.env.production?
# Copy the initilializer on the application folder
template 'config/initializers/devise.rb'
# Add secret key
inject_into_file 'config/initializers/devise.rb', after: "config.secret_key = '" do
SecureRandom.hex(64)
end
# Add pepper
inject_into_file 'config/initializers/devise.rb', after: "config.pepper = '" do
SecureRandom.hex(64)
end
# some other code for mailer ...
end
def create_credentials
#username = ask("What's your email? ['mail#domain.com']").presence || 'mail#domain.com'
#password = ask("What's your password? ['password']").presence || 'password'
Binda::User.create( email: #username, password: #password, password_confirmation: #password )
# Binda installer generator continue ...
Here the full code.
More info
I found out that even if I modify devise.rb file the new configuration is not loaded until the end of the installer. This means the new salt/pepper isn't considered while creating the first user. How can I reload it before running the create_credentials method?
Sorted!
The problem is that if you add salt/pepper configuration to the devise.rb of your application you need to reload the environment before being able to create a user with the new settings.
To do that I moved the user creation into a task. This way the installer to reload the environment (and Devise config as weel) and is able to set the encrypted password correctly.
Installer
# lib/generators/myEngine/install/install_generator.rb
def create_credentials
rake 'binda_create_initial_user'
end
Task
# lib/tasks/binda.rake
desc "Create initial user"
task :binda_create_initial_user => :environment do
username = 'mail#domain.com'
password = 'password'
Binda::User.create!( email: username, password: password, password_confirmation: password )
end
If you want to keep asking for mail and password:
# lib/tasks/binda.rake
desc "Create first user"
task :binda_create_initial_user => :environment do
STDOUT.puts "What is your email? [mail#domain.com]"
username = STDIN.gets.strip
username = 'mail#domain.com' if username.blank?
STDOUT.puts "What is your password? [password]"
password = STDIN.gets.strip
password = 'password' if password.blank?
Binda::User.create!( email: username, password: password, password_confirmation: password )
end

How to configure Cucumber in Rails 4

I'm trying to use Cucumber in a Rails 4. I've added the Cucumber-Rails gem, followed the steps in the instructions but when I wrote step definitions like so:
When(/^I submit a sign up with the following:$/) do |table|
user = User.create({
first_name: 'Name',
last_name: 'Last',
email: 'email#example.com',
domain: 'example.com',
password: 'foobar',
password_confirmation: 'foobar'
})
end
I get the following error: uninitialized constant User (NameError)
./features/step_definitions/users/sign_up_steps.rb:2:in/^I submit a sign up with the following:$/'
features/users/sign_up.feature:4:in When I submit a sign up with the following:'
What am I missing?
Please check , it's looking like you have missed the User model
User model is missing for which you are running tests.

Seeding data with Apartment gem for new schemas?

How can I seed a new tenant in a multi-tenant app that uses the Apartment gem for scoping?
I added the following to seeds.rb but it doesn't seem to work with apartment:
tenants = Tenant.create([
{
name: 'User1',
domain: 'user1'
},
{
name: 'User2',
domain: 'user2'
}
])
Even is the database is blank, seeds.rb tries to create the schema twice and fails with this error when I do rake db:seed:
Apartment::TenantExists: The schema user1 already exists.
Rails rake runs tasks for each tenants/schemas your are trying to create. So run your creation or seeding tasks by first checking if the current schema is a public.
Something like this would do.
if Apartment::Tenant.current == 'public'
Apartment::Tenant.create('tenant1')
Apartment::Tenant.create('tenant2')
end

How to resolve "not authorized for query on db.users" Mongoid

I've created a new Rails app using Rails 4 and Mongoid 4. I'm getting this error "not authorized for query on mydb.users" when I run my test:
RSpec.describe User, type: :model do
it "is invalid without a name" do
user = User.new(name: nil)
user.valid?
expect(user).to be_invalid
end
...
end
At first I thought this was an authorisation issue with Mongoid and MongoDB, but I can access mongoldb/db/collection without authenticate in the console without trouble.
Because I'm new to Rspec tests, I'm not sure the problem is with my test, mongoid or mongodb.
Any ideas?
MongoDB provide access on database level and it store on database level also. You need to provide access on this database like :
use mydb;
db.createUser(
{
user: "accountUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)

getting past authentication with capybara in rails application

I'm trying to use capybara on a ruby on rails application to do some content testing, as well I'm using the devise gem to implement user authentication. I'm having trouble logging into my application to perform my test cases.
Initially, my scenario was as follows:
scenario "User arrives at main page" do
visit "purchase_orders#index"
page.should have_content("All")
# some more tests and checks
end
Where purchase_orders#index is the authenticated root, where a user's purchase orders are shown.
But when I was running the tests, I was getting the following error :
expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us •
which tells me that its not getting past the log in page. I next tried adding the following to my scenario, before running the tests, to make it log in:
visit "purchase_orders#index"
fill_in('Email', :with => 'username#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
where username and password are actual created accounts, but again it fails and doesn't get past the sign in page. Finally, I tried adding a before(:each) method, as follows, to attempt to sign users in for test cases:
before(:each) do
visit "users/sessions#new"
fill_in('Email', :with => 'nico.dubus17#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
end
which, again, did not work for me. So my question is: What is the best practice and syntax for getting past the sign in page, and into the actual application? I've looked for documentation and answers on this, but haven't found anything.
Thank you!
Found an answer. I installed the warden gem to (gem 'warden') and factory girl gem (gem "factory_girl_rails", "~> 4.0") and ran a bundle install. I then created a user fixture with factory girl as follows in a factory.rb file in the spec folder:
# This is a user factory, to simulate a user object
FactoryGirl.define do
factory :user, class: User do
first_name "John"
last_name "Doe"
email "nico_dubus#hotmail.com"
encrypted_password "password"
end
end
in my rails helper file, I added this line to be able to use FactoryGirl's methods without calling it on the class every time:
config.include FactoryGirl::Syntax::Methods
Afterwards, I added these lines to the top of my capybara test page:
include Warden::Test::Helpers
Warden.test_mode!
Finally, I built a user object to use within the scope of the test:
user = FactoryGirl.build(:user)
And whenever I need to log in, I use warden's log in method
login_as(user, :scope => :user)
And voila!

Resources