forked from External/greenlight
test sessions controller and fix style
This commit is contained in:
parent
337345560d
commit
a6b313f38e
6
Gemfile
6
Gemfile
|
@ -58,6 +58,9 @@ gem 'tabler-rubygem'
|
|||
# Use Capistrano for deployment
|
||||
# gem 'capistrano-rails', group: :development
|
||||
|
||||
# Ruby linting.
|
||||
gem 'rubocop', require: false
|
||||
|
||||
group :production do
|
||||
# Use a postgres database in production.
|
||||
gem 'pg', '~> 0.18'
|
||||
|
@ -69,9 +72,6 @@ group :development, :test do
|
|||
|
||||
# Environment configuration.
|
||||
gem 'dotenv-rails'
|
||||
|
||||
# Ruby linting.
|
||||
gem 'rubocop'
|
||||
end
|
||||
|
||||
group :test do
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SessionsController < ApplicationController
|
||||
# GET /users/login
|
||||
def new
|
||||
end
|
||||
|
||||
# GET /users/logout
|
||||
def destroy
|
||||
logout
|
||||
|
@ -14,7 +10,7 @@ class SessionsController < ApplicationController
|
|||
# POST /users/login
|
||||
def create
|
||||
user = User.find_by(email: session_params[:email])
|
||||
if user.&authenticate(session_params[:password])
|
||||
if user&.authenticate(session_params[:password])
|
||||
login(user)
|
||||
end
|
||||
end
|
||||
|
@ -35,6 +31,9 @@ class SessionsController < ApplicationController
|
|||
def omniauth
|
||||
user = User.from_omniauth(request.env['omniauth.auth'])
|
||||
login(user)
|
||||
rescue => e
|
||||
logger.error "Error authenticating via omniauth: #{e}"
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
# POST /auth/failure
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe ErrorsController, type: :controller do
|
||||
|
||||
describe "GET #not_found" do
|
||||
it "returns not_found" do
|
||||
get :not_found
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe MainController, type: :controller do
|
||||
|
||||
describe "GET #index" do
|
||||
it "returns success" do
|
||||
get :index
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe RoomsController, type: :controller do
|
||||
|
||||
end
|
|
@ -0,0 +1,92 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe SessionsController, type: :controller do
|
||||
before(:all) do
|
||||
@user = create(:user, password: "example", password_confirmation: "example")
|
||||
end
|
||||
|
||||
describe "GET #destroy" do
|
||||
before(:each) do
|
||||
@request.session[:user_id] = @user.id
|
||||
get :destroy
|
||||
end
|
||||
|
||||
it "should logout user" do
|
||||
expect(@request.session[:user_id]).to be_nil
|
||||
end
|
||||
|
||||
it "should redirect to root" do
|
||||
expect(response).to redirect_to(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST #create" do
|
||||
it "should login user in if credentials valid" do
|
||||
post :create, params: {
|
||||
session: {
|
||||
email: @user.email,
|
||||
password: "example",
|
||||
},
|
||||
}
|
||||
|
||||
expect(@request.session[:user_id]).to eql(@user.id)
|
||||
end
|
||||
|
||||
it "should not login user in if credentials invalid" do
|
||||
post :create, params: {
|
||||
session: {
|
||||
email: @user.email,
|
||||
password: "invalid",
|
||||
},
|
||||
}
|
||||
|
||||
expect(@request.session[:user_id]).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET/POST #omniauth" do
|
||||
before(:all) do
|
||||
OmniAuth.config.test_mode = true
|
||||
|
||||
OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new(
|
||||
provider: "twitter",
|
||||
uid: "twitter-user",
|
||||
info: {
|
||||
email: "user@twitter.com",
|
||||
name: "Twitter User",
|
||||
nickname: "username",
|
||||
image: "example.png",
|
||||
},
|
||||
)
|
||||
|
||||
OmniAuth.config.on_failure = proc { |env|
|
||||
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
|
||||
}
|
||||
end
|
||||
|
||||
it "should create and login user with omniauth" do
|
||||
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
|
||||
get :omniauth, params: { provider: :twitter }
|
||||
|
||||
u = User.last
|
||||
expect(u.provider).to eql("twitter")
|
||||
expect(u.email).to eql("user@twitter.com")
|
||||
expect(@request.session[:user_id]).to eql(u.id)
|
||||
end
|
||||
|
||||
it "should redirect to root on invalid omniauth login" do
|
||||
request.env["omniauth.auth"] = :invalid_credentials
|
||||
get :omniauth, params: { provider: :twitter }
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
end
|
||||
|
||||
it "should not create session without omniauth env set" do
|
||||
get :omniauth, params: { provider: 'google' }
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,28 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe UsersController, type: :controller do
|
||||
|
||||
let(:user_params) {
|
||||
let(:user_params) do
|
||||
{
|
||||
user: {
|
||||
name: "Example",
|
||||
email: "example@example.com",
|
||||
password: "password",
|
||||
password_confirmation: "password"
|
||||
}
|
||||
password_confirmation: "password",
|
||||
},
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
let(:invalid_params) {
|
||||
let(:invalid_params) do
|
||||
{
|
||||
user: {
|
||||
name: "Invalid",
|
||||
email: "example.com",
|
||||
password: "pass",
|
||||
password_confirmation: "invalid"
|
||||
}
|
||||
passwrd_confirmation: "invalid",
|
||||
},
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
describe "GET #new" do
|
||||
it "assigns a blank user to the view" do
|
||||
|
@ -74,10 +75,6 @@ describe UsersController, type: :controller do
|
|||
expect(@user.email).to eql("example@example.com")
|
||||
end
|
||||
|
||||
it "properly updates user password" do
|
||||
|
||||
end
|
||||
|
||||
it "renders #edit on unsuccessful save" do
|
||||
@user = create(:user)
|
||||
|
||||
|
|
Loading…
Reference in New Issue