debian-mirror-gitlab/spec/controllers/admin/topics_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
6 KiB
Ruby
Raw Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Admin::TopicsController do
let_it_be(:topic) { create(:topic, name: 'topic') }
let_it_be(:admin) { create(:admin) }
let_it_be(:user) { create(:user) }
before do
sign_in(admin)
end
describe 'GET #index' do
it 'renders the template' do
get :index
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('index')
end
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
get :index
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe 'GET #new' do
it 'renders the template' do
get :new
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('new')
end
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
get :new
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe 'GET #edit' do
it 'renders the template' do
get :edit, params: { id: topic.id }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('edit')
end
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
get :edit, params: { id: topic.id }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe 'POST #create' do
it 'creates topic' do
expect do
2022-07-16 23:28:13 +05:30
post :create, params: { projects_topic: { name: 'test', title: 'Test' } }
2021-11-18 22:05:49 +05:30
end.to change { Projects::Topic.count }.by(1)
end
2022-07-16 23:28:13 +05:30
it 'shows error message for invalid topic name' do
post :create, params: { projects_topic: { name: nil, title: 'Test' } }
2021-11-18 22:05:49 +05:30
errors = assigns[:topic].errors
expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.blank')))
end
2022-07-16 23:28:13 +05:30
it 'shows error message if topic name not unique (case insensitive)' do
post :create, params: { projects_topic: { name: topic.name.upcase, title: topic.title } }
2022-05-07 20:08:51 +05:30
errors = assigns[:topic].errors
expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.taken')))
end
2022-07-16 23:28:13 +05:30
it 'shows error message for invalid topic title' do
post :create, params: { projects_topic: { name: 'test', title: nil } }
errors = assigns[:topic].errors
expect(errors).to contain_exactly(errors.full_message(:title, I18n.t('errors.messages.blank')))
end
2021-11-18 22:05:49 +05:30
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
post :create, params: { projects_topic: { name: 'test' } }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe 'PUT #update' do
it 'updates topic' do
put :update, params: { id: topic.id, projects_topic: { name: 'test' } }
expect(response).to redirect_to(edit_admin_topic_path(topic))
expect(topic.reload.name).to eq('test')
end
it 'shows error message for invalid topic' do
put :update, params: { id: topic.id, projects_topic: { name: nil } }
errors = assigns[:topic].errors
expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.blank')))
end
2022-05-07 20:08:51 +05:30
it 'shows error message if topic not unique (case insensitive)' do
other_topic = create(:topic, name: 'other-topic')
put :update, params: { id: topic.id, projects_topic: { name: other_topic.name.upcase } }
errors = assigns[:topic].errors
expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.taken')))
end
2021-11-18 22:05:49 +05:30
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
put :update, params: { id: topic.id, projects_topic: { name: 'test' } }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2022-08-13 15:12:31 +05:30
describe 'DELETE #destroy' do
it 'removes topic' do
delete :destroy, params: { id: topic.id }
expect(response).to redirect_to(admin_topics_path)
expect { topic.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
delete :destroy, params: { id: topic.id }
expect(response).to have_gitlab_http_status(:not_found)
expect { topic.reload }.not_to raise_error
end
end
end
2022-08-27 11:52:29 +05:30
describe 'POST #merge' do
let_it_be(:source_topic) { create(:topic, name: 'source_topic') }
let_it_be(:project) { create(:project, topic_list: source_topic.name ) }
it 'merges source topic into target topic' do
post :merge, params: { source_topic_id: source_topic.id, target_topic_id: topic.id }
expect(response).to redirect_to(admin_topics_path)
expect(topic.projects).to contain_exactly(project)
expect { source_topic.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'renders a 404 error for non-existing id' do
post :merge, params: { source_topic_id: non_existing_record_id, target_topic_id: topic.id }
expect(response).to have_gitlab_http_status(:not_found)
expect { topic.reload }.not_to raise_error
end
it 'renders a 400 error for identical topic ids' do
2022-10-11 01:57:18 +05:30
post :merge, params: { source_topic_id: topic.id, target_topic_id: topic.id }
2022-08-27 11:52:29 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
expect { topic.reload }.not_to raise_error
end
context 'as a normal user' do
before do
sign_in(user)
end
it 'renders a 404 error' do
post :merge, params: { source_topic_id: source_topic.id, target_topic_id: topic.id }
expect(response).to have_gitlab_http_status(:not_found)
expect { source_topic.reload }.not_to raise_error
end
end
end
2021-11-18 22:05:49 +05:30
end