Commit b7d29ce6 authored by tiagonbotelho's avatar tiagonbotelho

adds test to check whether or not an email is sent to label subscribers after...

adds test to check whether or not an email is sent to label subscribers after creating a new issue through the api
parent 7532c012
...@@ -154,35 +154,22 @@ module API ...@@ -154,35 +154,22 @@ module API
render_api_error!({ labels: errors }, 400) render_api_error!({ labels: errors }, 400)
end end
# Find or create labels
if params[:labels].present? if params[:labels].present?
params[:labels] = params[:labels].split(",").each { |word| word.strip! } attrs[:label_ids] = params[:labels].split(",").map do |label_name|
attrs[:label_ids] = [] user_project.labels.create_with(color: Label::DEFAULT_COLOR)
.find_or_create_by(title: label_name.strip)
params[:labels].each do |label| .id
existing_label = user_project.labels.where(title: label).first
unless existing_label.nil?
attrs[:label_ids] << existing_label.id
params[:labels].delete(label)
end end
end 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? if issue.spam?
render_api_error!({ error: 'Spam detected' }, 400) render_api_error!({ error: 'Spam detected' }, 400)
end end
if issue.valid? 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 present issue, with: Entities::Issue, current_user: current_user
else else
render_validation_error!(issue) render_validation_error!(issue)
......
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:non_member) { create(:user) } let(:non_member) { create(:user) }
...@@ -478,6 +479,18 @@ describe API::API, api: true do ...@@ -478,6 +479,18 @@ describe API::API, api: true do
expect(json_response['labels']).to eq(['label', 'label2']) expect(json_response['labels']).to eq(['label', 'label2'])
end 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 it "returns a 400 bad request if title not given" do
post api("/projects/#{project.id}/issues", user), labels: 'label, label2' post api("/projects/#{project.id}/issues", user), labels: 'label, label2'
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment