main
 1module TFA
 2  describe CLI do
 3    subject { CLI.new([], filename: SecureRandom.uuid, directory: Dir.tmpdir, passphrase: passphrase) }
 4    let(:passphrase) { SecureRandom.uuid }
 5
 6    def code_for(secret)
 7      ::ROTP::TOTP.new(secret).now
 8    end
 9
10    let(:dev_secret) { ::ROTP::Base32.random_base32 }
11    let(:prod_secret) { ::ROTP::Base32.random_base32 }
12
13    describe "#add" do
14      let(:key) { SecureRandom.uuid }
15
16      context "when a secret is added" do
17        it "adds the secret" do
18          subject.add(key, dev_secret)
19          expect(subject.show(key)).to eql(dev_secret)
20        end
21      end
22
23      context "when a full otpauth string is added" do
24        it "strips out the url for just the secret" do
25          url = "otpauth://totp/email@email.com?secret=#{dev_secret}&issuer="
26
27          subject.add(key, url)
28          expect(subject.show(key)).to eql(dev_secret)
29        end
30      end
31    end
32
33    describe "#show" do
34      context "when a single key is given" do
35        let(:key) { SecureRandom.uuid }
36
37        it "returns the secret" do
38          subject.add(key, dev_secret)
39          expect(subject.show(key)).to eql(dev_secret)
40        end
41      end
42
43      context "when no key is given" do
44        it "returns the name of each item" do
45          key = SecureRandom.uuid
46          subject.add(key, dev_secret)
47          subject.add("production", prod_secret)
48
49          result = subject.show.to_s
50          expect(result).to include(key)
51          expect(result).to include("production")
52        end
53      end
54    end
55
56    describe "#totp" do
57      context "when a single key is given" do
58        it "returns a time based one time password" do
59          key = SecureRandom.uuid
60          subject.add(key, dev_secret)
61          expect(subject.totp(key)).to eql(code_for(dev_secret))
62        end
63      end
64    end
65
66    describe "#destroy" do
67      let(:name) { "development" }
68
69      it "removes the secret with the given name" do
70        subject.add(name, dev_secret)
71        subject.destroy(name)
72
73        expect(subject.show(name)).to be_nil
74      end
75    end
76
77    describe "#now" do
78      it "returns a time based one time password for the given secret" do
79        expect(subject.now(dev_secret)).to eql(code_for(dev_secret))
80      end
81    end
82  end
83end