-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add helper generate and validate token #3907
Add helper generate and validate token #3907
Conversation
@Radhikadua123 , Does this seem fine? |
Generated by 🚫 Danger |
…_token' into add_helper_generate_and_validate_token
Aha, cool! Would you consider adding a simple test to |
@Radhikadua123 this is looking good, huh? 😄 |
Yeah i ll do that 👍
…On Wed, Nov 7, 2018, 10:18 AM Jeffrey Warren ***@***.***> wrote:
Aha, cool! Would you consider adding a simple test to
/test/unit/user_test.rb to confirm these work? Thank you!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3907 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARofxeWcB6fk8KLEDEqlWrx8dQ45RUEgks5usmYBgaJpZM4YRvcy>
.
|
@jywarren . I have added the tests. Thank you! |
test/unit/user_test.rb
Outdated
|
||
test 'generate token and validate token for user email verification' do | ||
all_users = User.where("id<?",3) | ||
for i in all_users |
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.
It's fine to run it for one user only. :)
Hi @shubhscoder, Thanks a lot for the PR! That's the way we wanted to implement it. :) Can you please add more test for it to cover all the cases ? e.g.
I'll close your other PRs to keep reviewboard clean. |
oh, this is looking awesome. Great work! And thanks Radhika for helping
this along!
…On Wed, Nov 7, 2018 at 12:08 PM Radhika Dua ***@***.***> wrote:
Hi @shubhscoder <https://github.com/shubhscoder>,
Thanks a lot for the PR! :)
Can you please add more test for it to cover all the cases ?
e.g.
1. Test to confirm that token generated 24hrs back from now should be
treated as invalid.
2. Test to confirm that token for one user can't be used for any other
user.
3. Test to confirm that invalid token raises respective exception
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3907 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABfJzCdOizo8xGyu3dIlUxj2qUSwXdAks5usxOLgaJpZM4YRvcy>
.
|
Yeah I ll do that 👍
…On Wed, Nov 7, 2018, 10:38 PM Radhika Dua ***@***.***> wrote:
Hi @shubhscoder <https://github.com/shubhscoder>,
Thanks a lot for the PR! :)
Can you please add more test for it to cover all the cases ?
e.g.
1. Test to confirm that token generated 24hrs back from now should be
treated as invalid.
2. Test to confirm that token for one user can't be used for any other
user.
3. Test to confirm that invalid token raises respective exception
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3907 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARofxWJtfM9axrr8SZOi3F2f-iK2j--Pks5usxONgaJpZM4YRvcy>
.
|
@jywarren @Radhikadua123 I have added the failing tests. Check if this looks good. Thank you! |
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.
Awesome work @shubhscoder!
Sorry for these requested changes! Though things would work even without these fixes. It's just to maintain quality of code. :)
test/unit/user_test.rb
Outdated
generated_token = generated_token[2,generated_token.length] | ||
begin | ||
assert_not_equal all_users[0].validate_token(generated_token), true | ||
rescue => error |
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.
Please use assert_raise
for testing this.
test/unit/user_test.rb
Outdated
@@ -220,4 +221,27 @@ class UserTest < ActiveSupport::TestCase | |||
#as the username as "jeff" exists, hence username = "jeff" + 2 digit alphanumeric code will be created | |||
assert_not_equal jeffrey.username, "jeff" | |||
end | |||
|
|||
test 'generate token and validate token for user email verification' do |
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.
Almost there, just split these all these tests into multiple functions.
In unit tests, you should try tests to be as small as possible so that they test one small functionality properly. When they fail, developer easily get to know which test failed.
app/models/user.rb
Outdated
@@ -416,6 +417,22 @@ def customize_digest(type) | |||
end | |||
end | |||
|
|||
def generate_token | |||
user_id_and_time = [id, Time.now] |
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.
May be we should use hash
here instead of array ?
{ :id => id, :timestamp => Time.now }
That would be more descriptive than using something like decrypted_data[0]
.
@Radhikadua123 I thinked I have fixed them all! And thank you for guiding me through the process. If this goes fine, then I will start working on #3850 . |
@shubhscoder Thanks for fixing the issues!
Awesome! @jywarren It looks good to me. Please review the changes. |
Oh, lovely - thanks!!! Great tests, too! |
* Added is_verified column to users with default value false * added helper functions for generation and validation of tokens * Delete 20181103114645_add_is_verified_to_users.rb * Minor code fixes * Added tests for implemented helper functions * Added failing tests for helper functions * Added test to make sure that a token is not validated 24 hours after gen * Code quality changes
Fixes #3848
Additions :
Thanks!