+ def test_new_get_with_params
+ # Now try creating a diary entry using get
+ assert_difference "DiaryEntry.count", 0 do
+ get :new,
+ :params => { :commit => "save",
+ :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
+ :longitude => "2.2", :language_code => "en" } },
+ :session => { :user => create(:user).id }
+ end
+ assert_response :success
+ assert_template :edit
+ end
+
+ def test_new_no_body
+ # Now try creating a invalid diary entry with an empty body
+ user = create(:user)
+ assert_no_difference "DiaryEntry.count" do
+ post :new,
+ :params => { :commit => "save",
+ :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
+ :longitude => "2.2", :language_code => "en" } },
+ :session => { :user => user.id }
+ end
+ assert_response :success
+ assert_template :edit
+
+ assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
+ end
+
+ def test_new_post
+ # Now try creating a diary entry
+ user = create(:user)
+ assert_difference "DiaryEntry.count", 1 do
+ post :new,
+ :params => { :commit => "save",
+ :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
+ :longitude => "2.2", :language_code => "en" } },
+ :session => { :user => user.id }
+ end
+ assert_response :redirect
+ assert_redirected_to :action => :index, :display_name => user.display_name
+ entry = DiaryEntry.order(:id).last
+ assert_equal user.id, entry.user_id
+ assert_equal "New Title", entry.title
+ assert_equal "This is a new body for the diary entry", entry.body
+ assert_equal "1.1".to_f, entry.latitude
+ assert_equal "2.2".to_f, entry.longitude
+ assert_equal "en", entry.language_code
+
+ # checks if user was subscribed
+ assert_equal 1, entry.subscribers.length
+
+ assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
+ end
+
+ def test_new_german
+ create(:language, :code => "de")
+ user = create(:user)
+
+ # Now try creating a diary entry in a different language
+ assert_difference "DiaryEntry.count", 1 do
+ post :new,
+ :params => { :commit => "save",
+ :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
+ :longitude => "2.2", :language_code => "de" } },
+ :session => { :user => user.id }
+ end
+ assert_response :redirect
+ assert_redirected_to :action => :index, :display_name => user.display_name
+ entry = DiaryEntry.order(:id).last
+ assert_equal user.id, entry.user_id
+ assert_equal "New Title", entry.title
+ assert_equal "This is a new body for the diary entry", entry.body
+ assert_equal "1.1".to_f, entry.latitude
+ assert_equal "2.2".to_f, entry.longitude
+ assert_equal "de", entry.language_code
+
+ # checks if user was subscribed
+ assert_equal 1, entry.subscribers.length
+
+ assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
+ end
+
+ def test_new_spammy
+ user = create(:user)
+ # Generate some spammy content
+ spammy_title = "Spam Spam Spam Spam Spam"
+ spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
+
+ # Try creating a spammy diary entry
+ assert_difference "DiaryEntry.count", 1 do
+ post :new,
+ :params => { :commit => "save",
+ :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
+ :session => { :user => user.id }
+ end
+ assert_response :redirect
+ assert_redirected_to :action => :index, :display_name => user.display_name
+ entry = DiaryEntry.order(:id).last
+ assert_equal user.id, entry.user_id
+ assert_equal spammy_title, entry.title
+ assert_equal spammy_body, entry.body
+ assert_equal "en", entry.language_code
+ assert_equal "suspended", User.find(user.id).status
+
+ # Follow the redirect
+ get :index,
+ :params => { :display_name => user.display_name },
+ :session => { :user => user }
+ assert_response :redirect
+ assert_redirected_to :controller => :users, :action => :suspended
+ end
+
+ def test_edit
+ user = create(:user)
+ other_user = create(:user)
+
+ entry = create(:diary_entry, :user => user)