akkounts/spec/services/create_account_spec.rb

107 lines
3.2 KiB
Ruby

require 'rails_helper'
require 'webmock/rspec'
require 'json'
RSpec.describe CreateAccount, type: :model do
let(:ldap_client_mock) { instance_double(Net::LDAP) }
before do
allow(service).to receive(:ldap_client).and_return(ldap_client_mock)
end
describe "#create_user_in_database" do
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn'
)}
it "creates a new user record in the akkounts database" do
expect(User.count).to eq(0)
service.send(:create_user_in_database)
expect(User.count).to eq(1)
expect(User.last.cn).to eq("isaacnewton")
expect(User.last.email).to eq("isaacnewton@example.com")
end
end
describe "#update_invitation" do
let(:invitation) { create :invitation }
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn',
invitation: invitation
)}
before(:each) do
service.send(:update_invitation, 23)
end
it "marks the invitation as used" do
expect(invitation.used_at).not_to be_nil
end
it "saves the invited user's ID" do
expect(invitation.invited_user_id).to eq(23)
end
end
describe "#add_ldap_document" do
let(:service) { CreateAccount.new(
username: 'halfinney',
email: 'halfinney@example.com',
password: 'remember-remember-the-5th-of-november'
)}
it "creates a new document with the correct attributes" do
expect(ldap_client_mock).to receive(:add).with(
dn: "cn=halfinney,ou=kosmos.org,cn=users,dc=kosmos,dc=org",
attributes: {
objectclass: ["top", "account", "person", "extensibleObject"],
cn: "halfinney",
sn: "halfinney",
uid: "halfinney",
mail: "halfinney@example.com",
userPassword: /^{SSHA512}.{171}=/
}
)
service.send(:add_ldap_document)
end
end
describe "#exchange_xmpp_contacts" do
let(:inviter) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:invitation) { create :invitation, user: inviter }
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn',
invitation: invitation
)}
before do
stub_request(:post, "http://xmpp.example.com/api/add_rosteritem")
.to_return(status: 200, body: "", headers: {})
end
it "posts add_rosteritem commands to the ejabberd API" do
service.send(:exchange_xmpp_contacts)
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == {
localuser: "isaacnewton", localhost: "kosmos.org",
user: "willherschel", host: "kosmos.org",
nick: "willherschel", group: "Friends", subs: "both"
}}
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == {
localuser: "willherschel", localhost: "kosmos.org",
user: "isaacnewton", host: "kosmos.org",
nick: "isaacnewton", group: "Friends", subs: "both"
}}
end
end
end