Skip to content

Commit

Permalink
* added public/download to .gitignore
Browse files Browse the repository at this point in the history
* completed BackupsController new/show views
* added part in BackupsController create to display error messages (hack)
  • Loading branch information
hone committed Jan 10, 2009
1 parent c84c243 commit f1ad26e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ tags
public/cache/**/*
doc/*
coverage/*
public/download/*
34 changes: 21 additions & 13 deletions app/controllers/backups_controller.rb
Expand Up @@ -8,21 +8,29 @@ def new
end

def create
@remote_mail = RemoteMail.new( params[:remote_mail] )
# hack needed to display errors
@remote_mail.save
@pop3 = Pop3.new( params[:remote_mail] )
@pop3.setup_mailer
result = @pop3.download

respond_to do |format|
case result[:status]
when Pop3::OK_FLAG
flash[:notice] = "Backup successfully created"
format.html { redirect_to( :action => :show, :mbox => { :inbox => result[:mbox_name] } ) }
when Pop3::AUTHENTICATION_ERROR_FLAG
flash[:notice] = "Could not create backup due to authentication problems"
format.html { render :action => :new }
# TODO add stuff here
when Pop3::TIMEOUT_ERROR_FLAG
flash[:notice] = "Could not create backup due to timeout error"
if @pop3.valid?
@pop3.setup_mailer
result = @pop3.download

case result[:status]
when Pop3::OK_FLAG
flash[:notice] = "Backup successfully created"
format.html { redirect_to( :action => :show, :mbox => { :inbox => result[:mbox_name] } ) }
when Pop3::AUTHENTICATION_ERROR_FLAG
flash[:notice] = "Could not create backup due to authentication problems"
format.html { render :action => :new }
# TODO add stuff here
when Pop3::TIMEOUT_ERROR_FLAG
flash[:notice] = "Could not create backup due to timeout error"
format.html { render :action => :new }
end
else
flash[:notice] = "Problems with backing up mail, invalid input"
format.html { render :action => :new }
end
end
Expand Down
33 changes: 33 additions & 0 deletions app/views/backups/new.html.erb
@@ -0,0 +1,33 @@
<h1>BackupMyMail</h1>

<% form_for :remote_mail, :url => { :action => :create } do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :email_address %>
<%= f.text_field :email_address %>
</p>
<p>
<%= f.label :server %>
<%= f.text_field :server %>
</p>
<p>
<%= f.label :username %>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :ssl, "SSL" %>
<%= f.check_box :ssl %>
</p>
<p>
<%= f.label :port %>
<%= f.text_field :port %>
</p>
<p>
<%= f.submit "Backup!" %>
</p>
<% end %>
5 changes: 5 additions & 0 deletions app/views/backups/show.html.erb
@@ -0,0 +1,5 @@
<h1>BackupMyMail Downloads</h1>
<p>To download, right click on the link and left click on "Save as" or your browser's equivalent."</p>
<% @downloads.each do |name, filename| %>
<p><%= link_to name, "/download/#{filename}" %></p>
<% end %>
12 changes: 12 additions & 0 deletions spec/controllers/backups_controller_spec.rb
Expand Up @@ -3,6 +3,7 @@
module BackupsControllerSpecHelper
def setup_create( result )
Pop3.should_receive(:new).once.and_return(@pop3)
@pop3.should_receive(:valid?).once.and_return(true)
@pop3.should_receive(:setup_mailer).once
@pop3.should_receive(:download).once.and_return( result )
end
Expand Down Expand Up @@ -77,6 +78,7 @@ def do_get
before do
@pop3 = mock_model( Pop3 )
Pop3.stub!(:new).and_return(@pop3)
@pop3.stub!(:valid).and_return(true)
@params = {
:email_address => 'test.otherinbox@gmail.com',
:server => 'pop.gmail.com',
Expand Down Expand Up @@ -105,6 +107,16 @@ def do_post
flash[:notice].should match /success/i
end

it "should render new page if invalid RemoteMail" do
@params = nil
@pop3.should_receive(:valid?).once.and_return(false)

do_post
response.should be_success
response.should render_template( :new )
flash[:notice].should match /problems/i
end

it "should show error for authentication problem" do
result =
{
Expand Down
9 changes: 1 addition & 8 deletions spec/models/pop3_spec.rb
Expand Up @@ -2,14 +2,7 @@

module Pop3SpecHelper
def setup_pop3( opts = {}, setup_mailer = true )
@valid_attributes = {
:email_address => 'test.otherinbox@gmail.com',
:server => 'pop.gmail.com',
:username => 'test.otherinbox@gmail.com',
:password => '0th3r1nb0x', # TODO need to encrypt this
:ssl => true,
:port => 995
}.merge( opts )
@valid_attributes = valid_pop3_attributes.merge( opts )

@pop3 = Pop3.new
@pop3.attributes = @valid_attributes
Expand Down
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -60,3 +60,14 @@ def setup_mock_time
Time.stub!(:now).and_return(@time)
@time.stub!(:to_s).and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
end

def valid_pop3_attributes
@valid_attributes = {
:email_address => 'test.otherinbox@gmail.com',
:server => 'pop.gmail.com',
:username => 'test.otherinbox@gmail.com',
:password => '0th3r1nb0x', # TODO need to encrypt this
:ssl => true,
:port => 995
}
end
35 changes: 35 additions & 0 deletions spec/views/backups/new.html.erb_spec.rb
@@ -0,0 +1,35 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "/backups/new" do
before(:each) do
@remote_mail = mock_model( RemoteMail )
valid_pop3_attributes.each do |key, value|
@remote_mail.stub!(key).and_return( value )
end
assigns[:remote_mail] = @remote_mail
end

it "should render new form" do
render 'backups/new'
response.should have_tag("form[action=?][method=post]", '/backups/create') do
with_tag( "input#remote_mail_email_address[name=?]", "remote_mail[email_address]" )
with_tag( "input#remote_mail_server[name=?]", "remote_mail[server]" )
with_tag( "input#remote_mail_username[name=?]", "remote_mail[username]" )
with_tag( "input#remote_mail_password[name=?][type=password]", "remote_mail[password]" )
with_tag( "input#remote_mail_ssl[name=?]", "remote_mail[ssl]" )
with_tag( "input#remote_mail_port[name=?]", "remote_mail[port]" )
with_tag( "input[type=submit]" )
end
end

it "should display error notices" do
@remote_mail = RemoteMail.new
@remote_mail.save
assigns[:remote_mail] = @remote_mail
render 'backups/new'

response.should have_tag("form[action=?][method=post]", '/backups/create') do
with_tag( "div[id=errorExplanation]" )
end
end
end
12 changes: 12 additions & 0 deletions spec/views/backups/show.html.erb_spec.rb
@@ -0,0 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "/backups/show" do
before(:each) do
assigns[:downloads] = { :inbox => MBOX_NAME }
render 'backups/show'
end

it "should show download link" do
response.should have_tag( "a[href=?]", "/download/#{MBOX_NAME}", "inbox" )
end
end

0 comments on commit f1ad26e

Please sign in to comment.