akkounts/spec/models/user_spec.rb
2023-04-04 12:27:49 +02:00

199 lines
6.6 KiB
Ruby

require 'rails_helper'
RSpec.describe User, type: :model do
let(:user) { create :user, cn: "philipp" }
let(:dn) { "cn=philipp,ou=kosmos.org,cn=users,dc=kosmos,dc=org" }
describe "#address" do
let(:user) { build :user, cn: "jimmy", ou: "kosmos.org" }
it "returns the user address" do
expect(user.address).to eq("jimmy@kosmos.org")
end
end
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
describe "#services_enabled" do
it "returns the entries from the LDAP service attribute" do
expect(user).to receive(:ldap_entry).and_return({
uid: user.cn, ou: user.ou, mail: user.email, admin: nil,
service: ["discourse", "gitea", "wiki", "xmpp"]
})
expect(user.services_enabled).to eq(["discourse", "gitea", "wiki", "xmpp"])
end
end
describe "#enable_service" do
before do
allow(user).to receive(:ldap_entry).and_return({
uid: user.cn, ou: user.ou, mail: user.email, admin: nil,
service: ["discourse", "gitea"]
})
allow(user).to receive(:dn).and_return(dn)
end
it "adds the service to the LDAP entry" do
expect_any_instance_of(LdapService).to receive(:replace_attribute)
.with(dn, :service, ["discourse", "gitea", "wiki"]).and_return(true)
user.enable_service(:wiki)
end
it "adds multiple service to the LDAP entry" do
expect_any_instance_of(LdapService).to receive(:replace_attribute)
.with(dn, :service, ["discourse", "gitea", "wiki", "xmpp"]).and_return(true)
user.enable_service([:wiki, :xmpp])
end
end
describe "#disable_service" do
before do
allow(user).to receive(:ldap_entry).and_return({
uid: user.cn, ou: user.ou, mail: user.email, admin: nil,
service: ["discourse", "gitea", "xmpp"]
})
allow(user).to receive(:dn).and_return(dn)
end
it "removes the service from the LDAP entry" do
expect_any_instance_of(LdapService).to receive(:replace_attribute)
.with(dn, :service, ["discourse", "gitea"]).and_return(true)
user.disable_service(:xmpp)
end
it "removes multiple services from the LDAP entry" do
expect_any_instance_of(LdapService).to receive(:replace_attribute)
.with(dn, :service, ["discourse"]).and_return(true)
user.disable_service([:xmpp, "gitea"])
end
end
describe "#disable_all_services" do
before do
allow(user).to receive(:dn).and_return(dn)
end
it "removes all services from the LDAP entry" do
expect_any_instance_of(LdapService).to receive(:delete_attribute)
.with(dn, :service).and_return(true)
user.disable_all_services
end
end
describe "#exchange_xmpp_contact_with_inviter" do
include ActiveJob::TestHelper
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:guest) { create :user, id: 2, cn: "isaacnewton", ou: "kosmos.org", email: "newt@example.com" }
before do
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:services_enabled).and_return(%w[ ejabberd ])
end
it "enqueues a job to exchange XMPP contacts between inviter and invitee" do
guest.send(:exchange_xmpp_contact_with_inviter)
expect(enqueued_jobs.size).to eq(1)
args = enqueued_jobs.first['arguments']
expect(args[0]['_aj_globalid']).to match('gid://akkounts/User')
expect(args[1]).to eq('isaacnewton')
expect(args[2]).to eq('kosmos.org')
end
after do
clear_enqueued_jobs
end
end
describe "#devise_after_confirmation" do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
it "enables default services" do
expect(user).to receive(:enable_service).with(%w[ discourse ejabberd gitea mediawiki ])
user.send(:devise_after_confirmation)
end
context "for invited user with ejabberd enabled" do
let(:guest) { create :user, id: 2, cn: "isaacnewton", ou: "kosmos.org", email: "newt@example.com" }
before do
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:enable_service).and_return(true)
end
it "exchanges XMPP contacts with the inviter" do
expect(guest).to receive(:exchange_xmpp_contact_with_inviter)
guest.send(:devise_after_confirmation)
end
end
end
describe "#pref_enabled?" do
describe "preference not set" do
# TODO return default value
it "returns false" do
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
end
describe "preference is set" do
it "returns true for boolean true" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => true}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for string 'true'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "true"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for string 'enabled'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "enabled"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns true for integer 1" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => 1}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(true)
end
it "returns false for boolean false" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => false}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
it "returns false for string 'false'" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => "false"}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
it "returns false for integer 0" do
user.preferences.merge!({"lightning" => {"notify_sats_received" => 0}})
expect(user.pref_enabled?("lightning:notify_sats_received")).to be(false)
end
end
end
end