-
Notifications
You must be signed in to change notification settings - Fork 6
Test Authentication against FireBase #28
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
Closed
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe V1::AuthenticatedController, type: :controller do | ||
| describe '#authentication' do | ||
|
|
||
| context 'when the to email is invalid' do | ||
| let(:invalid_email) { 'abc@xyz.' } | ||
|
|
||
| it 'shows json error response' do | ||
| post :authentication, params: { to: invalid_email } | ||
|
|
||
| response_json = JSON.parse response.body | ||
|
|
||
| expect(response.status).to be(200) | ||
| expect(response_json['success']).to be_falsey | ||
| expect(response_json['errors'].to_s).to include('This e-mail abc@xyz. is inválid') | ||
| end | ||
| end | ||
|
|
||
| context 'when the to email is valid' do | ||
| context 'user exists in Firebase'do | ||
| context 'user is confirmed', vcr: { cassette_name: 'firebase/existing_confirmed_user' } do | ||
| let(:confirm_email) { 'abc@example.com' } | ||
|
|
||
| before do | ||
| add_confirmed_user_to_firebase(confirm_email) | ||
| end | ||
|
|
||
| it 'redirects to authentication error page if a confirmed user tries to authenticate' do | ||
| post :authentication, params: { to: confirm_email } | ||
|
|
||
| expect(response).to redirect_to('http://www.freesendmails.com/authentication-user-error') | ||
| end | ||
| end | ||
|
|
||
| context 'user is not confirmed', vcr: { cassette_name: 'firebase/existing_unconfirmed_user' } do | ||
| let(:user_email) { 'xyz@example.com' } | ||
|
|
||
| before do | ||
| add_unconfirmed_user_to_firebase(user_email) | ||
| end | ||
|
|
||
| it 'adds the user to Firebase again' do | ||
| post :authentication, params: { to: user_email } | ||
|
|
||
| expect(firebase_user_emails.count{|x| x == user_email}).to be(2) | ||
| expect(response).to redirect_to('http://www.freesendmails.com/authentication-user-success') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'user do not exist in firebase', vcr: { cassette_name: 'firebase/add_user' } do | ||
| let(:new_email) { 'abcxyz@example.com' } | ||
|
|
||
| it 'adds the user to Firebase and redirects to authentication success page' do | ||
| post :authentication, params: { to: new_email } | ||
|
|
||
| expect(firebase_user_emails).to include(new_email) | ||
| expect(response).to redirect_to('http://www.freesendmails.com/authentication-user-success') | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'user confirmation #authentication_url' do | ||
| let(:user_email) { 'qwerty@example.com' } | ||
| let(:user_token) { 'token1234' } | ||
|
|
||
| it 'confirms user in Firebase', vcr: { cassette_name: 'firebase/confirm_user' } do | ||
| add_unconfirmed_user_to_firebase(user_email, user_token) | ||
|
|
||
| get :authentication_url, params: { token_authentication: user_token } | ||
|
|
||
| firebase_user = firebase_find_users(user_email).first | ||
|
|
||
| expect(firebase_user['confirmated']).to be_truthy | ||
| expect(response).to redirect_to('http://www.freesendmails.com/authentication-user-success-authenticated') | ||
| end | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| module FirebaseHelper | ||
| def firebase_users | ||
| firebase_client.get('users_authenticated') | ||
| end | ||
|
|
||
| def firebase_find_users(email) | ||
| if firebase_users && firebase_users.body.present? | ||
| firebase_users.body.values.select {|u| u['email'] == email} | ||
| end | ||
| end | ||
|
|
||
| def firebase_user_emails | ||
| firebase_users.body.values.map {|u| u['email']} if firebase_users && firebase_users.body.present? | ||
| end | ||
|
|
||
| def add_confirmed_user_to_firebase(email, token=nil) | ||
| user_params = { | ||
| email: email, | ||
| confirmated: true, | ||
| created: Date.today, | ||
| priority: 1, | ||
| token: token || 'lmno1234' | ||
| } | ||
| firebase_client.push('users_authenticated', user_params) | ||
| user_params | ||
| end | ||
|
|
||
| def add_unconfirmed_user_to_firebase(email, token=nil) | ||
| user_params = { | ||
| email: email, | ||
| confirmated: false, | ||
| created: Date.today, | ||
| priority: 1, | ||
| token: token || 'abcd9876' | ||
| } | ||
| firebase_client.push('users_authenticated', user_params) | ||
| user_params | ||
| end | ||
|
|
||
| def firebase_client | ||
| Firebase::Client.new( | ||
| Rails.application.secrets.secret_url_fire_base, | ||
| Rails.application.secrets.secret_key_fire_base | ||
| ) | ||
| end | ||
| end | ||
Oops, something went wrong.
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.
we can use
firebase_user_emailsfunction here and then filter email. Additional check can be preventedThere 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.
@prashuchaudhary I want the whole user JSON here, because I need to compare various other fields in the User hash in the specs. Using
firebase_user_emailswill give me just the email.