Move XMPP contacts exchange to background job
This commit is contained in:
		
							parent
							
								
									8ad85636d9
								
							
						
					
					
						commit
						58cc6811f9
					
				
							
								
								
									
										18
									
								
								app/jobs/exchange_xmpp_contacts_job.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								app/jobs/exchange_xmpp_contacts_job.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | |||||||
|  | class ExchangeXmppContactsJob < ApplicationJob | ||||||
|  |   queue_as :default | ||||||
|  | 
 | ||||||
|  |   def perform(inviter, username, domain) | ||||||
|  |     ejabberd = EjabberdApiClient.new | ||||||
|  | 
 | ||||||
|  |     ejabberd.add_rosteritem({ | ||||||
|  |       "localuser": username, "localhost": domain, | ||||||
|  |       "user": inviter.cn, "host": inviter.ou, | ||||||
|  |       "nick": inviter.cn, "group": "Friends", "subs": "both" | ||||||
|  |     }) | ||||||
|  |     ejabberd.add_rosteritem({ | ||||||
|  |       "localuser": inviter.cn, "localhost": inviter.ou, | ||||||
|  |       "user": username, "host": domain, | ||||||
|  |       "nick": username, "group": "Friends", "subs": "both" | ||||||
|  |     }) | ||||||
|  |   end | ||||||
|  | end | ||||||
| @ -36,25 +36,12 @@ class CreateAccount < ApplicationService | |||||||
|   # TODO move to confirmation |   # TODO move to confirmation | ||||||
|   def add_ldap_document |   def add_ldap_document | ||||||
|     hashed_pw = Devise.ldap_auth_password_builder.call(@password) |     hashed_pw = Devise.ldap_auth_password_builder.call(@password) | ||||||
|     CreateLdapUserJob.perform_later(@username, "kosmos.org", @email, hashed_pw) |     CreateLdapUserJob.perform_later(@username, @domain, @email, hashed_pw) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def exchange_xmpp_contacts |   def exchange_xmpp_contacts | ||||||
|     #TODO enable in development when we have easy setup of ejabberd etc. |     #TODO enable in development when we have easy setup of ejabberd etc. | ||||||
|     return if Rails.env.development? |     return if Rails.env.development? | ||||||
| 
 |     ExchangeXmppContactsJob.perform_later(@invitation.user, @username, @domain) | ||||||
|     ejabberd = EjabberdApiClient.new |  | ||||||
|     inviter = @invitation.user |  | ||||||
| 
 |  | ||||||
|     ejabberd.add_rosteritem({ |  | ||||||
|       "localuser": @username, "localhost": @domain, |  | ||||||
|       "user": inviter.cn, "host": inviter.ou, |  | ||||||
|       "nick": inviter.cn, "group": "Friends", "subs": "both" |  | ||||||
|     }) |  | ||||||
|     ejabberd.add_rosteritem({ |  | ||||||
|       "localuser": inviter.cn, "localhost": inviter.ou, |  | ||||||
|       "user": @username, "host": @domain, |  | ||||||
|       "nick": @username, "group": "Friends", "subs": "both" |  | ||||||
|     }) |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
							
								
								
									
										24
									
								
								spec/jobs/exchange_xmpp_contacts_job_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								spec/jobs/exchange_xmpp_contacts_job_spec.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | require 'rails_helper' | ||||||
|  | require 'webmock/rspec' | ||||||
|  | 
 | ||||||
|  | RSpec.describe ExchangeXmppContactsJob, type: :job do | ||||||
|  |   let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" } | ||||||
|  | 
 | ||||||
|  |   subject(:job) { | ||||||
|  |     described_class.perform_later(user, 'isaacnewton', 'kosmos.org') | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   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 | ||||||
|  |     perform_enqueued_jobs { job } | ||||||
|  | 
 | ||||||
|  |     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 | ||||||
| @ -1,6 +1,4 @@ | |||||||
| require 'rails_helper' | require 'rails_helper' | ||||||
| require 'webmock/rspec' |  | ||||||
| require 'json' |  | ||||||
| 
 | 
 | ||||||
| RSpec.describe CreateAccount, type: :model do | RSpec.describe CreateAccount, type: :model do | ||||||
|   describe "#create_user_in_database" do |   describe "#create_user_in_database" do | ||||||
| @ -68,6 +66,8 @@ RSpec.describe CreateAccount, type: :model do | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   describe "#exchange_xmpp_contacts" do |   describe "#exchange_xmpp_contacts" do | ||||||
|  |     include ActiveJob::TestHelper | ||||||
|  | 
 | ||||||
|     let(:inviter) { create :user, cn: "willherschel", ou: "kosmos.org" } |     let(:inviter) { create :user, cn: "willherschel", ou: "kosmos.org" } | ||||||
|     let(:invitation) { create :invitation, user: inviter } |     let(:invitation) { create :invitation, user: inviter } | ||||||
|     let(:service) { CreateAccount.new( |     let(:service) { CreateAccount.new( | ||||||
| @ -77,18 +77,19 @@ RSpec.describe CreateAccount, type: :model do | |||||||
|       invitation: invitation |       invitation: invitation | ||||||
|     )} |     )} | ||||||
| 
 | 
 | ||||||
|     before do |     it "enqueues a job to exchange XMPP contacts between inviter and invitee" 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) |       service.send(:exchange_xmpp_contacts) | ||||||
| 
 | 
 | ||||||
|       expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem") |       expect(enqueued_jobs.size).to eq(1) | ||||||
|         .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") |       args = enqueued_jobs.first['arguments'] | ||||||
|         .with { |req| req.body == '{"localuser":"willherschel","localhost":"kosmos.org","user":"isaacnewton","host":"kosmos.org","nick":"isaacnewton","group":"Friends","subs":"both"}' } |       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 | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user