Commit d39839c

mo khan <mo@mokhan.ca>
2016-11-18 17:07:47
add spec for invalid secret
1 parent 6301ea6
Changed files (2)
lib/tfa/totp_command.rb
@@ -12,11 +12,9 @@ module TFA
     private
 
     def password_for(secret)
-      begin
-        ::ROTP::TOTP.new(secret).now
-      rescue
-        "???"
-      end  
+      ::ROTP::TOTP.new(secret).now
+    rescue ROTP::Base32::Base32Error => error
+      "INVALID SECRET"
     end
 
     def all_passwords
spec/lib/totp_command_spec.rb
@@ -36,6 +36,17 @@ module TFA
           expect(subject.run(["blah"])).to be_empty
         end
       end
+
+      context "when the secret is invalid" do
+        let(:invalid_secret) { "hello world" }
+
+        it 'returns an error message' do
+          storage.save('development', invalid_secret)
+          expect(subject.run(["development"])).to eql([
+            { 'development' => "INVALID SECRET" }
+          ])
+        end
+      end
     end
   end
 end