From 8779490ce4c853d7359f313b9c8a7946013d0a41 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 24 Aug 2017 12:01:11 +0900 Subject: [PATCH] Move new controller specs to reqeusts folder. --- .../integration/managing_topic_status_spec.rb | 116 ----------------- .../admin/admin_controller_spec.rb | 2 +- .../admin/backups_controller_spec.rb} | 6 +- .../admin/emojis_controller_spec.rb} | 24 ++-- .../admin/flags_controller_spec.rb} | 18 +-- .../admin/groups_controller_spec.rb} | 6 +- .../groups_controller_spec.rb} | 26 ++-- .../omniauth_callbacks_controller_spec.rb} | 2 +- .../safe_mode_controller_spec.rb} | 4 +- .../tags_controller_spec.rb} | 4 +- spec/requests/topics_controller_spec.rb | 118 ++++++++++++++++++ .../users_controller_spec.rb} | 4 +- 12 files changed, 169 insertions(+), 161 deletions(-) delete mode 100644 spec/integration/managing_topic_status_spec.rb rename spec/{integration => requests}/admin/admin_controller_spec.rb (83%) rename spec/{integration/admin/backups_spec.rb => requests/admin/backups_controller_spec.rb} (87%) rename spec/{integration/admin/emojis_spec.rb => requests/admin/emojis_controller_spec.rb} (71%) rename spec/{integration/admin/flags_spec.rb => requests/admin/flags_controller_spec.rb} (64%) rename spec/{integration/admin/groups_spec.rb => requests/admin/groups_controller_spec.rb} (90%) rename spec/{integration/groups_spec.rb => requests/groups_controller_spec.rb} (97%) rename spec/{integration/omniauth_callbacks_spec.rb => requests/omniauth_callbacks_controller_spec.rb} (98%) rename spec/{integration/safe_mode_spec.rb => requests/safe_mode_controller_spec.rb} (78%) rename spec/{integration/tags_spec.rb => requests/tags_controller_spec.rb} (86%) create mode 100644 spec/requests/topics_controller_spec.rb rename spec/{integration/users_spec.rb => requests/users_controller_spec.rb} (95%) diff --git a/spec/integration/managing_topic_status_spec.rb b/spec/integration/managing_topic_status_spec.rb deleted file mode 100644 index eede1cbb096..00000000000 --- a/spec/integration/managing_topic_status_spec.rb +++ /dev/null @@ -1,116 +0,0 @@ -require 'rails_helper' - -RSpec.describe "Managing a topic's status update", type: :request do - let(:topic) { Fabricate(:topic) } - let(:user) { Fabricate(:user) } - - context 'when a user is not logged in' do - it 'should return the right response' do - expect do - post "/t/#{topic.id}/timer.json", - time: '24', - status_type: TopicTimer.types[1] - end.to raise_error(Discourse::NotLoggedIn) - end - end - - context 'when does not have permission' do - it 'should return the right response' do - sign_in(user) - - post "/t/#{topic.id}/timer.json", - time: '24', - status_type: TopicTimer.types[1] - - expect(response.status).to eq(403) - expect(JSON.parse(response.body)["error_type"]).to eq('invalid_access') - end - end - - context 'when logged in as an admin' do - let(:admin) { Fabricate(:admin) } - - before do - sign_in(admin) - end - - it 'should be able to create a topic status update' do - time = 24 - - post "/t/#{topic.id}/timer.json", - time: 24, - status_type: TopicTimer.types[1] - - expect(response).to be_success - - topic_status_update = TopicTimer.last - - expect(topic_status_update.topic).to eq(topic) - - expect(topic_status_update.execute_at) - .to be_within(1.second).of(24.hours.from_now) - - json = JSON.parse(response.body) - - expect(DateTime.parse(json['execute_at'])) - .to be_within(1.seconds).of(DateTime.parse(topic_status_update.execute_at.to_s)) - - expect(json['duration']).to eq(topic_status_update.duration) - expect(json['closed']).to eq(topic.reload.closed) - end - - it 'should be able to delete a topic status update' do - Fabricate(:topic_timer, topic: topic) - - post "/t/#{topic.id}/timer.json", - time: nil, - status_type: TopicTimer.types[1] - - expect(response).to be_success - expect(topic.reload.public_topic_timer).to eq(nil) - - json = JSON.parse(response.body) - - expect(json['execute_at']).to eq(nil) - expect(json['duration']).to eq(nil) - expect(json['closed']).to eq(topic.closed) - end - - describe 'publishing topic to category in the future' do - it 'should be able to create the topic status update' do - SiteSetting.queue_jobs = true - - post "/t/#{topic.id}/timer.json", - time: 24, - status_type: TopicTimer.types[3], - category_id: topic.category_id - - expect(response).to be_success - - topic_status_update = TopicTimer.last - - expect(topic_status_update.topic).to eq(topic) - - expect(topic_status_update.execute_at) - .to be_within(1.second).of(24.hours.from_now) - - expect(topic_status_update.status_type) - .to eq(TopicTimer.types[:publish_to_category]) - - json = JSON.parse(response.body) - - expect(json['category_id']).to eq(topic.category_id) - end - end - - describe 'invalid status type' do - it 'should raise the right error' do - expect do - post "/t/#{topic.id}/timer.json", - time: 10, - status_type: 'something' - end.to raise_error(Discourse::InvalidParameters) - end - end - end -end diff --git a/spec/integration/admin/admin_controller_spec.rb b/spec/requests/admin/admin_controller_spec.rb similarity index 83% rename from spec/integration/admin/admin_controller_spec.rb rename to spec/requests/admin/admin_controller_spec.rb index 62beaaca421..0d87a7e6e09 100644 --- a/spec/integration/admin/admin_controller_spec.rb +++ b/spec/requests/admin/admin_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "Admin::AdminController" do +RSpec.describe Admin::AdminController do it "should return the right response if user isn't a staff" do expect do get "/admin", api_key: 'asdiasiduga' diff --git a/spec/integration/admin/backups_spec.rb b/spec/requests/admin/backups_controller_spec.rb similarity index 87% rename from spec/integration/admin/backups_spec.rb rename to spec/requests/admin/backups_controller_spec.rb index 4520b06f475..88d92133357 100644 --- a/spec/integration/admin/backups_spec.rb +++ b/spec/requests/admin/backups_controller_spec.rb @@ -1,13 +1,13 @@ require 'rails_helper' -RSpec.describe "Managing Backups" do +RSpec.describe Admin::BackupsController do let(:admin) { Fabricate(:admin) } before do sign_in(admin) end - describe 'rolling back a restore' do + describe '#rollback' do it 'should rollback the restore' do BackupRestore.expects(:rollback!) @@ -22,7 +22,7 @@ RSpec.describe "Managing Backups" do end end - describe 'cancelling a backup' do + describe '#cancel' do it "should cancel an backup" do BackupRestore.expects(:cancel!) diff --git a/spec/integration/admin/emojis_spec.rb b/spec/requests/admin/emojis_controller_spec.rb similarity index 71% rename from spec/integration/admin/emojis_spec.rb rename to spec/requests/admin/emojis_controller_spec.rb index 54c3b2cecb8..352205c77cb 100644 --- a/spec/integration/admin/emojis_spec.rb +++ b/spec/requests/admin/emojis_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "Managing custom emojis" do +RSpec.describe Admin::EmojisController do let(:admin) { Fabricate(:admin) } let(:upload) { Fabricate(:upload) } @@ -8,12 +8,14 @@ RSpec.describe "Managing custom emojis" do sign_in(admin) end - describe "creating a custom emoji" do + describe "#create" do describe 'when upload is invalid' do it 'should publish the right error' do message = MessageBus.track_publish do - post("/admin/customize/emojis.json", name: 'test', - file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg")) + post("/admin/customize/emojis.json", + name: 'test', + file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg") + ) end.find { |m| m.channel == "/uploads/emoji" } expect(message.channel).to eq("/uploads/emoji") @@ -26,8 +28,10 @@ RSpec.describe "Managing custom emojis" do CustomEmoji.create!(name: 'test', upload: upload) message = MessageBus.track_publish do - post("/admin/customize/emojis.json", name: 'test', - file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")) + post("/admin/customize/emojis.json", + name: 'test', + file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png") + ) end.find { |m| m.channel == "/uploads/emoji" } expect(message.channel).to eq("/uploads/emoji") @@ -42,8 +46,10 @@ RSpec.describe "Managing custom emojis" do Emoji.expects(:clear_cache) message = MessageBus.track_publish do - post("/admin/customize/emojis.json", name: 'test', - file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")) + post("/admin/customize/emojis.json", + name: 'test', + file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png") + ) end.find { |m| m.channel == "/uploads/emoji" } custom_emoji = CustomEmoji.last @@ -57,7 +63,7 @@ RSpec.describe "Managing custom emojis" do end end - describe 'deleting a custom emoji' do + describe '#destroy' do it 'should allow an admin to delete a custom emoji' do custom_emoji = CustomEmoji.create!(name: 'test', upload: upload) Emoji.clear_cache diff --git a/spec/integration/admin/flags_spec.rb b/spec/requests/admin/flags_controller_spec.rb similarity index 64% rename from spec/integration/admin/flags_spec.rb rename to spec/requests/admin/flags_controller_spec.rb index 8c944276809..2906624d2f0 100644 --- a/spec/integration/admin/flags_spec.rb +++ b/spec/requests/admin/flags_controller_spec.rb @@ -1,15 +1,15 @@ require 'rails_helper' -RSpec.describe "Managing flags as an admin" do +RSpec.describe Admin::FlagsController do let(:admin) { Fabricate(:admin) } - let(:post) { Fabricate(:post) } + let(:post_1) { Fabricate(:post) } let(:user) { Fabricate(:user) } before do sign_in(admin) end - context 'viewing flags' do + context '#index' do it 'should return the right response when nothing is flagged' do get '/admin/flags.json' @@ -21,7 +21,7 @@ RSpec.describe "Managing flags as an admin" do end it 'should return the right response' do - PostAction.act(user, post, PostActionType.types[:spam]) + PostAction.act(user, post_1, PostActionType.types[:spam]) get '/admin/flags.json' @@ -33,13 +33,13 @@ RSpec.describe "Managing flags as an admin" do end end - context 'agreeing with a flag' do + context '#agree' do it 'should work' do SiteSetting.allow_user_locale = true - post_action = PostAction.act(user, post, PostActionType.types[:spam], message: 'bad') + post_action = PostAction.act(user, post_1, PostActionType.types[:spam], message: 'bad') admin.update!(locale: 'ja') - xhr :post, "/admin/flags/agree/#{post.id}" + post "/admin/flags/agree/#{post_1.id}.json" expect(response).to be_success @@ -47,9 +47,9 @@ RSpec.describe "Managing flags as an admin" do expect(post_action.agreed_by_id).to eq(admin.id) - post = Post.offset(1).last + post_1 = Post.offset(1).last - expect(post.raw).to eq(I18n.with_locale(:en) { I18n.t('flags_dispositions.agreed') }) + expect(post_1.raw).to eq(I18n.with_locale(:en) { I18n.t('flags_dispositions.agreed') }) end end end diff --git a/spec/integration/admin/groups_spec.rb b/spec/requests/admin/groups_controller_spec.rb similarity index 90% rename from spec/integration/admin/groups_spec.rb rename to spec/requests/admin/groups_controller_spec.rb index c553ba561a6..9448220c637 100644 --- a/spec/integration/admin/groups_spec.rb +++ b/spec/requests/admin/groups_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "Managing groups as an admin" do +RSpec.describe Admin::GroupsController do let(:admin) { Fabricate(:admin) } let(:user) { Fabricate(:user) } let(:group) { Fabricate(:group) } @@ -9,7 +9,7 @@ RSpec.describe "Managing groups as an admin" do sign_in(admin) end - describe 'creating a new group' do +describe '#create' do it 'should work' do post "/admin/groups.json", group: { name: 'testing', @@ -30,7 +30,7 @@ RSpec.describe "Managing groups as an admin" do end end - describe 'adding a new owner' do + describe '#add_owners' do it 'should work' do put "/admin/groups/#{group.id}/owners.json", group: { usernames: [user.username, admin.username].join(",") diff --git a/spec/integration/groups_spec.rb b/spec/requests/groups_controller_spec.rb similarity index 97% rename from spec/integration/groups_spec.rb rename to spec/requests/groups_controller_spec.rb index 90d1c61b13a..35e31d27516 100644 --- a/spec/integration/groups_spec.rb +++ b/spec/requests/groups_controller_spec.rb @@ -1,10 +1,10 @@ require 'rails_helper' -describe "Groups" do +describe GroupsController do let(:user) { Fabricate(:user) } let(:group) { Fabricate(:group, users: [user]) } - describe 'viewing groups' do + describe '#index' do let!(:staff_group) do Fabricate(:group, name: '0000', visibility_level: Group.visibility_levels[:staff]) end @@ -57,7 +57,7 @@ describe "Groups" do end end - describe "checking if a group can be mentioned" do + describe '#mentionable' do it "should return the right response" do sign_in(user) group.update_attributes!(name: 'test') @@ -79,7 +79,7 @@ describe "Groups" do end end - describe "group can be updated" do + describe '#update' do let(:group) do Fabricate(:group, name: 'test', @@ -103,7 +103,7 @@ describe "Groups" do group.update!(allow_membership_requests: false) expect do - xhr :put, "/groups/#{group.id}", group: { + put "/groups/#{group.id}.json", group: { flair_bg_color: 'FFF', flair_color: 'BBB', flair_url: 'fa-adjust', @@ -138,7 +138,7 @@ describe "Groups" do end it 'should be able to update the group' do - xhr :put, "/groups/#{group.id}", group: { flair_color: 'BBB' } + put "/groups/#{group.id}.json", group: { flair_color: 'BBB' } expect(response).to be_success expect(group.reload.flair_color).to eq('BBB') @@ -149,14 +149,14 @@ describe "Groups" do it 'should not be able to update the group' do sign_in(user) - xhr :put, "/groups/#{group.id}", group: { name: 'testing' } + put "/groups/#{group.id}.json", group: { name: 'testing' } expect(response.status).to eq(403) end end end - describe 'members' do + describe '#members' do let(:user1) do Fabricate(:user, last_seen_at: Time.zone.now, @@ -220,7 +220,7 @@ describe "Groups" do end end - describe "membership edit permissions" do + describe "edit" do let(:group) { Fabricate(:group) } context 'when user is not signed in' do @@ -287,7 +287,7 @@ describe "Groups" do sign_in(admin) end - context 'adding members' do + context 'add_members' do it "can make incremental adds" do user2 = Fabricate(:user) @@ -394,7 +394,7 @@ describe "Groups" do end end - context 'removing members' do + context '#remove_member' do it "cannot remove members from automatic groups" do group.update!(automatic: true) @@ -555,7 +555,7 @@ describe "Groups" do end end - describe "requesting membership for a group" do + describe '#request_membership' do let(:new_user) { Fabricate(:user) } it 'requires the user to log in' do @@ -602,7 +602,7 @@ describe "Groups" do end end - describe 'search for groups' do + describe '#search ' do let(:hidden_group) do Fabricate(:group, visibility_level: Group.visibility_levels[:owners], diff --git a/spec/integration/omniauth_callbacks_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rb similarity index 98% rename from spec/integration/omniauth_callbacks_spec.rb rename to spec/requests/omniauth_callbacks_controller_spec.rb index 2f855576963..eefa6c8be07 100644 --- a/spec/integration/omniauth_callbacks_spec.rb +++ b/spec/requests/omniauth_callbacks_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "OmniAuth Callbacks" do +RSpec.describe Users::OmniauthCallbacksController do let(:user) { Fabricate(:user) } before do diff --git a/spec/integration/safe_mode_spec.rb b/spec/requests/safe_mode_controller_spec.rb similarity index 78% rename from spec/integration/safe_mode_spec.rb rename to spec/requests/safe_mode_controller_spec.rb index f421c8fb976..a47d2f085c5 100644 --- a/spec/integration/safe_mode_spec.rb +++ b/spec/requests/safe_mode_controller_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' -RSpec.describe 'Safe mode' do - describe 'entering safe mode' do +RSpec.describe SafeModeController do + describe 'enter' do context 'when no params are given' do it 'should redirect back to safe mode page' do post '/safe-mode' diff --git a/spec/integration/tags_spec.rb b/spec/requests/tags_controller_spec.rb similarity index 86% rename from spec/integration/tags_spec.rb rename to spec/requests/tags_controller_spec.rb index 34b0977abb2..7e9f721d153 100644 --- a/spec/integration/tags_spec.rb +++ b/spec/requests/tags_controller_spec.rb @@ -1,11 +1,11 @@ require 'rails_helper' -describe "Tags" do +describe TagsController do before do SiteSetting.tagging_enabled = true end - describe "checking tag hashtags" do + describe '#check_hashtag' do let(:tag) { Fabricate(:tag, name: 'test') } it "should return the right response" do diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb new file mode 100644 index 00000000000..6da482ec63b --- /dev/null +++ b/spec/requests/topics_controller_spec.rb @@ -0,0 +1,118 @@ +require 'rails_helper' + +RSpec.describe TopicsController do + let(:topic) { Fabricate(:topic) } + let(:user) { Fabricate(:user) } + + describe '#timer' do + context 'when a user is not logged in' do + it 'should return the right response' do + expect do + post "/t/#{topic.id}/timer.json", + time: '24', + status_type: TopicTimer.types[1] + end.to raise_error(Discourse::NotLoggedIn) + end + end + + context 'when does not have permission' do + it 'should return the right response' do + sign_in(user) + + post "/t/#{topic.id}/timer.json", + time: '24', + status_type: TopicTimer.types[1] + + expect(response.status).to eq(403) + expect(JSON.parse(response.body)["error_type"]).to eq('invalid_access') + end + end + + context 'when logged in as an admin' do + let(:admin) { Fabricate(:admin) } + + before do + sign_in(admin) + end + + it 'should be able to create a topic status update' do + time = 24 + + post "/t/#{topic.id}/timer.json", + time: 24, + status_type: TopicTimer.types[1] + + expect(response).to be_success + + topic_status_update = TopicTimer.last + + expect(topic_status_update.topic).to eq(topic) + + expect(topic_status_update.execute_at) + .to be_within(1.second).of(24.hours.from_now) + + json = JSON.parse(response.body) + + expect(DateTime.parse(json['execute_at'])) + .to be_within(1.seconds).of(DateTime.parse(topic_status_update.execute_at.to_s)) + + expect(json['duration']).to eq(topic_status_update.duration) + expect(json['closed']).to eq(topic.reload.closed) + end + + it 'should be able to delete a topic status update' do + Fabricate(:topic_timer, topic: topic) + + post "/t/#{topic.id}/timer.json", + time: nil, + status_type: TopicTimer.types[1] + + expect(response).to be_success + expect(topic.reload.public_topic_timer).to eq(nil) + + json = JSON.parse(response.body) + + expect(json['execute_at']).to eq(nil) + expect(json['duration']).to eq(nil) + expect(json['closed']).to eq(topic.closed) + end + + describe 'publishing topic to category in the future' do + it 'should be able to create the topic status update' do + SiteSetting.queue_jobs = true + + post "/t/#{topic.id}/timer.json", + time: 24, + status_type: TopicTimer.types[3], + category_id: topic.category_id + + expect(response).to be_success + + topic_status_update = TopicTimer.last + + expect(topic_status_update.topic).to eq(topic) + + expect(topic_status_update.execute_at) + .to be_within(1.second).of(24.hours.from_now) + + expect(topic_status_update.status_type) + .to eq(TopicTimer.types[:publish_to_category]) + + json = JSON.parse(response.body) + + expect(json['category_id']).to eq(topic.category_id) + end + end + + describe 'invalid status type' do + it 'should raise the right error' do + expect do + post "/t/#{topic.id}/timer.json", + time: 10, + status_type: 'something' + end.to raise_error(Discourse::InvalidParameters) + end + end + end + end +end diff --git a/spec/integration/users_spec.rb b/spec/requests/users_controller_spec.rb similarity index 95% rename from spec/integration/users_spec.rb rename to spec/requests/users_controller_spec.rb index 78deb7539cf..23bf76dbd0f 100644 --- a/spec/integration/users_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe "Users" do +RSpec.describe UsersController do let(:user) { Fabricate(:user) } - describe "viewing a user" do + describe '#show' do it "should be able to view a user" do get "/u/#{user.username}"