-
Notifications
You must be signed in to change notification settings - Fork 112
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
Adds verified_account event #1210
Conversation
**Why**: So users can have a record of when their account was verified
I'm not certain what the best way to test that the |
get :index | ||
user.reload | ||
|
||
expect(user.events.where(event_type: 8).size).to be 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enums give us a handy shorthand so you can call user.events.account_verified
and it will return all of the events for that enum type. Seems like that might be clearer than looking for the integer 8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I saw in the documentation that you can call ClassName.my_enum
but didn't make the leap to trying it with an instance. Thanks!
event_type: Event.event_types['account_verified'] | ||
).empty? | ||
|
||
create_user_event(:account_verified) if no_verified_event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we should def write a test for this -- controller spec would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally agree we should test it -- my comment was that I wasn't sure how to test it. Sorry if that was unclear!
I was under the impression that we don't test private methods directly?
When I try calling get :index
twice in the controller spec, I get undefined method encrypted' for nil:NilClass
from the encrypt_recovery_pii
method in the profile model. Since I can refresh the page in browser and not get an error, I'm thinking the way I'm trying to test a repeated action is wrong.
**Why**: Its cleaner than calling `where` and passing in a magic string
If I'm reading the code correctly, what you want to test is that if a user has already verified their account, the What I would do is create a separate class that handles the logic of event creation, and test that class separately. In the controller spec, all you would need to test is that the proper class was called. class CreateAccountVerifiedEvent
def initialize(user)
@user = user
end
def call
create_event_if_user_not_verified
end
private
attr_reader :user
def create_event_if_user_not_verified
return if user_verified?
Event.create(user_id: user.id, event_type: :account_verified)
end
def user_verified?
user.events.account_verified.present?
end
end In the controller: CreateAccountVerifiedEvent.new(current_user).call In the controller spec: event_creator = instance_double(CreateAccountVerifiedEvent)
allow(CreateAccountVerifiedEvent).to receive(:new).with(subject.current_user).and_return(event_creator)
expect(event_creator).to receive(:call) |
Stubbing a method on a discrete class seems clearer to me, I'll go with this approach |
@monfresh is it generally just accepted ruby syntax to have methods named |
**Why**: Breaking out the functionality is easier to test
It doesn't have to be named Here's a sample discussion of how people name their service classes and methods: You'll find many blog posts about these debates. |
require 'rails_helper' | ||
|
||
describe CreateVerifiedAccountEvent do | ||
let(:password) { 'password' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The User Factory already creates a password. For this test, all that matters is that the Event is tied to a user. This should be sufficient:
let(:user) do
create(:user) do |user|
user.events.create(event_type: :account_verified)
end
end
let(:eventless_user) { create(:user) }
Hey I see I'm a little late to this, but I think it's a break in a bad way to use a separate service class to create the event. I was able to discover all the user events we create by doing a I would very much like to keep the single Is the reason for the service class to encapsulate the logic to decide if there is already an event? What if we made a service class for checking? Ex |
Having a |
You can easily grep for event creations by grepping for |
I think wanting to remove controller methods and logic like that is totally reasonable, but I really like having a single chokepoint in the meantime. I think it would be great to make sure we keep the entire codebase with one style (since there are a finite number of events, we can be extra consistent). Ex a follow-up PR could quickly remove the Re: |
I'm fine with creating a service class for creating events, which would be similar to our UpdateUser class. I still think the |
Yes, I'm fine with that (new service class, interface, whatever), my thing is that since there are a manageable number of events right now, I want them to all use the same exact way right now. So I dislike that this PR would 1 event with a new way, but there are 6 events already existing using 1 other way. I don't mind if we change them all, as long as they're all the same |
I don't see how this is "another" way. The event is being created with |
By "another way" I mean different syntax, which means something different to grep for But we're getting really off course here, please feel free to disregard this and just go ahead and review & merge the PR as needed |
let(:user) do | ||
create(:user, :signed_up, password: password) do |user| | ||
create(:user, :signed_up) do |user| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user doesn't need to be :signed up
either. This will avoid generating a recovery code, which will speed up the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm % comments
**Why**: Password is implicitly added when factory creates user
189c072
to
539becd
Compare
Why: So users can have a record of when their account was verified
Screenshot: