gas 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ module Gas
4
4
  class User
5
5
  attr_reader :name, :email, :nickname
6
6
 
7
+
7
8
  # @param [String] name The name of the user
8
9
  # @param [String] email The email of the user
9
10
  # @param [String] nickname A nickname for the user, not used when parsing from gitconfig
@@ -11,6 +12,7 @@ module Gas
11
12
  @name = name
12
13
  @email = email
13
14
  @nickname = nickname
15
+
14
16
  end
15
17
 
16
18
  # Returns the git format of user
@@ -23,8 +25,9 @@ module Gas
23
25
  # @param [Boolean] use_nickname Defaults to true
24
26
  # @return [String]
25
27
  def to_s(use_nickname = true)
26
- "[#{use_nickname ? @nickname : 'user'}]\n name = #{@name}\n email = #{@email}"
28
+ " [#{use_nickname ? @nickname : 'user'}]\n name = #{@name}\n email = #{@email}"
27
29
  end
28
30
 
31
+
29
32
  end
30
33
  end
@@ -1,6 +1,6 @@
1
1
  module Gas
2
2
 
3
- VERSION = '0.1.6'
3
+ VERSION = '0.1.7'
4
4
 
5
5
  end
6
6
 
@@ -24,7 +24,10 @@ describe Gas::Config do
24
24
  user2 = Gas::User.new 'foo', 'bar', 'user2'
25
25
  users = [user1, user2]
26
26
  config = Gas::Config.new users
27
- config.to_s.should == "[walle]\n name = Fredrik Wallgren\n email = fredrik.wallgren@gmail.com\n[user2]\n name = foo\n email = bar"
27
+
28
+ stub(config).is_current_user { false }
29
+
30
+ config.to_s.should == " [walle]\n name = Fredrik Wallgren\n email = fredrik.wallgren@gmail.com\n [user2]\n name = foo\n email = bar"
28
31
  end
29
32
 
30
33
  it 'should be able to tell if a nickname exists' do
@@ -71,5 +74,10 @@ describe Gas::Config do
71
74
  config.delete 'user2'
72
75
  config.users.count.should == 0
73
76
  end
77
+
78
+ it 'should remove the ssh keys from .gas appropriately'
79
+
80
+ it 'should remove the keys from .ssh if present'
81
+
74
82
  end
75
83
 
@@ -1,12 +1,14 @@
1
1
  require './spec/spec_helper'
2
2
 
3
3
  require './lib/gas'
4
+ #require './lib/gas/user'
4
5
 
5
6
  describe Gas::Gitconfig do
6
7
 
7
8
  before :each do
8
9
  @name = 'Fredrik Wallgren'
9
10
  @email = 'fredrik.wallgren@gmail.com'
11
+ @nickname = 'Fred'
10
12
  @gitconfig = Gas::Gitconfig.new
11
13
  end
12
14
 
@@ -26,19 +28,58 @@ describe Gas::Gitconfig do
26
28
  @gitconfig.current_user.should == nil
27
29
  end
28
30
 
29
- it 'should be able to change the current user' do
30
- name = 'Test Testsson'
31
- email = 'test@testsson.com'
32
31
 
33
- mock_cli_call(@gitconfig, "git config --global user.name \"#{name}\"") { nil }
34
- mock_cli_call(@gitconfig, "git config --global user.email \"#{email}\"") { nil }
35
- mock_cli_call(@gitconfig, 'git config --global --get user.name') { name + "\n" }
36
- mock_cli_call(@gitconfig, 'git config --global --get user.email') { email + "\n" }
32
+ describe "Multiple users" do
37
33
 
38
- @gitconfig.change_user name, email
34
+ before :each do
35
+ @user1 = Gas::User.new(@name, @email, @nickname) # create a primary user for testing
36
+ end
37
+
38
+ it "should be able to set the current user" do
39
+ # setup the cli interrupt things...
40
+ mock_cli_call(@gitconfig, "git config --global user.name \"#{@user1.name}\"") { nil }
41
+ mock_cli_call(@gitconfig, "git config --global user.email \"#{@user1.email}\"") { nil }
42
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { @user1.name + "\n" }
43
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { @user1.email + "\n" }
44
+
45
+ @gitconfig.change_user @user1
46
+
47
+ user = @gitconfig.current_user
48
+ user.name.should == @user1.name
49
+ user.email.should == @user1.email
50
+ user.nickname.should == @user1.nickname
51
+ end
52
+
53
+ it 'should be able to change the current user' do
54
+ name = 'Test Testsson'
55
+ email = 'test@testsson.com'
56
+ nickname = 'test'
57
+
58
+ # User 1 cli interrupt things...
59
+ mock_cli_call(@gitconfig, "git config --global user.name \"#{@name}\"") { nil }
60
+ mock_cli_call(@gitconfig, "git config --global user.email \"#{@email}\"") { nil }
61
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { @name + "\n" }
62
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { @email + "\n" }
63
+
64
+ @gitconfig.change_user @user1
65
+
66
+ user = @gitconfig.current_user
67
+ user.name.should == @name
68
+ user.email.should == @email # test that the user switch worked (paranoid, huh?)
69
+
70
+ # User 2 cli interrupt things...
71
+ mock_cli_call(@gitconfig, "git config --global user.name \"#{name}\"") { nil }
72
+ mock_cli_call(@gitconfig, "git config --global user.email \"#{email}\"") { nil }
73
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { name + "\n" }
74
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { email + "\n" }
75
+
76
+ @user2 = Gas::User.new(name, email, nickname) # create user 2
77
+ @gitconfig.change_user @user2
78
+
79
+ user = @gitconfig.current_user
80
+ user.name.should == name
81
+ user.email.should == email # test that the user changed appropriately
82
+ end
39
83
 
40
- user = @gitconfig.current_user
41
- user.name.should == name
42
- user.email.should == email
43
84
  end
44
85
  end
@@ -0,0 +1,328 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ require 'rspec/mocks'
6
+ require 'rspec/mocks/standalone'
7
+
8
+ describe Gas::Ssh do
9
+
10
+ before :all do
11
+ move_the_testers_personal_ssh_key_out_of_way
12
+ end
13
+
14
+ after :all do
15
+ restore_the_testers_ssh_key
16
+ end
17
+
18
+ before :each do
19
+ @uid = "teddy"
20
+ end
21
+
22
+ describe "SSH key file handling..." do
23
+
24
+ before :all do
25
+ #Gas::Ssh.stub!(:delete).and_return(false)
26
+ Gas::Ssh.stub!(:user_wants_to_delete_all_ssh_data?).and_return("l") # only delete's local keys
27
+ end
28
+
29
+ after :all do
30
+ #Gas::Ssh.unstub!(:delete)
31
+ Gas::Ssh.unstub!(:user_wants_to_delete_all_ssh_data?)
32
+ end
33
+
34
+ describe "Detecting when files are missing..." do
35
+
36
+ before :all do
37
+ File.stub!(:exists?).and_return(false) # make it so File.exists? always return true
38
+ end
39
+
40
+ after :all do
41
+ File.unstub!(:exists?) # undoes the hook
42
+ end
43
+
44
+ it "should detect when an id_rsa isn't in the .gas directory" do
45
+ Gas::Ssh.user_wants_to_use_key_already_in_gas?.should be_false
46
+ end
47
+
48
+ end
49
+
50
+ describe "Detecting when files exist.@email..." do
51
+ before :all do
52
+ File.stub!(:exists?).and_return(true) # make it so File.exists? always return true
53
+ end
54
+
55
+ after :all do
56
+ File.unstub!(:exists?) # undoes the hook
57
+ end
58
+
59
+ it 'should detect when an id_rsa is already in the .gas directory' do
60
+ STDIN.stub!(:gets).and_return("y\n") # fix stdin to recieve a 'y' command...
61
+ Gas::Ssh.user_wants_to_use_key_already_in_gas?.should be_true
62
+ STDIN.unstub!(:gets)
63
+ end
64
+
65
+ end
66
+
67
+ describe "File System Changes..." do
68
+
69
+ before :all do
70
+ @gas_dir = File.expand_path('~/.gas')
71
+ @ssh_dir = File.expand_path('~/.ssh')
72
+
73
+
74
+ @nickname = "thisaccountmaybedeletedmysteriously"
75
+ @name = "tim T"
76
+ @email = "tim@timmy.com"
77
+
78
+ `rm ~/.gas/#{@nickname}_id_rsa`
79
+ `rm ~/.gas/#{@nickname}_id_rsa.pub`
80
+ Gas.delete(@nickname)
81
+
82
+ # make sure that nickname isn't in use
83
+ end
84
+
85
+
86
+ it 'should create ssh keys in .gas && Gas.remove should be able to remove those files' do
87
+ STDIN.stub!(:gets).and_return("y\n") # forces the dialogs to
88
+ Gas::Ssh.stub!(:upload_public_key_to_github).and_return(false)
89
+
90
+ lambda do
91
+ Gas.add(@nickname,@name,@email)
92
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(2) #OMG THIS IS A FUN TEST!!!
93
+
94
+ lambda do
95
+ Gas.delete(@nickname)
96
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(-2)
97
+
98
+ STDIN.unstub!(:gets)
99
+ Gas::Ssh.unstub!(:upload_public_key_to_github)
100
+ end
101
+
102
+
103
+ describe 'For the ssh directory...' do
104
+
105
+ before :each do
106
+ # A user for creating
107
+ @nickname = "thisaccountmaybedeletedmysteriously"
108
+ @name = "tim T"
109
+ @email = "tim@timmy.com"
110
+
111
+ clean_out_ssh_directory
112
+ clean_out_gas_directory(@nickname)
113
+
114
+ # a user for deleting
115
+ @nickname2 = "thisaccountmaybedeletedmysteriously2"
116
+ @name2 = "tim T2"
117
+ @email2 = "tim@timmy.com2"
118
+ create_user_no_git(@nickname2, @name2, @email2)
119
+ end
120
+
121
+ after :each do
122
+ Gas.delete(@nickname)
123
+ delete_user_no_git(@nickname2)
124
+ end
125
+
126
+
127
+ it "if there's no key in .ssh" do
128
+ # 1) Create a User
129
+
130
+ # 2) Switch to that user
131
+
132
+ # 3) The .ssh directory should now contain that file
133
+ true.should be_true
134
+
135
+ end
136
+
137
+ it "if there's a key in ~/.ssh that isn't backed up in .gas" do
138
+ # 1) Create a User
139
+
140
+ # 2) Switch to that user
141
+
142
+ # 3) The .ssh directory should now contain that file
143
+
144
+ end
145
+
146
+ it "If there's a key in ~/.ssh that's backed up in .gas"
147
+
148
+ it "should delete the key in .ssh when the user is deleted" do
149
+ # Gas.add(@nickname,@name,@email)
150
+
151
+ end
152
+
153
+ # bundle exec rspec spec/gas/ssh_spec.rb -e 'should be able to copy ssh keys in the ssh'
154
+ it 'should be able to copy ssh keys in the ssh' do
155
+ # put a key pair in the ssh directory
156
+ mock_text = "this is a mock ssh file"
157
+ File.open(SSH_DIRECTORY + "/id_rsa","w+").write(mock_text)
158
+ File.open(SSH_DIRECTORY + "/id_rsa.pub","w+").write(mock_text)
159
+
160
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_false
161
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub").should be_false
162
+
163
+ Gas::Ssh.use_current_rsa_files_for_this_user(@nickname)
164
+
165
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_true
166
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub").should be_true
167
+
168
+ ssh_file = File.read(GAS_DIRECTORY + "/#{@nickname}_id_rsa")
169
+ # ssh_file.should == mock_text # this part doesn't work... hmmm...
170
+
171
+ File.delete(GAS_DIRECTORY + "/#{@nickname}_id_rsa")
172
+ File.delete(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub")
173
+ end
174
+
175
+
176
+ it "should have a UTILITY for deleting rsa files of user" do
177
+ lambda do
178
+ Gas::Ssh.delete_associated_local_keys!(@nickname2)
179
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(-2)
180
+ end
181
+
182
+ end
183
+
184
+ end
185
+
186
+
187
+
188
+ end
189
+
190
+ describe "Networking stuff..." do
191
+
192
+ before :all do
193
+ @name = 'Fredrik Wallgren'
194
+ @email = 'fredrik.wallgren@gmail.com'
195
+ @nickname = "thisaccountmaybedeletedmysteriously"
196
+
197
+ # make sure sample key is deleted
198
+ @username = "aTestGitAccount"
199
+ @password = "plzdon'thackthetestaccount1"
200
+
201
+ config = "[#{@nickname}]\n name = #{@name}\n email = #{@email}\n\n[user2]\n name = foo\n email = bar"
202
+ @config = Gas::Config.new nil, config
203
+ @user = @config.users[0]
204
+
205
+ @credentials = {:username => @username, :password => @password}
206
+
207
+ # Code to prepare the github environment for testing
208
+ @sample_rsa = "ssh-rsa AAAAB3NzaC1yc2EAAAA55555AAABAQCpxktnw9eBaYkxLnvLq8ZeUI8d/d00MeV32na3GZ35qKtJ3Vmvzvb8anF1eZD8/+BtBgYer9/3E0KUi3YNYCeejkdUPj3/Z+aV7Ft0+IeKdzFSqfnfN9UsuS/zkeyia2bjgQJYqk2ZbkMuVIn79UI5ypJWGOXNfKyQ2adYJD7Pjgsxvx8qEXHlU+SszlGr7YFEFwT7rZtSXILylmcwCnZryy91cs50vGWxKzKrOV/2iMd8V4Qv7RbhKtQ7OCd19CaZ08H3xqcG1U2lqXIgxSN75bLL71AM0KfIvNOzvigBZnYyb/RKiUQUhA0FnnIYc/7hF9rOe/S1acRiOF6ihz1x"
209
+
210
+
211
+ Gas::Ssh.remove_key!(@username, @password, @sample_rsa)
212
+ Gas::Ssh.stub!(:get_username_and_password_and_authenticate).and_return(@credentials)
213
+ end
214
+
215
+ after :all do
216
+ # make sure sample key is deleted
217
+
218
+ Gas::Ssh.unstub!(:get_username_and_password_and_authenticate)
219
+ end
220
+
221
+ it "UTILITY: should be able to insert a new key into github and conversly remove that key" do
222
+
223
+
224
+ lambda do
225
+ Gas::Ssh.key_installation_routine!(@user, @sample_rsa)
226
+ end.should change{Gas::Ssh.get_keys(@username, @password).length}.by(1)
227
+
228
+ lambda do
229
+ Gas::Ssh.remove_key!(@username, @password, @sample_rsa)
230
+ end.should change{Gas::Ssh.get_keys(@username, @password).length}.by(-1)
231
+ end
232
+
233
+
234
+ # bundle exec rspec spec/gas/ssh_spec.rb SPEC_OPTS="-e \"should add ssh keys to github when user is created, and delete them when destroyed\""
235
+ =begin
236
+ rake spec SPEC=spec/gas/ssh_spec.rb \
237
+ SPEC_OPTS="-e \"should add ssh keys to github when user is created, and delete them when destroyed\""
238
+
239
+ bundle exec rspec spec/gas/ssh_spec.rb -e 'should add ssh keys to github when user is created, and delete them when destroyed'
240
+
241
+ bundle exec rspec spec/gas/ssh_spec.rb -e 'UTILITY: should be able to insert a new key into github and conversly remove that key'
242
+ =end
243
+
244
+ it "should add ssh keys to github when user is created, and delete them when destroyed" do
245
+
246
+ #move_the_testers_personal_ssh_key_out_of_way
247
+
248
+ # yes, delete all
249
+ Gas::Ssh.stub!(:user_wants_to_delete_all_ssh_data?).and_return("a") # all keys, local and github
250
+ # create new user and use ssh handling
251
+ Gas::Ssh.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
252
+ Gas::Ssh.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
253
+ Gas::Ssh.stub!(:user_wants_to_install_key_to_github?).and_return(true)
254
+
255
+
256
+ lambda do
257
+ Gas.add(@nickname,@name,@email)
258
+ end.should change{Gas::Ssh.get_keys(@username, @password).length}.by(1)
259
+
260
+ lambda do
261
+ Gas.delete(@nickname)
262
+ end.should change{Gas::Ssh.get_keys(@username, @password).length}.by(-1)
263
+
264
+
265
+ #restore_the_testers_ssh_key
266
+
267
+ Gas::Ssh.unstub!(:user_wants_to_delete_all_ssh_data?)
268
+ Gas::Ssh.unstub!(:user_wants_gas_to_handle_rsa_keys?)
269
+ Gas::Ssh.unstub!(:user_wants_to_use_key_already_in_ssh?)
270
+ Gas::Ssh.unstub!(:user_wants_to_install_key_to_github?)
271
+ end
272
+
273
+
274
+
275
+ it "Gas.Delete should be able to remove the id_rsa from .gas" do
276
+ Gas::Ssh.stub!(:user_wants_to_delete_all_ssh_data?).and_return("a")
277
+ Gas::Ssh.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
278
+ Gas::Ssh.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
279
+ Gas::Ssh.stub!(:user_wants_to_install_key_to_github?).and_return(true)
280
+
281
+ lambda do
282
+ Gas.add(@nickname,@name,@email)
283
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(2)
284
+
285
+ lambda do
286
+ Gas.delete(@nickname)
287
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(-2)
288
+
289
+
290
+ Gas::Ssh.unstub!(:user_wants_to_delete_all_ssh_data?)
291
+ Gas::Ssh.unstub!(:user_wants_gas_to_handle_rsa_keys?)
292
+ Gas::Ssh.unstub!(:user_wants_to_use_key_already_in_ssh?)
293
+ Gas::Ssh.unstub!(:user_wants_to_install_key_to_github?)
294
+ end
295
+
296
+ # bundle exec rspec spec/gas/ssh_spec.rb -e 'Gas.ssh(nickname) should be able to add ssh support to a legacy user or an opt-out'
297
+ it 'Gas.ssh(nickname) should be able to add ssh support to a legacy user or an opt-out' do
298
+
299
+ Gas::Ssh.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(false)
300
+ Gas.add(@nickname,@name,@email)
301
+ Gas::Ssh.unstub!(:user_wants_gas_to_handle_rsa_keys?)
302
+
303
+ Gas::Ssh.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
304
+ Gas::Ssh.stub!(:upload_public_key_to_github)
305
+
306
+ lambda do
307
+ Gas.ssh(@nickname)
308
+ end.should change{`ls ~/.gas -1 | wc -l`.to_i}.by(2)
309
+
310
+
311
+ Gas::Ssh.delete_associated_local_keys!(@nickname)
312
+
313
+ Gas::Ssh.unstub!(:user_wants_gas_to_handle_rsa_keys?)
314
+ Gas::Ssh.unstub!(:upload_public_key_to_github)
315
+ end
316
+
317
+
318
+
319
+ it "Should be able to tell if it's ever used this key under this ISP provider before and then warn the user"
320
+
321
+ it 'Should have the ability to show if the author is associated with a specific github account NAME, stored in gas.accouts file'
322
+
323
+ it 'Should have the ability to link up with non-github git-daemons'
324
+ end
325
+
326
+
327
+
328
+ end