Add specs for User#is_admin?

This commit is contained in:
Basti 2020-11-18 09:17:56 +01:00
parent d8fee4bae2
commit 3030d6f0f3
Signed by untrusted user: basti
GPG Key ID: 9F88009D31D99C72
10 changed files with 59 additions and 28 deletions

View File

@ -50,6 +50,7 @@ end
group :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'capybara'
gem 'database_cleaner'
end

View File

@ -90,6 +90,11 @@ GEM
dotenv (= 2.7.2)
railties (>= 3.2, < 6.1)
erubi (1.9.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
ffi (1.13.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
@ -244,6 +249,7 @@ DEPENDENCIES
devise
devise_ldap_authenticatable
dotenv-rails
factory_bot_rails
jbuilder (~> 2.7)
letter_opener
letter_opener_web

8
spec/factories/devise.rb Normal file
View File

@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
id { 1 }
cn { "jimmy" }
email { "jimmy@example.com" }
password { "dis-muh-password" }
end
end

View File

@ -1,5 +1,23 @@
require 'rails_helper'
RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:user) { create :user }
describe "#is_admin?" do
it "returns true when admin flag is set in LDAP" do
expect(Devise::LDAP::Adapter).to receive(:get_ldap_param)
.with(user.cn, :admin)
.and_return("true")
expect(user.is_admin?).to be true
end
it "returns false when admin flag is not set in LDAP" do
expect(Devise::LDAP::Adapter).to receive(:get_ldap_param)
.with(user.cn, :admin)
.and_return(nil)
expect(user.is_admin?).to be false
end
end
end

View File

@ -7,6 +7,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'capybara'
require 'devise'
require 'support/controller_macros'
require 'support/database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc, in
@ -63,4 +65,8 @@ RSpec.configure do |config|
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include Devise::Test::ControllerHelpers, :type => :controller
config.include FactoryBot::Syntax::Methods
config.extend ControllerMacros, :type => :controller
end

View File

@ -1,12 +0,0 @@
require 'rails_helper'
RSpec.describe "Dashboards", type: :request do
describe "GET /index" do
it "returns http success" do
get "/dashboard/index"
expect(response).to have_http_status(:success)
end
end
end

View File

@ -0,0 +1,19 @@
module ControllerMacros
def login_user
# Before each test, create and login the user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryBot.create(:user)
# user.confirm! # Or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
end
end
# Not used in this tutorial, but left to show an example of different user types
# def login_admin
# before(:each) do
# @request.env["devise.mapping"] = Devise.mappings[:admin]
# sign_in FactoryBot.create(:admin) # Using factory bot as an example
# end
# end
end

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe "dashboard/index.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe "dashboard/index.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe "settings/index.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end