ruby-on-railsmongodbruby-on-rails-4rspecmongoid4

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?


Solution

  • 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" ] } )