Permalink
Browse files

Reimplement mailer code

  • Loading branch information...
1 parent 84be99c commit a228cded7101b829bfc9f0fa26c90d1da25cb269 @justinforce committed Jun 25, 2012
View
1 Gemfile
@@ -11,6 +11,7 @@ gem 'sass'
gem 'data_mapper', '~> 1.0'
gem 'sqlite3'
gem 'dm-sqlite-adapter'
+gem 'mail'
group :development, :test do
gem 'ladle'
View
10 Gemfile.lock
@@ -87,6 +87,7 @@ GEM
guard (>= 0.2.1)
guard-rspec (0.7.0)
guard (>= 0.10.0)
+ i18n (0.6.0)
json (1.6.6)
json_pure (1.6.6)
ladle (0.2.0)
@@ -97,6 +98,10 @@ GEM
libv8 (3.3.10.4)
libwebsocket (0.1.3)
addressable
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.0.4)
net-ldap (0.3.1)
@@ -107,6 +112,7 @@ GEM
fastthread (>= 1.0.1)
rack
rake (>= 0.8.1)
+ polyglot (0.3.3)
rack (1.4.1)
rack-protection (1.2.0)
rack
@@ -156,6 +162,9 @@ GEM
libv8 (~> 3.3.10)
thor (0.14.6)
tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
uuidtools (2.1.2)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -174,6 +183,7 @@ DEPENDENCIES
ladle
launchy
libnotify
+ mail
net-ldap
passenger
rspec
View
2 lib/account_manager.rb
@@ -3,4 +3,4 @@
require 'account_manager/directory'
require 'account_manager/models'
require 'account_manager/password_strength'
-require 'account_manager/mail'
+require 'account_manager/mailer'
View
45 lib/account_manager/mail.rb
@@ -1,45 +0,0 @@
-require 'account_manager'
-
-ResetTemplate = <<END
-From: %s
-To: %s
-Subject: Password reset for %s
-
-To reset your password, visit the following link and create a new one:
-
- %s
-END
-
-module AccountManager
-
- class Mail
-
- extend Configurable
-
- class << self
-
- def reset(url, token)
-
- mail_conf = conf['mail']
- from = mail_conf['from']
- account = Directory.mail(token.uid)
-
- to = Directory.forwarding_address(token.uid)
- return :no_forwarding_address if to.nil?
-
- mail = ResetTemplate % [from, to, account, "#{url}/#{token.slug}"]
-
- begin
- Net::SMTP.start mail_conf['host'], mail_conf['port'] do |smtp|
- smtp.starttls if smtp.capable_starttls?
- smtp.authenticate mail_conf['user'], mail_conf['password'] if mail_conf['password']
- smtp.send_message mail, from, to
- end
- rescue Exception
- throw :mail_error
- end
- :success
- end
- end
- end
-end
View
145 lib/account_manager/mailer.rb
@@ -0,0 +1,145 @@
+require 'account_manager'
+require 'mail'
+require 'base64'
+
+TEMPLATES = {}
+
+TEMPLATES[:reset_text] = <<END
+Information Technology Group, The Gevirtz School
+
+Someone has requested that the password for your account %{account} be reset.
+
+If you didn't make this request, you can disregard this email.
+
+If you did make this request, you can reset your password any time in the next 24 hours by following this link:
+
+ %{link}
+
+If you miss this window, don't worry. You can just request a new password reset here:
+
+ %{reset}
+
+Information Technology Group
+The Gevirtz School
+University of California, Santa Barbara
+%{site}
+%{from}
+%{phone}
+
+END
+
+TEMPLATES[:reset_html] = <<END
+<h1>
+ <img src="data:image/png;base64,%{logo64}" alt="ITG" style="vertical-align:middle">
+ The Gevirtz School
+</h1>
+<p>
+ Someone has requested that the password for your account %{account} be reset.
+</p>
+<p>
+ If you didn't make this request, you can disregard this email.
+</p>
+<p style="margin-top: 40px">
+ If you did make this request, you can
+ <strong>
+ reset your password
+ </strong>
+ any time in the next
+ <em>
+ 24 hours
+ </em>
+ by following this link:
+</p>
+
+<ul>
+ <li>
+ <a href="%{link}">%{link}</a>
+ </li>
+</ul>
+
+<p style="font-style: italic; margin-top: 40px">
+ If you miss this window, don't worry. You can just request a new password
+ reset <a href="%{reset}">here</a>.
+</p>
+
+<p style="color: #333; margin-top: 50px">
+ Information Technology Group
+ <br />
+ The Gevirtz School
+ <br />
+ University of California, Santa Barbara
+ <br />
+ <a href="%{site}">%{site}</a>
+ <br />
+ <a href="mailto:%{from}">%{from}</a>
+ <br />
+ %{phone}
+</p>
+
+END
+
+module AccountManager
+
+ class Mailer
+
+ extend Configurable
+
+ class << self
+
+ def reset(url, token)
+
+ mail_conf = conf['mail']
+
+ from = mail_conf['from']
+ host = mail_conf['host']
+ port = mail_conf['port']
+ user = mail_conf['user']
+ password = mail_conf['password']
+ phone = mail_conf['phone']
+ site = mail_conf['site']
+
+ account = Directory.mail token.uid
+
+ to = Directory.forwarding_address token.uid
+ return :no_forwarding_address if to.nil?
+
+
+ link = "#{url}/#{token.slug}"
+
+ smtp = Net::SMTP.new host, port
+ smtp.enable_starttls_auto
+ smtp.start host, user, password, :plain
+ Mail.defaults do
+ delivery_method :smtp_connection, { connection: smtp }
+ end
+
+ Mail.deliver do
+ to to
+ from "help@education.ucsb.edu"
+ subject "Password reset for #{account}"
+
+ args = {
+ logo64: Base64.encode64(open('public/images/itg.png') {|f| f.read}),
+ link: link,
+ account: account,
+ reset: url,
+ from: from,
+ phone: phone,
+ site: site
+ }
+
+ text_part do
+ body TEMPLATES[:reset_text] % args
+ end
+
+ html_part do
+ content_type 'text/html; charset=UTF-8'
+ body TEMPLATES[:reset_html] % args
+ end
+ end
+
+ :success
+ end
+ end
+ end
+end
View
2 lib/account_manager/models.rb
@@ -35,7 +35,7 @@ def request_for(url, uid)
Token.all(uid: uid).destroy
Token.create(uid: uid)
- Mail.reset url, Token.first(uid: uid)
+ Mailer.reset url, Token.first(uid: uid)
end
end
end
View
BIN public/images/itg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
11 spec/account_manager/mail_spec.rb → spec/account_manager/mailer_spec.rb
@@ -2,25 +2,20 @@
module AccountManager
- describe Mail do
+ describe Mailer do
describe '.reset' do
before :each do
Directory.stub(
mail: 'some_user@example.com',
- forwarding_address: 'some_alt@example.org'
+ forwarding_address: 'justin@education.ucsb.edu'
)
@token = mock Token, uid: 'some_user', slug: 'fake_slug'
end
it 'returns success if the mail operation worked' do
- Mail.reset(nil, @token).should be :success
- end
-
- it 'throws :mail_error if the mail operation did not work' do
- Net::SMTP.stub(:start).and_raise Exception
- expect {Mail.reset(nil, @token)}.to throw_symbol :mail_error
+ Mailer.reset(nil, @token).should be :success
end
end
end
View
14 spec/account_manager/models_spec.rb
@@ -35,6 +35,7 @@ module AccountManager
mail: ''
)
Net::SMTP.stub start: true
+ Mailer.stub reset: :success
end
it 'destroys any existing tokens for the user' do
@@ -52,20 +53,19 @@ module AccountManager
Token.request_for('','').should be :no_such_account
end
+ it 'returns success if a token was created and the email was sent' do
+ Token.request_for('','').should be :success
+ end
+
it 'returns :no_forwarding_address if no forwarding address could be found' do
- Mail.stub reset: :no_forwarding_address
+ Mailer.stub reset: :no_forwarding_address
Token.request_for('','').should be :no_forwarding_address
end
it 'throws an :mail_error if there was a problem sending the email' do
- Mail.stub(:reset).and_throw :mail_error
+ Mailer.stub(:reset).and_throw :mail_error
expect {Token.request_for('','')}.should throw_symbol :mail_error
end
-
- it 'returns success if a token was created and the email was sent' do
- Mail.stub reset: :success
- Token.request_for('','').should be :success
- end
end
end
end
View
2 spec/requests/reset_password_spec.rb
@@ -47,7 +47,7 @@ module AccountManager
exists?: true,
activated?: true
)
- Mail.stub reset: :success
+ Mailer.stub reset: :success
Token.request_for nil, 'some_user'
@token = Token.first uid: 'some_user'
end

0 comments on commit a228cde

Please sign in to comment.