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 end