cli_test.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. standard_add_shop_prompts
  26. $stdin.expects(:gets).times(4).returns("", "key", "pass", "y")
  27. @cli.expects(:puts).with("\nopen https://foo.myshopify.com/admin/api in your browser to get API credentials\n")
  28. @cli.expects(:puts).with("Default connection is foo")
  29. @cli.add('foo')
  30. config = YAML.load(File.read(config_file('foo')))
  31. assert_equal 'foo.myshopify.com', config['domain']
  32. assert_equal 'key', config['api_key']
  33. assert_equal 'pass', config['password']
  34. assert_equal 'pry', config['shell']
  35. assert_equal 'https', config['protocol']
  36. assert_equal config_file('foo'), File.readlink(@default_symlink)
  37. end
  38. test "add with explicit domain" do
  39. standard_add_shop_prompts
  40. $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "y")
  41. @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
  42. @cli.expects(:puts).with("Default connection is foo")
  43. @cli.add('foo')
  44. config = YAML.load(File.read(config_file('foo')))
  45. assert_equal 'bar.myshopify.com', config['domain']
  46. end
  47. test "add with irb as shell" do
  48. standard_add_shop_prompts
  49. $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "fuuuuuuu")
  50. @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
  51. @cli.expects(:puts).with("Default connection is foo")
  52. @cli.add('foo')
  53. config = YAML.load(File.read(config_file('foo')))
  54. assert_equal 'irb', config['shell']
  55. end
  56. test "list" do
  57. @cli.expects(:puts).with(" bar")
  58. @cli.expects(:puts).with(" * foo")
  59. @cli.list
  60. end
  61. test "show default" do
  62. @cli.expects(:puts).with("Default connection is foo")
  63. @cli.default
  64. end
  65. test "set default" do
  66. @cli.expects(:puts).with("Default connection is bar")
  67. @cli.default('bar')
  68. assert_equal config_file('bar'), File.readlink(@default_symlink)
  69. end
  70. test "remove default connection" do
  71. @cli.remove('foo')
  72. assert !File.exist?(@default_symlink)
  73. assert !File.exist?(config_file('foo'))
  74. assert File.exist?(config_file('bar'))
  75. end
  76. test "remove non-default connection" do
  77. @cli.remove('bar')
  78. assert_equal config_file('foo'), File.readlink(@default_symlink)
  79. assert File.exist?(config_file('foo'))
  80. assert !File.exist?(config_file('bar'))
  81. end
  82. private
  83. def valid_options
  84. {'domain' => 'snowdevil.myshopify.com', 'api_key' => 'key', 'password' => 'pass', 'shell' => 'pry', 'protocol' => 'https'}
  85. end
  86. def config_file(connection)
  87. File.join(@shop_config_dir, "#{connection}.yml")
  88. end
  89. def standard_add_shop_prompts
  90. `rm -rf #{@shop_config_dir}/*`
  91. $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
  92. $stdout.expects(:print).with("API key? ")
  93. $stdout.expects(:print).with("Password? ")
  94. $stdout.expects(:print).with("Would you like to use pry as your shell? (y/n) ")
  95. end
  96. end