-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support authenticate with facebook access token #793
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
Closed
Charlie-Hua
wants to merge
5
commits into
lynndylanhurley:master
from
Charlie-Hua:support-omniauth-facebook-access-token
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ced2d1d
add omniauth-facebook-access-token gem
Charlie-Hua 00c50f6
add tests for using facebook_access_token strategy
Charlie-Hua 7e8978a
add rendering for access_token strategy in omniauth_callbacks_controller
Charlie-Hua a932bad
Merge branch 'master' into support-omniauth-facebook-access-token
Charlie-Hua a9f275f
Merge branch 'master' into support-omniauth-facebook-access-token
Charlie-Hua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,183 @@ class OmniauthTest < ActionDispatch::IntegrationTest | |
@redirect_url = "http://ng-token-auth.dev/" | ||
end | ||
|
||
describe 'success callback' do | ||
setup do | ||
OmniAuth.config.mock_auth[:facebook_access_token] = OmniAuth::AuthHash.new({ | ||
:provider => 'facebook', | ||
:uid => '123545', | ||
:info => { | ||
name: 'chong', | ||
email: '[email protected]' | ||
} | ||
}) | ||
end | ||
|
||
before do | ||
DeviseTokenAuth::OmniauthCallbacksController.any_instance.stubs(:using_access_token_strategy?).returns(true) | ||
end | ||
|
||
after do | ||
DeviseTokenAuth::OmniauthCallbacksController.any_instance.unstub(:using_access_token_strategy?) | ||
end | ||
|
||
test 'user should have been created' do | ||
get_success | ||
assert @resource | ||
end | ||
|
||
test 'user should be assigned info from provider' do | ||
get_success | ||
assert_equal '[email protected]', @resource.email | ||
end | ||
|
||
test 'user should be assigned token' do | ||
get_success | ||
client_id = controller.auth_params[:client_id] | ||
token = controller.auth_params[:auth_token] | ||
expiry = controller.auth_params[:expiry] | ||
|
||
# the expiry should have been set | ||
assert_equal expiry, @resource.tokens[client_id][:expiry] | ||
# the token sent down to the client should now be valid | ||
assert @resource.valid_token?(token, client_id) | ||
end | ||
|
||
test 'response contains all expected data' do | ||
get_success | ||
assert_expected_data | ||
end | ||
|
||
test 'sign_in was called' do | ||
User.any_instance.expects(:sign_in) | ||
get_success | ||
end | ||
|
||
describe 'with default user model' do | ||
before do | ||
get_success | ||
end | ||
test 'request should determine the correct resource_class' do | ||
assert_equal 'User', controller.send(:omniauth_params)['resource_class'] | ||
end | ||
|
||
test 'user should be of the correct class' do | ||
assert_equal User, @resource.class | ||
end | ||
end | ||
|
||
describe 'with alternate user model' do | ||
before do | ||
get_via_redirect '/mangs/facebook_access_token', { | ||
favorite_color: @fav_color, | ||
name: @unpermitted_param | ||
} | ||
assert_equal 200, response.status | ||
@resource = assigns(:resource) | ||
end | ||
test 'request should determine the correct resource_class' do | ||
assert_equal 'Mang', controller.send(:omniauth_params)['resource_class'] | ||
end | ||
test 'user should be of the correct class' do | ||
assert_equal Mang, @resource.class | ||
end | ||
end | ||
|
||
describe 'pass additional params' do | ||
before do | ||
@fav_color = 'alizarin crimson' | ||
@unpermitted_param = "M. Bison" | ||
get_via_redirect '/auth/facebook_access_token', { | ||
favorite_color: @fav_color, | ||
name: @unpermitted_param | ||
} | ||
|
||
@resource = assigns(:resource) | ||
end | ||
|
||
test 'status shows success' do | ||
assert_equal 200, response.status | ||
end | ||
|
||
test 'additional attribute was passed' do | ||
assert_equal @fav_color, @resource.favorite_color | ||
end | ||
|
||
test 'non-whitelisted attributes are ignored' do | ||
refute_equal @unpermitted_param, @resource.name | ||
end | ||
end | ||
|
||
describe "oauth registration attr" do | ||
after do | ||
User.any_instance.unstub(:new_record?) | ||
end | ||
|
||
describe 'with existing user' do | ||
before do | ||
User.any_instance.expects(:new_record?).returns(false).at_least_once | ||
end | ||
|
||
test 'response does not contain oauth_registration attr' do | ||
|
||
get_via_redirect '/auth/facebook_access_token' | ||
|
||
assert_equal false, controller.auth_params.key?(:oauth_registration) | ||
end | ||
end | ||
|
||
describe 'with new user' do | ||
before do | ||
User.any_instance.expects(:new_record?).returns(true).at_least_once | ||
end | ||
|
||
test 'response contains oauth_registration attr' do | ||
|
||
get_via_redirect '/auth/facebook_access_token' | ||
|
||
assert_equal true, controller.auth_params[:oauth_registration] | ||
end | ||
end | ||
end | ||
|
||
describe 'using namespaces' do | ||
before do | ||
get_via_redirect '/api/v1/auth/facebook_access_token' | ||
|
||
@resource = assigns(:resource) | ||
end | ||
|
||
test 'request is successful' do | ||
assert_equal 200, response.status | ||
end | ||
|
||
test 'user should have been created' do | ||
assert @resource | ||
end | ||
|
||
test 'user should be of the correct class' do | ||
assert_equal User, @resource.class | ||
end | ||
end | ||
|
||
def assert_expected_data | ||
data_json = @response.body | ||
data = ActiveSupport::JSON.decode(data_json) | ||
expected_data = @resource.as_json.merge(controller.auth_params.as_json) | ||
expected_data = {'success' => true, 'data' => ActiveSupport::JSON.decode(expected_data.to_json).merge("message" => "deliverCredentials")} | ||
assert_equal(expected_data, data) | ||
end | ||
|
||
def get_success(params = {}) | ||
get_via_redirect '/auth/facebook_access_token', { | ||
favorite_color: @fav_color, | ||
name: @unpermitted_param | ||
}.merge(params) | ||
assert_equal 200, response.status | ||
@resource = assigns(:resource) | ||
end | ||
end | ||
|
||
describe 'success callback' do | ||
setup do | ||
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Extract this into a dedicated method, so it will be really easy to customize the response format.
Have a look at https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/registrations_controller.rb#L65 for an example.