user_type is a column in user model and it has to be :presence => true. It rejects in rspec when passing a nil to user_type. However the factory_girl can store a nil user_type into the field with sqlite3.
Running rails 3.1.0. rspec 2.6.0 and factory_girl 2.1.0
//user model:
validates :user_type, :presence => true
//Factory girl definition:
Factory.define :user do |user|
user.name "Test User"
user.email "test#test.com"
user.password "password1"
user.password_confirmation "password1"
user.status "active"
user.user_type "employee"
end
Factory.define :user_level do |level|
level.role "sales"
level.position "member"
level.team 1
level.association :user
end
//rspec case: passed
it "should reject user type nil" do
user = Factory.build(:user, :user_type => nil)
user.should_not be_valid
end
it "should take user type 'employee'" do
user = Factory.build(:user, :user_type => 'employee')
user.errors[:user_type].should be_empty
user.should be_valid
end
Any thoughts? thanks.
As Michael Hartl pointed out in his excellent Rails tutorial, Factory Girl bypasses attr_accessible in the model. For instance, you can overwrite the "magic" columns using Factory Girl. Perhaps this applies to validations as well?
It seems like it would be better in this case to actually create your user with User.new, since you are testing the process of creation and validation itself.
Related
I am trying to create User using
User.create ({email:"ayzabc66#gmail.com", password:"password",password_confirmation:"password"})
but I keep on getting below error:
/.rvm/gems/ruby-3.0.0/gems/activemodel-7.0.3/lib/active_model/attribute_assignment.rb:51:in `_assign_attribute': unknown attribute 'password' for User. (ActiveModel::UnknownAttributeError)
I have already uncommented gem "bcrypt", "~> 3.1.7" and ran bundle.
This is what my user.rb looks like:
class User < ApplicationRecord
#email :string
#password_digest:string
#password:string
#password_confirmation:string virtual
has_secure_password
end
And my db>migrate looks like:
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :email
t.string :password_digest
t.timestamps
end
end
end
If you run the following in Rails Console, do you get the list of attributes that you would expect?
=> User
If so, I would try uncommenting the lines that you have in your User model and see if that helps, so it would look like this:
class User < ApplicationRecord
email :string
password_digest:string
password:string
password_confirmation:string virtual
has_secure_password
end
I seem to be facing the general problem that directive driven mutations won't properly work when unit testing them. Using GraphQL Playground everything works as expected and the User model is auto detected. But if unit testing the same functionality it fails.
All config values are properly set (as I can see from regular requests not failing):
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => ClientName\PackageName\Models\User::class,
],
...
]
And lighthouse config values also set to the namespace above!
So for example this mutation would trigger the UserPolicy class and after that automatically call the create method of the User class:
type Mutation #protect(guards: ["api"]) {
"Create user"
createUser(data: NewUserInput! #spread): User
#can(ability: "create")
#create
}
But calling the same request via phpunit would result in errors:
for #can it returns a not authorized error and if I remove #can it only updates the email field of the user model as if mass assignment is not properly set.
When I now change the above schema definition to:
type Mutation #protect(guards: ["api"]) {
"Create user"
createUser(data: NewUserInput! #spread): User
#can(ability: "create" model: "ClientName\\PackageName\\Models\\User")
#create(model: "ClientName\\PackageName\\Models\\User")
}
And it works as expected within phpunit tests!
Now I suspect that within tests there is not User model found or not the appropriate but I cannot understand why this is.
Does anyone have any idea?
I'm using Meteor's account-entry package to handle the signin-signup action of my web app. To add a Confirm Password field to the sign up form, this is what I've done (in CoffeeScript):
AccountsEntry.config
logo: '/logo.png'
homeRoute: 'main'
dashboardRoute: 'main'
profileRoute: '/profile'
extraSignUpFields: [
field: "confirmPassword"
label: "Confirm Password"
type: "password"
,
field: "name"
label: "Full Name"
placeholder: "Full Name"
type: "text"
required: true
,
field: "position"
label: "Position"
placeholder: "Developer"
type: "text"
]
The problem with this approach is that: it also save the confirmPassword field to the database, so that when someone access the database > users collection, they can clearly see every users' password in confirmPassword field - which is very bad.
I don't know how to fix this problem yet. I think there may be an attribute which decide whether a specific field should be store in the database or not, but I haven't figured it out yet ! (the accounts-entry package documentation seems not detailed enough to me, I have to say :( )
Can you guys help me with this problem ? Thanks so much in advance !
The lack of a password confirmation field is a known issue with accounts-entry.
On the other hand, the publish function for the users collection should only publish the strictly necessary fields. By default, only username, emails and profile are published to the client.
Anyway, you should not store the confirmPassword in the database to begin with. To do that, hook into Accounts.onCreateUser and delete that field before returning the user object:
Accounts.onCreateUser(function (options, user) {
delete user.confirmPassword; // or: delete user.profile.confirmPassword;
return user;
});
I would like to create a user in my fixture and to be able to sign in as that user. For this reason, I need to somehow set the password of the user (if I try to sign in without a password, I get a 'User has no password set' validation error).
So far I only have:
var joeId = Meteor.users.insert({
username: 'joe'
})
Meteor complains about "Error: Accounts.createUser with callback not supported on the server yet." as of Meteor 0.6.4 if trying to create user on server side fixtures.
Just add the following after creating Joe:
Accounts.setPassword(joeId, 'my great password')
Using http://docs.meteor.com/#accounts_createuser you can do this:
Accounts.createUser({
'username' : 'John Doe',
'email' : 'john#doe.com',
'password' : 'abc123' //encrypted automatically
}, function(err){
if(typeof err === 'undefined'){
//account created successfully you might want to send an email to the user using Account.sendEnrollmentEmail()
}
});
Which will login with that user on successful creation.
Consider a JSON object such as:
{
name: 'ben',
employer: 'The Sherwin-Williams Company',
emails: ['ben#gmail.com', 'ben#sherwin-williams.com']
}
In MongoDB you can index the emails field such that you can find any object with 'ben#gmail.com' as an email. Is this possible in Riak? I couldn't tell from reading the docs.
You certainly can, but you have to manually enter the index entries.
Here's an example in Ruby:
require 'riak'
client = Riak::Client.new(:protocol => "pbc", :host => "127.0.0.1", :pb_port => 10047, :http_port => 10048)
ben = client['people'].get_or_new('ben')
ben.data = { :name => "ben",
:employer => "The Sherwin-Williams Company",
:emails => ['ben#gmail.com', 'ben#sherwin-williams.com'] }
ben.indexes['email_bin'] << "ben#gmail.com"
ben.indexes['email_bin'] << "ben#sherwin-williams.com"
ben.store
Now you can look it up via the ruby library, or through your web browser at http://127.0.0.1:10018/buckets/people/index/email_bin/ben#gmail.com
On my system this returns:
{"keys":["ben"]}
I know the Java and the Ruby Riak libraries support adding/editing index entries, I will have to check on the others and get back to you though.