cli_test.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'test_helper'
  2. require 'shopify_api/cli'
  3. require 'fileutils'
  4. class CliTest < Test::Unit::TestCase
  5. def setup
  6. @test_home = File.join(File.expand_path(File.dirname(__FILE__)), 'files', 'home')
  7. @shop_config_dir = File.join(@test_home, '.shopify', 'shops')
  8. @default_symlink = File.join(@shop_config_dir, 'default')
  9. `rm -rf #{@test_home}`
  10. ENV['HOME'] = @test_home
  11. @cli = ShopifyAPI::Cli.new
  12. FileUtils.mkdir_p(@shop_config_dir)
  13. File.open(config_file('foo'), 'w') do |file|
  14. file.puts valid_options.merge('domain' => 'foo.myshopify.com').to_yaml
  15. end
  16. File.symlink(config_file('foo'), @default_symlink)
  17. File.open(config_file('bar'), 'w') do |file|
  18. file.puts valid_options.merge('domain' => 'bar.myshopify.com').to_yaml
  19. end
  20. end
  21. def teardown
  22. `rm -rf #{@test_home}`
  23. end
  24. test "add with blank domain" do
  25. `rm -rf #{@shop_config_dir}/*`
  26. $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
  27. $stdout.expects(:print).with("API key? ")
  28. $stdout.expects(:print).with("Password? ")
  29. $stdout.expects(:print).with("Would you like to use pry as your shell? (y/n) ")
  30. $stdin.expects(:gets).times(4).returns("", "key", "pass", "y")
  31. @cli.expects(:puts).with("\nopen https://foo.myshopify.com/admin/api in your browser to get API credentials\n")
  32. @cli.expects(:puts).with("Default connection is foo")
  33. @cli.add('foo')
  34. config = YAML.load(File.read(config_file('foo')))
  35. assert_equal 'foo.myshopify.com', config['domain']
  36. assert_equal 'key', config['api_key']
  37. assert_equal 'pass', config['password']
  38. assert_equal 'pry', config['shell']
  39. assert_equal 'https', config['protocol']
  40. assert_equal config_file('foo'), File.readlink(@default_symlink)
  41. end
  42. test "add with explicit domain" do
  43. `rm -rf #{@shop_config_dir}/*`
  44. $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
  45. $stdout.expects(:print).with("API key? ")
  46. $stdout.expects(:print).with("Password? ")
  47. $stdout.expects(:print).with("Would you like to use pry as your shell? (y/n) ")
  48. $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "y")
  49. @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
  50. @cli.expects(:puts).with("Default connection is foo")
  51. @cli.add('foo')
  52. config = YAML.load(File.read(config_file('foo')))
  53. assert_equal 'bar.myshopify.com', config['domain']
  54. end
  55. test "add with irb as shell" do
  56. `rm -rf #{@shop_config_dir}/*`
  57. $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
  58. $stdout.expects(:print).with("API key? ")
  59. $stdout.expects(:print).with("Password? ")
  60. $stdout.expects(:print).with("Would you like to use pry as your shell? (y/n) ")
  61. $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "fuuuuuuu")
  62. @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
  63. @cli.expects(:puts).with("Default connection is foo")
  64. @cli.add('foo')
  65. config = YAML.load(File.read(config_file('foo')))
  66. assert_equal 'irb', config['shell']
  67. end
  68. test "list" do
  69. @cli.expects(:puts).with(" bar")
  70. @cli.expects(:puts).with(" * foo")
  71. @cli.list
  72. end
  73. test "show default" do
  74. @cli.expects(:puts).with("Default connection is foo")
  75. @cli.default
  76. end
  77. test "set default" do
  78. @cli.expects(:puts).with("Default connection is bar")
  79. @cli.default('bar')
  80. assert_equal config_file('bar'), File.readlink(@default_symlink)
  81. end
  82. test "remove default connection" do
  83. @cli.remove('foo')
  84. assert !File.exist?(@default_symlink)
  85. assert !File.exist?(config_file('foo'))
  86. assert File.exist?(config_file('bar'))
  87. end
  88. test "remove non-default connection" do
  89. @cli.remove('bar')
  90. assert_equal config_file('foo'), File.readlink(@default_symlink)
  91. assert File.exist?(config_file('foo'))
  92. assert !File.exist?(config_file('bar'))
  93. end
  94. private
  95. def valid_options
  96. {'domain' => 'snowdevil.myshopify.com', 'api_key' => 'key', 'password' => 'pass', 'shell' => 'pry', 'protocol' => 'https'}
  97. end
  98. def config_file(connection)
  99. File.join(@shop_config_dir, "#{connection}.yml")
  100. end
  101. end