From 18f5c2a9ad2f96865fe29320864de8ef8d8d0368 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 17 Jul 2023 15:40:38 +0200 Subject: [PATCH] merge only v3 api tokens --- app/models/administrateur.rb | 2 +- spec/models/administrateur_spec.rb | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index c3f1e08f6..2a2386af1 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -140,7 +140,7 @@ class Administrateur < ApplicationRecord i.administrateurs.delete(old_admin) end - old_admin.api_tokens.each do |token| + old_admin.api_tokens.where('version >= ?', 3).find_each do |token| self.api_tokens << token end end diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index 940807433..03eaeb8e7 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -166,13 +166,24 @@ describe Administrateur, type: :model do end end - context 'when the old admin has an api token' do + context 'when the old admin has an v3 api token' do let(:old_admin) { create(:administrateur, :with_api_token) } it 'transferts the api token' do + token = old_admin.api_tokens.first subject expect(new_admin.api_tokens.count).to eq 1 - expect(new_admin.api_tokens.first).to eq old_admin.api_tokens.first + expect(new_admin.api_tokens.first).to eq token + end + end + + context 'when the old admin has an old api token' do + let(:old_admin) { create(:administrateur, :with_api_token) } + + it 'does not transfer the api token' do + old_admin.api_tokens.first.update(version: 2) + subject + expect(new_admin.api_tokens.count).to eq 0 end end