gmail 0.5.0 → 0.6.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.
- checksums.yaml +4 -4
- data/.gitignore +27 -27
- data/.rspec +1 -1
- data/.rubocop.yml +13 -13
- data/.rubocop_todo.yml +239 -239
- data/.travis.yml +19 -19
- data/CHANGELOG.md +145 -139
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +355 -354
- data/Rakefile +46 -46
- data/gmail.gemspec +34 -34
- data/lib/gmail.rb +78 -78
- data/lib/gmail/client.rb +34 -34
- data/lib/gmail/client/base.rb +229 -229
- data/lib/gmail/client/plain.rb +24 -24
- data/lib/gmail/client/xoauth.rb +68 -68
- data/lib/gmail/client/xoauth2.rb +39 -39
- data/lib/gmail/imap_extensions.rb +159 -150
- data/lib/gmail/labels.rb +79 -79
- data/lib/gmail/mailbox.rb +175 -135
- data/lib/gmail/message.rb +207 -207
- data/lib/gmail/version.rb +3 -3
- data/spec/account.yml.example +1 -1
- data/spec/account.yml.obfus +2 -2
- data/spec/gmail/client/base_spec.rb +5 -5
- data/spec/gmail/client/plain_spec.rb +169 -168
- data/spec/gmail/client/xoauth2_spec.rb +186 -186
- data/spec/gmail/client/xoauth_spec.rb +5 -5
- data/spec/gmail/client_spec.rb +5 -5
- data/spec/gmail/imap_extensions_spec.rb +47 -12
- data/spec/gmail/labels_spec.rb +27 -18
- data/spec/gmail/mailbox_spec.rb +84 -47
- data/spec/gmail/message_spec.rb +181 -181
- data/spec/gmail_spec.rb +39 -39
- data/spec/recordings/gmail/_new_connects_with_client_and_give_it_context_when_block_given.yml +28 -28
- data/spec/recordings/gmail/_new_connects_with_gmail_service_and_return_valid_connection_object.yml +28 -28
- data/spec/recordings/gmail/_new_does_not_raise_error_when_couldn_t_connect_with_given_account.yml +13 -13
- data/spec/recordings/gmail/_new_raises_error_when_couldn_t_connect_with_given_account.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/_connection_automatically_logs_in_to_gmail_account_when_it_s_called.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/delivers_inline_composed_email.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/does_not_raise_error_even_though_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/labels/checks_if_there_is_given_label_defined.yml +196 -196
- data/spec/recordings/gmail_client_plain/instance/labels/creates_given_label.yml +151 -151
- data/spec/recordings/gmail_client_plain/instance/labels/removes_existing_label.yml +146 -146
- data/spec/recordings/gmail_client_plain/instance/labels/returns_list_of_all_available_labels.yml +113 -113
- data/spec/recordings/gmail_client_plain/instance/properly_logs_in_to_valid_gmail_account.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/properly_logs_out_from_gmail.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox.yml +109 -109
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox_using_block_style.yml +109 -109
- data/spec/recordings/gmail_client_plain/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/checks_if_there_is_given_label_defined.yml +27 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/creates_given_label.yml +39 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/removes_existing_label.yml +39 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/returns_list_of_all_available_labels.yml +27 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_in_to_valid_gmail_account.yml +15 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_out_from_gmail.yml +15 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox.yml +40 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox_using_block_style.yml +40 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -13
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/all/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/and_the_mailbox_does_not_exist/returns_the_mailbox_name_as_a_string.yml +110 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/drafts/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/flagged/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/important/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/inbox/localizes_into_the_appropriate_label.yml +42 -42
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/junk/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/sent/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/trash/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_mailbox/instance/counts_all_emails.yml +277 -277
- data/spec/recordings/gmail_mailbox/instance/finds_messages.yml +586 -586
- data/spec/recordings/gmail_mailbox/instance/waits_once.yml +136 -0
- data/spec/recordings/gmail_mailbox/instance/waits_repeatedly.yml +141 -0
- data/spec/recordings/gmail_mailbox/instance/waits_with_29-minute_re-issue.yml +136 -0
- data/spec/recordings/gmail_mailbox/instance/waits_with_an_unblocked_connection.yml +207 -0
- data/spec/recordings/gmail_mailbox/on_initialize/sets_client_and_name.yml +42 -42
- data/spec/recordings/gmail_mailbox/on_initialize/works_in_inbox_by_default.yml +42 -42
- data/spec/recordings/gmail_message/initialize/sets_prefetch_attrs.yml +578 -578
- data/spec/recordings/gmail_message/initialize/sets_uid_and_mailbox.yml +580 -580
- data/spec/recordings/gmail_message/instance_methods/deletes_itself.yml +637 -637
- data/spec/recordings/gmail_message/instance_methods/marks_itself_read.yml +682 -682
- data/spec/recordings/gmail_message/instance_methods/marks_itself_unread.yml +686 -686
- data/spec/recordings/gmail_message/instance_methods/moves_from_one_tag_to_other.yml +862 -862
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label.yml +776 -776
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label_with_old_method.yml +776 -776
- data/spec/recordings/gmail_message/instance_methods/sets_given_label.yml +690 -690
- data/spec/recordings/gmail_message/instance_methods/sets_given_label_with_old_method.yml +691 -691
- data/spec/spec_helper.rb +53 -53
- data/spec/support/imap_mock.rb +181 -129
- data/spec/support/obfuscation.rb +52 -52
- metadata +78 -5
data/spec/spec_helper.rb
CHANGED
@@ -1,53 +1,53 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'rspec'
|
5
|
-
require 'yaml'
|
6
|
-
require 'gmail'
|
7
|
-
|
8
|
-
# require_support_files
|
9
|
-
Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each { |f| require f }
|
10
|
-
|
11
|
-
RSpec.configure do |config|
|
12
|
-
Spec::ImapMock.configure_rspec!(config)
|
13
|
-
end
|
14
|
-
|
15
|
-
def within_gmail(&block)
|
16
|
-
Gmail.connect!(*TEST_ACCOUNT) do |gmail|
|
17
|
-
yield(gmail)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def mock_client(&block)
|
22
|
-
client = Gmail::Client::Plain.new(*TEST_ACCOUNT)
|
23
|
-
client.connect
|
24
|
-
|
25
|
-
if block_given?
|
26
|
-
client.login
|
27
|
-
yield client
|
28
|
-
client.logout
|
29
|
-
end
|
30
|
-
|
31
|
-
client
|
32
|
-
end
|
33
|
-
|
34
|
-
def mock_mailbox(box = "INBOX", &block)
|
35
|
-
within_gmail do |gmail|
|
36
|
-
mailbox = gmail.mailbox(box)
|
37
|
-
yield(mailbox) if block_given?
|
38
|
-
mailbox
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# TODO: move this to it's own dir; get rid of global variable
|
43
|
-
# Run test by creating your own test account with credentials in account.yml
|
44
|
-
# Otherwise default credentials from an obfuscated file are used.
|
45
|
-
clear_file = File.join(File.dirname(__FILE__), 'account.yml')
|
46
|
-
obfus_file = File.join(File.dirname(__FILE__), 'account.yml.obfus')
|
47
|
-
if File.exist?(clear_file)
|
48
|
-
TEST_ACCOUNT = YAML.load_file(clear_file)
|
49
|
-
elsif File.exist?(obfus_file)
|
50
|
-
TEST_ACCOUNT = Spec::Obfuscation.decrypt_file(obfus_file)
|
51
|
-
else
|
52
|
-
raise 'account.yml file not found'
|
53
|
-
end
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rspec'
|
5
|
+
require 'yaml'
|
6
|
+
require 'gmail'
|
7
|
+
|
8
|
+
# require_support_files
|
9
|
+
Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each { |f| require f }
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
Spec::ImapMock.configure_rspec!(config)
|
13
|
+
end
|
14
|
+
|
15
|
+
def within_gmail(&block)
|
16
|
+
Gmail.connect!(*TEST_ACCOUNT) do |gmail|
|
17
|
+
yield(gmail)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def mock_client(&block)
|
22
|
+
client = Gmail::Client::Plain.new(*TEST_ACCOUNT)
|
23
|
+
client.connect
|
24
|
+
|
25
|
+
if block_given?
|
26
|
+
client.login
|
27
|
+
yield client
|
28
|
+
client.logout
|
29
|
+
end
|
30
|
+
|
31
|
+
client
|
32
|
+
end
|
33
|
+
|
34
|
+
def mock_mailbox(box = "INBOX", &block)
|
35
|
+
within_gmail do |gmail|
|
36
|
+
mailbox = gmail.mailbox(box)
|
37
|
+
yield(mailbox) if block_given?
|
38
|
+
mailbox
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# TODO: move this to it's own dir; get rid of global variable
|
43
|
+
# Run test by creating your own test account with credentials in account.yml
|
44
|
+
# Otherwise default credentials from an obfuscated file are used.
|
45
|
+
clear_file = File.join(File.dirname(__FILE__), 'account.yml')
|
46
|
+
obfus_file = File.join(File.dirname(__FILE__), 'account.yml.obfus')
|
47
|
+
if File.exist?(clear_file)
|
48
|
+
TEST_ACCOUNT = YAML.load_file(clear_file)
|
49
|
+
elsif File.exist?(obfus_file)
|
50
|
+
TEST_ACCOUNT = Spec::Obfuscation.decrypt_file(obfus_file)
|
51
|
+
else
|
52
|
+
raise 'account.yml file not found'
|
53
|
+
end
|
data/spec/support/imap_mock.rb
CHANGED
@@ -1,129 +1,181 @@
|
|
1
|
-
module Net
|
2
|
-
class IMAP
|
3
|
-
class << self
|
4
|
-
def recordings=(value)
|
5
|
-
@replaying = !value.nil?
|
6
|
-
@recordings = value
|
7
|
-
end
|
8
|
-
|
9
|
-
def recordings
|
10
|
-
@recordings ||= {}
|
11
|
-
end
|
12
|
-
|
13
|
-
def replaying?
|
14
|
-
@replaying
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
.
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
1
|
+
module Net
|
2
|
+
class IMAP
|
3
|
+
class << self
|
4
|
+
def recordings=(value)
|
5
|
+
@replaying = !value.nil?
|
6
|
+
@recordings = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def recordings
|
10
|
+
@recordings ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def replaying?
|
14
|
+
@replaying
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :_idle, :idle
|
19
|
+
alias_method :_idle_done, :idle_done
|
20
|
+
|
21
|
+
def idle(&response_handler)
|
22
|
+
if Net::IMAP.replaying?
|
23
|
+
@idle_done_cond = new_cond
|
24
|
+
@idle_done = false
|
25
|
+
end
|
26
|
+
|
27
|
+
response = mock_command(:_idle, 'IDLE', &response_handler)
|
28
|
+
|
29
|
+
if Net::IMAP.replaying?
|
30
|
+
synchronize do
|
31
|
+
unless @idle_done
|
32
|
+
@idle_done_cond.wait(0.1)
|
33
|
+
raise('The IDLE has not done') unless @idle_done
|
34
|
+
end
|
35
|
+
@idle_done_cond = nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
response
|
40
|
+
end
|
41
|
+
|
42
|
+
def idle_done
|
43
|
+
if Net::IMAP.replaying?
|
44
|
+
synchronize do
|
45
|
+
if @idle_done_cond.nil?
|
46
|
+
raise Net::IMAP::Error, 'not during IDLE'
|
47
|
+
end
|
48
|
+
@idle_done = true
|
49
|
+
idle_done_cond.signal
|
50
|
+
end
|
51
|
+
else
|
52
|
+
_idle_done
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
alias_method :_send_command, :send_command
|
59
|
+
|
60
|
+
def self.force_utf8(data)
|
61
|
+
case data.class.to_s
|
62
|
+
when /String/
|
63
|
+
data.force_encoding('utf-8')
|
64
|
+
when /Hash/
|
65
|
+
data.each { |k, v| data[k] = force_utf8(v) }
|
66
|
+
when /Array/
|
67
|
+
data.map { |s| force_utf8(s) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def send_command(cmd, *args, &block)
|
72
|
+
mock_command(:_send_command, cmd, *args, &block)
|
73
|
+
end
|
74
|
+
|
75
|
+
def mock_command(method, cmd, *args, &block)
|
76
|
+
# In Ruby 1.9.x, strings default to binary which causes the digest to be
|
77
|
+
# different.
|
78
|
+
clean_args = args.dup.each do |s|
|
79
|
+
Net::IMAP.force_utf8(s)
|
80
|
+
end
|
81
|
+
|
82
|
+
yaml_dump = YAML.dump([cmd] + clean_args)
|
83
|
+
|
84
|
+
if RUBY_VERSION =~ /^(1.9|2.0)/
|
85
|
+
# From 1.9 to 2.0 to 2.1, the way YAML encodes special characters changed.
|
86
|
+
# Here's what each returns for: YAML.dump(["", "%"])
|
87
|
+
# 1.9.x: "---\n- ''\n- ! '%'\n"
|
88
|
+
# 2.0.x: "---\n- ''\n- '%'\n"
|
89
|
+
# 2.1.x: "---\n- ''\n- \"%\"\n"
|
90
|
+
# The `gsub` here converts the older format into the 2.1.x.
|
91
|
+
yaml_dump.gsub!(/(?:! )?'(.+)'/, '"\1"')
|
92
|
+
|
93
|
+
# In 1.9 and 2.0 strings starting with `+` or `-` are not escaped in quotes, but
|
94
|
+
# they are in 2.1+. This addresses that.
|
95
|
+
yaml_dump.gsub!(/ ([+-](?:X-GM-\w+|FLAGS))/, ' "\1"')
|
96
|
+
|
97
|
+
# In 1.9 and 2.0 strings starting with `\` are not escaped in quotes, but
|
98
|
+
# they are in 2.1+. This addresses that. Yes we need all those backslashes :|
|
99
|
+
yaml_dump.gsub!(/ \\(\w+)/, ' "\\\\\\\\\1"')
|
100
|
+
end
|
101
|
+
|
102
|
+
digest = "#{cmd}-#{Digest::MD5.hexdigest(yaml_dump)}"
|
103
|
+
|
104
|
+
if Net::IMAP.replaying?
|
105
|
+
recordings = Net::IMAP.recordings[digest] || []
|
106
|
+
if recordings.empty?
|
107
|
+
# Be lenient if LOGOUT is called but wasn't explicitly recorded. This
|
108
|
+
# comes up often when called from `at_exit`.
|
109
|
+
cmd == 'LOGOUT' ? return : raise('Could not find recording')
|
110
|
+
end
|
111
|
+
|
112
|
+
action, response, @responses, all_responses = recordings.shift
|
113
|
+
|
114
|
+
if block && all_responses
|
115
|
+
all_responses.each do |resp|
|
116
|
+
block.call(resp)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
else
|
120
|
+
action = :return
|
121
|
+
all_responses = []
|
122
|
+
begin
|
123
|
+
response = send(method, cmd, *args) do |resp|
|
124
|
+
all_responses << resp
|
125
|
+
block.call(resp)
|
126
|
+
end
|
127
|
+
rescue => e
|
128
|
+
action = :raise
|
129
|
+
response = e
|
130
|
+
end
|
131
|
+
|
132
|
+
# @responses (the third argument here) contains untagged responses captured
|
133
|
+
# via the Net::IMAP#record_response method.
|
134
|
+
Net::IMAP.recordings[digest] ||= []
|
135
|
+
Net::IMAP.recordings[digest] << [action, response.dup, @responses ? @responses.dup : nil, all_responses]
|
136
|
+
end
|
137
|
+
|
138
|
+
raise(response) if action == :raise
|
139
|
+
|
140
|
+
response
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
module Spec
|
146
|
+
module ImapMock
|
147
|
+
# Configures RSpec with an around(:each) block to use IMAP mocks
|
148
|
+
def self.configure_rspec!(config)
|
149
|
+
config.around(:each) do |example|
|
150
|
+
Spec::ImapMock.run_rspec_example(example)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# Run an RSpec example using IMAP mocks
|
155
|
+
def self.run_rspec_example(example)
|
156
|
+
# The path is determined by the rspec `describe`s and `context`s
|
157
|
+
mock_path = example.example_group.to_s
|
158
|
+
.gsub(/RSpec::ExampleGroups::/, '')
|
159
|
+
.gsub(/(\w)([A-Z])/, '\1_\2')
|
160
|
+
.gsub(/::/, '/')
|
161
|
+
.downcase
|
162
|
+
|
163
|
+
# The name is determined by the description of the example.
|
164
|
+
mock_name = example.description.gsub(/[^\w\-\/]+/, '_').downcase
|
165
|
+
|
166
|
+
filename = File.join('spec/recordings/', mock_path, "#{mock_name}.yml")
|
167
|
+
|
168
|
+
# If we've already recorded this spec load the recordings
|
169
|
+
Net::IMAP.recordings = File.exist?(filename) ? YAML.load_file(filename) : nil
|
170
|
+
|
171
|
+
example.run
|
172
|
+
|
173
|
+
# If we haven't yet recorded the spec and there were some recordings,
|
174
|
+
# write them to a file.
|
175
|
+
unless File.exist?(filename) or Net::IMAP.recordings.empty?
|
176
|
+
FileUtils.mkdir_p(File.dirname(filename))
|
177
|
+
File.open(filename, 'w') { |f| YAML.dump(Net::IMAP.recordings, f) }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
data/spec/support/obfuscation.rb
CHANGED
@@ -1,52 +1,52 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# Lightweight obfuscation wrapper used to obfuscate account.yml file.
|
5
|
-
#
|
6
|
-
# CAUTION; this is not intended to be a robust security mechanism. It is simple
|
7
|
-
# obfuscation (security through obscurity). There's no strong reason why we couldn't
|
8
|
-
# store the credentials in clear text, but just taking an extra step to prevent trouble.
|
9
|
-
|
10
|
-
module Spec
|
11
|
-
module Obfuscation
|
12
|
-
def encrypt(data)
|
13
|
-
rot13(Base64.encode64(data))
|
14
|
-
end
|
15
|
-
|
16
|
-
def decrypt(data)
|
17
|
-
Base64.decode64(rot13(data))
|
18
|
-
end
|
19
|
-
|
20
|
-
def rot13(data)
|
21
|
-
data.tr!("A-Za-z", "N-ZA-Mn-za-m")
|
22
|
-
end
|
23
|
-
|
24
|
-
def encrypt_file(file)
|
25
|
-
data = read_if_exist!(file)
|
26
|
-
begin
|
27
|
-
File.open("#{file}.obfus", 'w') { |file| file.write(encrypt(data)) }
|
28
|
-
rescue Exception => e
|
29
|
-
raise "Unable to encrypt #{file}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def decrypt_file(file)
|
34
|
-
data = read_if_exist!(file)
|
35
|
-
begin
|
36
|
-
return ::YAML::load(decrypt(data))
|
37
|
-
rescue Exception => e
|
38
|
-
raise "Unable to decrypt #{file}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def read_if_exist!(file)
|
43
|
-
if File.exist?(file)
|
44
|
-
IO.read(file)
|
45
|
-
else
|
46
|
-
raise "File not found #{file}"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
extend self
|
51
|
-
end
|
52
|
-
end
|
1
|
+
require 'base64'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
# Lightweight obfuscation wrapper used to obfuscate account.yml file.
|
5
|
+
#
|
6
|
+
# CAUTION; this is not intended to be a robust security mechanism. It is simple
|
7
|
+
# obfuscation (security through obscurity). There's no strong reason why we couldn't
|
8
|
+
# store the credentials in clear text, but just taking an extra step to prevent trouble.
|
9
|
+
|
10
|
+
module Spec
|
11
|
+
module Obfuscation
|
12
|
+
def encrypt(data)
|
13
|
+
rot13(Base64.encode64(data))
|
14
|
+
end
|
15
|
+
|
16
|
+
def decrypt(data)
|
17
|
+
Base64.decode64(rot13(data))
|
18
|
+
end
|
19
|
+
|
20
|
+
def rot13(data)
|
21
|
+
data.tr!("A-Za-z", "N-ZA-Mn-za-m")
|
22
|
+
end
|
23
|
+
|
24
|
+
def encrypt_file(file)
|
25
|
+
data = read_if_exist!(file)
|
26
|
+
begin
|
27
|
+
File.open("#{file}.obfus", 'w') { |file| file.write(encrypt(data)) }
|
28
|
+
rescue Exception => e
|
29
|
+
raise "Unable to encrypt #{file}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def decrypt_file(file)
|
34
|
+
data = read_if_exist!(file)
|
35
|
+
begin
|
36
|
+
return ::YAML::load(decrypt(data))
|
37
|
+
rescue Exception => e
|
38
|
+
raise "Unable to decrypt #{file}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def read_if_exist!(file)
|
43
|
+
if File.exist?(file)
|
44
|
+
IO.read(file)
|
45
|
+
else
|
46
|
+
raise "File not found #{file}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
extend self
|
51
|
+
end
|
52
|
+
end
|