shelly 0.2.28 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ describe Shelly::CLI::Organization do
10
10
  Shelly::Client.stub(:new).and_return(@client)
11
11
  $stdout.stub(:puts)
12
12
  $stdout.stub(:print)
13
- @client.stub(:token).and_return("abc")
13
+ @client.stub(:authorize!)
14
14
  FileUtils.mkdir_p("/projects/foo")
15
15
  Dir.chdir("/projects/foo")
16
16
  end
@@ -69,7 +69,7 @@ describe Shelly::CLI::Runner do
69
69
  before do
70
70
  @client = mock
71
71
  Shelly::Client.stub(:new).and_return(@client)
72
- @client.stub(:token).and_return("abc")
72
+ @client.stub(:authorize!).and_return(true)
73
73
  @runner = Shelly::CLI::Runner.new(%w(status))
74
74
  end
75
75
 
@@ -11,7 +11,7 @@ describe Shelly::CLI::User do
11
11
  Shelly::Client.stub(:new).and_return(@client)
12
12
  $stdout.stub(:puts)
13
13
  $stdout.stub(:print)
14
- @client.stub(:token).and_return("abc")
14
+ @client.stub(:authorize!)
15
15
  FileUtils.mkdir_p("/projects/foo")
16
16
  Dir.chdir("/projects/foo")
17
17
  @app = Shelly::App.new("foo-staging")
@@ -46,14 +46,20 @@ describe Shelly::Client::APIException do
46
46
  end
47
47
 
48
48
  describe Shelly::Client do
49
+ let(:email) { "bob@example.com" }
50
+ let(:api_key) { "123123" }
51
+
49
52
  before do
50
53
  ENV['SHELLY_URL'] = nil
51
- @client = Shelly::Client.new("bob@example.com", "secret")
54
+ @client = Shelly::Client.new
55
+ FileUtils.mkpath(File.expand_path("~"))
56
+ File.open("~/.netrc", "w") { |f|
57
+ f << "machine api.shellycloud.com\n login #{email}\n password #{api_key}" }
58
+ FileUtils.chmod(0600, "~/.netrc")
52
59
  end
53
60
 
54
61
  def api_url(resource = "")
55
- auth = "#{CGI.escape(@client.email)}:#{@client.password}@"
56
- "https://#{auth}api.shellycloud.com/apiv2/#{resource}"
62
+ "https://#{CGI.escape(email)}:#{api_key}@api.shellycloud.com/apiv2/#{resource}"
57
63
  end
58
64
 
59
65
  describe "#api_url" do
@@ -87,10 +93,9 @@ describe Shelly::Client do
87
93
  end
88
94
  end
89
95
 
90
- describe "#token" do
91
- it "should get authentication token" do
92
- @client.should_receive(:get).with("/token")
93
- @client.token
96
+ describe "#user_email" do
97
+ it "should take user email from .netrc" do
98
+ @client.user_email.should == email
94
99
  end
95
100
  end
96
101
 
@@ -271,12 +276,19 @@ describe Shelly::Client do
271
276
  end
272
277
 
273
278
  describe "#add_ssh_key" do
274
- it "should send put with give SSH key" do
279
+ it "should send put with given SSH key" do
275
280
  @client.should_receive(:post).with("/ssh_keys", {:ssh_key => "abc"})
276
281
  @client.add_ssh_key("abc")
277
282
  end
278
283
  end
279
284
 
285
+ describe "#delete_ssh_key" do
286
+ it "should send delete with given SSH key" do
287
+ @client.should_receive(:delete).with("/ssh_keys", {:ssh_key => "abc"})
288
+ @client.delete_ssh_key("abc")
289
+ end
290
+ end
291
+
280
292
  describe "#apps" do
281
293
  it "should send get requests for user's applications list" do
282
294
  @client.should_receive(:get).with("/apps")
@@ -335,7 +347,7 @@ describe Shelly::Client do
335
347
  describe "#download_file" do
336
348
  before do
337
349
  @filename = "2011.11.26.04.00.10.foo.postgres.tar.gz"
338
- @url = "https://bob%40example.com:secret@backup.example.com/file.gz"
350
+ @url = "https://#{CGI.escape(email)}:#{api_key}@backup.example.com/file.gz"
339
351
  response = Net::HTTPResponse.new('', '', '')
340
352
  # Streaming
341
353
  response.stub(:read_body).and_yield("aaa").and_yield("bbbbb").and_yield("dddf")
@@ -360,20 +372,19 @@ describe Shelly::Client do
360
372
  end
361
373
 
362
374
  describe "#application_logs_tail" do
375
+ let(:logs_url) { "http://#{CGI.escape(email)}:#{api_key}@logs.example.com/fooo" }
363
376
  before do
364
377
  FakeWeb.register_uri(:get, api_url("apps/fooo/application_logs/tail"),
365
378
  :body => {"url" => "http://logs.example.com/fooo"}.to_json)
366
379
  end
367
380
 
368
381
  it "should fetch tail url" do
369
- FakeWeb.register_uri(:get, "http://bob%40example.com:secret@logs.example.com/fooo",
370
- :body => {}.to_json)
382
+ FakeWeb.register_uri(:get, logs_url, :body => {}.to_json)
371
383
  @client.application_logs_tail("fooo") { }
372
384
  end
373
385
 
374
386
  it "should execute block for received data" do
375
- FakeWeb.register_uri(:get, "http://bob%40example.com:secret@logs.example.com/fooo",
376
- :body => "GET / 127.0.0.1")
387
+ FakeWeb.register_uri(:get, logs_url, :body => "GET / 127.0.0.1")
377
388
  out = ""
378
389
  @client.application_logs_tail("fooo") { |logs| out << logs }
379
390
  out.should == "GET / 127.0.0.1"
@@ -388,20 +399,20 @@ describe Shelly::Client do
388
399
  :headers => @client.headers,
389
400
  :payload => {:name => "bob"}.to_json,
390
401
  :user => "bob@example.com",
391
- :password => "secret"
402
+ :password => "123123"
392
403
  }
393
404
  @client.request_parameters("/account", :post, :name => "bob").should == expected
394
405
  end
395
406
 
396
407
  it "should not include user credentials when they are blank" do
397
- client = Shelly::Client.new
408
+ FileUtils.rm("~/.netrc")
398
409
  expected = {
399
410
  :method => :get,
400
411
  :url => "#{@client.api_url}/account",
401
412
  :headers => @client.headers,
402
413
  :payload => {}.to_json
403
414
  }
404
- client.request_parameters("/account", :get).should == expected
415
+ @client.request_parameters("/account", :get).should == expected
405
416
  end
406
417
  end
407
418
 
@@ -7,12 +7,10 @@ describe Shelly::Model do
7
7
  end
8
8
 
9
9
  describe "#current_user" do
10
- it "should return user with loaded credentials" do
11
- File.open(File.join("~/.shelly/credentials"), "w") { |f| f << "superman@example.com\nthe-kal-el" }
10
+ it "should return a user" do
12
11
  base = Shelly::Model.new
13
12
  user = base.current_user
14
- user.email.should == "superman@example.com"
15
- user.password.should == "the-kal-el"
13
+ user.should be_a(Shelly::User)
16
14
  end
17
15
  end
18
16
  end
@@ -1,14 +1,16 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Shelly::User do
4
+ let(:email) { "bob@example.com" }
5
+ let(:password) { "secret" }
6
+
4
7
  before do
5
8
  FileUtils.mkdir_p("~/.ssh")
6
9
  File.open("~/.ssh/id_rsa.pub", "w") { |f| f << "rsa-key AAbbcc" }
7
10
  File.open("~/.ssh/id_dsa.pub", "w") { |f| f << "dsa-key AAbbcc" }
8
11
  @client = mock
9
12
  Shelly::Client.stub(:new).and_return(@client)
10
- @user = Shelly::User.new("bob@example.com", "secret")
11
- @user.stub(:set_credentials_permissions)
13
+ @user = Shelly::User.new
12
14
  end
13
15
 
14
16
  describe ".guess_email" do
@@ -22,88 +24,33 @@ describe Shelly::User do
22
24
  describe "#register" do
23
25
  before do
24
26
  @client.stub(:register_user)
27
+ @client.stub(:authorize_with_email_and_password)
25
28
  end
26
29
 
27
30
  it "should register user at Shelly Cloud" do
28
- @client.should_receive(:register_user).with("bob@example.com", "secret", "dsa-key AAbbcc")
29
- @user.register
30
- end
31
-
32
- it "should save credentials after successful registration" do
33
- @user.should_receive(:save_credentials)
34
- @user.register
31
+ @client.should_receive(:register_user).with(email, password, "dsa-key AAbbcc")
32
+ @user.register(email, password)
35
33
  end
36
34
 
37
35
  context "when ssh key is not available" do
38
36
  it "should register without it" do
39
37
  FileUtils.rm_rf("~/.ssh/id_rsa.pub")
40
38
  FileUtils.rm_rf("~/.ssh/id_dsa.pub")
41
- @client.should_receive(:register_user).with("bob@example.com", "secret", nil)
42
- @user.register
39
+ @client.should_receive(:register_user).with(email, password, nil)
40
+ @user.register(email, password)
43
41
  end
44
42
  end
45
43
  end
46
44
 
47
- describe "#token" do
48
- it "should return token" do
49
- @client.should_receive(:token).and_return({"token" => "abc"})
50
- @user.token.should == "abc"
51
- end
52
- end
53
-
54
- describe "#save_credentials" do
55
- it "should save credentials to file" do
56
- File.exists?("~/.shelly/credentials").should be_false
57
- @user.save_credentials
58
- File.read("~/.shelly/credentials").should == "bob@example.com\nsecret"
59
- end
60
-
61
- it "should create config_dir if it doesn't exist" do
62
- File.exists?("~/.shelly").should be_false
63
- @user.save_credentials
64
- File.exists?("~/.shelly").should be_true
65
- end
66
-
67
- it "should set proper permissions on config_dir and credentials file" do
68
- user = Shelly::User.new("bob@example.com", "secret")
69
- FileUtils.should_receive(:chmod).with(0700, File.expand_path("~/.shelly"))
70
- FileUtils.should_receive(:chmod).with(0600, File.expand_path("~/.shelly/credentials"))
71
- user.save_credentials
72
- end
73
- end
74
-
75
45
  describe "#delete_credentials" do
76
46
  it "should delete credentials from file" do
77
- @user.save_credentials
78
- File.exists?("~/.shelly/credentials").should be_true
79
- File.read("~/.shelly/credentials").should == "bob@example.com\nsecret"
47
+ FileUtils.mkdir_p("~/.shelly")
48
+ File.open("~/.shelly/credentials", "w") { |f| f << "bob@example.com\nsecret" }
80
49
  @user.delete_credentials
81
50
  File.exists?("~/.shelly/credentials").should be_false
82
51
  end
83
52
  end
84
53
 
85
- describe "#load_credentials" do
86
- it "should load credentials from file" do
87
- config_dir = File.expand_path("~/.shelly")
88
- FileUtils.mkdir_p(config_dir)
89
- File.open(File.join(config_dir, "credentials"), "w") { |f| f << "superman@example.com\nkal-el" }
90
-
91
- user = Shelly::User.new
92
- user.load_credentials
93
- user.email.should == "superman@example.com"
94
- user.password.should == "kal-el"
95
- end
96
-
97
- context "credentials file doesn't exist" do
98
- it "should return nil" do
99
- user = Shelly::User.new
100
- user.load_credentials.should be_nil
101
- user.email.should be_nil
102
- user.password.should be_nil
103
- end
104
- end
105
- end
106
-
107
54
  describe "#ssh_key_path" do
108
55
  it "should return path to public dsa key file in the first place" do
109
56
  @user.ssh_key_path.should == File.expand_path("~/.ssh/id_dsa.pub")
@@ -127,16 +74,16 @@ describe Shelly::User do
127
74
 
128
75
  describe "#delete_ssh_key" do
129
76
  it "should invoke logout when ssh key exists" do
130
- @client.should_receive(:logout).with('rsa-key AAbbcc')
131
- @client.should_receive(:logout).with('dsa-key AAbbcc')
132
- @user.delete_ssh_key
77
+ @client.should_receive(:delete_ssh_key).with('rsa-key AAbbcc').and_return(true)
78
+ @client.should_receive(:delete_ssh_key).with('dsa-key AAbbcc').and_return(true)
79
+ @user.delete_ssh_key.should be_true
133
80
  end
134
81
 
135
82
  it "should not invoke logout when ssh key doesn't exist" do
136
83
  FileUtils.rm_rf("~/.ssh/id_rsa.pub")
137
84
  FileUtils.rm_rf("~/.ssh/id_dsa.pub")
138
85
  @client.should_not_receive(:logout)
139
- @user.delete_ssh_key
86
+ @user.delete_ssh_key.should be_false
140
87
  end
141
88
  end
142
89
 
@@ -149,28 +96,43 @@ describe Shelly::User do
149
96
 
150
97
  describe "#login" do
151
98
  before do
152
- @client.stub(:token)
99
+ @client.stub(:authorize_with_email_and_password)
153
100
  end
154
101
 
155
102
  it "should try to login with given credentials" do
156
- @client.should_receive(:token)
157
- @user.login
103
+ @client.should_receive(:authorize_with_email_and_password).
104
+ with(email, password)
105
+ @user.login(email, password)
158
106
  end
159
107
 
160
- context "on successful authentication" do
161
- it "should save user's credentials" do
162
- @user.should_receive(:save_credentials)
163
- @user.login
164
- end
108
+ it "should remove legacy credentials" do
109
+ @user.should_receive(:delete_credentials)
110
+ @user.login(email, password)
165
111
  end
112
+ end
113
+
114
+ describe "#authorize!" do
115
+ it "should authorize via client" do
116
+ @client.should_receive(:authorize!)
117
+ @user.authorize!
118
+ end
119
+
120
+ context "when old credentials file exists" do
121
+ before do
122
+ FileUtils.mkdir_p("~/.shelly")
123
+ File.open("~/.shelly/credentials", "w") { |f| f << "bob@example.com\nsecret" }
124
+ end
125
+
126
+ it "should authorize using email and password from that file" do
127
+ @client.should_receive(:authorize_with_email_and_password).
128
+ with("bob@example.com", "secret")
129
+ @user.authorize!
130
+ end
166
131
 
167
- context "on unsuccessful authentication" do
168
- it "should not save credentials" do
169
- @client.stub(:token).and_raise(RestClient::Unauthorized.new)
170
- @client.should_not_receive(:save_credentials)
171
- lambda {
172
- @user.login
173
- }.should raise_error
132
+ it "should remove the file after authorization" do
133
+ @client.stub(:authorize_with_email_and_password)
134
+ @user.authorize!
135
+ File.exists?("~/.shelly/credentials").should be_false
174
136
  end
175
137
  end
176
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.28
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shelly Cloud team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-18 00:00:00.000000000 Z
11
+ date: 2013-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +206,20 @@ dependencies:
206
206
  type: :runtime
207
207
  prerelease: false
208
208
  name: launchy
209
+ - !ruby/object:Gem::Dependency
210
+ requirement: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ! '>='
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ version_requirements: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ! '>='
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ type: :runtime
221
+ prerelease: false
222
+ name: netrc
209
223
  description: Tool for managing applications and clouds at shellycloud.com
210
224
  email:
211
225
  - devs@shellycloud.com
@@ -239,6 +253,18 @@ files:
239
253
  - lib/shelly/cli/runner.rb
240
254
  - lib/shelly/cli/user.rb
241
255
  - lib/shelly/client.rb
256
+ - lib/shelly/client/application_logs.rb
257
+ - lib/shelly/client/apps.rb
258
+ - lib/shelly/client/auth.rb
259
+ - lib/shelly/client/configs.rb
260
+ - lib/shelly/client/database_backups.rb
261
+ - lib/shelly/client/deployment_logs.rb
262
+ - lib/shelly/client/deploys.rb
263
+ - lib/shelly/client/errors.rb
264
+ - lib/shelly/client/organizations.rb
265
+ - lib/shelly/client/shellyapp.rb
266
+ - lib/shelly/client/ssh_keys.rb
267
+ - lib/shelly/client/users.rb
242
268
  - lib/shelly/cloudfile.rb
243
269
  - lib/shelly/download_progress_bar.rb
244
270
  - lib/shelly/helpers.rb