Skip to content

Commit

Permalink
rename actions to create, fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkraemer committed May 10, 2013
1 parent 80187b9 commit c8d9f52
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
22 changes: 12 additions & 10 deletions app/controllers/notices_controller.rb
Expand Up @@ -12,12 +12,12 @@ class NoticesController < ActionController::Base
/^\d+:/
]

def index_v2
def create_v2
#logger.debug {"received v2 request:\n#{@notice.inspect}\nwith redmine_params:\n#{@redmine_params.inspect}"}
create_or_update_issue @redmine_params, @notice
end

def index
def create
#logger.debug {"received v1 request:\n#{@notice.inspect}\nwith redmine_params:\n#{@redmine_params.inspect}"}
notice = v2_notice_hash(@notice)
#logger.debug {"transformed arguments:\n#{notice.inspect}"}
Expand Down Expand Up @@ -76,7 +76,9 @@ def create_or_update_issue(redmine_params, notice)
# set standard redmine issue fields
issue.category = IssueCategory.find_by_name(redmine_params["category"]) unless redmine_params["category"].blank?
issue.assigned_to = (User.find_by_login(redmine_params["assigned_to"]) || Group.find_by_lastname(redmine_params["assigned_to"])) unless redmine_params["assigned_to"].blank?
issue.priority_id = redmine_params["priority"] unless redmine_params["priority"].blank?
issue.priority_id = redmine_params["priority"].blank? ?
IssuePriority.default.id :
redmine_params["priority"]
issue.description = description

ensure_project_has_fields(project)
Expand Down Expand Up @@ -195,7 +197,7 @@ def check_enabled
def parse_request
logger.debug { "hoptoad error notification:\n#{request.raw_post}" }
case params[:action]
when 'index_v2'
when 'create_v2'
if defined?(Nokogiri)
@notice = HoptoadV2Notice.new request.raw_post
@redmine_params = @notice.redmine_params
Expand All @@ -206,7 +208,7 @@ def parse_request
@notice['error']['backtrace'] = @notice['error']['backtrace']['line']
@redmine_params = YAML.load(@notice['api_key'], :safe => true)
end
when 'index'
when 'create'
@notice = YAML.load(request.raw_post, :safe => true)['notice']
@redmine_params = YAML.load(@notice['api_key'], :safe => true)
else
Expand All @@ -226,31 +228,31 @@ def find_or_create_custom_fields
@error_class_field = IssueCustomField.find_or_initialize_by_name('Error class')
if @error_class_field.new_record?
@error_class_field.attributes = {:field_format => 'string', :searchable => true, :is_filter => true}
@error_class_field.save(false)
@error_class_field.save(:validate => false)
end

@occurences_field = IssueCustomField.find_or_initialize_by_name('# Occurences')
if @occurences_field.new_record?
@occurences_field.attributes = {:field_format => 'int', :default_value => '0', :is_filter => true}
@occurences_field.save(false)
@occurences_field.save(:validate => false)
end

@environment_field = IssueCustomField.find_or_initialize_by_name('Environment')
if @environment_field.new_record?
@environment_field.attributes = {:field_format => 'string', :searchable => true, :is_filter => true}
@environment_field.save(false)
@environment_field.save(:validate => false)
end

@trace_filter_field = ProjectCustomField.find_or_initialize_by_name('Backtrace filter')
if @trace_filter_field.new_record?
@trace_filter_field.attributes = {:field_format => 'text'}
@trace_filter_field.save(false)
@trace_filter_field.save(:validate => false)
end

@repository_root_field = ProjectCustomField.find_or_initialize_by_name('Repository root')
if @repository_root_field.new_record?
@repository_root_field.attributes = {:field_format => 'string'}
@repository_root_field.save(false)
@repository_root_field.save(:validate => false)
end
end

Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
@@ -1,7 +1,7 @@

# API v1 (hoptoad)
post 'notices', :to => 'notices#index'
post 'notices', :to => 'notices#create'

# API v2 (hoptoad / airbrake, xml based)
post 'notifier_api/v2/notices', :to => 'notices#index_v2'
post 'notifier_api/v2/notices', :to => 'notices#create_v2'

18 changes: 10 additions & 8 deletions test/functional/notices_controller_test.rb
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'

class NoticesControllerTest < ActionController::TestCase
fixtures :projects, :users, :trackers, :projects_trackers
fixtures :projects, :users, :trackers, :projects_trackers, :enumerations, :issue_statuses

def setup
Setting.mail_handler_api_key = 'asdfghjk'
Expand All @@ -12,21 +12,23 @@ def setup
test 'should create an issue with journal entry' do
assert_difference "Issue.count", 1 do
assert_difference "Journal.count", 1 do
raw_post :index_v2, {}, create_error.to_xml
raw_post :create_v2, {}, create_error.to_xml
end
end
assert_response :success
assert issue = Issue.find(:first,
:conditions => "subject like 'RuntimeError in vendor/plugins/redmine_hoptoad_server/test/functional/notices_controller_test.rb%'")
assert issue = Issue.where("subject like ?",
'RuntimeError in plugins/redmine_hoptoad_server/test/functional/notices_controller_test.rb%'
).first
assert_equal(1, issue.journals.size)
assert_equal(6, issue.priority_id)
assert occurences_field = IssueCustomField.find_by_name('# Occurences')
assert occurences_value = issue.custom_value_for(occurences_field)
assert_equal('1', occurences_value.value)


assert_no_difference 'Issue.count' do
assert_difference "Journal.count", 1 do
raw_post :index_v2, {}, create_error.to_xml
raw_post :create_v2, {}, create_error.to_xml
end
end
occurences_value.reload
Expand All @@ -36,7 +38,7 @@ def setup
test "should render 404 for non existing project" do
assert_no_difference "Issue.count" do
assert_no_difference "Journal.count" do
raw_post :index_v2, {}, create_error(:project => 'Unknown').to_xml
raw_post :create_v2, {}, create_error(:project => 'Unknown').to_xml
end
end
assert_response 404
Expand All @@ -45,7 +47,7 @@ def setup
test "should render 404 for non existing tracker" do
assert_no_difference "Issue.count" do
assert_no_difference "Journal.count" do
raw_post :index_v2, {}, create_error(:tracker => 'Unknown').to_xml
raw_post :create_v2, {}, create_error(:tracker => 'Unknown').to_xml
end
end
assert_response 404
Expand All @@ -61,7 +63,7 @@ def create_error(options = {})
:project => @project.identifier,
:tracker => @tracker.name,
:api_key => 'asdfghjk',
:priority => 5
:priority => 6
}.merge(options).to_yaml)
end
end
3 changes: 0 additions & 3 deletions test/test_helper.rb
@@ -1,9 +1,6 @@
# Load the normal Rails helper
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

# Ensure that we are using the temporary fixture path
Engines::Testing.set_fixture_path

class ActionController::TestCase

def raw_post(action, params, body = '')
Expand Down

0 comments on commit c8d9f52

Please sign in to comment.