forked from External/greenlight
work on tests
This commit is contained in:
parent
5347d902c0
commit
b452932767
|
@ -7,6 +7,7 @@ class SessionsController < ApplicationController
|
||||||
# GET /logout
|
# GET /logout
|
||||||
def destroy
|
def destroy
|
||||||
logout if current_user
|
logout if current_user
|
||||||
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|
||||||
# POST /login
|
# POST /login
|
||||||
|
@ -22,7 +23,7 @@ class SessionsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET/POST /auth/:provider/callback
|
# GET/POST /auth/:provider/callback
|
||||||
def omniauth_session
|
def omniauth
|
||||||
user = User.from_omniauth(request.env['omniauth.auth'])
|
user = User.from_omniauth(request.env['omniauth.auth'])
|
||||||
login(user)
|
login(user)
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,8 @@ class UsersController < ApplicationController
|
||||||
# POST /signup
|
# POST /signup
|
||||||
def create
|
def create
|
||||||
user = User.new(user_params)
|
user = User.new(user_params)
|
||||||
|
user.provider = "greenlight"
|
||||||
|
|
||||||
if user.save
|
if user.save
|
||||||
login(user)
|
login(user)
|
||||||
else
|
else
|
||||||
|
@ -18,6 +20,6 @@ class UsersController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def user_params
|
def user_params
|
||||||
params.require(:user).permit(:name, :email, :password, :password_confirmation)
|
params.require(:user).permit(:name, :email, :username, :password, :password_confirmation)
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -22,7 +22,7 @@ Rails.application.routes.draw do
|
||||||
get '/logout', to: 'sessions#destroy'
|
get '/logout', to: 'sessions#destroy'
|
||||||
|
|
||||||
# Handles Omniauth authentication.
|
# Handles Omniauth authentication.
|
||||||
match '/auth/:provider/callback', to: 'sessions#omniauth_session', via: [:get, :post], as: :omniauth_session
|
match '/auth/:provider/callback', to: 'sessions#omniauth', via: [:get, :post], as: :omniauth_session
|
||||||
get '/auth/failure', to: 'sessions#fail'
|
get '/auth/failure', to: 'sessions#fail'
|
||||||
|
|
||||||
root to: 'main#index'
|
root to: 'main#index'
|
||||||
|
|
|
@ -1,7 +1,35 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class RoomsControllerTest < ActionDispatch::IntegrationTest
|
class RoomsControllerTest < ActionDispatch::IntegrationTest
|
||||||
# test "the truth" do
|
|
||||||
# assert true
|
def setup
|
||||||
# end
|
@steve = users(:steve)
|
||||||
|
@mark = users(:mark)
|
||||||
|
|
||||||
|
@kitchen = rooms(:kitchen)
|
||||||
|
@garage = rooms(:garage)
|
||||||
|
|
||||||
|
@steve.room = @kitchen
|
||||||
|
@mark.room = @garage
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should redirect to root if not logged in.' do
|
||||||
|
get room_path(@kitchen.uid)
|
||||||
|
|
||||||
|
assert_redirected_to root_path
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should redirect to correct room if incorrect room.' do
|
||||||
|
post create_session_path, params: {session: {email: @mark.email, password: "mark12345"}}
|
||||||
|
get room_path(@kitchen.uid)
|
||||||
|
|
||||||
|
assert_redirected_to room_path(@garage.uid)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should render room if user is owner.' do
|
||||||
|
post create_session_path, params: {session: {email: @steve.email, password: "steve12345"}}
|
||||||
|
get room_path(@kitchen.uid)
|
||||||
|
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,27 +3,22 @@ require 'test_helper'
|
||||||
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@user = User.new(
|
@steve = users(:steve)
|
||||||
name: "Example User",
|
@kitchen = rooms(:kitchen)
|
||||||
username: "Username",
|
|
||||||
provider: "greenlight",
|
|
||||||
email: "user@example.com",
|
|
||||||
password: "example",
|
|
||||||
password_confirmation: "example"
|
|
||||||
)
|
|
||||||
|
|
||||||
@user.save!
|
@steve.room = @kitchen
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'can get login page.' do
|
||||||
|
get login_path
|
||||||
|
assert_response :success
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'can signin with greenlight account.' do
|
test 'can signin with greenlight account.' do
|
||||||
post create_session_path, params: {session: {email: @user.email, password: @user.password}}
|
post create_session_path, params: {session: {email: @steve.email, password: "steve12345"}}
|
||||||
|
|
||||||
assert_redirected_to room_path(@user.room.uid)
|
assert_redirected_to room_path(@steve.room.uid)
|
||||||
assert @user.id, session[:user_id]
|
assert @steve.id, session[:user_id]
|
||||||
end
|
|
||||||
|
|
||||||
test 'can signup for greenlight account.' do
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'can signup/login with omniauth.' do
|
test 'can signup/login with omniauth.' do
|
||||||
|
@ -49,15 +44,32 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
assert_not_nil user
|
assert_not_nil user
|
||||||
assert_redirected_to room_path(user.room.uid)
|
assert_redirected_to room_path(user.room.uid)
|
||||||
assert @user.id, session[:user_id]
|
assert user.id, session[:user_id]
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'handles omniauth failure.' do
|
||||||
|
OmniAuth.config.on_failure = Proc.new do |env|
|
||||||
|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
|
||||||
|
end
|
||||||
|
|
||||||
|
OmniAuth.config.mock_auth[:twitter] = :invalid_credentials
|
||||||
|
|
||||||
|
get "/auth/twitter"
|
||||||
|
|
||||||
|
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
|
||||||
|
|
||||||
|
assert_no_difference 'User.count' do
|
||||||
|
get omniauth_session_path(provider: "twitter")
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_redirected_to auth_failure_path(message: "invalid_credentials", strategy: "twitter")
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'can logout.' do
|
test 'can logout.' do
|
||||||
post create_session_path, params: {session: {email: @user.email, password: @user.password}}
|
post create_session_path, params: {session: {email: @steve.email, password: "steve12345"}}
|
||||||
assert @user.id, session[:user_id]
|
assert @steve.id, session[:user_id]
|
||||||
|
|
||||||
get logout_path
|
get logout_path
|
||||||
assert_not_equal @user.id, session[:user_id]
|
assert_not_equal @steve.id, session[:user_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,22 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class UsersControllerTest < ActionDispatch::IntegrationTest
|
class UsersControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
|
test 'can signup for greenlight account.' do
|
||||||
|
post signup_path, params: {
|
||||||
|
user: {
|
||||||
|
name: "Greenlight User",
|
||||||
|
username: "greenlight_user",
|
||||||
|
email: "greenlight@example.com",
|
||||||
|
password: "password",
|
||||||
|
password_confirmation: "password"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
user = User.find_by(email: "greenlight@example.com")
|
||||||
|
|
||||||
|
assert_not_nil user
|
||||||
|
assert_redirected_to room_path(user.room.uid)
|
||||||
|
assert user.id, session[:user_id]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
# model remove the '{}' from the fixture names and add the columns immediately
|
# model remove the '{}' from the fixture names and add the columns immediately
|
||||||
# below each fixture, per the syntax in the comments below
|
# below each fixture, per the syntax in the comments below
|
||||||
#
|
#
|
||||||
one: {}
|
|
||||||
# column: value
|
breakfast:
|
||||||
#
|
name: "Breakfast"
|
||||||
two: {}
|
|
||||||
# column: value
|
|
|
@ -4,8 +4,9 @@
|
||||||
# model remove the '{}' from the fixture names and add the columns immediately
|
# model remove the '{}' from the fixture names and add the columns immediately
|
||||||
# below each fixture, per the syntax in the comments below
|
# below each fixture, per the syntax in the comments below
|
||||||
#
|
#
|
||||||
one: {}
|
|
||||||
# column: value
|
kitchen:
|
||||||
#
|
uid: "13579"
|
||||||
two: {}
|
|
||||||
# column: value
|
garage:
|
||||||
|
uid: "02468"
|
|
@ -4,8 +4,17 @@
|
||||||
# model remove the '{}' from the fixture names and add the columns immediately
|
# model remove the '{}' from the fixture names and add the columns immediately
|
||||||
# below each fixture, per the syntax in the comments below
|
# below each fixture, per the syntax in the comments below
|
||||||
#
|
#
|
||||||
one: {}
|
|
||||||
# column: value
|
steve:
|
||||||
#
|
name: "Steve User"
|
||||||
two: {}
|
username: "steve"
|
||||||
# column: value
|
provider: "greenlight"
|
||||||
|
email: "steve@example.com"
|
||||||
|
password_digest: <%= BCrypt::Password.create('steve12345') %>
|
||||||
|
|
||||||
|
mark:
|
||||||
|
name: "Mark User"
|
||||||
|
username: "mark"
|
||||||
|
provider: "greenlight"
|
||||||
|
email: "mark@example.com"
|
||||||
|
password_digest: <%= BCrypt::Password.create('mark12345') %>
|
||||||
|
|
|
@ -3,30 +3,21 @@ require 'test_helper'
|
||||||
class MeetingTest < ActiveSupport::TestCase
|
class MeetingTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@user = User.new(
|
@steve = users(:steve)
|
||||||
name: "Example User",
|
|
||||||
username: "Username",
|
|
||||||
provider: "greenlight",
|
|
||||||
email: "user@example.com",
|
|
||||||
password: "example",
|
|
||||||
password_confirmation: "example"
|
|
||||||
)
|
|
||||||
|
|
||||||
@room = Room.new(user: @user)
|
@kitchen = rooms(:kitchen)
|
||||||
|
|
||||||
@meeting = Meeting.new(
|
@breakfast = meetings(:breakfast)
|
||||||
name: "Test Meeting",
|
@breakfast.room = @kitchen
|
||||||
room: @room
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "name should be present." do
|
test "name should be present." do
|
||||||
@meeting.name = nil
|
@breakfast.name = nil
|
||||||
assert_not @meeting.valid?
|
assert_not @breakfast.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should set uid on creation." do
|
test "should set uid on creation." do
|
||||||
@meeting.save
|
@breakfast.send(:generate_meeting_id)
|
||||||
assert @meeting.uid
|
assert @breakfast.uid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,37 +3,18 @@ require 'test_helper'
|
||||||
class RoomTest < ActiveSupport::TestCase
|
class RoomTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@user = User.new(
|
@steve = users(:steve)
|
||||||
name: "Example User",
|
@mark = users(:mark)
|
||||||
username: "Username",
|
|
||||||
provider: "greenlight",
|
|
||||||
email: "user@example.com",
|
|
||||||
password: "example",
|
|
||||||
password_confirmation: "example"
|
|
||||||
)
|
|
||||||
|
|
||||||
@room = Room.new(
|
@kitchen = rooms(:kitchen)
|
||||||
user: @user
|
@kitchen.user = @steve
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "#owned_by? should identify correct owner." do
|
test "#owned_by? should identify correct owner." do
|
||||||
assert @room.owned_by?(@user)
|
assert @kitchen.owned_by?(@steve)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "#owned_by? should identify incorrect owner." do
|
test "#owned_by? should identify incorrect owner." do
|
||||||
diff_user = User.new(
|
assert_not @kitchen.owned_by?(@mark)
|
||||||
name: "Different User",
|
|
||||||
username: "Diffname",
|
|
||||||
provider: "greenlight",
|
|
||||||
email: "diff@example.com",
|
|
||||||
)
|
|
||||||
|
|
||||||
assert_not @room.owned_by?(diff_user)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "should set uid on creation." do
|
|
||||||
@room.save
|
|
||||||
assert @room.uid
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,50 +1,48 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class UserTest < ActiveSupport::TestCase
|
class UserTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@user = User.new(
|
@steve = users(:steve)
|
||||||
name: "Example User",
|
|
||||||
username: "Username",
|
|
||||||
provider: "greenlight",
|
|
||||||
email: "user@example.com",
|
|
||||||
password: "example",
|
|
||||||
password_confirmation: "example"
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should be valid." do
|
test "should be valid." do
|
||||||
assert @user.valid?
|
assert @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "name should be present." do
|
test "name should be present." do
|
||||||
@user.name = nil
|
@steve.name = nil
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "email should be present." do
|
test "email should be present." do
|
||||||
@user.email = nil
|
@steve.email = nil
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "username should be present." do
|
test "username should be present." do
|
||||||
@user.username = nil
|
@steve.username = nil
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "provider should be present." do
|
test "provider should be present." do
|
||||||
@user.provider = nil
|
@steve.provider = nil
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should allow nil uid." do
|
test "should allow nil uid." do
|
||||||
@user.uid = nil
|
@steve.uid = nil
|
||||||
assert @user.valid?
|
assert @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should allow nil password." do
|
test "should allow nil password." do
|
||||||
@user.password = @user.password_confirmation = nil
|
@steve.password = @steve.password_confirmation = nil
|
||||||
assert @user.valid?
|
assert @steve.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
test "password should be longer than 6 characters if it exists." do
|
||||||
|
@steve.password = "short"
|
||||||
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should create user from omniauth" do
|
test "should create user from omniauth" do
|
||||||
|
@ -70,51 +68,51 @@ class UserTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
test "email addresses should be saved as lower-case." do
|
test "email addresses should be saved as lower-case." do
|
||||||
mixed_case = "ExAmPlE@eXaMpLe.CoM"
|
mixed_case = "ExAmPlE@eXaMpLe.CoM"
|
||||||
@user.email = mixed_case
|
@steve.email = mixed_case
|
||||||
@user.save
|
@steve.save
|
||||||
assert_equal mixed_case.downcase, @user.email
|
assert_equal mixed_case.downcase, @steve.email
|
||||||
end
|
end
|
||||||
|
|
||||||
test "email validation should reject invalid addresses." do
|
test "email validation should reject invalid addresses." do
|
||||||
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com]
|
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com]
|
||||||
invalid_addresses.each do |invalid_address|
|
invalid_addresses.each do |invalid_address|
|
||||||
@user.email = invalid_address
|
@steve.email = invalid_address
|
||||||
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid."
|
assert_not @steve.valid?, "#{invalid_address.inspect} should be invalid."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "email should be unique." do
|
test "email should be unique." do
|
||||||
duplicate_user = @user.dup
|
duplicate_user = @steve.dup
|
||||||
duplicate_user.email = @user.email.upcase
|
duplicate_user.email = @steve.email.upcase
|
||||||
@user.save
|
@steve.save
|
||||||
assert_not duplicate_user.valid?
|
assert_not duplicate_user.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "name should not be too long." do
|
test "name should not be too long." do
|
||||||
@user.name = "a" * 25
|
@steve.name = "a" * 25
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "email should not be too long." do
|
test "email should not be too long." do
|
||||||
@user.email = "a" * 50 + "@example.com"
|
@steve.email = "a" * 50 + "@example.com"
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "password should have a minimum length." do
|
test "password should have a minimum length." do
|
||||||
@user.password = @user.password_confirmation = "a" * 5
|
@steve.password = @steve.password_confirmation = "a" * 5
|
||||||
assert_not @user.valid?
|
assert_not @steve.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should authenticate on valid password." do
|
test "should authenticate on valid password." do
|
||||||
assert_not_equal @user.authenticate('example'), false
|
assert @steve.authenticate("steve12345")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should not authenticate on invalid password." do
|
test "should not authenticate on invalid password." do
|
||||||
assert_not @user.authenticate('incorrect')
|
assert_not @steve.authenticate('incorrect')
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should create room when saved." do
|
test "#initialize_room should create room." do
|
||||||
@user.save
|
@steve.send(:initialize_room)
|
||||||
assert @user.room
|
assert @steve.room
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue