|
1 | 1 | require 'spec_helper' |
2 | 2 | require 'tempfile' |
| 3 | +require 'github/auth/key' |
3 | 4 | require 'github/auth/keys_file' |
4 | 5 |
|
5 | 6 | describe Github::Auth::KeysFile do |
|
38 | 39 |
|
39 | 40 | describe '#write!' do |
40 | 41 | shared_examples_for 'a successful key addition' do |
41 | | - it 'writes the key to the keys file' do |
| 42 | + it 'writes the key and github url to the keys file' do |
42 | 43 | subject.write! keys |
43 | 44 |
|
44 | 45 | keys_file.read.tap do |keys_file_content| |
45 | | - keys.each { |key| expect(keys_file_content).to include key } |
| 46 | + keys.each do |key| |
| 47 | + expect(keys_file_content).to include "#{key.key} #{key.url}" |
| 48 | + end |
46 | 49 | end |
47 | 50 | end |
48 | 51 |
|
|
56 | 59 | end |
57 | 60 |
|
58 | 61 | context 'with many keys' do |
59 | | - let(:keys) { %w(abc123 def456 ghi789) } |
| 62 | + let(:keys) {[ |
| 63 | + Github::Auth::Key.new('chris', 'abc123'), |
| 64 | + Github::Auth::Key.new('chris', 'def456'), |
| 65 | + Github::Auth::Key.new('doug', 'ghi789') |
| 66 | + ]} |
60 | 67 |
|
61 | 68 | it_should_behave_like 'a successful key addition' |
62 | 69 | end |
63 | 70 |
|
64 | 71 | context 'with a single key' do |
65 | | - let(:keys) { %w(abc123) } |
| 72 | + let(:keys) {[ Github::Auth::Key.new('chris', 'abc123') ]} |
66 | 73 |
|
67 | 74 | it_should_behave_like 'a successful key addition' |
68 | 75 | end |
69 | 76 |
|
70 | 77 | context 'with existing keys in the keys file' do |
71 | 78 | let(:existing_keys) { %w(abc123 def456 ghi789) } |
72 | | - let(:keys) { %w(jkl012) } |
| 79 | + let(:keys) {[ Github::Auth::Key.new('chris', 'jkl012') ]} |
73 | 80 |
|
74 | 81 | before do |
75 | 82 | keys_file.write existing_keys.join("\n") |
|
89 | 96 | end |
90 | 97 |
|
91 | 98 | it 'does not write duplicate keys into the keys file' do |
92 | | - subject.write! existing_keys.first |
| 99 | + subject.write! Github::Auth::Key.new('chris', existing_keys.first) |
93 | 100 |
|
94 | 101 | expect(keys_file.readlines.count).to eq existing_keys.count |
95 | 102 | end |
|
100 | 107 |
|
101 | 108 | it 'raises PermissionDeniedError' do |
102 | 109 | expect { |
103 | | - subject.write! %w(abc123 def456) |
| 110 | + subject.write! Github::Auth::Key.new('chris', 'abc123') |
104 | 111 | }.to raise_error Github::Auth::KeysFile::PermissionDeniedError |
105 | 112 | end |
106 | 113 | end |
|
117 | 124 | end |
118 | 125 |
|
119 | 126 | describe '#delete!' do |
120 | | - let(:keys) { %w(abc123 def456 ghi789) } |
| 127 | + let(:keys) {[ |
| 128 | + Github::Auth::Key.new('chris', 'abc123'), |
| 129 | + Github::Auth::Key.new('chris', 'def456'), |
| 130 | + Github::Auth::Key.new('doug', 'ghi789') |
| 131 | + ]} |
121 | 132 |
|
122 | 133 | before do |
123 | 134 | keys_file.write keys.join("\n") |
|
128 | 139 | it 'removes the key from the keys file' do |
129 | 140 | subject.delete! key |
130 | 141 |
|
131 | | - expect(keys_file.read).to_not include key |
| 142 | + expect(keys_file.read).to_not include key.to_s |
132 | 143 | end |
133 | 144 |
|
134 | 145 | it 'does not remove the other keys from the keys file' do |
135 | 146 | subject.delete! key |
136 | 147 |
|
137 | 148 | keys_file.read.tap do |keys_file_content| |
138 | | - keys.reject { |other_key| other_key =~ /#{key}/ }.each do |key| |
139 | | - expect(keys_file_content).to include key |
| 149 | + keys.reject { |other_key| other_key == key }.each do |key| |
| 150 | + expect(keys_file_content).to include key.to_s |
140 | 151 | end |
141 | 152 | end |
142 | 153 | end |
|
168 | 179 | it_should_behave_like 'a successful key removal' |
169 | 180 | end |
170 | 181 |
|
171 | | - context 'when the key has a comment' do |
172 | | - let(:keys) {[ 'abc123', "#{key} #{comment}", 'ghi789' ]} |
173 | | - let(:key) { 'def456' } |
174 | | - let(:comment) { 'this is a comment' } |
175 | | - |
176 | | - it_should_behave_like 'a successful key removal' |
177 | | - |
178 | | - it 'removes the comment from the keys file' do |
179 | | - subject.delete! key |
180 | | - |
181 | | - expect(keys_file.read).to_not include comment |
182 | | - end |
183 | | - end |
184 | | - |
185 | 182 | context 'when the keys file does not have the key' do |
186 | | - let(:key) { 'not-in-the-keys-file' } |
| 183 | + let(:key) { Github::Auth::Key.new('sallie', 'not-in-the-keys-file') } |
187 | 184 |
|
188 | 185 | it 'does not modify the keys file' do |
189 | 186 | keys_file.read.tap do |original_keys_file_content| |
|
0 commit comments