akkounts/spec/models/user_spec.rb
Râu Cao 5b46f3adf5
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Move exchanging of XMPP contacts to account confirmation
Since the ejabberd service is now being enabled after the confirmation,
we also need to move the exchanging of roster contacts to that point.
2023-03-20 17:59:43 +07:00

153 lines
4.8 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
end