Add method to generate subscriptions_token

Note that we only update a user with a new token if the user has not
yet been assigned one.
This commit is contained in:
taitus
2020-12-18 09:39:02 +01:00
parent 630ea22ece
commit 2bef215fc6
2 changed files with 22 additions and 0 deletions

View File

@@ -415,6 +415,10 @@ class User < ApplicationRecord
devise_mailer.send(notification, self, *args).deliver_later
end
def add_subscriptions_token
update!(subscriptions_token: SecureRandom.base58(32)) if subscriptions_token.blank?
end
private
def clean_document_number

View File

@@ -823,4 +823,22 @@ describe User do
expect(other_user_legislation_proposal.reload).to be_hidden
end
end
describe "#add_subscriptions_token" do
let(:user) { build(:user, subscriptions_token: nil) }
it "generates a subscriptions token when the user doesn't have one" do
user.add_subscriptions_token
expect(user.subscriptions_token).to be_present
end
it "keeps the existing subscriptions token when the user already has one" do
user.update!(subscriptions_token: "already_set")
user.add_subscriptions_token
expect(user.subscriptions_token).to eq "already_set"
end
end
end