From b7d29ce659412e9a2acc411c841420eb13d115ba Mon Sep 17 00:00:00 2001 From: tiagonbotelho Date: Tue, 9 Aug 2016 23:08:59 +0100 Subject: [PATCH] adds test to check whether or not an email is sent to label subscribers after creating a new issue through the api --- lib/api/issues.rb | 25 ++++++------------------- spec/requests/api/issues_spec.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 1121285f0af..9a042e6e70d 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,35 +154,22 @@ module API render_api_error!({ labels: errors }, 400) end + # Find or create labels if params[:labels].present? - params[:labels] = params[:labels].split(",").each { |word| word.strip! } - attrs[:label_ids] = [] - - params[:labels].each do |label| - existing_label = user_project.labels.where(title: label).first - - unless existing_label.nil? - attrs[:label_ids] << existing_label.id - params[:labels].delete(label) - end + attrs[:label_ids] = params[:labels].split(",").map do |label_name| + user_project.labels.create_with(color: Label::DEFAULT_COLOR) + .find_or_create_by(title: label_name.strip) + .id end end - project = user_project - - issue = ::Issues::CreateService.new(project, current_user, attrs.merge(request: request, api: true)).execute + issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute if issue.spam? render_api_error!({ error: 'Spam detected' }, 400) end if issue.valid? - # create new labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels].present? - issue.add_labels_by_names(params[:labels]) - end - present issue, with: Entities::Issue, current_user: current_user else render_validation_error!(issue) diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index b8038fc85a1..a4c91252472 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe API::API, api: true do include ApiHelpers + let(:user) { create(:user) } let(:user2) { create(:user) } let(:non_member) { create(:user) } @@ -478,6 +479,18 @@ describe API::API, api: true do expect(json_response['labels']).to eq(['label', 'label2']) end + it "emails label subscribers" do + clear_enqueued_jobs + label = project.labels.first + label.toggle_subscription(user2) + + expect do + post api("/projects/#{project.id}/issues", user), + title: 'new issue', labels: label.title + end.to change{enqueued_jobs.size}.by(1) + expect(response.status).to eq(201) + end + it "returns a 400 bad request if title not given" do post api("/projects/#{project.id}/issues", user), labels: 'label, label2' expect(response).to have_http_status(400)