/spec/heroku/auth_spec.rb

https://github.com/wuputah/heroku · Ruby · 163 lines · 156 code · 7 blank · 0 comment · 4 complexity · b942909f49adc8a83e7c1641f4e66af8 MD5 · raw file

  1. require "spec_helper"
  2. require "heroku/auth"
  3. module Heroku
  4. describe Auth do
  5. before do
  6. @cli = Heroku::Auth
  7. @cli.stub!(:check)
  8. @cli.stub!(:display)
  9. @cli.stub!(:running_on_a_mac?).and_return(false)
  10. @cli.stub!(:set_credentials_permissions)
  11. @cli.credentials = nil
  12. FakeFS.activate!
  13. File.open(@cli.credentials_file, "w") do |file|
  14. file.puts "user\npass"
  15. end
  16. end
  17. after do
  18. FakeFS.deactivate!
  19. end
  20. it "reads credentials from the credentials file" do
  21. @cli.read_credentials.should == %w(user pass)
  22. end
  23. it "takes the user from the first line and the password from the second line" do
  24. @cli.read_credentials
  25. @cli.user.should == 'user'
  26. @cli.password.should == 'pass'
  27. end
  28. it "asks for credentials when the file doesn't exist" do
  29. @cli.delete_credentials
  30. @cli.should_receive(:ask_for_credentials).and_return(["u", "p"])
  31. @cli.should_receive(:check_for_associated_ssh_key)
  32. @cli.user.should == 'u'
  33. @cli.password.should == 'p'
  34. end
  35. it "writes the credentials to a file" do
  36. @cli.should_receive(:credentials).and_return(%w( one two ))
  37. @cli.should_receive(:set_credentials_permissions)
  38. @cli.write_credentials
  39. File.read(@cli.credentials_file).should == "one\ntwo\n"
  40. end
  41. it "sets ~/.heroku/credentials to be readable only by the user" do
  42. @cli.should_receive(:set_credentials_permissions)
  43. @cli.write_credentials
  44. end
  45. it "writes credentials and uploads authkey when credentials are saved" do
  46. @cli.stub!(:credentials)
  47. @cli.stub!(:check)
  48. @cli.stub!(:ask_for_credentials).and_return("username", "apikey")
  49. @cli.should_receive(:write_credentials)
  50. @cli.should_receive(:check_for_associated_ssh_key)
  51. @cli.ask_for_and_save_credentials
  52. end
  53. it "save_credentials deletes the credentials when the upload authkey is unauthorized" do
  54. @cli.stub!(:write_credentials)
  55. @cli.stub!(:retry_login?).and_return(false)
  56. @cli.stub!(:ask_for_credentials).and_return("username", "apikey")
  57. @cli.stub!(:check) { raise RestClient::Unauthorized }
  58. @cli.should_receive(:delete_credentials)
  59. lambda { @cli.ask_for_and_save_credentials }.should raise_error(SystemExit)
  60. end
  61. it "asks for login again when not authorized, for three times" do
  62. @cli.stub!(:read_credentials)
  63. @cli.stub!(:write_credentials)
  64. @cli.stub!(:delete_credentials)
  65. @cli.stub!(:ask_for_credentials).and_return("username", "apikey")
  66. @cli.stub!(:check) { raise RestClient::Unauthorized }
  67. @cli.should_receive(:ask_for_credentials).exactly(3).times
  68. lambda { @cli.ask_for_and_save_credentials }.should raise_error(SystemExit)
  69. end
  70. it "deletes the credentials file" do
  71. FileUtils.should_receive(:rm_f).with(@cli.credentials_file)
  72. @cli.delete_credentials
  73. end
  74. it "writes the login information to the credentials file for the 'heroku login' command" do
  75. @cli.stub!(:ask_for_credentials).and_return(['one', 'two'])
  76. @cli.stub!(:check)
  77. @cli.should_receive(:set_credentials_permissions)
  78. @cli.should_receive(:check_for_associated_ssh_key)
  79. @cli.reauthorize
  80. File.read(@cli.credentials_file).should == "one\ntwo\n"
  81. end
  82. describe "automatic key uploading" do
  83. before(:each) do
  84. FileUtils.mkdir_p("~/.ssh")
  85. @cli.stub!(:ask_for_credentials).and_return("username", "apikey")
  86. end
  87. describe "an account with existing keys" do
  88. before :each do
  89. @client = mock(Object)
  90. @client.should_receive(:keys).and_return(["existingkey"])
  91. @cli.should_receive(:client).and_return(@client)
  92. end
  93. it "should not do anything if the account already has keys" do
  94. @cli.should_not_receive(:associate_key)
  95. @cli.check_for_associated_ssh_key
  96. end
  97. end
  98. describe "an account with no keys" do
  99. before :each do
  100. @client = mock(Object)
  101. @client.should_receive(:keys).and_return([])
  102. @cli.should_receive(:client).and_return(@client)
  103. end
  104. describe "with zero public keys" do
  105. it "should ask to generate a key" do
  106. @cli.should_receive(:ask).and_return("y")
  107. @cli.should_receive(:generate_ssh_key).with("id_rsa")
  108. @cli.should_receive(:associate_key).with(File.expand_path("~/.ssh/id_rsa.pub"))
  109. @cli.check_for_associated_ssh_key
  110. end
  111. end
  112. describe "with one public key" do
  113. before(:each) { FileUtils.touch("~/.ssh/id_rsa.pub") }
  114. after(:each) { FileUtils.rm("~/.ssh/id_rsa.pub") }
  115. it "should upload the key" do
  116. @cli.should_receive(:associate_key).with(File.expand_path("~/.ssh/id_rsa.pub"))
  117. @cli.check_for_associated_ssh_key
  118. end
  119. end
  120. describe "with many public keys" do
  121. before(:each) do
  122. FileUtils.touch("#{@cli.home_directory}/.ssh/id_rsa.pub")
  123. FileUtils.touch("~/.ssh/id_rsa2.pub")
  124. end
  125. after(:each) do
  126. FileUtils.rm("~/.ssh/id_rsa.pub")
  127. FileUtils.rm("~/.ssh/id_rsa2.pub")
  128. end
  129. it "should ask which key to upload" do
  130. File.open("#{@cli.home_directory}/.ssh/id_rsa.pub", "w") { |f| f.puts }
  131. @cli.should_receive(:associate_key).with(File.expand_path("~/.ssh/id_rsa2.pub"))
  132. @cli.should_receive(:ask).and_return("2")
  133. @cli.check_for_associated_ssh_key
  134. end
  135. end
  136. end
  137. end
  138. end
  139. end