mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-04 04:57:51 +00:00 
			
		
		
		
	## Linear: - https://github.com/chatwoot/chatwoot/issues/486 ## Description This PR implements Multi-Factor Authentication (MFA) support for user accounts, enhancing security by requiring a second form of verification during login. The feature adds TOTP (Time-based One-Time Password) authentication with QR code generation and backup codes for account recovery. ## Type of change - [ ] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? - Added comprehensive RSpec tests for MFA controller functionality - Tested MFA setup flow with QR code generation - Verified OTP validation and backup code generation - Tested login flow with MFA enabled/disabled ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Pranav <pranav@chatwoot.com> Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
require 'rails_helper'
 | 
						|
 | 
						|
describe BaseTokenService do
 | 
						|
  let(:payload) { { user_id: 1, exp: 5.minutes.from_now.to_i } }
 | 
						|
  let(:token_service) { described_class.new(payload: payload) }
 | 
						|
 | 
						|
  describe '#generate_token' do
 | 
						|
    it 'generates a JWT token with the provided payload' do
 | 
						|
      token = token_service.generate_token
 | 
						|
      expect(token).to be_present
 | 
						|
      expect(token).to be_a(String)
 | 
						|
    end
 | 
						|
 | 
						|
    it 'encodes the payload correctly' do
 | 
						|
      token = token_service.generate_token
 | 
						|
      decoded = JWT.decode(token, Rails.application.secret_key_base, true, algorithm: 'HS256').first
 | 
						|
      expect(decoded['user_id']).to eq(1)
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  describe '#decode_token' do
 | 
						|
    let(:token) { token_service.generate_token }
 | 
						|
    let(:decoder_service) { described_class.new(token: token) }
 | 
						|
 | 
						|
    it 'decodes a valid JWT token' do
 | 
						|
      decoded = decoder_service.decode_token
 | 
						|
      expect(decoded[:user_id]).to eq(1)
 | 
						|
    end
 | 
						|
 | 
						|
    it 'returns empty hash for invalid token' do
 | 
						|
      invalid_service = described_class.new(token: 'invalid_token')
 | 
						|
      expect(invalid_service.decode_token).to eq({})
 | 
						|
    end
 | 
						|
 | 
						|
    it 'returns empty hash for expired token' do
 | 
						|
      expired_payload = { user_id: 1, exp: 1.minute.ago.to_i }
 | 
						|
      expired_token = JWT.encode(expired_payload, Rails.application.secret_key_base, 'HS256')
 | 
						|
      expired_service = described_class.new(token: expired_token)
 | 
						|
      expect(expired_service.decode_token).to eq({})
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |