omniauthのテストの方法は色々と記載あったのですが、request.env
の制御方法などの記事があまりなかったので、まとめました
※詳細な実装方法は参考記事から確認お願いします🙏
# support/omniauth_mock.rb
module OmniauthMocks
def line_mock
OmniAuth.config.mock_auth[:line] = OmniAuth::AuthHash.new({
"expires" => true,
"expires_at" => 1916195174, # unix timeは2030/9/21で生成
"refresh_token" => "UNEnCk8Z0Lzb62DaFY0a",
"token" => "eyJhbGciOiJIUzI1NiJ9.I3YcVOETzqoKsg0CqpjEDXETdYutQXJpOfH_wjrYCqtwjJbUoZMMy4M1IsZHB-PuU8IbkRZrJ6aoSyYmlTH-SLJdj51mMigJoJHJRtEmaICqTyX-UsLwFTwH6dP6JHz7VN7EqIYRpKKDcPMJQFGk_KRChXE_C7Mj-9eq8GHMRE4.I3YGhJ1gMDEz7F_nW6QpaUI6NjEV34y12XljcmhUvgA",
"provider" => "line",
"uid" => "123456",
"info" => {
"name" => "Mock User",
"image" => "http://mock_image_url.com",
"location" => "",
"email" => "mock@example.com",
"description" => "",
},
"credentials" => {
"token" => "mock_credentails_token",
"secret" => "mock_credentails_secret"
},
})
end
end
# requestのテスト一例
describe "GET /line" do
before { Rails.application.env_config['omniauth.auth'] = line_mock } # ここでenvを設定!!
it "oauthが渡ってこない場合エラーになる" do
Rails.application.env_config['omniauth.auth'] = nil
get user_line_omniauth_callback_path
# expect { subject }.to raise_error("request.env[omniauth.auth]がありません")
end
end