Skip to content
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

Add sessions and user controller tests, fix syntax #511

Merged
merged 2 commits into from May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Expand Up @@ -3,7 +3,7 @@ class CommentsController < ApplicationController
before_filter :require_login, :only => [:spectrum, :spectra_set, :delete]

def index
@comments = Comment.find :all, :order => "id DESC"
@comments = Comment.all.order(id: :desc)
end

def spectrum
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -41,7 +41,7 @@ def show

def index
if logged_in? && current_user.role == "admin"
@users = User.find :all
@users = User.all
else
flash[:error] = "You must be an admin to view the users listing."
redirect_to "/login"
Expand Down
84 changes: 84 additions & 0 deletions test/functional/sessions_controller_test.rb
Expand Up @@ -8,4 +8,88 @@ class SessionsControllerTest < ActionController::TestCase

fixtures :users

test "should redirect if logged in" do
session[:user_id] = User.first.id
get :login

assert_response :redirect
end

test "should not redirect if not logged in" do
get :login

assert_response :success
end

test "should create new user and login" do

def @controller.openid_authentication(openid_url, back_to)
alaxalves marked this conversation as resolved.
Show resolved Hide resolved
@user = User.new
@user.login = "test"
@user.email = "who@what.com"
@user.role = "basic"
@user.save!
@current_user = @user
successful_login(params[:back_to], nil)
end

post :new,
subaction: "github",
back_to: "/dashboard"

assert_equal "You have successfully logged in.", flash[:success]
assert_response :redirect
assert_redirected_to "/dashboard"
end

test "should fail to create a new user if something goes wrong" do
def @controller.openid_authentication(openid_url, back_to)
@user = User.new
@user.login = "test"
@user.email = "who@what.com"
@user.role = "basic"
@user.save!
@current_user = @user
failed_login("Something went wrong")
end

post :new,
subaction: "github",
back_to: "/dashboard"

assert_equal "Something went wrong", flash[:danger]
assert_response :redirect
assert_redirected_to "/"

end

test "should logout user" do
session[:user_id] = User.first.id
get :logout

assert_equal "You have successfully logged out.", flash[:success]
assert_nil session[:user_id]
assert_response :redirect
assert_redirected_to "/"
end

test "should login locally" do
session[:user_id] = User.first.login
APP_CONFIG["local"] = true
post :local,
login: session[:user_id]

assert_equal "You have successfully logged in.", flash[:success]
assert_response :redirect
end

test "should not initiate local login if not local" do
session[:user_id] = User.first.login
APP_CONFIG["local"] = false
post :local,
login: session[:user_id]

assert_equal "Forbidden", flash[:error]
assert_response :redirect
end
end
18 changes: 17 additions & 1 deletion test/functional/users_controller_test.rb
Expand Up @@ -30,9 +30,25 @@ class UsersControllerTest < ActionController::TestCase
end

test "should get top contributors" do
get :contributors
get :top_contributors

assert_response :success
assert_not_nil :users
end

test "should get user list if admin" do
session[:user_id] = users(:admin).id
get :index

assert_response :success
end

test "should not get user list if not admin" do
session[:user_id] = users(:quentin).id
get :index

assert_equal "You must be an admin to view the users listing.", flash[:error]
assert_response :redirect
assert_redirected_to "/login"
end
end