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

Use redirect url to edit from initializer as well #1228

Merged

Conversation

MaicolBen
Copy link
Collaborator

Resolves #1219

@MaicolBen
Copy link
Collaborator Author

can you @dks17 review this one?

@@ -188,5 +182,13 @@ def with_reset_password_token token
def render_not_found_error
render_error(404, I18n.t('devise_token_auth.passwords.user_not_found', email: @email))
end

def redirect_url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks not bad.
I think you may rewrite it something like this:

    before_action :validate_redirect_url_param, only: [:create, :edit]

    private

    def validate_redirect_url_param
      # give redirect value from params priority
      @redirect_url ||= params.fetch(
        :redirect_url,
        DeviseTokenAuth.default_password_reset_url
      )

      render_create_error_missing_redirect_url unless @redirect_url
      render_create_error_not_allowed_redirect_url if blacklisted_redirect_url?
    end

    def redirect_url
      @redirect_url
    end

By this approach you can cover two actions at the same code. I suppose edit action needs the same redirect_url validation.

Copy link
Contributor

@dks17 dks17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to add some tests for edit action. To test redirect_url validation you may use tests for create action.

      describe 'not redirect_url should return 401' do
        before do
          @auth_headers = @resource.create_new_auth_token
          @new_password = Faker::Internet.password

          put :edit,
               params: { email: 'chester@cheet.ah' }
          @data = JSON.parse(response.body)
        end

        test 'response should fail' do
          assert_equal 401, response.status
        end

        test 'error message should be returned' do
          assert @data['errors']
          assert_equal(
            @data['errors'],
            [I18n.t('devise_token_auth.passwords.missing_redirect_url')]
          )
        end
      end

@MaicolBen MaicolBen force-pushed the chore/add-redirect-to-edit branch 3 times, most recently from 5a99e6f to b164291 Compare November 4, 2018 21:28
Copy link
Collaborator Author

@MaicolBen MaicolBen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dks17 thank you for your review! I used your code

app/controllers/devise_token_auth/passwords_controller.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@dks17 dks17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check error rendering if a redirect_url is blacklisted.
return render_error_not_allowed_redirect_url if blacklisted_redirect_url?

Please, add test into group is below for edit action too. Like you did it good for missing redirect_url.

describe 'Using redirect_whitelist' do
before do
@resource = create(:user, :confirmed)
@good_redirect_url = Faker::Internet.url
@bad_redirect_url = Faker::Internet.url
DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
end
teardown do
DeviseTokenAuth.redirect_whitelist = nil
end
test 'request to whitelisted redirect should be successful' do
post :create,
params: { email: @resource.email,
redirect_url: @good_redirect_url }
assert_equal 200, response.status
end
test 'request to non-whitelisted redirect should fail' do
post :create,
params: { email: @resource.email,
redirect_url: @bad_redirect_url }
assert_equal 422, response.status
end
test 'request to non-whitelisted redirect should return error message' do
post :create,
params: { email: @resource.email,
redirect_url: @bad_redirect_url }
@data = JSON.parse(response.body)
assert @data['errors']
assert_equal @data['errors'],
[I18n.t('devise_token_auth.passwords.not_allowed_redirect_url',
redirect_url: @bad_redirect_url)]
end
end

@MaicolBen MaicolBen force-pushed the chore/add-redirect-to-edit branch 3 times, most recently from 0d1f5fd to f2b2961 Compare November 6, 2018 01:39
@MaicolBen MaicolBen force-pushed the chore/add-redirect-to-edit branch from f2b2961 to a512341 Compare December 1, 2018 15:31
@MaicolBen
Copy link
Collaborator Author

@dks17 ready! sorry for the delay

@dks17
Copy link
Contributor

dks17 commented Dec 2, 2018

@MaicolBen Please, read I have wrote in suggested changes message. I don't insist you have to add those tests. I would like you do it.

So you have fixed #1219 @MaicolBen. Good job. Approved.

@MaicolBen
Copy link
Collaborator Author

MaicolBen commented Dec 3, 2018

Please, add test into group is below for edit action too

This one? I added it 20 days ago, thanks

@MaicolBen MaicolBen merged commit 2e9f001 into lynndylanhurley:master Dec 3, 2018
@MaicolBen MaicolBen deleted the chore/add-redirect-to-edit branch December 3, 2018 13:48
@dks17
Copy link
Contributor

dks17 commented Dec 3, 2018

You added tests in this group

describe 'not redirect_url should return 401' do

to cover that line
return render_create_error_missing_redirect_url unless @redirect_url

You did it well.

I asked you add tests in this group for edit action too


to cover that line
return render_error_not_allowed_redirect_url if blacklisted_redirect_url?

Maybe I expressed my thought unclearly before.

@MaicolBen
Copy link
Collaborator Author

@dks17 Sorry I misunderstood you, you wrote it right, the PR is #1247

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.

2 participants