diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-31 18:57:00 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-31 18:57:00 +0300 |
commit | fe16ce0acb9b69b1b18afc009df8d63f7648b05e (patch) | |
tree | 7d755c3a76cd64b1d901f4bb272e9145f0bce841 /spec/controllers | |
parent | 3cda8c93d879282af2ab5b21ca2d89daf0238f17 (diff) | |
parent | 1a95603510accdcdb233fa00f101da119eb1fa5c (diff) |
Merge branch 'master' into sh-support-bitbucket-server-import
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/application_controller_spec.rb | 44 | ||||
-rw-r--r-- | spec/controllers/boards/issues_controller_spec.rb | 4 | ||||
-rw-r--r-- | spec/controllers/projects/wikis_controller_spec.rb | 121 |
3 files changed, 147 insertions, 22 deletions
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index f1165c73847..bad7a28556c 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -57,6 +57,10 @@ describe ApplicationController do end describe "#authenticate_user_from_personal_access_token!" do + before do + stub_authentication_activity_metrics(debug: false) + end + controller(described_class) do def index render text: 'authenticated' @@ -67,7 +71,13 @@ describe ApplicationController do context "when the 'personal_access_token' param is populated with the personal access token" do it "logs the user in" do + expect(authentication_metrics) + .to increment(:user_authenticated_counter) + .and increment(:user_session_override_counter) + .and increment(:user_sessionless_authentication_counter) + get :index, private_token: personal_access_token.token + expect(response).to have_gitlab_http_status(200) expect(response.body).to eq('authenticated') end @@ -75,15 +85,25 @@ describe ApplicationController do context "when the 'PERSONAL_ACCESS_TOKEN' header is populated with the personal access token" do it "logs the user in" do + expect(authentication_metrics) + .to increment(:user_authenticated_counter) + .and increment(:user_session_override_counter) + .and increment(:user_sessionless_authentication_counter) + @request.headers["PRIVATE-TOKEN"] = personal_access_token.token get :index + expect(response).to have_gitlab_http_status(200) expect(response.body).to eq('authenticated') end end it "doesn't log the user in otherwise" do + expect(authentication_metrics) + .to increment(:user_unauthenticated_counter) + get :index, private_token: "token" + expect(response.status).not_to eq(200) expect(response.body).not_to eq('authenticated') end @@ -174,6 +194,10 @@ describe ApplicationController do end describe '#authenticate_sessionless_user!' do + before do + stub_authentication_activity_metrics(debug: false) + end + describe 'authenticating a user from a feed token' do controller(described_class) do def index @@ -184,7 +208,13 @@ describe ApplicationController do context "when the 'feed_token' param is populated with the feed token" do context 'when the request format is atom' do it "logs the user in" do + expect(authentication_metrics) + .to increment(:user_authenticated_counter) + .and increment(:user_session_override_counter) + .and increment(:user_sessionless_authentication_counter) + get :index, feed_token: user.feed_token, format: :atom + expect(response).to have_gitlab_http_status 200 expect(response.body).to eq 'authenticated' end @@ -192,7 +222,13 @@ describe ApplicationController do context 'when the request format is ics' do it "logs the user in" do + expect(authentication_metrics) + .to increment(:user_authenticated_counter) + .and increment(:user_session_override_counter) + .and increment(:user_sessionless_authentication_counter) + get :index, feed_token: user.feed_token, format: :ics + expect(response).to have_gitlab_http_status 200 expect(response.body).to eq 'authenticated' end @@ -200,7 +236,11 @@ describe ApplicationController do context 'when the request format is neither atom nor ics' do it "doesn't log the user in" do + expect(authentication_metrics) + .to increment(:user_unauthenticated_counter) + get :index, feed_token: user.feed_token + expect(response.status).not_to have_gitlab_http_status 200 expect(response.body).not_to eq 'authenticated' end @@ -209,7 +249,11 @@ describe ApplicationController do context "when the 'feed_token' param is populated with an invalid feed token" do it "doesn't log the user" do + expect(authentication_metrics) + .to increment(:user_unauthenticated_counter) + get :index, feed_token: 'token', format: :atom + expect(response.status).not_to eq 200 expect(response.body).not_to eq 'authenticated' end diff --git a/spec/controllers/boards/issues_controller_spec.rb b/spec/controllers/boards/issues_controller_spec.rb index ce7762691c9..d98e6ff0df8 100644 --- a/spec/controllers/boards/issues_controller_spec.rb +++ b/spec/controllers/boards/issues_controller_spec.rb @@ -42,7 +42,7 @@ describe Boards::IssuesController do parsed_response = JSON.parse(response.body) expect(response).to match_response_schema('issues') - expect(parsed_response.length).to eq 2 + expect(parsed_response['issues'].length).to eq 2 expect(development.issues.map(&:relative_position)).not_to include(nil) end @@ -80,7 +80,7 @@ describe Boards::IssuesController do parsed_response = JSON.parse(response.body) expect(response).to match_response_schema('issues') - expect(parsed_response.length).to eq 2 + expect(parsed_response['issues'].length).to eq 2 end end diff --git a/spec/controllers/projects/wikis_controller_spec.rb b/spec/controllers/projects/wikis_controller_spec.rb index fed6677935e..30623b6cb3d 100644 --- a/spec/controllers/projects/wikis_controller_spec.rb +++ b/spec/controllers/projects/wikis_controller_spec.rb @@ -2,50 +2,131 @@ require 'spec_helper' describe Projects::WikisController do let(:project) { create(:project, :public, :repository) } - let(:user) { create(:user) } - let(:wiki) { ProjectWiki.new(project, user) } + let(:user) { project.owner } + let(:project_wiki) { ProjectWiki.new(project, user) } + let(:wiki) { project_wiki.wiki } + let(:wiki_title) { 'page-title-test' } - describe 'GET #show' do - let(:wiki_title) { 'page-title-test' } + before do + create_page(wiki_title, 'hello world') + + sign_in(user) + end + + after do + destroy_page(wiki_title) + end + describe 'GET #show' do render_views - before do - create_page(wiki_title, 'hello world') - end + subject { get :show, namespace_id: project.namespace, project_id: project, id: wiki_title } - it 'limits the retrieved pages for the sidebar' do - sign_in(user) + context 'when page content encoding is invalid' do + it 'limits the retrieved pages for the sidebar' do + expect(controller).to receive(:load_wiki).and_return(project_wiki) - expect(controller).to receive(:load_wiki).and_return(wiki) + # empty? call + expect(project_wiki).to receive(:pages).with(limit: 1).and_call_original + # Sidebar entries + expect(project_wiki).to receive(:pages).with(limit: 15).and_call_original - # empty? call - expect(wiki).to receive(:pages).with(limit: 1).and_call_original - # Sidebar entries - expect(wiki).to receive(:pages).with(limit: 15).and_call_original + subject + + expect(response).to have_http_status(:ok) + expect(response.body).to include(wiki_title) + end + end - get :show, namespace_id: project.namespace, project_id: project, id: wiki_title + context 'when page content encoding is invalid' do + it 'sets flash error' do + allow(controller).to receive(:valid_encoding?).and_return(false) - expect(response).to have_http_status(:ok) - expect(response.body).to include(wiki_title) + subject + + expect(response).to have_http_status(:ok) + expect(flash[:notice]).to eq 'The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository.' + end end end describe 'POST #preview_markdown' do it 'renders json in a correct format' do - sign_in(user) - post :preview_markdown, namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text' expect(JSON.parse(response.body).keys).to match_array(%w(body references)) end end + describe 'GET #edit' do + subject { get(:edit, namespace_id: project.namespace, project_id: project, id: wiki_title) } + + context 'when page content encoding is invalid' do + it 'redirects to show' do + allow(controller).to receive(:valid_encoding?).and_return(false) + + subject + + expect(response).to redirect_to(project_wiki_path(project, project_wiki.pages.first)) + end + end + + context 'when page content encoding is valid' do + render_views + + it 'shows the edit page' do + subject + + expect(response).to have_http_status(:ok) + expect(response.body).to include('Edit Page') + end + end + end + + describe 'PATCH #update' do + let(:new_title) { 'New title' } + let(:new_content) { 'New content' } + subject do + patch(:update, + namespace_id: project.namespace, + project_id: project, + id: wiki_title, + wiki: { title: new_title, content: new_content }) + end + + context 'when page content encoding is invalid' do + it 'redirects to show' do + allow(controller).to receive(:valid_encoding?).and_return(false) + + subject + expect(response).to redirect_to(project_wiki_path(project, project_wiki.pages.first)) + end + end + + context 'when page content encoding is valid' do + render_views + + it 'updates the page' do + subject + + wiki_page = project_wiki.pages.first + + expect(wiki_page.title).to eq new_title + expect(wiki_page.content).to eq new_content + end + end + end + def create_page(name, content) - project.wiki.wiki.write_page(name, :markdown, content, commit_details(name)) + wiki.write_page(name, :markdown, content, commit_details(name)) end def commit_details(name) Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "created page #{name}") end + + def destroy_page(title, dir = '') + page = wiki.page(title: title, dir: dir) + project_wiki.delete_page(page, "test commit") + end end |