gas2 0.1.7b

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,148 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ require 'fileutils'
5
+ include FileUtils
6
+
7
+ # Create a virtual directory in the tmp folder so
8
+ # we don't risk damaging files on the running machine
9
+ fake_home = '/tmp/gas-virtual-fs'
10
+ rm_rf fake_home if File.exists? fake_home
11
+ mkdir_p fake_home
12
+ mkdir_p fake_home + '/.ssh'
13
+ ENV['HOME'] = fake_home
14
+
15
+
16
+ RSpec.configure do |config|
17
+ config.mock_with :rr
18
+ end
19
+
20
+ # Configure VCR, this thing alows you to record HTTP traffic so you never
21
+ # Need to connect to a server. Tests run offline just fine!
22
+ require 'vcr'
23
+
24
+ VCR.configure do |c|
25
+ #c.allow_http_connections_when_no_cassette = true # set to true if you're refreshing the cassets in fixtures
26
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
27
+ c.hook_into :webmock # or :fakeweb
28
+ end
29
+
30
+
31
+ # Mocks a cli call using ` with rr.
32
+ # Takes a block to use as rr return block
33
+ # @param [Object] mock_object The object to mock
34
+ # @param [String] command The command to mock
35
+ def mock_cli_call(mock_object, command)
36
+ # To be able to mock ` (http://blog.astrails.com/2010/7/5/how-to-mock-backticks-operator-in-your-test-specs-using-rr)
37
+ mock(mock_object).__double_definition_create__.call(:`, command) { yield }
38
+ end
39
+
40
+
41
+
42
+ def clean_out_ssh_directory
43
+ if File.exists?(SSH_DIRECTORY + "/id_rsa")
44
+ File.delete(SSH_DIRECTORY + "/id_rsa")
45
+ end
46
+
47
+ if File.exists?(SSH_DIRECTORY + "/id_rsa.pub")
48
+ File.delete(SSH_DIRECTORY + "/id_rsa.pub")
49
+ end
50
+ end
51
+
52
+ def clean_out_gas_directory(nickname)
53
+ if File.exists?(GAS_DIRECTORY + "/#{nickname}_id_rsa")
54
+ File.delete(GAS_DIRECTORY + "/#{nickname}_id_rsa")
55
+ end
56
+
57
+ if File.exists?(SSH_DIRECTORY + "/#{nickname}_id_rsa.pub")
58
+ File.delete(@ssh_dir + "/#{nickname}_id_rsa.pub")
59
+ end
60
+ end
61
+
62
+ # this function either mutates an existing file, or creates a new file that won't
63
+ # have been backed up by gas
64
+ def plant_bogus_rsa_keys_in_ssh_directory
65
+ id_rsa = "this rsa file is bogus and not backed up by .gas yet"
66
+ id_rsa_pub = "this pub rsa file is bogus and not backed up by .gas yet"
67
+
68
+ File.open(SSH_DIRECTORY + "/id_rsa","w+") do |f|
69
+ f.puts id_rsa
70
+ end
71
+
72
+ File.open(SSH_DIRECTORY + "/id_rsa.pub","w+") do |f|
73
+ f.puts id_rsa_pub
74
+ end
75
+
76
+ return [id_rsa, id_rsa_pub]
77
+ end
78
+
79
+
80
+ def create_user_no_git(nickname, name, email)
81
+ Gas::Prompter.stub!(:user_wants_gas_to_handle_rsa_keys?).and_return(true)
82
+ #Gas::Ssh.stub!(:user_wants_to_use_key_already_in_ssh?).and_return(false)
83
+ Gas::Prompter.stub!(:user_wants_to_install_key_to_github?).and_return(false)
84
+
85
+ Gas.add(nickname,name,email)
86
+
87
+ Gas::Prompter.unstub!(:user_wants_gas_to_handle_rsa_keys?)
88
+ #Gas::Ssh.unstub!(:user_wants_to_use_key_already_in_ssh?)
89
+ Gas::Prompter.unstub!(:user_wants_to_install_key_to_github?)
90
+ end
91
+
92
+ # toasts ssh keys for a given nickname and removal from gas.authors
93
+ def delete_user_no_git(nickname)
94
+ Gas.delete(nickname)
95
+ end
96
+
97
+
98
+ # Cycles through github, looking to see if rsa exists as a public key, then deletes it if it does
99
+ def remove_key_from_github_account(username, password, rsa)
100
+ # get all keys
101
+ keys = Gas::Ssh.get_keys(username, password)
102
+ # loop through arrays checking against 'key'
103
+ keys.each do |key|
104
+ if key["key"] == rsa
105
+ return Gas::Ssh.remove_key_by_id!(username, password, key["id"])
106
+ end
107
+ end
108
+
109
+ return false # key not found
110
+ end
111
+
112
+ def delete_all_keys_in_github_account!(github_speaker)
113
+ VCR.use_cassette('delete_all_keys_in_github_account', :record => :new_episodes) do
114
+ github_speaker.keys.each do |key|
115
+ Gas::GithubSpeaker.publicize_methods do
116
+ github_speaker.remove_key_by_id! key['id']
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ def get_keys(username, password)
123
+ server = 'api.github.com'
124
+ path = '/user/keys'
125
+
126
+
127
+ http = Net::HTTP.new(server,443)
128
+ req = Net::HTTP::Get.new(path)
129
+ http.use_ssl = true
130
+ req.basic_auth username, password
131
+ response = http.request(req)
132
+
133
+ return JSON.parse(response.body)
134
+ end
135
+
136
+ def count_of_files_in(directory_path)
137
+ Dir.glob(File.join(directory_path, '**', '*')).select { |file| File.file?(file) }.count
138
+ end
139
+
140
+ # This is used for publicizing the methods of a class so you can use TDD for projects, even in RUBY!
141
+ class Class
142
+ def publicize_methods
143
+ saved_private_instance_methods = self.private_instance_methods
144
+ self.class_eval { public(*saved_private_instance_methods) }
145
+ yield
146
+ self.class_eval { private(*saved_private_instance_methods) }
147
+ end
148
+ 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
@@ -0,0 +1,107 @@
1
+ require './spec/spec_helper'
2
+ require './lib/gas'
3
+
4
+
5
+ describe Gas::GithubSpeaker do
6
+
7
+ before :each do
8
+ config = "[#{@nickname}]\n name = #{@name}\n email = #{@email}\n\n[user2]\n name = foo\n email = bar"
9
+ @config = Gas::Config.new nil, config
10
+ @user = @config.users[0]
11
+
12
+ @username = "aTestGitAccount" # be VERY careful when plugging in your own testing account here...
13
+ @password = "plzdon'thackthetestaccount1" # It will delete ALL keys associated with that account if you run the tests
14
+
15
+ @sample_rsa = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDn74QR9yHb+hcid8iH3+FTaEwnKtwjttseJDbIA2PaivN2uvESrvHlp8Ss/cRox3fFu34QR5DpdOhlfULjTX7yKVuxhaNrAJaqg8rX8hgr9U1Botnyy1DBueEyyA3o1fxRkmwTf6FNnkt1BxWP635tD0lbmUubwaadXjQqPOf3Uw=="
16
+
17
+ VCR.use_cassette('instantiate_github_speaker') do
18
+ @github_speaker = Gas::GithubSpeaker.new(@user, @username, @password)
19
+ end
20
+ end
21
+
22
+ describe "test keys" do
23
+
24
+ describe "with no keys..." do
25
+ after :each do
26
+ delete_all_keys_in_github_account!(@github_speaker)
27
+ end
28
+
29
+ it "should post_key! all the way up to github" do
30
+ initial_length = 'VCR introduces scope =('
31
+ final_length = ''
32
+
33
+ VCR.use_cassette('get_keys-find_none') do
34
+ initial_length = get_keys(@username, @password).length
35
+ end
36
+
37
+ VCR.use_cassette('githubspeaker-post_key') do # this test has been saved under fixtures/install-delete-a-key.yml
38
+ @github_speaker.post_key! @sample_rsa
39
+ end
40
+
41
+ VCR.use_cassette('get_keys-find_one') do
42
+ final_length = get_keys(@username, @password).length
43
+ end
44
+
45
+ (final_length - initial_length).should be(1)
46
+ end
47
+
48
+ it "should expose an empty array when no keys..." do
49
+ VCR.use_cassette('check_keys-empty') do
50
+ @github_speaker.keys.empty?.should be_true
51
+ end
52
+ end
53
+
54
+ it "should expose a key created..." do
55
+ VCR.use_cassette('github_speaker-post_key') do
56
+ @github_speaker.post_key! @sample_rsa
57
+ end
58
+
59
+ @github_speaker.keys.empty?.should be_false
60
+ end
61
+ end
62
+
63
+ describe "with a key" do
64
+ before :each do
65
+ VCR.use_cassette('github_speaker-post_key') do
66
+ @github_speaker.post_key! @sample_rsa
67
+ end
68
+
69
+ @key_id = @github_speaker.keys.first['id']
70
+ end
71
+
72
+ after :each do
73
+ delete_all_keys_in_github_account!(@github_speaker)
74
+ end
75
+
76
+ it "should remove_key! from github" do
77
+ initial_length = ''
78
+ final_length = ''
79
+
80
+ VCR.use_cassette('get_keys-find_one') do
81
+ initial_length = get_keys(@username, @password).length
82
+ end
83
+
84
+ VCR.use_cassette('githubspeaker-remove_key') do
85
+ @github_speaker.remove_key! @sample_rsa
86
+ end
87
+
88
+ VCR.use_cassette('get_keys-find_none') do
89
+ final_length = get_keys(@username, @password).length
90
+ end
91
+
92
+ (final_length - initial_length).should be(-1)
93
+ end
94
+
95
+ it "should remove a key from @keys" do
96
+ #require 'pry';binding.pry
97
+ Gas::GithubSpeaker.publicize_methods do
98
+ @github_speaker.remove_key_from_keys @key_id
99
+ end
100
+
101
+ @github_speaker.keys.empty?.should be_true
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,56 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ describe Gas::Settings do
6
+
7
+ before(:all) do
8
+ @subject = Gas::Settings.new
9
+ end
10
+
11
+ it 'should respond to base_dir' do
12
+ @subject.should respond_to :base_dir
13
+ end
14
+
15
+ it 'should give default value for base_dir' do
16
+ @subject.base_dir.should == '~'
17
+ end
18
+
19
+ it 'should respond to gas_dir' do
20
+ @subject.should respond_to :gas_dir
21
+ end
22
+
23
+ it 'should give default value for gas_dir' do
24
+ @subject.gas_dir.should == "#{@subject.base_dir}/.gas"
25
+ end
26
+
27
+ it 'should respond to ssh_dir' do
28
+ @subject.should respond_to :ssh_dir
29
+ end
30
+
31
+ it 'should give default value for ssh_dir' do
32
+ @subject.ssh_dir.should == "#{@subject.base_dir}/.ssh"
33
+ end
34
+
35
+ it 'should respond to github_server' do
36
+ @subject.should respond_to :github_server
37
+ end
38
+
39
+ it 'should give default value for github_server' do
40
+ @subject.github_server.should == 'api.github.com'
41
+ end
42
+
43
+ it 'should be configurable' do
44
+ @subject.configure do |s|
45
+ s.base_dir = 'test'
46
+ s.gas_dir = 'foo'
47
+ s.ssh_dir = 'bar'
48
+ s.github_server = 'foobar'
49
+ end
50
+ @subject.base_dir.should eq 'test'
51
+ @subject.gas_dir.should eq "#{@subject.base_dir}/foo"
52
+ @subject.ssh_dir.should eq "#{@subject.base_dir}/bar"
53
+ @subject.github_server.should eq 'foobar'
54
+ end
55
+
56
+ end