Commit 271e0e6

mo <mo.khan@gmail.com>
2018-09-09 16:59:23
raise error when token has already been revoked.
1 parent 357f498
Changed files (2)
app/models/authorization.rb
@@ -10,6 +10,7 @@ class Authorization < ApplicationRecord
   end
 
   def revoke!
+    raise 'already revoked' if revoked?
     update!(revoked_at: Time.now)
   end
 
spec/models/authorization_spec.rb
@@ -1,5 +1,23 @@
 require 'rails_helper'
 
 RSpec.describe Authorization, type: :model do
-  pending "add some examples to (or delete) #{__FILE__}"
+  describe '#revoke!' do
+    subject { create(:authorization) }
+
+    context "when the authorization has not been revoked" do
+      before { subject.revoke! }
+
+      specify { expect(subject.revoked_at).to be_present }
+    end
+
+    context "when the authorization has already been revoked" do
+      before { subject.revoke! }
+
+      specify do
+        expect do
+          subject.revoke!
+        end.to raise_error(/already revoked/)
+      end
+    end
+  end
 end