Commit 0c1d9125 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'rs-issue-2012' into 'master'

Make username parameter for Users#show case-insensitive

Closes #2012

See merge request !996
parents 7e262ebf 5ce6d186
...@@ -274,6 +274,10 @@ class User < ActiveRecord::Base ...@@ -274,6 +274,10 @@ class User < ActiveRecord::Base
value: login.to_s.downcase).first value: login.to_s.downcase).first
end end
def find_by_username!(username)
find_by!('lower(username) = ?', username.downcase)
end
def by_username_or_id(name_or_id) def by_username_or_id(name_or_id)
where('users.username = ? OR users.id = ?', name_or_id.to_s, name_or_id.to_i).first where('users.username = ? OR users.id = ?', name_or_id.to_s, name_or_id.to_i).first
end end
......
require 'spec_helper' require 'spec_helper'
describe UsersController do describe UsersController do
let(:user) { create(:user, username: 'user1', name: 'User 1', email: 'user1@gitlab.com') } let(:user) { create(:user) }
before do
sign_in(user)
end
describe 'GET #show' do describe 'GET #show' do
render_views it 'is case-insensitive' do
user = create(:user, username: 'CamelCaseUser')
sign_in(user)
get :show, username: user.username.downcase
it 'renders the show template' do expect(response).to be_success
get :show, username: user.username end
expect(response.status).to eq(200)
expect(response).to render_template('show') context 'with rendered views' do
render_views
it 'renders the show template' do
sign_in(user)
get :show, username: user.username
expect(response).to be_success
expect(response).to render_template('show')
end
end end
end end
describe 'GET #calendar' do describe 'GET #calendar' do
it 'renders calendar' do it 'renders calendar' do
sign_in(user)
get :calendar, username: user.username get :calendar, username: user.username
expect(response).to render_template('calendar') expect(response).to render_template('calendar')
end end
end end
...@@ -30,6 +43,8 @@ describe UsersController do ...@@ -30,6 +43,8 @@ describe UsersController do
before do before do
allow_any_instance_of(User).to receive(:contributed_projects_ids).and_return([project.id]) allow_any_instance_of(User).to receive(:contributed_projects_ids).and_return([project.id])
sign_in(user)
project.team << [user, :developer] project.team << [user, :developer]
end end
......
...@@ -442,6 +442,18 @@ describe User do ...@@ -442,6 +442,18 @@ describe User do
end end
end end
describe '.find_by_username!' do
it 'raises RecordNotFound' do
expect { described_class.find_by_username!('JohnDoe') }.
to raise_error(ActiveRecord::RecordNotFound)
end
it 'is case-insensitive' do
user = create(:user, username: 'JohnDoe')
expect(described_class.find_by_username!('JOHNDOE')).to eq user
end
end
describe 'all_ssh_keys' do describe 'all_ssh_keys' do
it { is_expected.to have_many(:keys).dependent(:destroy) } it { is_expected.to have_many(:keys).dependent(:destroy) }
......
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