akkounts/spec/services/create_account_spec.rb
Râu Cao e62bf67262
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Use v2 API for creating new lndhub accounts
2022-12-23 12:39:57 +07:00

119 lines
3.2 KiB
Ruby

require 'rails_helper'
RSpec.describe CreateAccount, type: :model do
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
include ActiveJob::TestHelper
let(:service) { CreateAccount.new(
username: 'halfinney',
email: 'halfinney@example.com',
password: 'remember-remember-the-5th-of-november'
)}
it "enqueues a job to create the LDAP user document" do
service.send(:add_ldap_document)
expect(enqueued_jobs.size).to eq(1)
args = enqueued_jobs.first['arguments']
expect(args[0]).to eq('halfinney')
expect(args[1]).to eq('kosmos.org')
expect(args[2]).to eq('halfinney@example.com')
expect(args[3]).to match(/^{SSHA512}.{171}=/)
end
after do
clear_enqueued_jobs
end
end
describe "#exchange_xmpp_contacts" do
include ActiveJob::TestHelper
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
)}
it "enqueues a job to exchange XMPP contacts between inviter and invitee" do
service.send(:exchange_xmpp_contacts)
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 "#create_lndhub_account" do
include ActiveJob::TestHelper
let(:service) { CreateAccount.new(
username: 'halfinney', email: 'halfinney@example.com',
password: 'bright-ideas-in-winter'
)}
let(:new_user) { create :user, cn: "halfinney", ou: "kosmos.org" }
it "enqueues a job to create an LndHub account" do
service.send(:create_lndhub_account, new_user)
expect(enqueued_jobs.size).to eq(1)
args = enqueued_jobs.first['arguments']
expect(args[0]['_aj_globalid']).to match('gid://akkounts/User')
end
after do
clear_enqueued_jobs
end
end
end