Move user db creation to service
This commit is contained in:
		
							parent
							
								
									44fadb12d6
								
							
						
					
					
						commit
						e8c1a6066a
					
				| @ -94,14 +94,15 @@ class SignupController < ApplicationController | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def complete_signup |   def complete_signup | ||||||
|     @user.save! |  | ||||||
|     session[:new_user] = nil |     session[:new_user] = nil | ||||||
|     session[:validation_error] = nil |     session[:validation_error] = nil | ||||||
| 
 | 
 | ||||||
|     CreateAccount.call( |     CreateAccount.call( | ||||||
|       username: @user.cn, |       username: @user.cn, | ||||||
|  |       domain: "kosmos.org", | ||||||
|       email: @user.email, |       email: @user.email, | ||||||
|       password: @user.password |       password: @user.password, | ||||||
|  |       invitation: @invitation | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
|     @invitation.update! invited_user_id: @user.id, used_at: DateTime.now |     @invitation.update! invited_user_id: @user.id, used_at: DateTime.now | ||||||
|  | |||||||
| @ -1,16 +1,29 @@ | |||||||
| class CreateAccount < ApplicationService | class CreateAccount < ApplicationService | ||||||
|   def initialize(args) |   def initialize(args) | ||||||
|     @username = args[:username] |     @username   = args[:username] | ||||||
|     @email = args[:email] |     @domain     = args[:ou] || "kosmos.org" | ||||||
|     @password = args[:password] |     @email      = args[:email] | ||||||
|  |     @password   = args[:password] | ||||||
|  |     @invitation = args[:invitation] | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def call |   def call | ||||||
|  |     create_user_in_database | ||||||
|     add_ldap_document |     add_ldap_document | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   private |   private | ||||||
| 
 | 
 | ||||||
|  |   def create_user_in_database | ||||||
|  |     User.create!( | ||||||
|  |       cn: @username, | ||||||
|  |       ou: @domain, | ||||||
|  |       email: @email, | ||||||
|  |       password: @password, | ||||||
|  |       password_confirmation: @password | ||||||
|  |     ) | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   def add_ldap_document |   def add_ldap_document | ||||||
|     dn = "cn=#{@username},ou=kosmos.org,cn=users,dc=kosmos,dc=org" |     dn = "cn=#{@username},ou=kosmos.org,cn=users,dc=kosmos,dc=org" | ||||||
|     attr = { |     attr = { | ||||||
|  | |||||||
| @ -53,8 +53,11 @@ RSpec.describe "Signup", type: :feature do | |||||||
|       expect(page).to have_content("Choose a password") |       expect(page).to have_content("Choose a password") | ||||||
| 
 | 
 | ||||||
|       expect(CreateAccount).to receive(:call) |       expect(CreateAccount).to receive(:call) | ||||||
|         .with(username: "tony", email: "tony@example.com", password: "a-valid-password") |         .with( | ||||||
|         .and_return(true) |           username: "tony", domain: "kosmos.org", | ||||||
|  |           email: "tony@example.com", password: "a-valid-password", | ||||||
|  |           invitation: Invitation.last | ||||||
|  |         ).and_return(true) | ||||||
| 
 | 
 | ||||||
|       fill_in "user_password", with: "a-valid-password" |       fill_in "user_password", with: "a-valid-password" | ||||||
|       click_button "Create account" |       click_button "Create account" | ||||||
| @ -62,7 +65,6 @@ RSpec.describe "Signup", type: :feature do | |||||||
|         expect(page).to have_content("confirm your address") |         expect(page).to have_content("confirm your address") | ||||||
|       end |       end | ||||||
|       expect(page).to have_content("close this window or tab now") |       expect(page).to have_content("close this window or tab now") | ||||||
|       expect(User.last.confirmed_at).to be_nil |  | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     scenario "Validation errors" do |     scenario "Validation errors" do | ||||||
| @ -89,15 +91,17 @@ RSpec.describe "Signup", type: :feature do | |||||||
|       expect(page).to have_content("Password is too short") |       expect(page).to have_content("Password is too short") | ||||||
| 
 | 
 | ||||||
|       expect(CreateAccount).to receive(:call) |       expect(CreateAccount).to receive(:call) | ||||||
|         .with(username: "tony", email: "tony@example.com", password: "a-valid-password") |         .with( | ||||||
|         .and_return(true) |           username: "tony", domain: "kosmos.org", | ||||||
|  |           email: "tony@example.com", password: "a-valid-password", | ||||||
|  |           invitation: Invitation.last | ||||||
|  |         ).and_return(true) | ||||||
| 
 | 
 | ||||||
|       fill_in "user_password", with: "a-valid-password" |       fill_in "user_password", with: "a-valid-password" | ||||||
|       click_button "Create account" |       click_button "Create account" | ||||||
|       within ".flash-msg.notice" do |       within ".flash-msg.notice" do | ||||||
|         expect(page).to have_content("confirm your address") |         expect(page).to have_content("confirm your address") | ||||||
|       end |       end | ||||||
|       expect(User.last.cn).to eq("tony") |  | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
| @ -7,6 +7,22 @@ RSpec.describe CreateAccount, type: :model do | |||||||
|     allow(service).to receive(:ldap_client).and_return(ldap_client_mock) |     allow(service).to receive(:ldap_client).and_return(ldap_client_mock) | ||||||
|   end |   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 "#add_ldap_document" do |   describe "#add_ldap_document" do | ||||||
|     let(:service) { CreateAccount.new( |     let(:service) { CreateAccount.new( | ||||||
|       username: 'halfinney', |       username: 'halfinney', | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user