Skip to content
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

Merged

Conversation

shubhscoder
Copy link
Contributor

Fixes #3848
Additions :

  1. Added generate_token functions that generates a token for the user
  2. Added validate_token function that verifies the generated token and makes sure that the token is generated within 24 hours.

Thanks!

@shubhscoder
Copy link
Contributor Author

@Radhikadua123 , Does this seem fine?
@publiclab/reviewers

@plotsbot
Copy link
Collaborator

plotsbot commented Nov 7, 2018

1 Message
📖 @shubhscoder Thank you for your pull request! I’m here to help with some tips and recommendations. Please take a look at the list provided and help us review and accept your contribution! And don’t be discouraged if you see errors – we’re here to help.

Generated by 🚫 Danger

…_token' into add_helper_generate_and_validate_token
@jywarren
Copy link
Member

jywarren commented Nov 7, 2018

Aha, cool! Would you consider adding a simple test to /test/unit/user_test.rb to confirm these work? Thank you!

@jywarren
Copy link
Member

jywarren commented Nov 7, 2018

@Radhikadua123 this is looking good, huh? 😄

@shubhscoder
Copy link
Contributor Author

shubhscoder commented Nov 7, 2018 via email

@shubhscoder
Copy link
Contributor Author

@jywarren . I have added the tests. Thank you!


test 'generate token and validate token for user email verification' do
all_users = User.where("id<?",3)
for i in all_users
Copy link
Contributor

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. :)

@Radhikadua123
Copy link
Contributor

Radhikadua123 commented Nov 7, 2018

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.

  1. Test to confirm that token generated more than 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

I'll close your other PRs to keep reviewboard clean.

@jywarren
Copy link
Member

jywarren commented Nov 8, 2018 via email

@shubhscoder
Copy link
Contributor Author

shubhscoder commented Nov 8, 2018 via email

@shubhscoder
Copy link
Contributor Author

@jywarren @Radhikadua123 I have added the failing tests. Check if this looks good. Thank you!

Copy link
Contributor

@Radhikadua123 Radhikadua123 left a 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. :)

generated_token = generated_token[2,generated_token.length]
begin
assert_not_equal all_users[0].validate_token(generated_token), true
rescue => error
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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.

@@ -416,6 +417,22 @@ def customize_digest(type)
end
end

def generate_token
user_id_and_time = [id, Time.now]
Copy link
Contributor

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].

@shubhscoder
Copy link
Contributor Author

@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 .

@Radhikadua123
Copy link
Contributor

Radhikadua123 commented Nov 9, 2018

@shubhscoder Thanks for fixing the issues!

If this goes fine, then I will start working on #3850 .

Awesome!

@jywarren It looks good to me. Please review the changes.

@jywarren
Copy link
Member

jywarren commented Nov 9, 2018

Oh, lovely - thanks!!! Great tests, too!

@jywarren jywarren merged commit 788ff6f into publiclab:master Nov 9, 2018
@shubhscoder shubhscoder deleted the add_helper_generate_and_validate_token branch November 10, 2018 09:17
@SidharthBansal SidharthBansal added this to the OAuth milestone Dec 18, 2018
SrinandanPai pushed a commit to SrinandanPai/plots2 that referenced this pull request May 5, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants