You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

otp-cli_spec.rb 1.9KB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'tempfile'
  2. describe OtpCli::Config do
  3. around(:each) do |example|
  4. Tempfile.open 'otp' do |file|
  5. ENV['OTP_CONFIG'] = file.path
  6. @otp = OtpCli::Config.new
  7. example.run
  8. end
  9. end
  10. describe '#add' do
  11. it 'must add OTP from string' do
  12. otp = @otp.add 'otpauth://totp/test?secret=TEST'
  13. expect(otp).not_to be_nil
  14. end
  15. it 'must refuse invalid OTP from string' do
  16. expect { @otp.add 'otpauth://totp/test' }
  17. .to raise_error 'Invalid OTP secret otpauth://totp/test'
  18. end
  19. end
  20. describe '#add_qrcode' do
  21. it 'must add OTP from QR code' do
  22. Tempfile.open 'qrcode' do |qr|
  23. img = RQRCode::QRCode.new 'otpauth://totp/test?secret=TEST'
  24. IO.write qr, img.as_png
  25. otp = @otp.add_qrcode qr.path
  26. expect(otp).not_to be_nil
  27. end
  28. end
  29. it 'must refuse invalid OTP from string' do
  30. expect do
  31. Tempfile.open 'qrcode' do |qr|
  32. img = RQRCode::QRCode.new 'otpauth://totp/test'
  33. IO.write qr, img.as_png
  34. otp = @otp.add_qrcode qr.path
  35. expect(otp).not_to be_nil
  36. end
  37. end.to raise_error 'Invalid OTP secret otpauth://totp/test'
  38. end
  39. end
  40. describe '#select' do
  41. it 'must return the OTP if only one match' do
  42. @otp.add 'otpauth://totp/test1?secret=TEST'
  43. @otp.add 'otpauth://totp/test2?secret=TEST'
  44. otp = @otp.select 'test1'
  45. expect(otp.name).to eq 'test1'
  46. otp = @otp.select 'test2'
  47. expect(otp.name).to eq 'test2'
  48. end
  49. it 'must ask the OTP if only one match' do
  50. @otp.add 'otpauth://totp/test1?secret=TEST'
  51. @otp.add 'otpauth://totp/test2?secret=TEST'
  52. allow(STDIN).to receive(:gets).and_return '1', '2'
  53. otp = @otp.select 'test'
  54. expect(otp.name).to eq 'test1'
  55. otp = @otp.select 'test'
  56. expect(otp.name).to eq 'test2'
  57. end
  58. it 'must return no OTP if nothing match' do
  59. @otp.add 'otpauth://totp/test1?secret=TEST'
  60. @otp.add 'otpauth://totp/test2?secret=TEST'
  61. expect { @otp.select 'foo' }.to raise_error 'No such OTP'
  62. end
  63. end
  64. end