Commit 38cb80f

mo khan <mo@mokhan.ca>
2014-07-25 23:06:44
dump all totp passwords not no key is given.
1 parent d12953c
Changed files (2)
lib/tfa/totp_command.rb
@@ -5,7 +5,26 @@ module TFA
     end
 
     def run(arguments)
-      ::ROTP::TOTP.new(@storage.secret_for(arguments.first)).now
+      return password_for(secret_for(arguments.first)) if arguments.any?
+      all_passwords
+    end
+
+    private
+
+    def password_for(secret)
+      ::ROTP::TOTP.new(secret).now
+    end
+
+    def all_passwords
+      secrets = @storage.all_secrets
+      secrets.each do |hash|
+        hash[hash.keys.first] = password_for(hash[hash.keys.first])
+      end
+      secrets
+    end
+
+    def secret_for(key)
+      @storage.secret_for(key)     
     end
   end
 end
spec/lib/totp_command_spec.rb
@@ -4,14 +4,30 @@ module TFA
     let(:secret) { ::ROTP::Base32.random_base32 }
     let(:storage) { Storage.new(Tempfile.new('test').path) }
 
-    before :each do
-      storage.save('development', secret)
+    def code_for(secret)
+      ::ROTP::TOTP.new(secret).now
     end
 
     describe "#run" do
-      it "returns a time based one time password for the authentication secret given" do
-        correct_code = ::ROTP::TOTP.new(secret).now
-        expect(subject.run(["development"])).to eql(correct_code)
+      context "when a single key is given" do
+        it "returns a time based one time password for the authentication secret given" do
+          storage.save('development', secret)
+          expect(subject.run(["development"])).to eql(code_for(secret))
+        end
+      end
+
+      context "when no arguments are given" do
+        let(:development_secret) { ::ROTP::Base32.random_base32 }
+        let(:staging_secret) { ::ROTP::Base32.random_base32 }
+
+        it "returns the one time password for all keys" do
+          storage.save('development', development_secret)
+          storage.save('staging', staging_secret)
+          expect(subject.run([])).to eql([
+            { 'development' => code_for(development_secret) }, 
+            { 'staging' => code_for(staging_secret) }
+          ])
+        end
       end
     end
   end