simple_gas 0.1.9

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.
@@ -0,0 +1,338 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ require 'rspec/mocks'
6
+ require 'rspec/mocks/standalone'
7
+ require 'pry'
8
+
9
+
10
+ describe Gas::Ssh do
11
+ before :all do
12
+ @nickname = "thisaccountmaybedeletedmysteriously"
13
+ @name = "tim T"
14
+ @email = "tim@timmy.com"
15
+ end
16
+
17
+ before :each do
18
+ @uid = "teddy"
19
+ end
20
+
21
+ describe "SSH key file handling..." do
22
+
23
+ before :all do
24
+ Gas::Prompter.stub!(:user_wants_to_delete_all_ssh_data?).and_return("l") # only delete's local keys
25
+ end
26
+
27
+ after :all do
28
+ Gas::Prompter.unstub!(:user_wants_to_delete_all_ssh_data?)
29
+ end
30
+
31
+ describe "Detecting when files are missing..." do
32
+ it "should detect when an id_rsa isn't in the .gas directory" do
33
+ Gas::Ssh.corresponding_rsa_files_exist?(@uid).should be_false
34
+ end
35
+ end
36
+
37
+ describe "Detecting when files exist..." do
38
+ before :each do
39
+ create_user_no_git @uid, @uid, "a@b.com"
40
+ end
41
+
42
+ after :each do
43
+ delete_user_no_git @uid
44
+ end
45
+
46
+ it 'should detect when an id_rsa is already in the .gas directory' do
47
+ Gas::Ssh.corresponding_rsa_files_exist?(@uid).should be_true
48
+ end
49
+ end
50
+
51
+ describe "File System Changes..." do
52
+
53
+ it 'should create ssh keys in .gas && Gas.remove should be able to remove those files' do
54
+ STDIN.stub!(:gets).and_return("y\n") # forces the dialogs to
55
+ Gas::Ssh.stub!(:upload_public_key_to_github).and_return(false)
56
+
57
+ lambda do
58
+ Gas.add(@nickname,@name,@email)
59
+ end.should change{count_of_files_in(GAS_DIRECTORY)}.by(2)
60
+
61
+ lambda do
62
+ Gas.delete(@nickname)
63
+ end.should change{count_of_files_in(GAS_DIRECTORY)}.by(-2)
64
+
65
+ STDIN.unstub!(:gets)
66
+ Gas::Ssh.unstub!(:upload_public_key_to_github)
67
+ end
68
+
69
+
70
+ describe 'For the ssh directory...' do
71
+ before :each do
72
+ clean_out_ssh_directory
73
+ clean_out_gas_directory(@nickname)
74
+
75
+ # a second user for deleting
76
+ @nickname2 = "thisaccountmaybedeletedmysteriously2"
77
+ @name2 = "tim T2"
78
+ @email2 = "tim@timmy.com2"
79
+
80
+ create_user_no_git(@nickname2, @name2, @email2)
81
+ end
82
+
83
+ after :each do
84
+ Gas.delete(@nickname)
85
+ delete_user_no_git(@nickname2)
86
+ end
87
+
88
+
89
+ it "if there's no key in .ssh, the use command should place a key there" do
90
+
91
+ Gas.use @nickname2
92
+ # 3) The .ssh directory should now contain that file
93
+ File.exist?(SSH_DIRECTORY + "/id_rsa.pub").should be_true
94
+ end
95
+
96
+ it "shouldn't overwrite an existing key in ~/.ssh that isn't backed up in .gas and the user aborts" do
97
+ # 2) Create a bogus id_rsa in the .ssh directory
98
+ id_rsa, id_rsa_pub = plant_bogus_rsa_keys_in_ssh_directory
99
+ # 3) Switch to that user
100
+ Gas::Prompter.stub!(:user_wants_to_overwrite_existing_rsa_key?).and_return(false)
101
+ Gas.use @nickname2
102
+ Gas::Prompter.unstub!(:user_wants_to_overwrite_existing_rsa_key?)
103
+ # 4) The .ssh directory should not be changed
104
+ File.open(SSH_DIRECTORY + "/id_rsa", "r") do |f|
105
+ f.read.strip.should eq id_rsa
106
+ end
107
+ File.open(SSH_DIRECTORY + "/id_rsa.pub", "r") do |f|
108
+ f.read.strip.should eq id_rsa_pub
109
+ end
110
+ end
111
+
112
+ it "should overwrite an existing, unbacked-up key in ~/.ssh if user wants" do
113
+ # 2) Create a bogus id_rsa in the .ssh directory
114
+ id_rsa, id_rsa_pub = plant_bogus_rsa_keys_in_ssh_directory
115
+ # 3) Switch to that user
116
+ Gas::Prompter.stub!(:user_wants_to_overwrite_existing_rsa_key?).and_return(true)
117
+ Gas.use @nickname2
118
+ Gas::Prompter.unstub!(:user_wants_to_overwrite_existing_rsa_key?)
119
+ # 4) The .ssh directory should not be changed
120
+ File.open(SSH_DIRECTORY + "/id_rsa", "r") do |f|
121
+ f.read.strip.should_not eq id_rsa
122
+ end
123
+ end
124
+
125
+ it "If there's a key in ~/.ssh that's backed up in .gas" do
126
+ # 1) Create an alternate user
127
+ create_user_no_git(@nickname, @name, @email)
128
+ rsa, rsa_pub = Gas::Ssh.get_associated_rsa_key(@nickname)
129
+ rsa2, rsa2_pub = Gas::Ssh.get_associated_rsa_key(@nickname2)
130
+ # 2)
131
+ Gas.use @nickname2
132
+ File.open(SSH_DIRECTORY + "/id_rsa.pub", "r") do |f|
133
+ f.read.strip.should eq rsa2
134
+ end
135
+ Gas.use @nickname
136
+ File.open(SSH_DIRECTORY + "/id_rsa.pub", "r") do |f|
137
+ f.read.strip.should eq rsa
138
+ end
139
+ end
140
+
141
+ it "should delete the key in .ssh when the user is deleted" do
142
+ create_user_no_git(@nickname, @name, @email)
143
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_true
144
+ Gas.delete @nickname
145
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_false
146
+ end
147
+
148
+ it 'should be able to copy ssh keys in the ssh' do
149
+ # put a key pair in the ssh directory
150
+ mock_text = "this is a mock ssh file"
151
+ File.open(SSH_DIRECTORY + "/id_rsa","w+") do |f|
152
+ f.write(mock_text)
153
+ end
154
+ File.open(SSH_DIRECTORY + "/id_rsa.pub","w+") do |f|
155
+ f.write(mock_text)
156
+ end
157
+
158
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_false
159
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub").should be_false
160
+
161
+ Gas::Ssh.use_current_rsa_files_for_this_user(@nickname)
162
+
163
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa").should be_true
164
+ File.exists?(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub").should be_true
165
+
166
+ File.read(GAS_DIRECTORY + "/#{@nickname}_id_rsa") do |f|
167
+ f.read.should == mock_text # this part doesn't work... hmmm...
168
+ end
169
+
170
+ File.delete(GAS_DIRECTORY + "/#{@nickname}_id_rsa")
171
+ File.delete(GAS_DIRECTORY + "/#{@nickname}_id_rsa.pub")
172
+ end
173
+
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{count_of_files_in(GAS_DIRECTORY)}.by(-2)
180
+ end
181
+ end
182
+
183
+ end
184
+
185
+ end
186
+
187
+ describe "Networking stuff..." do
188
+ before :all do
189
+ # make sure sample key is deleted in the github web client if you incur issues
190
+ @username = "aTestGitAccount"
191
+ @password = "plzdon'thackthetestaccount1"
192
+
193
+ config = "[#{@nickname}]\n name = #{@name}\n email = #{@email}\n\n[user2]\n name = foo\n email = bar"
194
+ @config = Gas::Config.new nil, config
195
+ @user = @config.users[0]
196
+
197
+ @credentials = {:username => @username, :password => @password}
198
+
199
+ # Code to prepare the github environment for testing
200
+ @sample_rsa = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDn74QR9yHb+hcid8iH3+FTaEwnKtwjttseJDbIA2PaivN2uvESrvHlp8Ss/cRox3fFu34QR5DpdOhlfULjTX7yKVuxhaNrAJaqg8rX8hgr9U1Botnyy1DBueEyyA3o1fxRkmwTf6FNnkt1BxWP635tD0lbmUubwaadXjQqPOf3Uw=="
201
+
202
+ Gas.delete(@nickname)
203
+ Gas::Ssh.stub!(:get_username_and_password_and_authenticate).and_return(@credentials)
204
+
205
+ VCR.use_cassette('instantiate_github_speaker', :record => :new_episodes) do
206
+ @github_speaker = Gas::GithubSpeaker.new(@user, @username, @password)
207
+ end
208
+ end
209
+
210
+ after :all do
211
+ Gas.delete(@nickname)
212
+ Gas::Ssh.unstub!(:get_username_and_password_and_authenticate)
213
+ end
214
+
215
+ describe "Should remove and insert keys into github" do
216
+ it 'UTILITY: should insert a new key into github and conversly remove that key' do
217
+ initial_request = ''
218
+ subsequent_request = ''
219
+
220
+ VCR.use_cassette('get_keys-find_none') do
221
+ initial_request = get_keys(@username, @password).length
222
+ end
223
+
224
+ VCR.use_cassette('key_installation_routine-Add_key', :record => :new_episodes) do
225
+ Gas::Ssh.key_installation_routine!(@user, @sample_rsa, @github_speaker)
226
+ end
227
+
228
+ VCR.use_cassette('get_keys-find_one') do
229
+ subsequent_request = get_keys(@username, @password).length
230
+ end
231
+
232
+ (subsequent_request - initial_request).should be(1)
233
+ end
234
+
235
+ it 'should remove the key that it just inserted, so DONT RUN ALONE' do
236
+ initial_request = ''
237
+ subsequent_request = ''
238
+
239
+ VCR.use_cassette('get_keys-find_one') do
240
+ initial_request = get_keys(@username, @password).length
241
+ end
242
+
243
+ VCR.use_cassette('key_installation_routine-Remove_key') do
244
+ lambda do
245
+ @github_speaker.remove_key! @sample_rsa
246
+ end.should change{get_keys(@username, @password).length}.by(-1)
247
+ end
248
+
249
+ VCR.use_cassette('get_keys-find_none') do
250
+ subsequent_request = get_keys(@username, @password).length
251
+ end
252
+
253
+ (subsequent_request - initial_request).should be(-1)
254
+ end
255
+
256
+ end
257
+
258
+
259
+ it "should add ssh keys to github when user is created, and delete them when destroyed" do
260
+ # yes, delete all
261
+ Gas::Prompter.stub!(:user_wants_to_delete_all_ssh_data?).and_return("a") # all keys, local and github
262
+ # create new user and use ssh handling
263
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
264
+ Gas::Prompter.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
265
+ Gas::Prompter.stub!(:user_wants_to_install_key_to_github?).and_return(true)
266
+
267
+ VCR.use_cassette('add-on-crteation-delete-on-deletion', :record => :new_episodes) do
268
+ lambda do
269
+ Gas.add(@nickname,@name,@email, @github_speaker)
270
+ end.should change{get_keys(@username, @password).length}.by(1)
271
+
272
+ lambda do
273
+ Gas::Ssh.stub!(:get_nils).and_return({ :username => @username, :password => @password })
274
+ Gas.delete(@nickname)
275
+ Gas::Ssh.unstub!(:get_nils)
276
+ end.should change{get_keys(@username, @password).length}.by(-1)
277
+ end
278
+
279
+ Gas::Prompter.unstub!(:user_wants_to_delete_all_ssh_data?)
280
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
281
+ Gas::Prompter.unstub!(:user_wants_to_use_key_already_in_ssh?)
282
+ Gas::Prompter.unstub!(:user_wants_to_install_key_to_github?)
283
+ end
284
+
285
+ it "Gas.Delete should be able to remove the id_rsa from .gas" do
286
+ Gas::Prompter.stub!(:user_wants_to_delete_all_ssh_data?).and_return("a")
287
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
288
+ Gas::Prompter.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
289
+ Gas::Prompter.stub!(:user_wants_to_install_key_to_github?).and_return(true)
290
+
291
+ VCR.use_cassette('add-on-crteation-delete-on-deletion') do
292
+ lambda do
293
+ Gas.add(@nickname,@name,@email, @github_speaker)
294
+ end.should change{count_of_files_in(GAS_DIRECTORY)}.by(2)
295
+
296
+ lambda do
297
+ Gas::Ssh.stub!(:get_nils).and_return({:username => @username, :password => @password })
298
+ Gas.delete(@nickname)
299
+ Gas::Ssh.unstub!(:get_nils)
300
+ end.should change{count_of_files_in(GAS_DIRECTORY)}.by(-2)
301
+ end
302
+
303
+ Gas::Prompter.unstub!(:user_wants_to_delete_all_ssh_data?)
304
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
305
+ Gas::Prompter.unstub!(:user_wants_to_use_key_already_in_ssh?)
306
+ Gas::Prompter.unstub!(:user_wants_to_install_key_to_github?)
307
+ end
308
+
309
+ it 'Gas.ssh(nickname) should be able to add ssh support to a legacy user or an opt-out' do
310
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(false)
311
+ Gas.add(@nickname,@name,@email)
312
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
313
+
314
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
315
+ Gas::Ssh.stub!(:upload_public_key_to_github)
316
+
317
+ lambda do
318
+ Gas.ssh(@nickname)
319
+ end.should change{count_of_files_in(GAS_DIRECTORY)}.by(2)
320
+
321
+ Gas::Ssh.delete_associated_local_keys!(@nickname)
322
+
323
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
324
+ Gas::Ssh.unstub!(:upload_public_key_to_github)
325
+ end
326
+
327
+
328
+
329
+ it "Should be able to tell if it's ever used this key under this ISP provider before and then warn the user"
330
+
331
+ it 'Should have the ability to show if the author is associated with a specific github account NAME, stored in gas.accouts file'
332
+
333
+ it 'Should have the ability to link up with non-github git-daemons'
334
+
335
+ end
336
+
337
+
338
+ end
@@ -0,0 +1,163 @@
1
+ # encoding: utf-8
2
+
3
+ if ENV['COVERAGE']
4
+ require 'simplecov'
5
+ SimpleCov.start
6
+ end
7
+
8
+ require 'fileutils'
9
+ include FileUtils
10
+
11
+ # Create a virtual directory in the tmp folder so
12
+ # we don't risk damaging files on the running machine
13
+ fake_home = '/tmp/gas-virtual-fs'
14
+ rm_rf fake_home if File.exists? fake_home
15
+ mkdir_p fake_home
16
+ mkdir_p fake_home + '/.ssh'
17
+ ENV['HOME'] = fake_home
18
+
19
+
20
+ RSpec.configure do |config|
21
+ config.mock_with :rr
22
+ end
23
+
24
+ # Configure VCR, this thing alows you to record HTTP traffic so you never
25
+ # Need to connect to a server. Tests run offline just fine!
26
+ require 'vcr'
27
+
28
+ VCR.configure do |c|
29
+ #c.allow_http_connections_when_no_cassette = true # set to true if you're refreshing the cassets in fixtures
30
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
31
+ c.hook_into :webmock # or :fakeweb
32
+ end
33
+
34
+
35
+ # Mocks a cli call using ` with rr.
36
+ # Takes a block to use as rr return block
37
+ # @param [Object] mock_object The object to mock
38
+ # @param [String] command The command to mock
39
+ def mock_cli_call(mock_object, command)
40
+ # To be able to mock ` (http://blog.astrails.com/2010/7/5/how-to-mock-backticks-operator-in-your-test-specs-using-rr)
41
+ mock(mock_object).__double_definition_create__.call(:`, command) { yield }
42
+ end
43
+
44
+ def clean_out_ssh_directory
45
+ if File.exists?(SSH_DIRECTORY + "/id_rsa")
46
+ File.delete(SSH_DIRECTORY + "/id_rsa")
47
+ end
48
+
49
+ if File.exists?(SSH_DIRECTORY + "/id_rsa.pub")
50
+ File.delete(SSH_DIRECTORY + "/id_rsa.pub")
51
+ end
52
+
53
+ end
54
+
55
+ def restore_the_testers_ssh_key
56
+ if File.exists?(GAS_DIRECTORY + "/temp_test")
57
+ @pattern_to_restore_privl = File.open(GAS_DIRECTORY + "/temp_test","r").read # this test requires some juggling of files that may already exist.
58
+ File.open(SSH_DIRECTORY + "/id_rsa","w+").puts @pattern_to_restore_privl
59
+ File.delete(GAS_DIRECTORY + "/temp_test")
60
+ end
61
+
62
+ if File.exists?(GAS_DIRECTORY + "/temp_test.pub")
63
+ @pattern_to_restore_publ = File.open(GAS_DIRECTORY + "/temp_test.pub","r").read # We don't want to mess things up for the tester, so we will need to save these files and then delete them
64
+ File.open(SSH_DIRECTORY + "/id_rsa.pub","w+").puts @pattern_to_restore_publ
65
+ File.delete(GAS_DIRECTORY + "/temp_test.pub")
66
+ end
67
+ end
68
+
69
+ def clean_out_gas_directory(nickname)
70
+ if File.exists?(GAS_DIRECTORY + "/#{nickname}_id_rsa")
71
+ File.delete(GAS_DIRECTORY + "/#{nickname}_id_rsa")
72
+ end
73
+
74
+ if File.exists?(SSH_DIRECTORY + "/#{nickname}_id_rsa.pub")
75
+ File.delete(@ssh_dir + "/#{nickname}_id_rsa.pub")
76
+ end
77
+ end
78
+
79
+ # this function either mutates an existing file, or creates a new file that won't
80
+ # have been backed up by gas
81
+ def plant_bogus_rsa_keys_in_ssh_directory
82
+ id_rsa = "this rsa file is bogus and not backed up by .gas yet"
83
+ id_rsa_pub = "this pub rsa file is bogus and not backed up by .gas yet"
84
+
85
+ File.open(SSH_DIRECTORY + "/id_rsa","w+") do |f|
86
+ f.puts id_rsa
87
+ end
88
+
89
+ File.open(SSH_DIRECTORY + "/id_rsa.pub","w+") do |f|
90
+ f.puts id_rsa_pub
91
+ end
92
+
93
+ return [id_rsa, id_rsa_pub]
94
+ end
95
+
96
+
97
+ def create_user_no_git(nickname, name, email)
98
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
99
+ #Gas::Ssh.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
100
+ Gas::Prompter.stub!(:user_wants_to_install_key_to_github?).and_return(false)
101
+
102
+ Gas.add(nickname,name,email)
103
+
104
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
105
+ #Gas::Ssh.unstub!(:user_wants_to_use_key_already_in_ssh?)
106
+ Gas::Prompter.unstub!(:user_wants_to_install_key_to_github?)
107
+ end
108
+
109
+ # toasts ssh keys for a given nickname and removal from gas.authors
110
+ def delete_user_no_git(nickname)
111
+ Gas.delete(nickname)
112
+ end
113
+
114
+ # Cycles through github, looking to see if rsa exists as a public key, then deletes it if it does
115
+ def remove_key_from_github_account(username, password, rsa)
116
+ # get all keys
117
+ keys = Gas::Ssh.get_keys(username, password)
118
+ # loop through arrays checking against 'key'
119
+ keys.each do |key|
120
+ if key["key"] == rsa
121
+ return Gas::Ssh.remove_key_by_id!(username, password, key["id"])
122
+ end
123
+ end
124
+
125
+ return false # key not found
126
+ end
127
+
128
+ def delete_all_keys_in_github_account!(github_speaker)
129
+ VCR.use_cassette('delete_all_keys_in_github_account', :record => :new_episodes) do
130
+ github_speaker.keys.each do |key|
131
+ Gas::GithubSpeaker.publicize_methods do
132
+ github_speaker.remove_key_by_id! key['id']
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def get_keys(username, password)
139
+ server = 'api.github.com'
140
+ path = '/user/keys'
141
+
142
+ http = Net::HTTP.new(server,443)
143
+ req = Net::HTTP::Get.new(path)
144
+ http.use_ssl = true
145
+ req.basic_auth username, password
146
+ response = http.request(req)
147
+
148
+ return JSON.parse(response.body)
149
+ end
150
+
151
+ def count_of_files_in(directory_path)
152
+ Dir.glob(File.join(directory_path, '**', '*')).select { |file| File.file?(file) }.count
153
+ end
154
+
155
+ # This is used for publicizing the methods of a class so you can use TDD for projects, even in RUBY!
156
+ class Class
157
+ def publicize_methods
158
+ saved_private_instance_methods = self.private_instance_methods
159
+ self.class_eval { public(*saved_private_instance_methods) }
160
+ yield
161
+ self.class_eval { private(*saved_private_instance_methods) }
162
+ end
163
+ end
@@ -0,0 +1,83 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ describe Gas::Config do
6
+
7
+ before :each do
8
+ @name = 'Fredrik Wallgren'
9
+ @email = 'fredrik.wallgren@gmail.com'
10
+ @nickname = 'walle'
11
+ config = "[#{@nickname}]\n name = #{@name}\n email = #{@email}\n\n[user2]\n name = foo\n email = bar"
12
+ @config = Gas::Config.new nil, config
13
+ end
14
+
15
+ it 'should be able to parse users from config format' do
16
+ @config.users.count.should be 2
17
+ @config.users[0].name.should eq @name
18
+ @config.users[0].email.should eq @email
19
+ @config.users[0].nickname.should eq @nickname
20
+ end
21
+
22
+ it 'should output the users in the correct format' do
23
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
24
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
25
+ users = [user1, user2]
26
+ config = Gas::Config.new users
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"
31
+ end
32
+
33
+ it 'should be able to tell if a nickname exists' do
34
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
35
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
36
+ users = [user1, user2]
37
+ config = Gas::Config.new users
38
+ config.exists?('walle').should be_true
39
+ config.exists?('foo').should be_false
40
+ config.exists?('user2').should be_true
41
+ end
42
+
43
+ it 'should be able to get a user from a nickname' do
44
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
45
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
46
+ users = [user1, user2]
47
+ config = Gas::Config.new users
48
+ config.get('walle').should eq user1
49
+ config.get('user2').should eq user2
50
+ config['walle'].should eq user1
51
+ config['user2'].should eq user2
52
+ config[:walle].should eq user1
53
+ config[:user2].should eq user2
54
+ end
55
+
56
+ it 'should be able to add users' do
57
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
58
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
59
+ users = [user1]
60
+ config = Gas::Config.new users
61
+ config.users.count.should be 1
62
+ config.add user2
63
+ config.users.count.should be 2
64
+ end
65
+
66
+ it 'should be able to delete users by nickname' do
67
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
68
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
69
+ users = [user1, user2]
70
+ config = Gas::Config.new users
71
+ config.users.count.should be 2
72
+ config.delete 'walle'
73
+ config.users.count.should be 1
74
+ config.delete 'user2'
75
+ config.users.count.should be 0
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
+
82
+ end
83
+
@@ -0,0 +1,85 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+ #require './lib/gas/user'
5
+
6
+ describe Gas::Gitconfig do
7
+
8
+ before :each do
9
+ @name = 'Fredrik Wallgren'
10
+ @email = 'fredrik.wallgren@gmail.com'
11
+ @nickname = 'Fred'
12
+ @gitconfig = Gas::Gitconfig.new
13
+ end
14
+
15
+ it 'should be able to get current user from gitconfig' do
16
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { @name + "\n" }
17
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { @email + "\n" }
18
+
19
+ user = @gitconfig.current_user
20
+ user.name.should eq @name
21
+ user.email.should eq @email
22
+ end
23
+
24
+ it 'should return nil if no current user is present in gitconfig' do
25
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { nil }
26
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { nil }
27
+
28
+ @gitconfig.current_user.should be_nil
29
+ end
30
+
31
+
32
+ describe "Multiple users" do
33
+
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 eq @user1.name
49
+ user.email.should eq @user1.email
50
+ user.nickname.should eq @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 eq @name
68
+ user.email.should eq @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 eq name
81
+ user.email.should eq email # test that the user changed appropriately
82
+ end
83
+
84
+ end
85
+ end