how Inject headers to OmniAuth mock tests - ruby-on-rails

I'm using omniauth-google-oauth2 gem to log in via google account. recently I saw this post https://developers.googleblog.com/2020/08/guidance-for-our-effort-to-block-less-secure-browser-and-apps.html. even though it will not affect to the regular users I'm worried about unit tests. which i guess use headless browsers. so I would like to test that by injecting headers as described in this post(Google-Accounts-Check-OAuth-Login:true). couldn't find any api methods for that on omiauth documentations.
OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new(
provider: "google",
uid: "123545",
info: {
email: EMAIL,
first_name: FIRST_NAME,
last_name: LAST_NAME,
}
)
visit new_user_registration_path
click_link "Sign in with Google"

Related

How to assign global variable inside Rswag 2.3.2 spec tests?

I'm new to RoR, a real newbie. But I got a project that need RoR for the backend development. And in this project for the API documentation is using Rswag. and I hit a wall when tried to authorizing the endpoints using JWT, which I want to create the user and the JWT token on the global, similar to what Rspec can do.
But I got a problem when trying to assign a global variable into the rswag tests, I already tried using the before(:each), let() and let!() as well, but they still didn't work which working on the Rspec
require "swagger_helper"
RSpec.describe "Profile API", type: :request do
# I also tried to assign the variables before each test cases
#
# let(:user) { FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456799") }
# --- OR ---
# before(:each) do
# #user = FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456799")
# end
path "/api/v1/profiles" do
post "Create new profile" do
tags "Profile"
consumes "application/json"
produces "application/json"
security [ Bearer: {} ]
parameter name: :profile, in: :body, schema: {
type: :object,
properties: {
first_name: {type: :string},
last_name: {type: :string},
},
required: [:first_name]
}
# I've tried put it here so all the test blocks can access it
# let!(:user) { FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456789") }
response 201, "All fields filled" do
let(:"Authorization") { "Bearer #{auth_header(#user)}" }
let(:profile) { {first_name: "John", last_name: "Doe"} }
run_test! do |response|
expect(response_body).to eq({
"first_name" => "John",
"last_name" => "Doe"
})
end
end
end
end
And here is the error that I got when I tried using let() or let!()
and if I tried to use the Before Block, it doesn't return any errors, but it's also didn't get triggered when I put the byebug in the before block.
currently here are the installed version lists:
Ruby version 3.0.0
Rails version 6.1.1
Rspec-rails gem version 4.0.2
Rswag gem version 2.3.2
And here is the closest answer I can found in the issues, but this doesn't resolve the problem I'm facing now
https://github.com/rswag/rswag/issues/168#issuecomment-454797784
Any bits of help is appreciated, thank you :smile:

Omniauth-facebook hash not showing Facebook 'username' field, using rails 3

I've set up my rails 3 app to use omniauth-facebook, everything works fine but when I look at the omniauth hash as yaml there is no 'nickname' field, I can get the 'name' and 'email' but my validations fail to create a user because I can't grab the nickname for my app's username requirement.
I've double checked my facebook account, under "Settings", and my username is displayed. ANy ideas why this would be happening? Thanks for the help.
Here is an example of my omniauth hash, i've placed 'X' for privacy reason:
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: 'XXXXXXXXXXXXX'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
email: XXXX#XXXX.XX
name: XXXX XXXX
first_name: XXXX
last_name: XXXX
image: http://graph.facebook.com/XXXXXXXXXXXXX/picture
urls: !ruby/hash:OmniAuth::AuthHash
Facebook: https://www.facebook.com/app_scoped_user_id/XXXXXXXXXXXXX/
verified: true
credentials: !ruby/hash:OmniAuth::AuthHash
token: ACCESS_TOKEN
expires_at: 1406357169
expires: true
extra: !ruby/hash:OmniAuth::AuthHash
raw_info: !ruby/hash:OmniAuth::AuthHash
id: 'XXXXXXXXXXXXXXX'
email: XXXX#XXXX.XX
first_name: XXXX
gender: male
last_name: XXXX
link: https://www.facebook.com/app_scoped_user_id/XXXXXXXXXXXXX/
locale: en_US
name: XXXX XXXX
timezone: 7
updated_time: '2014-05-07T00:58:12+0000'
verified: true
There's no field nickname, do you mean username? If so it's no longer available if you use the Graph API v2.0.
References:
https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
https://developers.facebook.com/docs/graph-api/reference/v2.0/user/#fields
I know this is an old question.
Since facebook now does not support fetching username at time of login, a workaround for getting user name would be, to call GraphRequest.newMeRequest() from the onSuccess() method of facebook login button

HTTParty giving 401 using http basic authentication

I have two applications. One contains an API (Rails) and the second (Sinatra) is a test application to check if the API is working correctly.
In the test application I can get all of the information provided by my API using HTTParty. However, when I try to add http basic authentication in the API application,
the test application always gets a 401 error.
In the controller which provides the API I have written
http_basic_authenticate_with name: "admin", password: "admin"
In my test application I have written
authenticate = {name: "admin", password: "admin"}
options = {query: {username: params[:username], email: params[:email]},
basic_auth: authenticate}
response = HTTParty.get('http://localhost:3000/search.json', options)
What am I doing wrong?
Your key for the username is incorrect:
authenticate = {:username => 'admin', :password => 'admin'}
This should work

Cucumber devise sign in step failing (rails 3.2, cucumber, capybara, rspec)

Everytime i run the default devise user_step test " sign in" it fails.
Strange thing is the error i get
Scenario: User signs in successfully # features/users/sign_in.feature:12
Given I exist as a user # features/step_definitions/user_steps.rb:58
And I am not logged in # features/step_definitions/user_steps.rb:49
When I sign in with valid credentials # features/step_definitions/user_steps.rb:72
Then show me the page # features/step_definitions/user_steps.rb:152
And I see a successful sign in message # features/step_definitions/user_steps.rb:156
expected to find text "Signed in successfully." in "Mywebsite Follow us How it works More Login × **Invalid email or password**. Login * Email Password Remember me Not a member yet? Sign Up Now!Forgot your password?Didn't receive confirmation instructions? Rails Tutorial " (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/user_steps.rb:157:in `/^I see a successful sign in message$/'
features/users/sign_in.feature:17:in `And I see a successful sign in message'
As you see capybara/cucumber tries to connect but gets "Invalid email or password"
So I used a trick seen on SO and added to see what capybara really was getting
Then /^show me the page$/ do
save_and_open_page
end
It tries to use the credentials I put on top of my file user_steps.rb:
def create_visitor
#visitor ||= { :name => "Testy McUserton", :email => "example#example.com",
:password => "please", :password_confirmation => "please" }
end
But it gets me "invalid (i can see example#example.com written in the email field on the capyabra page so it understands that i want this to be the email.
I'm lost. Why doesn't it work ?
You need to create the user in the database first. User.create(name: "Testy McUserton", email: "example#example.com", password: "please", :password_confirmation: "please"). Then you can login with the credentials. You can also use factorygirl link

http basic authentication testing in cucumber

I'm trying to test logging in via http basic but am continually getting errors, I've uploaded the code here
I have http_basic_authenticate_with name: "name", password: "password"
in my application controller
My step definition for logging in is;
Given /^I login as admin$/ do
authorize "name", "password"
end
but it doesn't work and I get the error
expected there to be content "Posts" in "HTTP Basic: Access denied.\n"
(RSpec::Expectations::ExpectationNotMetError)
can someone tell me how to get cucumber/capybara to log in?
I got it to work with one of two different approaches; take your pick:
### Following works ONLY if performed first before even going to a page!!!
if page.driver.respond_to?(:basic_auth)
puts 'Responds to basic_auth'
page.driver.basic_auth(username, password)
elsif page.driver.respond_to?(:basic_authorize)
puts 'Responds to basic_authorize'
page.driver.basic_authorize(username, password)
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
puts 'Responds to browser_basic_authorize'
page.driver.browser.basic_authorize(username, password)
else
raise "I don't know how to log in!"
end
My tests responded to browser_basic_authorize
or
encoded_login = ["#{username}:#{password}"].pack("m*")
page.driver.header 'Authorization', "Basic #{encoded_login}"
both of which I found at several places in my search.
Use the selenium web driver and visit the website with page.driver.visit("https://username:password#example.com")

Resources