From 9264297a513757d5dca16f79690c295ee02edf1f Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Thu, 18 Feb 2021 18:05:05 -0600 Subject: [PATCH] Add super_admin? method to User --- app/models/user.rb | 9 +++++++++ spec/models/user_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 spec/models/user_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index 30ddcef5..e98f7a8e 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,6 +47,15 @@ class User < ApplicationRecord self end + # + # Is this user a super_admin? + # + # @return [boolean] True if the user is, false otherwise + # + def super_admin? + roles.super_admins.any? + end + # This creates the user in the normal way, but also sends the devise email confirmation email, which we don't want to send to np admins or anyone else def self.register_donor!(params) u = User.create!(params) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 00000000..615e923d --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE +require 'rails_helper' + +RSpec.describe User, type: :model do + describe 'super_admin?' do + let(:super_admin) { + sa = create(:user) + + sa.roles.create(name: 'super_admin') + sa + } + + let(:not_super_admin) { + sa = create(:user) + sa.roles.create(name: 'nonprofit_admin') + sa.roles.create(name: 'nonprofit_associate') + sa + } + + it 'returns true for super admin' do + expect(super_admin).to be_super_admin + end + + it 'returns false for not super admin' do + expect(not_super_admin).to_not be_super_admin + end + end +end \ No newline at end of file