Skip to content

Commit

Permalink
Add UUID to StoreCredit#generate_authorization_code
Browse files Browse the repository at this point in the history
The value returned by `StoreCredit#generate_authorization_code`
depends on time, which means that it may not be unique when a
store credit is saved more than once at the same moment. This
is not very likely to happen, but not impossible at all.

By introducing a UUID into the code we can be sure that this
code will end up being unique. The previous format of the code
is retained, since there may be applications relying on it.
  • Loading branch information
spaghetticode committed May 14, 2021
1 parent 66b9440 commit a4a3b57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/app/models/spree/store_credit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ def can_void?(payment)
end

def generate_authorization_code
"#{id}-SC-#{Time.current.utc.strftime('%Y%m%d%H%M%S%6N')}"
[
id,
'SC',
Time.current.utc.strftime('%Y%m%d%H%M%S%6N'),
SecureRandom.uuid
].join('-')
end

def editable?
Expand Down
8 changes: 8 additions & 0 deletions core/spec/models/spree/store_credit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,12 @@
end
end
end

describe "#generate_authorization_code" do
it "doesn't rely on time for uniqueness" do
freeze_time do
expect(subject.generate_authorization_code).not_to eq(subject.generate_authorization_code)
end
end
end
end

0 comments on commit a4a3b57

Please sign in to comment.